2
* FCKeditor - The text editor for Internet - http://www.fckeditor.net
3
* Copyright (C) 2003-2010 Frederico Caldeira Knabben
7
* Licensed under the terms of any of the following licenses at your
10
* - GNU General Public License Version 2 or later (the "GPL")
11
* http://www.gnu.org/licenses/gpl.html
13
* - GNU Lesser General Public License Version 2.1 or later (the "LGPL")
14
* http://www.gnu.org/licenses/lgpl.html
16
* - Mozilla Public License Version 1.1 or later (the "MPL")
17
* http://www.mozilla.org/MPL/MPL-1.1.html
21
* FCKContextMenu Class: renders an control a context menu.
24
var FCKContextMenu = function( parentWindow, langDir )
26
this.CtrlDisable = false ;
28
var oPanel = this._Panel = new FCKPanel( parentWindow ) ;
29
oPanel.AppendStyleSheet( FCKConfig.SkinEditorCSS ) ;
30
oPanel.IsContextMenu = true ;
32
// The FCKTools.DisableSelection doesn't seems to work to avoid dragging of the icons in Mozilla
33
// so we stop the start of the dragging
34
if ( FCKBrowserInfo.IsGecko )
35
oPanel.Document.addEventListener( 'draggesture', function(e) {e.preventDefault(); return false;}, true ) ;
37
var oMenuBlock = this._MenuBlock = new FCKMenuBlock() ;
38
oMenuBlock.Panel = oPanel ;
39
oMenuBlock.OnClick = FCKTools.CreateEventListener( FCKContextMenu_MenuBlock_OnClick, this ) ;
45
FCKContextMenu.prototype.SetMouseClickWindow = function( mouseClickWindow )
47
if ( !FCKBrowserInfo.IsIE )
49
this._Document = mouseClickWindow.document ;
50
if ( FCKBrowserInfo.IsOpera && !( 'oncontextmenu' in document.createElement('foo') ) )
52
this._Document.addEventListener( 'mousedown', FCKContextMenu_Document_OnMouseDown, false ) ;
53
this._Document.addEventListener( 'mouseup', FCKContextMenu_Document_OnMouseUp, false ) ;
55
this._Document.addEventListener( 'contextmenu', FCKContextMenu_Document_OnContextMenu, false ) ;
60
The customData parameter is just a value that will be send to the command that is executed,
61
so it's possible to reuse the same command for several items just by assigning different data for each one.
63
FCKContextMenu.prototype.AddItem = function( name, label, iconPathOrStripInfoArrayOrIndex, isDisabled, customData )
65
var oItem = this._MenuBlock.AddItem( name, label, iconPathOrStripInfoArrayOrIndex, isDisabled, customData ) ;
70
FCKContextMenu.prototype.AddSeparator = function()
72
this._MenuBlock.AddSeparator() ;
76
FCKContextMenu.prototype.RemoveAllItems = function()
78
this._MenuBlock.RemoveAllItems() ;
82
FCKContextMenu.prototype.AttachToElement = function( element )
84
if ( FCKBrowserInfo.IsIE )
85
FCKTools.AddEventListenerEx( element, 'contextmenu', FCKContextMenu_AttachedElement_OnContextMenu, this ) ;
87
element._FCKContextMenu = this ;
90
function FCKContextMenu_Document_OnContextMenu( e )
92
if ( FCKConfig.BrowserContextMenu )
99
if ( el._FCKContextMenu )
101
if ( el._FCKContextMenu.CtrlDisable && ( e.ctrlKey || e.metaKey ) )
104
FCKTools.CancelEvent( e ) ;
105
FCKContextMenu_AttachedElement_OnContextMenu( e, el._FCKContextMenu, el ) ;
113
var FCKContextMenu_OverrideButton ;
115
function FCKContextMenu_Document_OnMouseDown( e )
117
if( !e || e.button != 2 )
120
if ( FCKConfig.BrowserContextMenu )
127
if ( el._FCKContextMenu )
129
if ( el._FCKContextMenu.CtrlDisable && ( e.ctrlKey || e.metaKey ) )
132
var overrideButton = FCKContextMenu_OverrideButton ;
133
if( !overrideButton )
135
var doc = FCKTools.GetElementDocument( e.target ) ;
136
overrideButton = FCKContextMenu_OverrideButton = doc.createElement('input') ;
137
overrideButton.type = 'button' ;
138
var buttonHolder = doc.createElement('p') ;
139
doc.body.appendChild( buttonHolder ) ;
140
buttonHolder.appendChild( overrideButton ) ;
143
overrideButton.style.cssText = 'position:absolute;top:' + ( e.clientY - 2 ) +
144
'px;left:' + ( e.clientX - 2 ) +
145
'px;width:5px;height:5px;opacity:0.01' ;
152
function FCKContextMenu_Document_OnMouseUp( e )
154
if ( FCKConfig.BrowserContextMenu )
157
var overrideButton = FCKContextMenu_OverrideButton ;
159
if ( overrideButton )
161
var parent = overrideButton.parentNode ;
162
parent.parentNode.removeChild( parent ) ;
163
FCKContextMenu_OverrideButton = undefined ;
165
if( e && e.button == 2 )
167
FCKContextMenu_Document_OnContextMenu( e ) ;
174
function FCKContextMenu_AttachedElement_OnContextMenu( ev, fckContextMenu, el )
176
if ( ( fckContextMenu.CtrlDisable && ( ev.ctrlKey || ev.metaKey ) ) || FCKConfig.BrowserContextMenu )
179
var eTarget = el || this ;
181
if ( fckContextMenu.OnBeforeOpen )
182
fckContextMenu.OnBeforeOpen.call( fckContextMenu, eTarget ) ;
184
if ( fckContextMenu._MenuBlock.Count() == 0 )
187
if ( fckContextMenu._Redraw )
189
fckContextMenu._MenuBlock.Create( fckContextMenu._Panel.MainNode ) ;
190
fckContextMenu._Redraw = false ;
193
// This will avoid that the content of the context menu can be dragged in IE
194
// as the content of the panel is recreated we need to do it every time
195
FCKTools.DisableSelection( fckContextMenu._Panel.Document.body ) ;
199
if ( FCKBrowserInfo.IsIE )
204
else if ( FCKBrowserInfo.IsSafari )
214
fckContextMenu._Panel.Show( x, y, ev.currentTarget || null ) ;
219
function FCKContextMenu_MenuBlock_OnClick( menuItem, contextMenu )
221
contextMenu._Panel.Hide() ;
222
FCKTools.RunFunction( contextMenu.OnItemClick, contextMenu, menuItem ) ;