X-Git-Url: https://jasonwoof.com/gitweb/?a=blobdiff_plain;f=_source%2Fplugins%2Fcontextmenu%2Fplugin.js;h=7772820e4f0ad813badf19fe756949e5a0653b27;hb=a272c66d841421f8bf933c16535bdcde1c4649fc;hp=b009dee02344480c88f80fdbbb243358aa9f716b;hpb=8665a7c6c60586526e32e8941fe2896739b6ebfb;p=ckeditor.git
diff --git a/_source/plugins/contextmenu/plugin.js b/_source/plugins/contextmenu/plugin.js
index b009dee..7772820 100644
--- a/_source/plugins/contextmenu/plugin.js
+++ b/_source/plugins/contextmenu/plugin.js
@@ -1,5 +1,5 @@
/*
-Copyright (c) 2003-2010, CKSource - Frederico Knabben. All rights reserved.
+Copyright (c) 2003-2013, CKSource - Frederico Knabben. All rights reserved.
For licensing, see LICENSE.html or http://ckeditor.com/license
*/
@@ -7,270 +7,173 @@ CKEDITOR.plugins.add( 'contextmenu',
{
requires : [ 'menu' ],
- beforeInit : function( editor )
+ // Make sure the base class (CKEDITOR.menu) is loaded before it (#3318).
+ onLoad : function()
{
- editor.contextMenu = new CKEDITOR.plugins.contextMenu( editor );
+ CKEDITOR.plugins.contextMenu = CKEDITOR.tools.createClass(
+ {
+ base : CKEDITOR.menu,
- editor.addCommand( 'contextMenu',
+ $ : function( editor )
{
- exec : function()
+ this.base.call( this, editor,
+ {
+ panel:
{
- editor.contextMenu.show( editor.document.getBody() );
+ className : editor.skinClass + ' cke_contextmenu',
+ attributes :
+ {
+ 'aria-label' : editor.lang.contextmenu.options
+ }
}
- });
- }
-});
-
-CKEDITOR.plugins.contextMenu = CKEDITOR.tools.createClass(
-{
- $ : function( editor )
- {
- this.id = 'cke_' + CKEDITOR.tools.getNextNumber();
- this.editor = editor;
- this._.listeners = [];
- this._.functionId = CKEDITOR.tools.addFunction( function( commandName )
- {
- this._.panel.hide();
- editor.focus();
- editor.execCommand( commandName );
+ });
},
- this);
- this.definition =
- {
- panel:
+ proto :
{
- className : editor.skinClass + ' cke_contextmenu',
- attributes :
+ addTarget : function( element, nativeContextMenuOnCtrl )
{
- 'aria-label' : editor.lang.contextmenu.options
- }
- }
- };
- },
-
- _ :
- {
- onMenu : function( offsetParent, corner, offsetX, offsetY )
- {
- var menu = this._.menu,
- editor = this.editor;
-
- if ( menu )
- {
- menu.hide();
- menu.removeAll();
- }
- else
- {
- menu = this._.menu = new CKEDITOR.menu( editor, this.definition );
- menu.onClick = CKEDITOR.tools.bind( function( item )
- {
- menu.hide();
-
- if ( item.onClick )
- item.onClick();
- else if ( item.command )
- editor.execCommand( item.command );
-
- }, this );
-
- menu.onEscape = function( keystroke )
- {
- var parent = this.parent;
- // 1. If it's sub-menu, restore the last focused item
- // of upper level menu.
- // 2. In case of a top-menu, close it.
- if ( parent )
- {
- parent._.panel.hideChild();
- // Restore parent block item focus.
- var parentBlock = parent._.panel._.panel._.currentBlock,
- parentFocusIndex = parentBlock._.focusIndex;
- parentBlock._.markItem( parentFocusIndex );
- }
- else if ( keystroke == 27 )
+ // Opera doesn't support 'contextmenu' event, we have duo approaches employed here:
+ // 1. Inherit the 'button override' hack we introduced in v2 (#4530), while this require the Opera browser
+ // option 'Allow script to detect context menu/right click events' to be always turned on.
+ // 2. Considering the fact that ctrl/meta key is not been occupied
+ // for multiple range selecting (like Gecko), we use this key
+ // combination as a fallback for triggering context-menu. (#4530)
+ if ( CKEDITOR.env.opera && !( 'oncontextmenu' in document.body ))
{
- this.hide();
- editor.focus();
+ var contextMenuOverrideButton;
+ element.on( 'mousedown', function( evt )
+ {
+ evt = evt.data;
+ if ( evt.$.button != 2 )
+ {
+ if ( evt.getKeystroke() == CKEDITOR.CTRL + 1 )
+ element.fire( 'contextmenu', evt );
+ return;
+ }
+
+ if ( nativeContextMenuOnCtrl
+ && ( CKEDITOR.env.mac ? evt.$.metaKey : evt.$.ctrlKey ) )
+ return;
+
+ var target = evt.getTarget();
+
+ if ( !contextMenuOverrideButton )
+ {
+ var ownerDoc = target.getDocument();
+ contextMenuOverrideButton = ownerDoc.createElement( 'input' ) ;
+ contextMenuOverrideButton.$.type = 'button' ;
+ ownerDoc.getBody().append( contextMenuOverrideButton ) ;
+ }
+
+ contextMenuOverrideButton.setAttribute( 'style', 'position:absolute;top:' + ( evt.$.clientY - 2 ) +
+ 'px;left:' + ( evt.$.clientX - 2 ) +
+ 'px;width:5px;height:5px;opacity:0.01' );
+
+ } );
+
+ element.on( 'mouseup', function ( evt )
+ {
+ if ( contextMenuOverrideButton )
+ {
+ contextMenuOverrideButton.remove();
+ contextMenuOverrideButton = undefined;
+ // Simulate 'contextmenu' event.
+ element.fire( 'contextmenu', evt.data );
+ }
+ } );
}
- return false;
- };
- }
- var listeners = this._.listeners,
- includedItems = [];
+ element.on( 'contextmenu', function( event )
+ {
+ var domEvent = event.data;
- var selection = this.editor.getSelection(),
- element = selection && selection.getStartElement();
+ if ( nativeContextMenuOnCtrl &&
+ // Safari on Windows always show 'ctrlKey' as true in 'contextmenu' event,
+ // which make this property unreliable. (#4826)
+ ( CKEDITOR.env.webkit ? holdCtrlKey : ( CKEDITOR.env.mac ? domEvent.$.metaKey : domEvent.$.ctrlKey ) ) )
+ return;
- menu.onHide = CKEDITOR.tools.bind( function()
- {
- menu.onHide = null;
- if ( CKEDITOR.env.ie )
- {
- var selection = editor.getSelection();
- selection && selection.unlock();
- }
+ // Cancel the browser context menu.
+ domEvent.preventDefault();
- this.onHide && this.onHide();
- },
- this );
+ var offsetParent = domEvent.getTarget().getDocument().getDocumentElement(),
+ offsetX = domEvent.$.clientX,
+ offsetY = domEvent.$.clientY;
- // Call all listeners, filling the list of items to be displayed.
- for ( var i = 0 ; i < listeners.length ; i++ )
- {
- var listenerItems = listeners[ i ]( element, selection );
+ CKEDITOR.tools.setTimeout( function()
+ {
+ this.open( offsetParent, null, offsetX, offsetY );
- if ( listenerItems )
- {
- for ( var itemName in listenerItems )
- {
- var item = this.editor.getMenuItem( itemName );
+ // IE needs a short while to allow selection change before opening menu. (#7908)
+ }, CKEDITOR.env.ie? 200 : 0, this );
+ },
+ this );
- if ( item )
+ if ( CKEDITOR.env.opera )
+ {
+ // 'contextmenu' event triggered by Windows menu key is unpreventable,
+ // cancel the key event itself. (#6534)
+ element.on( 'keypress' , function ( evt )
{
- item.state = listenerItems[ itemName ];
- menu.add( item );
- }
- }
- }
- }
+ var domEvent = evt.data;
- // Don't show context menu with zero items.
- menu.items.length && menu.show( offsetParent, corner || ( editor.lang.dir == 'rtl' ? 2 : 1 ), offsetX, offsetY );
- }
- },
-
- proto :
- {
- addTarget : function( element, nativeContextMenuOnCtrl )
- {
- // Opera doesn't support 'contextmenu' event, we have duo approaches employed here:
- // 1. Inherit the 'button override' hack we introduced in v2 (#4530), while this require the Opera browser
- // option 'Allow script to detect context menu/right click events' to be always turned on.
- // 2. Considering the fact that ctrl/meta key is not been occupied
- // for multiple range selecting (like Gecko), we use this key
- // combination as a fallback for triggering context-menu. (#4530)
- if ( CKEDITOR.env.opera )
- {
- var contextMenuOverrideButton;
- element.on( 'mousedown', function( evt )
- {
- evt = evt.data;
- if ( evt.$.button != 2 )
- {
- if ( evt.getKeystroke() == CKEDITOR.CTRL + 1 )
- element.fire( 'contextmenu', evt );
- return;
+ if ( domEvent.$.keyCode === 0 )
+ domEvent.preventDefault();
+ });
}
- if ( nativeContextMenuOnCtrl
- && ( CKEDITOR.env.mac ? evt.$.metaKey : evt.$.ctrlKey ) )
- return;
-
- var target = evt.getTarget();
-
- if ( !contextMenuOverrideButton )
+ if ( CKEDITOR.env.webkit )
{
- var ownerDoc = target.getDocument();
- contextMenuOverrideButton = ownerDoc.createElement( 'input' ) ;
- contextMenuOverrideButton.$.type = 'button' ;
- ownerDoc.getBody().append( contextMenuOverrideButton ) ;
+ var holdCtrlKey,
+ onKeyDown = function( event )
+ {
+ holdCtrlKey = CKEDITOR.env.mac ? event.data.$.metaKey : event.data.$.ctrlKey ;
+ },
+ resetOnKeyUp = function()
+ {
+ holdCtrlKey = 0;
+ };
+
+ element.on( 'keydown', onKeyDown );
+ element.on( 'keyup', resetOnKeyUp );
+ element.on( 'contextmenu', resetOnKeyUp );
}
+ },
- contextMenuOverrideButton.setAttribute( 'style', 'position:absolute;top:' + ( evt.$.clientY - 2 ) +
- 'px;left:' + ( evt.$.clientX - 2 ) +
- 'px;width:5px;height:5px;opacity:0.01' );
-
- } );
-
- element.on( 'mouseup', function ( evt )
+ open : function( offsetParent, corner, offsetX, offsetY )
{
- if ( contextMenuOverrideButton )
- {
- contextMenuOverrideButton.remove();
- contextMenuOverrideButton = undefined;
- // Simulate 'contextmenu' event.
- element.fire( 'contextmenu', evt.data );
- }
- } );
+ this.editor.focus();
+ offsetParent = offsetParent || CKEDITOR.document.getDocumentElement();
+ this.show( offsetParent, corner, offsetX, offsetY );
+ }
}
+ });
+ },
- element.on( 'contextmenu', function( event )
- {
- var domEvent = event.data;
-
- if ( nativeContextMenuOnCtrl &&
- // Safari on Windows always show 'ctrlKey' as true in 'contextmenu' event,
- // which make this property unreliable. (#4826)
- ( CKEDITOR.env.webkit ? holdCtrlKey : ( CKEDITOR.env.mac ? domEvent.$.metaKey : domEvent.$.ctrlKey ) ) )
- return;
-
-
- // Cancel the browser context menu.
- domEvent.preventDefault();
-
- var offsetParent = domEvent.getTarget().getDocument().getDocumentElement(),
- offsetX = domEvent.$.clientX,
- offsetY = domEvent.$.clientY;
-
- CKEDITOR.tools.setTimeout( function()
- {
- this.show( offsetParent, null, offsetX, offsetY );
- },
- 0, this );
- },
- this );
+ beforeInit : function( editor )
+ {
+ editor.contextMenu = new CKEDITOR.plugins.contextMenu( editor );
- if ( CKEDITOR.env.webkit )
+ editor.addCommand( 'contextMenu',
{
- var holdCtrlKey,
- onKeyDown = function( event )
- {
- holdCtrlKey = CKEDITOR.env.mac ? event.data.$.metaKey : event.data.$.ctrlKey ;
- },
- resetOnKeyUp = function()
+ exec : function()
{
- holdCtrlKey = 0;
- };
-
- element.on( 'keydown', onKeyDown );
- element.on( 'keyup', resetOnKeyUp );
- element.on( 'contextmenu', resetOnKeyUp );
- }
- },
-
- addListener : function( listenerFn )
- {
- this._.listeners.push( listenerFn );
- },
-
- show : function( offsetParent, corner, offsetX, offsetY )
- {
- this.editor.focus();
-
- // Selection will be unavailable after context menu shows up
- // in IE, lock it now.
- if ( CKEDITOR.env.ie )
- {
- var selection = this.editor.getSelection();
- selection && selection.lock();
- }
-
- this._.onMenu( offsetParent || CKEDITOR.document.getDocumentElement(), corner, offsetX || 0, offsetY || 0 );
- }
+ editor.contextMenu.open( editor.document.getBody() );
+ }
+ });
}
});
/**
- * Whether to show the browser native context menu when the CTRL or the
- * META (Mac) key is pressed while opening the context menu.
+ * Whether to show the browser native context menu when the Ctrl or
+ * Meta (Mac) key is pressed on opening the context menu with the
+ * right mouse button click or the Menu key.
* @name CKEDITOR.config.browserContextMenuOnCtrl
* @since 3.0.2
* @type Boolean
- * @default true
+ * @default true
* @example
* config.browserContextMenuOnCtrl = false;
*/