2 Copyright (c) 2003-2011, 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 if ( CKEDITOR.env.ie )
\r
180 var selection = this.editor.getSelection();
\r
181 selection && selection.unlock();
\r
184 this.onHide && this.onHide();
\r
187 showSubMenu : function( index )
\r
189 var menu = this._.subMenu,
\r
190 item = this.items[ index ],
\r
191 subItemDefs = item.getItems && item.getItems();
\r
193 // If this item has no subitems, we just hide the submenu, if
\r
194 // available, and return back.
\r
195 if ( !subItemDefs )
\r
197 this._.panel.hideChild();
\r
201 // Record parent menu focused item first (#3389).
\r
202 var block = this._.panel.getBlock( this.id );
\r
203 block._.focusIndex = index;
\r
205 // Create the submenu, if not available, or clean the existing
\r
211 menu = this._.subMenu = new CKEDITOR.menu( this.editor,
\r
212 CKEDITOR.tools.extend( {}, this._.definition, { level : this._.level + 1 }, true ) );
\r
213 menu.parent = this;
\r
214 menu._.onClick = CKEDITOR.tools.bind( this._.onClick, this );
\r
217 // Add all submenu items to the menu.
\r
218 for ( var subItemName in subItemDefs )
\r
220 var subItem = this.editor.getMenuItem( subItemName );
\r
223 subItem.state = subItemDefs[ subItemName ];
\r
224 menu.add( subItem );
\r
228 // Get the element representing the current item.
\r
229 var element = this._.panel.getBlock( this.id ).element.getDocument().getById( this.id + String( index ) );
\r
231 // Show the submenu.
\r
232 menu.show( element, 2 );
\r
238 add : function( item )
\r
240 // Later we may sort the items, but Array#sort is not stable in
\r
241 // some browsers, here we're forcing the original sequence with
\r
242 // 'order' attribute if it hasn't been assigned. (#3868)
\r
244 item.order = this.items.length;
\r
246 this.items.push( item );
\r
249 removeAll : function()
\r
254 show : function( offsetParent, corner, offsetX, offsetY )
\r
256 // Not for sub menu.
\r
257 if ( !this.parent )
\r
260 // Don't menu with zero items.
\r
261 if ( ! this.items.length )
\r
265 corner = corner || ( this.editor.lang.dir == 'rtl' ? 2 : 1 );
\r
267 var items = this.items,
\r
268 editor = this.editor,
\r
269 panel = this._.panel,
\r
270 element = this._.element;
\r
272 // Create the floating panel for this menu.
\r
275 panel = this._.panel = new CKEDITOR.ui.floatPanel( this.editor,
\r
276 CKEDITOR.document.getBody(),
\r
277 this._.panelDefinition,
\r
280 panel.onEscape = CKEDITOR.tools.bind( function( keystroke )
\r
282 if ( this._.onEscape( keystroke ) === false )
\r
287 panel.onHide = CKEDITOR.tools.bind( function()
\r
289 this._.onHide && this._.onHide();
\r
293 // Create an autosize block inside the panel.
\r
294 var block = panel.addBlock( this.id, this._.panelDefinition.block );
\r
295 block.autoSize = true;
\r
297 var keys = block.keys;
\r
298 keys[ 40 ] = 'next'; // ARROW-DOWN
\r
299 keys[ 9 ] = 'next'; // TAB
\r
300 keys[ 38 ] = 'prev'; // ARROW-UP
\r
301 keys[ CKEDITOR.SHIFT + 9 ] = 'prev'; // SHIFT + TAB
\r
302 keys[ ( editor.lang.dir == 'rtl' ? 37 : 39 ) ]= CKEDITOR.env.ie ? 'mouseup' : 'click'; // ARROW-RIGHT/ARROW-LEFT(rtl)
\r
303 keys[ 32 ] = CKEDITOR.env.ie ? 'mouseup' : 'click'; // SPACE
\r
304 CKEDITOR.env.ie && ( keys[ 13 ] = 'mouseup' ); // Manage ENTER, since onclick is blocked in IE (#8041).
\r
306 element = this._.element = block.element;
\r
307 element.addClass( editor.skinClass );
\r
309 var elementDoc = element.getDocument();
\r
310 elementDoc.getBody().setStyle( 'overflow', 'hidden' );
\r
311 elementDoc.getElementsByTag( 'html' ).getItem( 0 ).setStyle( 'overflow', 'hidden' );
\r
313 this._.itemOverFn = CKEDITOR.tools.addFunction( function( index )
\r
315 clearTimeout( this._.showSubTimeout );
\r
316 this._.showSubTimeout = CKEDITOR.tools.setTimeout( this._.showSubMenu, editor.config.menu_subMenuDelay || 400, this, [ index ] );
\r
320 this._.itemOutFn = CKEDITOR.tools.addFunction( function( index )
\r
322 clearTimeout( this._.showSubTimeout );
\r
326 this._.itemClickFn = CKEDITOR.tools.addFunction( function( index )
\r
328 var item = this.items[ index ];
\r
330 if ( item.state == CKEDITOR.TRISTATE_DISABLED )
\r
336 if ( item.getItems )
\r
337 this._.showSubMenu( index );
\r
339 this._.onClick( item );
\r
344 // Put the items in the right order.
\r
345 sortItems( items );
\r
347 var chromeRoot = editor.container.getChild( 1 ),
\r
348 mixedContentClass = chromeRoot.hasClass( 'cke_mixed_dir_content' ) ? ' cke_mixed_dir_content' : '';
\r
350 // Build the HTML that composes the menu and its items.
\r
351 var output = [ '<div class="cke_menu' + mixedContentClass + '" role="presentation">' ];
\r
353 var length = items.length,
\r
354 lastGroup = length && items[ 0 ].group;
\r
356 for ( var i = 0 ; i < length ; i++ )
\r
358 var item = items[ i ];
\r
359 if ( lastGroup != item.group )
\r
361 output.push( '<div class="cke_menuseparator" role="separator"></div>' );
\r
362 lastGroup = item.group;
\r
365 item.render( this, i, output );
\r
368 output.push( '</div>' );
\r
370 // Inject the HTML inside the panel.
\r
371 element.setHtml( output.join( '' ) );
\r
373 CKEDITOR.ui.fire( 'ready', this );
\r
377 this.parent._.panel.showAsChild( panel, this.id, offsetParent, corner, offsetX, offsetY );
\r
379 panel.showBlock( this.id, offsetParent, corner, offsetX, offsetY );
\r
381 editor.fire( 'menuShow', [ panel ] );
\r
384 addListener : function( listenerFn )
\r
386 this._.listeners.push( listenerFn );
\r
389 hide : function( returnFocus )
\r
391 this._.onHide && this._.onHide();
\r
392 this._.panel && this._.panel.hide( returnFocus );
\r
397 function sortItems( items )
\r
399 items.sort( function( itemA, itemB )
\r
401 if ( itemA.group < itemB.group )
\r
403 else if ( itemA.group > itemB.group )
\r
406 return itemA.order < itemB.order ? -1 :
\r
407 itemA.order > itemB.order ? 1 :
\r
411 CKEDITOR.menuItem = CKEDITOR.tools.createClass(
\r
413 $ : function( editor, name, definition )
\r
415 CKEDITOR.tools.extend( this, definition,
\r
419 className : 'cke_button_' + name
\r
422 // Transform the group name into its order number.
\r
423 this.group = editor._.menuGroups[ this.group ];
\r
425 this.editor = editor;
\r
431 render : function( menu, index, output )
\r
433 var id = menu.id + String( index ),
\r
434 state = ( typeof this.state == 'undefined' ) ? CKEDITOR.TRISTATE_OFF : this.state;
\r
436 var classes = ' cke_' + (
\r
437 state == CKEDITOR.TRISTATE_ON ? 'on' :
\r
438 state == CKEDITOR.TRISTATE_DISABLED ? 'disabled' :
\r
441 var htmlLabel = this.label;
\r
443 if ( this.className )
\r
444 classes += ' ' + this.className;
\r
446 var hasSubMenu = this.getItems;
\r
449 '<span class="cke_menuitem' + ( this.icon && this.icon.indexOf( '.png' ) == -1 ? ' cke_noalphafix' : '' ) + '">' +
\r
450 '<a id="', id, '"' +
\r
451 ' class="', classes, '" href="javascript:void(\'', ( this.label || '' ).replace( "'", '' ), '\')"' +
\r
452 ' title="', this.label, '"' +
\r
455 ' hidefocus="true"' +
\r
456 ' role="menuitem"' +
\r
457 ( hasSubMenu ? 'aria-haspopup="true"' : '' ) +
\r
458 ( state == CKEDITOR.TRISTATE_DISABLED ? 'aria-disabled="true"' : '' ) +
\r
459 ( state == CKEDITOR.TRISTATE_ON ? 'aria-pressed="true"' : '' ) );
\r
461 // Some browsers don't cancel key events in the keydown but in the
\r
463 // TODO: Check if really needed for Gecko+Mac.
\r
464 if ( CKEDITOR.env.opera || ( CKEDITOR.env.gecko && CKEDITOR.env.mac ) )
\r
467 ' onkeypress="return false;"' );
\r
470 // With Firefox, we need to force the button to redraw, otherwise it
\r
471 // will remain in the focus state.
\r
472 if ( CKEDITOR.env.gecko )
\r
475 ' onblur="this.style.cssText = this.style.cssText;"' );
\r
478 var offset = ( this.iconOffset || 0 ) * -16;
\r
480 // ' onkeydown="return CKEDITOR.ui.button._.keydown(', index, ', event);"' +
\r
481 ' onmouseover="CKEDITOR.tools.callFunction(', menu._.itemOverFn, ',', index, ');"' +
\r
482 ' onmouseout="CKEDITOR.tools.callFunction(', menu._.itemOutFn, ',', index, ');" ' +
\r
483 ( CKEDITOR.env.ie ? 'onclick="return false;" onmouseup' : 'onclick' ) + // #188
\r
484 '="CKEDITOR.tools.callFunction(', menu._.itemClickFn, ',', index, '); return false;"' +
\r
486 '<span class="cke_icon_wrapper"><span class="cke_icon"' +
\r
487 ( this.icon ? ' style="background-image:url(' + CKEDITOR.getUrl( this.icon ) + ');background-position:0 ' + offset + 'px;"'
\r
489 '></span></span>' +
\r
490 '<span class="cke_label">' );
\r
495 '<span class="cke_menuarrow">',
\r
497 ( this.editor.lang.dir == 'rtl' ?
\r
498 '9668' : // BLACK LEFT-POINTING POINTER
\r
499 '9658' ), // BLACK RIGHT-POINTING POINTER
\r
517 * The amount of time, in milliseconds, the editor waits before displaying submenu
\r
518 * options when moving the mouse over options that contain submenus, like the
\r
519 * "Cell Properties" entry for tables.
\r
523 * // Remove the submenu delay.
\r
524 * config.menu_subMenuDelay = 0;
\r
528 * A comma separated list of items group names to be displayed in the context
\r
529 * menu. The order of items will reflect the order specified in this list if
\r
530 * no priority was defined in the groups.
\r
532 * @default 'clipboard,form,tablecell,tablecellproperties,tablerow,tablecolumn,table,anchor,link,image,flash,checkbox,radio,textfield,hiddenfield,imagebutton,button,select,textarea'
\r
534 * config.menu_groups = 'clipboard,table,anchor,link,image';
\r
536 CKEDITOR.config.menu_groups =
\r
539 'tablecell,tablecellproperties,tablerow,tablecolumn,table,'+
\r
540 'anchor,link,image,flash,' +
\r
541 'checkbox,radio,textfield,hiddenfield,imagebutton,button,select,textarea,div';
\r