1 | /*
|
---|
2 | * FCKeditor - The text editor for Internet - http://www.fckeditor.net
|
---|
3 | * Copyright (C) 2003-2009 Frederico Caldeira Knabben
|
---|
4 | *
|
---|
5 | * == BEGIN LICENSE ==
|
---|
6 | *
|
---|
7 | * Licensed under the terms of any of the following licenses at your
|
---|
8 | * choice:
|
---|
9 | *
|
---|
10 | * - GNU General Public License Version 2 or later (the "GPL")
|
---|
11 | * http://www.gnu.org/licenses/gpl.html
|
---|
12 | *
|
---|
13 | * - GNU Lesser General Public License Version 2.1 or later (the "LGPL")
|
---|
14 | * http://www.gnu.org/licenses/lgpl.html
|
---|
15 | *
|
---|
16 | * - Mozilla Public License Version 1.1 or later (the "MPL")
|
---|
17 | * http://www.mozilla.org/MPL/MPL-1.1.html
|
---|
18 | *
|
---|
19 | * == END LICENSE ==
|
---|
20 | *
|
---|
21 | * FCKContextMenu Class: renders an control a context menu.
|
---|
22 | */
|
---|
23 |
|
---|
24 | var FCKContextMenu = function( parentWindow, langDir )
|
---|
25 | {
|
---|
26 | this.CtrlDisable = false ;
|
---|
27 |
|
---|
28 | var oPanel = this._Panel = new FCKPanel( parentWindow ) ;
|
---|
29 | oPanel.AppendStyleSheet( FCKConfig.SkinEditorCSS ) ;
|
---|
30 | oPanel.IsContextMenu = true ;
|
---|
31 |
|
---|
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 ) ;
|
---|
36 |
|
---|
37 | var oMenuBlock = this._MenuBlock = new FCKMenuBlock() ;
|
---|
38 | oMenuBlock.Panel = oPanel ;
|
---|
39 | oMenuBlock.OnClick = FCKTools.CreateEventListener( FCKContextMenu_MenuBlock_OnClick, this ) ;
|
---|
40 |
|
---|
41 | this._Redraw = true ;
|
---|
42 | }
|
---|
43 |
|
---|
44 |
|
---|
45 | FCKContextMenu.prototype.SetMouseClickWindow = function( mouseClickWindow )
|
---|
46 | {
|
---|
47 | if ( !FCKBrowserInfo.IsIE )
|
---|
48 | {
|
---|
49 | this._Document = mouseClickWindow.document ;
|
---|
50 | if ( FCKBrowserInfo.IsOpera && !( 'oncontextmenu' in document.createElement('foo') ) )
|
---|
51 | {
|
---|
52 | this._Document.addEventListener( 'mousedown', FCKContextMenu_Document_OnMouseDown, false ) ;
|
---|
53 | this._Document.addEventListener( 'mouseup', FCKContextMenu_Document_OnMouseUp, false ) ;
|
---|
54 | }
|
---|
55 | this._Document.addEventListener( 'contextmenu', FCKContextMenu_Document_OnContextMenu, false ) ;
|
---|
56 | }
|
---|
57 | }
|
---|
58 |
|
---|
59 | /**
|
---|
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.
|
---|
62 | */
|
---|
63 | FCKContextMenu.prototype.AddItem = function( name, label, iconPathOrStripInfoArrayOrIndex, isDisabled, customData )
|
---|
64 | {
|
---|
65 | var oItem = this._MenuBlock.AddItem( name, label, iconPathOrStripInfoArrayOrIndex, isDisabled, customData ) ;
|
---|
66 | this._Redraw = true ;
|
---|
67 | return oItem ;
|
---|
68 | }
|
---|
69 |
|
---|
70 | FCKContextMenu.prototype.AddSeparator = function()
|
---|
71 | {
|
---|
72 | this._MenuBlock.AddSeparator() ;
|
---|
73 | this._Redraw = true ;
|
---|
74 | }
|
---|
75 |
|
---|
76 | FCKContextMenu.prototype.RemoveAllItems = function()
|
---|
77 | {
|
---|
78 | this._MenuBlock.RemoveAllItems() ;
|
---|
79 | this._Redraw = true ;
|
---|
80 | }
|
---|
81 |
|
---|
82 | FCKContextMenu.prototype.AttachToElement = function( element )
|
---|
83 | {
|
---|
84 | if ( FCKBrowserInfo.IsIE )
|
---|
85 | FCKTools.AddEventListenerEx( element, 'contextmenu', FCKContextMenu_AttachedElement_OnContextMenu, this ) ;
|
---|
86 | else
|
---|
87 | element._FCKContextMenu = this ;
|
---|
88 | }
|
---|
89 |
|
---|
90 | function FCKContextMenu_Document_OnContextMenu( e )
|
---|
91 | {
|
---|
92 | if ( FCKConfig.BrowserContextMenu )
|
---|
93 | return true ;
|
---|
94 |
|
---|
95 | var el = e.target ;
|
---|
96 |
|
---|
97 | while ( el )
|
---|
98 | {
|
---|
99 | if ( el._FCKContextMenu )
|
---|
100 | {
|
---|
101 | if ( el._FCKContextMenu.CtrlDisable && ( e.ctrlKey || e.metaKey ) )
|
---|
102 | return true ;
|
---|
103 |
|
---|
104 | FCKTools.CancelEvent( e ) ;
|
---|
105 | FCKContextMenu_AttachedElement_OnContextMenu( e, el._FCKContextMenu, el ) ;
|
---|
106 | return false ;
|
---|
107 | }
|
---|
108 | el = el.parentNode ;
|
---|
109 | }
|
---|
110 | return true ;
|
---|
111 | }
|
---|
112 |
|
---|
113 | var FCKContextMenu_OverrideButton ;
|
---|
114 |
|
---|
115 | function FCKContextMenu_Document_OnMouseDown( e )
|
---|
116 | {
|
---|
117 | if( !e || e.button != 2 )
|
---|
118 | return false ;
|
---|
119 |
|
---|
120 | if ( FCKConfig.BrowserContextMenu )
|
---|
121 | return true ;
|
---|
122 |
|
---|
123 | var el = e.target ;
|
---|
124 |
|
---|
125 | while ( el )
|
---|
126 | {
|
---|
127 | if ( el._FCKContextMenu )
|
---|
128 | {
|
---|
129 | if ( el._FCKContextMenu.CtrlDisable && ( e.ctrlKey || e.metaKey ) )
|
---|
130 | return true ;
|
---|
131 |
|
---|
132 | var overrideButton = FCKContextMenu_OverrideButton ;
|
---|
133 | if( !overrideButton )
|
---|
134 | {
|
---|
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 ) ;
|
---|
141 | }
|
---|
142 |
|
---|
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' ;
|
---|
146 | }
|
---|
147 | el = el.parentNode ;
|
---|
148 | }
|
---|
149 | return false ;
|
---|
150 | }
|
---|
151 |
|
---|
152 | function FCKContextMenu_Document_OnMouseUp( e )
|
---|
153 | {
|
---|
154 | if ( FCKConfig.BrowserContextMenu )
|
---|
155 | return true ;
|
---|
156 |
|
---|
157 | var overrideButton = FCKContextMenu_OverrideButton ;
|
---|
158 |
|
---|
159 | if ( overrideButton )
|
---|
160 | {
|
---|
161 | var parent = overrideButton.parentNode ;
|
---|
162 | parent.parentNode.removeChild( parent ) ;
|
---|
163 | FCKContextMenu_OverrideButton = undefined ;
|
---|
164 |
|
---|
165 | if( e && e.button == 2 )
|
---|
166 | {
|
---|
167 | FCKContextMenu_Document_OnContextMenu( e ) ;
|
---|
168 | return false ;
|
---|
169 | }
|
---|
170 | }
|
---|
171 | return true ;
|
---|
172 | }
|
---|
173 |
|
---|
174 | function FCKContextMenu_AttachedElement_OnContextMenu( ev, fckContextMenu, el )
|
---|
175 | {
|
---|
176 | if ( ( fckContextMenu.CtrlDisable && ( ev.ctrlKey || ev.metaKey ) ) || FCKConfig.BrowserContextMenu )
|
---|
177 | return true ;
|
---|
178 |
|
---|
179 | var eTarget = el || this ;
|
---|
180 |
|
---|
181 | if ( fckContextMenu.OnBeforeOpen )
|
---|
182 | fckContextMenu.OnBeforeOpen.call( fckContextMenu, eTarget ) ;
|
---|
183 |
|
---|
184 | if ( fckContextMenu._MenuBlock.Count() == 0 )
|
---|
185 | return false ;
|
---|
186 |
|
---|
187 | if ( fckContextMenu._Redraw )
|
---|
188 | {
|
---|
189 | fckContextMenu._MenuBlock.Create( fckContextMenu._Panel.MainNode ) ;
|
---|
190 | fckContextMenu._Redraw = false ;
|
---|
191 | }
|
---|
192 |
|
---|
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 ) ;
|
---|
196 |
|
---|
197 | var x = 0 ;
|
---|
198 | var y = 0 ;
|
---|
199 | if ( FCKBrowserInfo.IsIE )
|
---|
200 | {
|
---|
201 | x = ev.screenX ;
|
---|
202 | y = ev.screenY ;
|
---|
203 | }
|
---|
204 | else if ( FCKBrowserInfo.IsSafari )
|
---|
205 | {
|
---|
206 | x = ev.clientX ;
|
---|
207 | y = ev.clientY ;
|
---|
208 | }
|
---|
209 | else
|
---|
210 | {
|
---|
211 | x = ev.pageX ;
|
---|
212 | y = ev.pageY ;
|
---|
213 | }
|
---|
214 | fckContextMenu._Panel.Show( x, y, ev.currentTarget || null ) ;
|
---|
215 |
|
---|
216 | return false ;
|
---|
217 | }
|
---|
218 |
|
---|
219 | function FCKContextMenu_MenuBlock_OnClick( menuItem, contextMenu )
|
---|
220 | {
|
---|
221 | contextMenu._Panel.Hide() ;
|
---|
222 | FCKTools.RunFunction( contextMenu.OnItemClick, contextMenu, menuItem ) ;
|
---|
223 | }
|
---|