Skip to content

Commit

Permalink
Merge branch 't/11114-initial-cleanup'
Browse files Browse the repository at this point in the history
  • Loading branch information
Reinmar committed Nov 25, 2013
2 parents b734293 + 63f5c9a commit bace56b
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 66 deletions.
102 changes: 36 additions & 66 deletions plugins/contextmenu/plugin.js
Expand Up @@ -10,14 +10,19 @@ CKEDITOR.plugins.add( 'contextmenu', {
// Make sure the base class (CKEDITOR.menu) is loaded before it (#3318).
onLoad: function() {
/**
* Class replacing the non-configurable native context menu with configurable CKEditor's equivalent.
*
* @class
* @extends CKEDITOR.menu
*/
CKEDITOR.plugins.contextMenu = CKEDITOR.tools.createClass({
CKEDITOR.plugins.contextMenu = CKEDITOR.tools.createClass( {
base: CKEDITOR.menu,

/**
* Creates the CKEDITOR.plugins.contextMenu class instance.
*
* @constructor
* @param {CKEDITOR.editor} editor
*/
$: function( editor ) {
this.base.call( this, editor, {
Expand All @@ -27,65 +32,28 @@ CKEDITOR.plugins.add( 'contextmenu', {
'aria-label': editor.lang.contextmenu.options
}
}
});
} );
},

proto: {
/**
* Starts watching on native context menu triggers (option key, right click) on given element.
*
* @param {CKEDITOR.dom.element} element
* @param {Boolean} [nativeContextMenuOnCtrl] Whether to open native context menu if
* *Ctrl* key is hold on opening the context menu. See {@link CKEDITOR.config#browserContextMenuOnCtrl}.
*/
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 && !( 'oncontextmenu' in document.body ) ) {
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 );
}
});
}

element.on( 'contextmenu', function( event ) {
var domEvent = event.data;
var domEvent = event.data,
isCtrlKeyDown =
// 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 ) );

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 ) ) )
if ( nativeContextMenuOnCtrl && isCtrlKeyDown )
return;


// Cancel the browser context menu.
domEvent.preventDefault();

Expand All @@ -103,17 +71,6 @@ CKEDITOR.plugins.add( 'contextmenu', {
}, CKEDITOR.env.ie ? 200 : 0, this );
}, this );

if ( CKEDITOR.env.opera ) {
// 'contextmenu' event triggered by Windows menu key is unpreventable,
// cancel the key event itself. (#6534)
element.on( 'keypress', function( evt ) {
var domEvent = evt.data;

if ( domEvent.$.keyCode === 0 )
domEvent.preventDefault();
});
}

if ( CKEDITOR.env.webkit ) {
var holdCtrlKey,
onKeyDown = function( event ) {
Expand All @@ -129,6 +86,14 @@ CKEDITOR.plugins.add( 'contextmenu', {
}
},

/**
* Opens context menu in given location. See the {@link CKEDITOR.menu#show} method.
*
* @param {CKEDITOR.dom.element} offsetParent
* @param {Number} [corner]
* @param {Number} [offsetX]
* @param {Number} [offsetY]
*/
open: function( offsetParent, corner, offsetX, offsetY ) {
this.editor.focus();
offsetParent = offsetParent || CKEDITOR.document.getDocumentElement();
Expand All @@ -143,22 +108,27 @@ CKEDITOR.plugins.add( 'contextmenu', {
},

beforeInit: function( editor ) {
/**
* @readonly
* @property {CKEDITOR.plugins.contextMenu} contextMenu
* @member CKEDITOR.editor
*/
var contextMenu = editor.contextMenu = new CKEDITOR.plugins.contextMenu( editor );

editor.on( 'contentDom', function() {
contextMenu.addTarget( editor.editable(), editor.config.browserContextMenuOnCtrl !== false );
});
} );

editor.addCommand( 'contextMenu', {
exec: function() {
editor.contextMenu.open( editor.document.getBody() );
}
});
} );

editor.setKeystroke( CKEDITOR.SHIFT + 121 /*F10*/, 'contextMenu' );
editor.setKeystroke( CKEDITOR.CTRL + CKEDITOR.SHIFT + 121 /*F10*/, 'contextMenu' );
}
});
} );

/**
* Whether to show the browser native context menu when the *Ctrl* or
Expand Down
34 changes: 34 additions & 0 deletions plugins/menu/plugin.js
Expand Up @@ -133,6 +133,9 @@ CKEDITOR.plugins.add( 'menu', {
* @todo
*/
CKEDITOR.menu = CKEDITOR.tools.createClass({
/**
* @constructor
*/
$: function( editor, definition ) {
definition = this._.definition = definition || {};
this.id = CKEDITOR.tools.getNextId();
Expand Down Expand Up @@ -250,6 +253,11 @@ CKEDITOR.plugins.add( 'menu', {
},

proto: {
/**
* Adds an item.
*
* @param item
*/
add: function( item ) {
// Later we may sort the items, but Array#sort is not stable in
// some browsers, here we're forcing the original sequence with
Expand All @@ -260,10 +268,21 @@ CKEDITOR.plugins.add( 'menu', {
this.items.push( item );
},

/**
* Removes all items.
*/
removeAll: function() {
this.items = [];
},

/**
* Shows the menu in given location.
*
* @param {CKEDITOR.dom.element} offsetParent
* @param {Number} [corner]
* @param {Number} [offsetX]
* @param {Number} [offsetY]
*/
show: function( offsetParent, corner, offsetX, offsetY ) {
// Not for sub menu.
if ( !this.parent ) {
Expand Down Expand Up @@ -381,10 +400,25 @@ CKEDITOR.plugins.add( 'menu', {
editor.fire( 'menuShow', [ panel ] );
},

/**
* Adds a callback executed on opening the menu. Items
* returned by that callback are added to the menu.
*
* @param {Function} listenerFn
* @param {CKEDITOR.dom.element} listenerFn.startElement The selection start anchor element.
* @param {CKEDITOR.dom.selection} listenerFn.selection The current selection.
* @param {CKEDITOR.dom.elementPath} listenerFn.path The current elements path.
* @param listenerFn.return Object (`commandName` => `state`) of items that should be added to the menu.
*/
addListener: function( listenerFn ) {
this._.listeners.push( listenerFn );
},

/**
* Hides the menu.
*
* @param {Boolean} [returnFocus]
*/
hide: function( returnFocus ) {
this._.onHide && this._.onHide();
this._.panel && this._.panel.hide( returnFocus );
Expand Down

0 comments on commit bace56b

Please sign in to comment.