2 Copyright (c) 2003-2009, CKSource - Frederico Knabben. All rights reserved.
\r
3 For licensing, see LICENSE.html or http://ckeditor.com/license
\r
6 CKEDITOR.plugins.add( 'contextmenu',
\r
8 requires : [ 'menu' ],
\r
10 beforeInit : function( editor )
\r
12 editor.contextMenu = new CKEDITOR.plugins.contextMenu( editor );
\r
14 editor.addCommand( 'contextMenu',
\r
18 editor.contextMenu.show();
\r
24 CKEDITOR.plugins.contextMenu = CKEDITOR.tools.createClass(
\r
26 $ : function( editor )
\r
28 this.id = 'cke_' + CKEDITOR.tools.getNextNumber();
\r
29 this.editor = editor;
\r
30 this._.listeners = [];
\r
31 this._.functionId = CKEDITOR.tools.addFunction( function( commandName )
\r
33 this._.panel.hide();
\r
35 editor.execCommand( commandName );
\r
42 onMenu : function( offsetParent, corner, offsetX, offsetY )
\r
44 var menu = this._.menu,
\r
45 editor = this.editor;
\r
54 menu = this._.menu = new CKEDITOR.menu( editor );
\r
55 menu.onClick = CKEDITOR.tools.bind( function( item )
\r
57 var noUnlock = true;
\r
60 if ( CKEDITOR.env.ie )
\r
65 else if ( item.command )
\r
66 editor.execCommand( item.command );
\r
71 menu.onEscape = function()
\r
75 if ( CKEDITOR.env.ie )
\r
76 editor.getSelection().unlock( true );
\r
80 var listeners = this._.listeners,
\r
83 var selection = this.editor.getSelection(),
\r
84 element = selection && selection.getStartElement();
\r
86 // Lock the selection in IE, so it can be restored when closing the
\r
88 if ( CKEDITOR.env.ie )
\r
91 menu.onHide = CKEDITOR.tools.bind( function()
\r
95 if ( CKEDITOR.env.ie )
\r
96 editor.getSelection().unlock();
\r
98 this.onHide && this.onHide();
\r
102 // Call all listeners, filling the list of items to be displayed.
\r
103 for ( var i = 0 ; i < listeners.length ; i++ )
\r
105 var listenerItems = listeners[ i ]( element, selection );
\r
107 if ( listenerItems )
\r
109 for ( var itemName in listenerItems )
\r
111 var item = this.editor.getMenuItem( itemName );
\r
115 item.state = listenerItems[ itemName ];
\r
122 menu.show( offsetParent, corner || ( editor.lang.dir == 'rtl' ? 2 : 1 ), offsetX, offsetY );
\r
128 addTarget : function( element )
\r
130 element.on( 'contextmenu', function( event )
\r
132 var domEvent = event.data;
\r
134 // Cancel the browser context menu.
\r
135 domEvent.preventDefault();
\r
137 var offsetParent = domEvent.getTarget().getDocument().getDocumentElement(),
\r
138 offsetX = domEvent.$.clientX,
\r
139 offsetY = domEvent.$.clientY;
\r
141 CKEDITOR.tools.setTimeout( function()
\r
143 this._.onMenu( offsetParent, null, offsetX, offsetY );
\r
150 addListener : function( listenerFn )
\r
152 this._.listeners.push( listenerFn );
\r
155 show : function( offsetParent, corner, offsetX, offsetY )
\r
157 this.editor.focus();
\r
158 this._.onMenu( offsetParent || CKEDITOR.document.getDocumentElement(), corner, offsetX || 0, offsetY || 0 );
\r
163 // Fix the "contextmenu" event for DOM elements.
\r
164 // We may do this if we identify browsers that don't support the context meny
\r
165 // event on element directly. Leaving here for reference.
\r
166 //if ( <specific browsers> )
\r
168 // CKEDITOR.dom.element.prototype.on = CKEDITOR.tools.override( CKEDITOR.dom.element.prototype.on, function( originalOn )
\r
170 // return function( eventName )
\r
172 // if ( eventName != 'contextmenu' )
\r
173 // return originalOn.apply( this, arguments );
\r
175 // // TODO : Implement the fix.
\r