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