| 1 |
/* |
| 2 |
|
| 3 |
* FCKeditor - The text editor for Internet - http://www.fckeditor.net |
| 4 |
|
| 5 |
* Copyright (C) 2003-2009 Frederico Caldeira Knabben |
| 6 |
|
| 7 |
* |
| 8 |
|
| 9 |
* == BEGIN LICENSE == |
| 10 |
|
| 11 |
* |
| 12 |
|
| 13 |
* Licensed under the terms of any of the following licenses at your |
| 14 |
|
| 15 |
* choice: |
| 16 |
|
| 17 |
* |
| 18 |
|
| 19 |
* - GNU General Public License Version 2 or later (the "GPL") |
| 20 |
|
| 21 |
* http://www.gnu.org/licenses/gpl.html |
| 22 |
|
| 23 |
* |
| 24 |
|
| 25 |
* - GNU Lesser General Public License Version 2.1 or later (the "LGPL") |
| 26 |
|
| 27 |
* http://www.gnu.org/licenses/lgpl.html |
| 28 |
|
| 29 |
* |
| 30 |
|
| 31 |
* - Mozilla Public License Version 1.1 or later (the "MPL") |
| 32 |
|
| 33 |
* http://www.mozilla.org/MPL/MPL-1.1.html |
| 34 |
|
| 35 |
* |
| 36 |
|
| 37 |
* == END LICENSE == |
| 38 |
|
| 39 |
* |
| 40 |
|
| 41 |
* FCKContextMenu Class: renders an control a context menu. |
| 42 |
|
| 43 |
*/ |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
var FCKContextMenu = function( parentWindow, langDir ) |
| 48 |
|
| 49 |
{ |
| 50 |
|
| 51 |
this.CtrlDisable = false ; |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
var oPanel = this._Panel = new FCKPanel( parentWindow ) ; |
| 56 |
|
| 57 |
oPanel.AppendStyleSheet( FCKConfig.SkinEditorCSS ) ; |
| 58 |
|
| 59 |
oPanel.IsContextMenu = true ; |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
// The FCKTools.DisableSelection doesn't seems to work to avoid dragging of the icons in Mozilla |
| 64 |
|
| 65 |
// so we stop the start of the dragging |
| 66 |
|
| 67 |
if ( FCKBrowserInfo.IsGecko ) |
| 68 |
|
| 69 |
oPanel.Document.addEventListener( 'draggesture', function(e) {e.preventDefault(); return false;}, true ) ; |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
var oMenuBlock = this._MenuBlock = new FCKMenuBlock() ; |
| 74 |
|
| 75 |
oMenuBlock.Panel = oPanel ; |
| 76 |
|
| 77 |
oMenuBlock.OnClick = FCKTools.CreateEventListener( FCKContextMenu_MenuBlock_OnClick, this ) ; |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
this._Redraw = true ; |
| 82 |
|
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
FCKContextMenu.prototype.SetMouseClickWindow = function( mouseClickWindow ) |
| 90 |
|
| 91 |
{ |
| 92 |
|
| 93 |
if ( !FCKBrowserInfo.IsIE ) |
| 94 |
|
| 95 |
{ |
| 96 |
|
| 97 |
this._Document = mouseClickWindow.document ; |
| 98 |
|
| 99 |
if ( FCKBrowserInfo.IsOpera && !( 'oncontextmenu' in document.createElement('foo') ) ) |
| 100 |
|
| 101 |
{ |
| 102 |
|
| 103 |
this._Document.addEventListener( 'mousedown', FCKContextMenu_Document_OnMouseDown, false ) ; |
| 104 |
|
| 105 |
this._Document.addEventListener( 'mouseup', FCKContextMenu_Document_OnMouseUp, false ) ; |
| 106 |
|
| 107 |
} |
| 108 |
|
| 109 |
this._Document.addEventListener( 'contextmenu', FCKContextMenu_Document_OnContextMenu, false ) ; |
| 110 |
|
| 111 |
} |
| 112 |
|
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
/** |
| 118 |
|
| 119 |
The customData parameter is just a value that will be send to the command that is executed, |
| 120 |
|
| 121 |
so it's possible to reuse the same command for several items just by assigning different data for each one. |
| 122 |
|
| 123 |
*/ |
| 124 |
|
| 125 |
FCKContextMenu.prototype.AddItem = function( name, label, iconPathOrStripInfoArrayOrIndex, isDisabled, customData ) |
| 126 |
|
| 127 |
{ |
| 128 |
|
| 129 |
var oItem = this._MenuBlock.AddItem( name, label, iconPathOrStripInfoArrayOrIndex, isDisabled, customData ) ; |
| 130 |
|
| 131 |
this._Redraw = true ; |
| 132 |
|
| 133 |
return oItem ; |
| 134 |
|
| 135 |
} |
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
FCKContextMenu.prototype.AddSeparator = function() |
| 140 |
|
| 141 |
{ |
| 142 |
|
| 143 |
this._MenuBlock.AddSeparator() ; |
| 144 |
|
| 145 |
this._Redraw = true ; |
| 146 |
|
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
FCKContextMenu.prototype.RemoveAllItems = function() |
| 152 |
|
| 153 |
{ |
| 154 |
|
| 155 |
this._MenuBlock.RemoveAllItems() ; |
| 156 |
|
| 157 |
this._Redraw = true ; |
| 158 |
|
| 159 |
} |
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
FCKContextMenu.prototype.AttachToElement = function( element ) |
| 164 |
|
| 165 |
{ |
| 166 |
|
| 167 |
if ( FCKBrowserInfo.IsIE ) |
| 168 |
|
| 169 |
FCKTools.AddEventListenerEx( element, 'contextmenu', FCKContextMenu_AttachedElement_OnContextMenu, this ) ; |
| 170 |
|
| 171 |
else |
| 172 |
|
| 173 |
element._FCKContextMenu = this ; |
| 174 |
|
| 175 |
} |
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
function FCKContextMenu_Document_OnContextMenu( e ) |
| 180 |
|
| 181 |
{ |
| 182 |
|
| 183 |
if ( FCKConfig.BrowserContextMenu ) |
| 184 |
|
| 185 |
return true ; |
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
var el = e.target ; |
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
while ( el ) |
| 194 |
|
| 195 |
{ |
| 196 |
|
| 197 |
if ( el._FCKContextMenu ) |
| 198 |
|
| 199 |
{ |
| 200 |
|
| 201 |
if ( el._FCKContextMenu.CtrlDisable && ( e.ctrlKey || e.metaKey ) ) |
| 202 |
|
| 203 |
return true ; |
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
FCKTools.CancelEvent( e ) ; |
| 208 |
|
| 209 |
FCKContextMenu_AttachedElement_OnContextMenu( e, el._FCKContextMenu, el ) ; |
| 210 |
|
| 211 |
return false ; |
| 212 |
|
| 213 |
} |
| 214 |
|
| 215 |
el = el.parentNode ; |
| 216 |
|
| 217 |
} |
| 218 |
|
| 219 |
return true ; |
| 220 |
|
| 221 |
} |
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
var FCKContextMenu_OverrideButton ; |
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
function FCKContextMenu_Document_OnMouseDown( e ) |
| 230 |
|
| 231 |
{ |
| 232 |
|
| 233 |
if( !e || e.button != 2 ) |
| 234 |
|
| 235 |
return false ; |
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
if ( FCKConfig.BrowserContextMenu ) |
| 240 |
|
| 241 |
return true ; |
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
var el = e.target ; |
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
while ( el ) |
| 250 |
|
| 251 |
{ |
| 252 |
|
| 253 |
if ( el._FCKContextMenu ) |
| 254 |
|
| 255 |
{ |
| 256 |
|
| 257 |
if ( el._FCKContextMenu.CtrlDisable && ( e.ctrlKey || e.metaKey ) ) |
| 258 |
|
| 259 |
return true ; |
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
var overrideButton = FCKContextMenu_OverrideButton ; |
| 264 |
|
| 265 |
if( !overrideButton ) |
| 266 |
|
| 267 |
{ |
| 268 |
|
| 269 |
var doc = FCKTools.GetElementDocument( e.target ) ; |
| 270 |
|
| 271 |
overrideButton = FCKContextMenu_OverrideButton = doc.createElement('input') ; |
| 272 |
|
| 273 |
overrideButton.type = 'button' ; |
| 274 |
|
| 275 |
var buttonHolder = doc.createElement('p') ; |
| 276 |
|
| 277 |
doc.body.appendChild( buttonHolder ) ; |
| 278 |
|
| 279 |
buttonHolder.appendChild( overrideButton ) ; |
| 280 |
|
| 281 |
} |
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
overrideButton.style.cssText = 'position:absolute;top:' + ( e.clientY - 2 ) + |
| 286 |
|
| 287 |
'px;left:' + ( e.clientX - 2 ) + |
| 288 |
|
| 289 |
'px;width:5px;height:5px;opacity:0.01' ; |
| 290 |
|
| 291 |
} |
| 292 |
|
| 293 |
el = el.parentNode ; |
| 294 |
|
| 295 |
} |
| 296 |
|
| 297 |
return false ; |
| 298 |
|
| 299 |
} |
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
function FCKContextMenu_Document_OnMouseUp( e ) |
| 304 |
|
| 305 |
{ |
| 306 |
|
| 307 |
if ( FCKConfig.BrowserContextMenu ) |
| 308 |
|
| 309 |
return true ; |
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
var overrideButton = FCKContextMenu_OverrideButton ; |
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
if ( overrideButton ) |
| 318 |
|
| 319 |
{ |
| 320 |
|
| 321 |
var parent = overrideButton.parentNode ; |
| 322 |
|
| 323 |
parent.parentNode.removeChild( parent ) ; |
| 324 |
|
| 325 |
FCKContextMenu_OverrideButton = undefined ; |
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
if( e && e.button == 2 ) |
| 330 |
|
| 331 |
{ |
| 332 |
|
| 333 |
FCKContextMenu_Document_OnContextMenu( e ) ; |
| 334 |
|
| 335 |
return false ; |
| 336 |
|
| 337 |
} |
| 338 |
|
| 339 |
} |
| 340 |
|
| 341 |
return true ; |
| 342 |
|
| 343 |
} |
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
function FCKContextMenu_AttachedElement_OnContextMenu( ev, fckContextMenu, el ) |
| 348 |
|
| 349 |
{ |
| 350 |
|
| 351 |
if ( ( fckContextMenu.CtrlDisable && ( ev.ctrlKey || ev.metaKey ) ) || FCKConfig.BrowserContextMenu ) |
| 352 |
|
| 353 |
return true ; |
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
var eTarget = el || this ; |
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
if ( fckContextMenu.OnBeforeOpen ) |
| 362 |
|
| 363 |
fckContextMenu.OnBeforeOpen.call( fckContextMenu, eTarget ) ; |
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
if ( fckContextMenu._MenuBlock.Count() == 0 ) |
| 368 |
|
| 369 |
return false ; |
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
if ( fckContextMenu._Redraw ) |
| 374 |
|
| 375 |
{ |
| 376 |
|
| 377 |
fckContextMenu._MenuBlock.Create( fckContextMenu._Panel.MainNode ) ; |
| 378 |
|
| 379 |
fckContextMenu._Redraw = false ; |
| 380 |
|
| 381 |
} |
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
// This will avoid that the content of the context menu can be dragged in IE |
| 386 |
|
| 387 |
// as the content of the panel is recreated we need to do it every time |
| 388 |
|
| 389 |
FCKTools.DisableSelection( fckContextMenu._Panel.Document.body ) ; |
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
var x = 0 ; |
| 394 |
|
| 395 |
var y = 0 ; |
| 396 |
|
| 397 |
if ( FCKBrowserInfo.IsIE ) |
| 398 |
|
| 399 |
{ |
| 400 |
|
| 401 |
x = ev.screenX ; |
| 402 |
|
| 403 |
y = ev.screenY ; |
| 404 |
|
| 405 |
} |
| 406 |
|
| 407 |
else if ( FCKBrowserInfo.IsSafari ) |
| 408 |
|
| 409 |
{ |
| 410 |
|
| 411 |
x = ev.clientX ; |
| 412 |
|
| 413 |
y = ev.clientY ; |
| 414 |
|
| 415 |
} |
| 416 |
|
| 417 |
else |
| 418 |
|
| 419 |
{ |
| 420 |
|
| 421 |
x = ev.pageX ; |
| 422 |
|
| 423 |
y = ev.pageY ; |
| 424 |
|
| 425 |
} |
| 426 |
|
| 427 |
fckContextMenu._Panel.Show( x, y, ev.currentTarget || null ) ; |
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
return false ; |
| 432 |
|
| 433 |
} |
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
function FCKContextMenu_MenuBlock_OnClick( menuItem, contextMenu ) |
| 438 |
|
| 439 |
{ |
| 440 |
|
| 441 |
contextMenu._Panel.Hide() ; |
| 442 |
|
| 443 |
FCKTools.RunFunction( contextMenu.OnItemClick, contextMenu, menuItem ) ; |
| 444 |
|
| 445 |
} |
| 446 |
|
| 447 |
|