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