2 Copyright (c) 2003-2013, CKSource - Frederico Knabben. All rights reserved.
\r
3 For licensing, see LICENSE.html or http://ckeditor.com/license
\r
6 CKEDITOR.plugins.add( 'menu',
\r
8 beforeInit : function( editor )
\r
10 var groups = editor.config.menu_groups.split( ',' ),
\r
11 groupsOrder = editor._.menuGroups = {},
\r
12 menuItems = editor._.menuItems = {};
\r
14 for ( var i = 0 ; i < groups.length ; i++ )
\r
15 groupsOrder[ groups[ i ] ] = i + 1;
\r
18 * Registers an item group to the editor context menu in order to make it
\r
19 * possible to associate it with menu items later.
\r
20 * @name CKEDITOR.editor.prototype.addMenuGroup
\r
21 * @param {String} name Specify a group name.
\r
22 * @param {Number} [order=100] Define the display sequence of this group
\r
23 * inside the menu. A smaller value gets displayed first.
\r
25 editor.addMenuGroup = function( name, order )
\r
27 groupsOrder[ name ] = order || 100;
\r
31 * Adds an item from the specified definition to the editor context menu.
\r
32 * @name CKEDITOR.editor.prototype.addMenuItem
\r
33 * @param {String} name The menu item name.
\r
34 * @param {CKEDITOR.menu.definition} definition The menu item definition.
\r
36 editor.addMenuItem = function( name, definition )
\r
38 if ( groupsOrder[ definition.group ] )
\r
39 menuItems[ name ] = new CKEDITOR.menuItem( this, name, definition );
\r
43 * Adds one or more items from the specified definition array to the editor context menu.
\r
44 * @name CKEDITOR.editor.prototype.addMenuItems
\r
45 * @param {Array} definitions List of definitions for each menu item as if {@link CKEDITOR.editor.addMenuItem} is called.
\r
47 editor.addMenuItems = function( definitions )
\r
49 for ( var itemName in definitions )
\r
51 this.addMenuItem( itemName, definitions[ itemName ] );
\r
56 * Retrieves a particular menu item definition from the editor context menu.
\r
57 * @name CKEDITOR.editor.prototype.getMenuItem
\r
58 * @param {String} name The name of the desired menu item.
\r
59 * @return {CKEDITOR.menu.definition}
\r
61 editor.getMenuItem = function( name )
\r
63 return menuItems[ name ];
\r
67 * Removes a particular menu item added before from the editor context menu.
\r
68 * @name CKEDITOR.editor.prototype.removeMenuItem
\r
69 * @param {String} name The name of the desired menu item.
\r
72 editor.removeMenuItem = function( name )
\r
74 delete menuItems[ name ];
\r
78 requires : [ 'floatpanel' ]
\r
83 CKEDITOR.menu = CKEDITOR.tools.createClass(
\r
85 $ : function( editor, definition )
\r
87 definition = this._.definition = definition || {};
\r
88 this.id = CKEDITOR.tools.getNextId();
\r
90 this.editor = editor;
\r
92 this._.listeners = [];
\r
94 this._.level = definition.level || 1;
\r
96 var panelDefinition = CKEDITOR.tools.extend( {}, definition.panel,
\r
98 css : editor.skin.editor.css,
\r
99 level : this._.level - 1,
\r
103 var attrs = panelDefinition.block.attributes = ( panelDefinition.attributes || {} );
\r
104 // Provide default role of 'menu'.
\r
105 !attrs.role && ( attrs.role = 'menu' );
\r
106 this._.panelDefinition = panelDefinition;
\r
111 onShow : function()
\r
113 var selection = this.editor.getSelection();
\r
115 // Selection will be unavailable after menu shows up
\r
116 // in IE, lock it now.
\r
117 if ( CKEDITOR.env.ie )
\r
118 selection && selection.lock();
\r
120 var element = selection && selection.getStartElement(),
\r
121 listeners = this._.listeners,
\r
122 includedItems = [];
\r
125 // Call all listeners, filling the list of items to be displayed.
\r
126 for ( var i = 0 ; i < listeners.length ; i++ )
\r
128 var listenerItems = listeners[ i ]( element, selection );
\r
130 if ( listenerItems )
\r
132 for ( var itemName in listenerItems )
\r
134 var item = this.editor.getMenuItem( itemName );
\r
136 if ( item && ( !item.command || this.editor.getCommand( item.command ).state ) )
\r
138 item.state = listenerItems[ itemName ];
\r
146 onClick : function( item )
\r
148 this.hide( false );
\r
150 if ( item.onClick )
\r
152 else if ( item.command )
\r
153 this.editor.execCommand( item.command );
\r
156 onEscape : function( keystroke )
\r
158 var parent = this.parent;
\r
159 // 1. If it's sub-menu, restore the last focused item
\r
160 // of upper level menu.
\r
161 // 2. In case of a top-menu, close it.
\r
164 parent._.panel.hideChild();
\r
165 // Restore parent block item focus.
\r
166 var parentBlock = parent._.panel._.panel._.currentBlock,
\r
167 parentFocusIndex = parentBlock._.focusIndex;
\r
168 parentBlock._.markItem( parentFocusIndex );
\r
170 else if ( keystroke == 27 )
\r
176 onHide : function()
\r
178 this._.unlockSelection();
\r
179 this.onHide && this.onHide();
\r
182 unlockSelection : function() {
\r
183 // Unlock the selection upon first panel closing.
\r
184 if ( CKEDITOR.env.ie && !this.parent )
\r
186 var selection = this.editor.getSelection();
\r
187 selection && selection.unlock( true );
\r
191 showSubMenu : function( index )
\r
193 var menu = this._.subMenu,
\r
194 item = this.items[ index ],
\r
195 subItemDefs = item.getItems && item.getItems();
\r
197 // If this item has no subitems, we just hide the submenu, if
\r
198 // available, and return back.
\r
199 if ( !subItemDefs )
\r
201 this._.panel.hideChild();
\r
205 // Record parent menu focused item first (#3389).
\r
206 var block = this._.panel.getBlock( this.id );
\r
207 block._.focusIndex = index;
\r
209 // Create the submenu, if not available, or clean the existing
\r
215 menu = this._.subMenu = new CKEDITOR.menu( this.editor,
\r
216 CKEDITOR.tools.extend( {}, this._.definition, { level : this._.level + 1 }, true ) );
\r
217 menu.parent = this;
\r
218 menu._.onClick = CKEDITOR.tools.bind( this._.onClick, this );
\r
221 // Add all submenu items to the menu.
\r
222 for ( var subItemName in subItemDefs )
\r
224 var subItem = this.editor.getMenuItem( subItemName );
\r
227 subItem.state = subItemDefs[ subItemName ];
\r
228 menu.add( subItem );
\r
232 // Get the element representing the current item.
\r
233 var element = this._.panel.getBlock( this.id ).element.getDocument().getById( this.id + String( index ) );
\r
235 // Show the submenu.
\r
236 menu.show( element, 2 );
\r
242 add : function( item )
\r
244 // Later we may sort the items, but Array#sort is not stable in
\r
245 // some browsers, here we're forcing the original sequence with
\r
246 // 'order' attribute if it hasn't been assigned. (#3868)
\r
248 item.order = this.items.length;
\r
250 this.items.push( item );
\r
253 removeAll : function()
\r
258 show : function( offsetParent, corner, offsetX, offsetY )
\r
260 // Not for sub menu.
\r
261 if ( !this.parent )
\r
264 // Don't menu with zero items.
\r
265 if ( ! this.items.length ) {
\r
266 this._.unlockSelection();
\r
271 corner = corner || ( this.editor.lang.dir == 'rtl' ? 2 : 1 );
\r
273 var items = this.items,
\r
274 editor = this.editor,
\r
275 panel = this._.panel,
\r
276 element = this._.element;
\r
278 // Create the floating panel for this menu.
\r
281 panel = this._.panel = new CKEDITOR.ui.floatPanel( this.editor,
\r
282 CKEDITOR.document.getBody(),
\r
283 this._.panelDefinition,
\r
286 panel.onEscape = CKEDITOR.tools.bind( function( keystroke )
\r
288 if ( this._.onEscape( keystroke ) === false )
\r
293 panel.onHide = CKEDITOR.tools.bind( function()
\r
295 this._.onHide && this._.onHide();
\r
299 // Create an autosize block inside the panel.
\r
300 var block = panel.addBlock( this.id, this._.panelDefinition.block );
\r
301 block.autoSize = true;
\r
303 var keys = block.keys;
\r
304 keys[ 40 ] = 'next'; // ARROW-DOWN
\r
305 keys[ 9 ] = 'next'; // TAB
\r
306 keys[ 38 ] = 'prev'; // ARROW-UP
\r
307 keys[ CKEDITOR.SHIFT + 9 ] = 'prev'; // SHIFT + TAB
\r
308 keys[ ( editor.lang.dir == 'rtl' ? 37 : 39 ) ]= CKEDITOR.env.ie ? 'mouseup' : 'click'; // ARROW-RIGHT/ARROW-LEFT(rtl)
\r
309 keys[ 32 ] = CKEDITOR.env.ie ? 'mouseup' : 'click'; // SPACE
\r
310 CKEDITOR.env.ie && ( keys[ 13 ] = 'mouseup' ); // Manage ENTER, since onclick is blocked in IE (#8041).
\r
312 element = this._.element = block.element;
\r
313 element.addClass( editor.skinClass );
\r
315 var elementDoc = element.getDocument();
\r
316 elementDoc.getBody().setStyle( 'overflow', 'hidden' );
\r
317 elementDoc.getElementsByTag( 'html' ).getItem( 0 ).setStyle( 'overflow', 'hidden' );
\r
319 this._.itemOverFn = CKEDITOR.tools.addFunction( function( index )
\r
321 clearTimeout( this._.showSubTimeout );
\r
322 this._.showSubTimeout = CKEDITOR.tools.setTimeout( this._.showSubMenu, editor.config.menu_subMenuDelay || 400, this, [ index ] );
\r
326 this._.itemOutFn = CKEDITOR.tools.addFunction( function( index )
\r
328 clearTimeout( this._.showSubTimeout );
\r
332 this._.itemClickFn = CKEDITOR.tools.addFunction( function( index )
\r
334 var item = this.items[ index ];
\r
336 if ( item.state == CKEDITOR.TRISTATE_DISABLED )
\r
342 if ( item.getItems )
\r
343 this._.showSubMenu( index );
\r
345 this._.onClick( item );
\r
350 // Put the items in the right order.
\r
351 sortItems( items );
\r
353 var chromeRoot = editor.container.getChild( 1 ),
\r
354 mixedContentClass = chromeRoot.hasClass( 'cke_mixed_dir_content' ) ? ' cke_mixed_dir_content' : '';
\r
356 // Build the HTML that composes the menu and its items.
\r
357 var output = [ '<div class="cke_menu' + mixedContentClass + '" role="presentation">' ];
\r
359 var length = items.length,
\r
360 lastGroup = length && items[ 0 ].group;
\r
362 for ( var i = 0 ; i < length ; i++ )
\r
364 var item = items[ i ];
\r
365 if ( lastGroup != item.group )
\r
367 output.push( '<div class="cke_menuseparator" role="separator"></div>' );
\r
368 lastGroup = item.group;
\r
371 item.render( this, i, output );
\r
374 output.push( '</div>' );
\r
376 // Inject the HTML inside the panel.
\r
377 element.setHtml( output.join( '' ) );
\r
379 CKEDITOR.ui.fire( 'ready', this );
\r
383 this.parent._.panel.showAsChild( panel, this.id, offsetParent, corner, offsetX, offsetY );
\r
385 panel.showBlock( this.id, offsetParent, corner, offsetX, offsetY );
\r
387 editor.fire( 'menuShow', [ panel ] );
\r
390 addListener : function( listenerFn )
\r
392 this._.listeners.push( listenerFn );
\r
395 hide : function( returnFocus )
\r
397 this._.onHide && this._.onHide();
\r
398 this._.panel && this._.panel.hide( returnFocus );
\r
403 function sortItems( items )
\r
405 items.sort( function( itemA, itemB )
\r
407 if ( itemA.group < itemB.group )
\r
409 else if ( itemA.group > itemB.group )
\r
412 return itemA.order < itemB.order ? -1 :
\r
413 itemA.order > itemB.order ? 1 :
\r
417 CKEDITOR.menuItem = CKEDITOR.tools.createClass(
\r
419 $ : function( editor, name, definition )
\r
421 CKEDITOR.tools.extend( this, definition,
\r
425 className : 'cke_button_' + name
\r
428 // Transform the group name into its order number.
\r
429 this.group = editor._.menuGroups[ this.group ];
\r
431 this.editor = editor;
\r
437 render : function( menu, index, output )
\r
439 var id = menu.id + String( index ),
\r
440 state = ( typeof this.state == 'undefined' ) ? CKEDITOR.TRISTATE_OFF : this.state;
\r
442 var classes = ' cke_' + (
\r
443 state == CKEDITOR.TRISTATE_ON ? 'on' :
\r
444 state == CKEDITOR.TRISTATE_DISABLED ? 'disabled' :
\r
447 var htmlLabel = this.label;
\r
449 if ( this.className )
\r
450 classes += ' ' + this.className;
\r
452 var hasSubMenu = this.getItems;
\r
455 '<span class="cke_menuitem' + ( this.icon && this.icon.indexOf( '.png' ) == -1 ? ' cke_noalphafix' : '' ) + '">' +
\r
456 '<a id="', id, '"' +
\r
457 ' class="', classes, '" href="javascript:void(\'', ( this.label || '' ).replace( "'", '' ), '\')"' +
\r
458 ' title="', this.label, '"' +
\r
461 ' hidefocus="true"' +
\r
462 ' role="menuitem"' +
\r
463 ( hasSubMenu ? 'aria-haspopup="true"' : '' ) +
\r
464 ( state == CKEDITOR.TRISTATE_DISABLED ? 'aria-disabled="true"' : '' ) +
\r
465 ( state == CKEDITOR.TRISTATE_ON ? 'aria-pressed="true"' : '' ) );
\r
467 // Some browsers don't cancel key events in the keydown but in the
\r
469 // TODO: Check if really needed for Gecko+Mac.
\r
470 if ( CKEDITOR.env.opera || ( CKEDITOR.env.gecko && CKEDITOR.env.mac ) )
\r
473 ' onkeypress="return false;"' );
\r
476 // With Firefox, we need to force the button to redraw, otherwise it
\r
477 // will remain in the focus state.
\r
478 if ( CKEDITOR.env.gecko )
\r
481 ' onblur="this.style.cssText = this.style.cssText;"' );
\r
484 var offset = ( this.iconOffset || 0 ) * -16;
\r
486 // ' onkeydown="return CKEDITOR.ui.button._.keydown(', index, ', event);"' +
\r
487 ' onmouseover="CKEDITOR.tools.callFunction(', menu._.itemOverFn, ',', index, ');"' +
\r
488 ' onmouseout="CKEDITOR.tools.callFunction(', menu._.itemOutFn, ',', index, ');" ' +
\r
489 ( CKEDITOR.env.ie ? 'onclick="return false;" onmouseup' : 'onclick' ) + // #188
\r
490 '="CKEDITOR.tools.callFunction(', menu._.itemClickFn, ',', index, '); return false;"' +
\r
492 '<span class="cke_icon_wrapper"><span class="cke_icon"' +
\r
493 ( this.icon ? ' style="background-image:url(' + CKEDITOR.getUrl( this.icon ) + ');background-position:0 ' + offset + 'px;"'
\r
495 '></span></span>' +
\r
496 '<span class="cke_label">' );
\r
501 '<span class="cke_menuarrow">',
\r
503 ( this.editor.lang.dir == 'rtl' ?
\r
504 '9668' : // BLACK LEFT-POINTING POINTER
\r
505 '9658' ), // BLACK RIGHT-POINTING POINTER
\r
523 * The amount of time, in milliseconds, the editor waits before displaying submenu
\r
524 * options when moving the mouse over options that contain submenus, like the
\r
525 * "Cell Properties" entry for tables.
\r
529 * // Remove the submenu delay.
\r
530 * config.menu_subMenuDelay = 0;
\r
534 * A comma separated list of items group names to be displayed in the context
\r
535 * menu. The order of items will reflect the order specified in this list if
\r
536 * no priority was defined in the groups.
\r
538 * @default 'clipboard,form,tablecell,tablecellproperties,tablerow,tablecolumn,table,anchor,link,image,flash,checkbox,radio,textfield,hiddenfield,imagebutton,button,select,textarea'
\r
540 * config.menu_groups = 'clipboard,table,anchor,link,image';
\r
542 CKEDITOR.config.menu_groups =
\r
545 'tablecell,tablecellproperties,tablerow,tablecolumn,table,'+
\r
546 'anchor,link,image,flash,' +
\r
547 'checkbox,radio,textfield,hiddenfield,imagebutton,button,select,textarea,div';
\r