2 Copyright (c) 2003-2010, 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
17 editor.addMenuGroup = function( name, order )
\r
19 groupsOrder[ name ] = order || 100;
\r
22 editor.addMenuItem = function( name, definition )
\r
24 if ( groupsOrder[ definition.group ] )
\r
25 menuItems[ name ] = new CKEDITOR.menuItem( this, name, definition );
\r
28 editor.addMenuItems = function( definitions )
\r
30 for ( var itemName in definitions )
\r
32 this.addMenuItem( itemName, definitions[ itemName ] );
\r
36 editor.getMenuItem = function( name )
\r
38 return menuItems[ name ];
\r
42 requires : [ 'floatpanel' ]
\r
47 CKEDITOR.menu = CKEDITOR.tools.createClass(
\r
49 $ : function( editor, definition )
\r
51 definition = this._.definition = definition || {};
\r
52 this.id = 'cke_' + CKEDITOR.tools.getNextNumber();
\r
54 this.editor = editor;
\r
57 this._.level = definition.level || 1;
\r
59 var panelDefinition = CKEDITOR.tools.extend( {}, definition.panel,
\r
61 css : editor.skin.editor.css,
\r
62 level : this._.level - 1,
\r
66 var attrs = panelDefinition.block.attributes = ( panelDefinition.attributes || {} );
\r
67 // Provide default role of 'menu'.
\r
68 !attrs.role && ( attrs.role = 'menu' );
\r
69 this._.panelDefinition = panelDefinition;
\r
74 showSubMenu : function( index )
\r
76 var menu = this._.subMenu,
\r
77 item = this.items[ index ],
\r
78 subItemDefs = item.getItems && item.getItems();
\r
80 // If this item has no subitems, we just hide the submenu, if
\r
81 // available, and return back.
\r
84 this._.panel.hideChild();
\r
88 // Record parent menu focused item first (#3389).
\r
89 var block = this._.panel.getBlock( this.id );
\r
90 block._.focusIndex = index;
\r
92 // Create the submenu, if not available, or clean the existing
\r
98 menu = this._.subMenu = new CKEDITOR.menu( this.editor,
\r
99 CKEDITOR.tools.extend( {}, this._.definition, { level : this._.level + 1 }, true ) );
\r
100 menu.parent = this;
\r
101 menu.onClick = CKEDITOR.tools.bind( this.onClick, this );
\r
102 // Sub menu use their own scope for binding onEscape.
\r
103 menu.onEscape = this.onEscape;
\r
106 // Add all submenu items to the menu.
\r
107 for ( var subItemName in subItemDefs )
\r
109 var subItem = this.editor.getMenuItem( subItemName );
\r
112 subItem.state = subItemDefs[ subItemName ];
\r
113 menu.add( subItem );
\r
117 // Get the element representing the current item.
\r
118 var element = this._.panel.getBlock( this.id ).element.getDocument().getById( this.id + String( index ) );
\r
120 // Show the submenu.
\r
121 menu.show( element, 2 );
\r
127 add : function( item )
\r
129 // Later we may sort the items, but Array#sort is not stable in
\r
130 // some browsers, here we're forcing the original sequence with
\r
131 // 'order' attribute if it hasn't been assigned. (#3868)
\r
133 item.order = this.items.length;
\r
135 this.items.push( item );
\r
138 removeAll : function()
\r
143 show : function( offsetParent, corner, offsetX, offsetY )
\r
145 var items = this.items,
\r
146 editor = this.editor,
\r
147 panel = this._.panel,
\r
148 element = this._.element;
\r
150 // Create the floating panel for this menu.
\r
153 panel = this._.panel = new CKEDITOR.ui.floatPanel( this.editor,
\r
154 CKEDITOR.document.getBody(),
\r
155 this._.panelDefinition,
\r
158 panel.onEscape = CKEDITOR.tools.bind( function( keystroke )
\r
160 if ( this.onEscape && this.onEscape( keystroke ) === false )
\r
165 panel.onHide = CKEDITOR.tools.bind( function()
\r
167 this.onHide && this.onHide();
\r
171 // Create an autosize block inside the panel.
\r
172 var block = panel.addBlock( this.id, this._.panelDefinition.block );
\r
173 block.autoSize = true;
\r
175 var keys = block.keys;
\r
176 keys[ 40 ] = 'next'; // ARROW-DOWN
\r
177 keys[ 9 ] = 'next'; // TAB
\r
178 keys[ 38 ] = 'prev'; // ARROW-UP
\r
179 keys[ CKEDITOR.SHIFT + 9 ] = 'prev'; // SHIFT + TAB
\r
180 keys[ 32 ] = 'click'; // SPACE
\r
181 keys[ ( editor.lang.dir == 'rtl' ? 37 : 39 ) ] = 'click'; // ARROW-RIGHT/ARROW-LEFT(rtl)
\r
183 element = this._.element = block.element;
\r
184 element.addClass( editor.skinClass );
\r
186 var elementDoc = element.getDocument();
\r
187 elementDoc.getBody().setStyle( 'overflow', 'hidden' );
\r
188 elementDoc.getElementsByTag( 'html' ).getItem( 0 ).setStyle( 'overflow', 'hidden' );
\r
190 this._.itemOverFn = CKEDITOR.tools.addFunction( function( index )
\r
192 clearTimeout( this._.showSubTimeout );
\r
193 this._.showSubTimeout = CKEDITOR.tools.setTimeout( this._.showSubMenu, editor.config.menu_subMenuDelay, this, [ index ] );
\r
197 this._.itemOutFn = CKEDITOR.tools.addFunction( function( index )
\r
199 clearTimeout( this._.showSubTimeout );
\r
203 this._.itemClickFn = CKEDITOR.tools.addFunction( function( index )
\r
205 var item = this.items[ index ];
\r
207 if ( item.state == CKEDITOR.TRISTATE_DISABLED )
\r
213 if ( item.getItems )
\r
214 this._.showSubMenu( index );
\r
216 this.onClick && this.onClick( item );
\r
221 // Put the items in the right order.
\r
222 sortItems( items );
\r
224 // Build the HTML that composes the menu and its items.
\r
225 var output = [ '<div class="cke_menu" role="presentation">' ];
\r
227 var length = items.length,
\r
228 lastGroup = length && items[ 0 ].group;
\r
230 for ( var i = 0 ; i < length ; i++ )
\r
232 var item = items[ i ];
\r
233 if ( lastGroup != item.group )
\r
235 output.push( '<div class="cke_menuseparator" role="separator"></div>' );
\r
236 lastGroup = item.group;
\r
239 item.render( this, i, output );
\r
242 output.push( '</div>' );
\r
244 // Inject the HTML inside the panel.
\r
245 element.setHtml( output.join( '' ) );
\r
249 this.parent._.panel.showAsChild( panel, this.id, offsetParent, corner, offsetX, offsetY );
\r
251 panel.showBlock( this.id, offsetParent, corner, offsetX, offsetY );
\r
253 editor.fire( 'menuShow', [ panel ] );
\r
258 this._.panel && this._.panel.hide();
\r
263 function sortItems( items )
\r
265 items.sort( function( itemA, itemB )
\r
267 if ( itemA.group < itemB.group )
\r
269 else if ( itemA.group > itemB.group )
\r
272 return itemA.order < itemB.order ? -1 :
\r
273 itemA.order > itemB.order ? 1 :
\r
279 CKEDITOR.menuItem = CKEDITOR.tools.createClass(
\r
281 $ : function( editor, name, definition )
\r
283 CKEDITOR.tools.extend( this, definition,
\r
287 className : 'cke_button_' + name
\r
290 // Transform the group name into its order number.
\r
291 this.group = editor._.menuGroups[ this.group ];
\r
293 this.editor = editor;
\r
299 render : function( menu, index, output )
\r
301 var id = menu.id + String( index ),
\r
302 state = ( typeof this.state == 'undefined' ) ? CKEDITOR.TRISTATE_OFF : this.state;
\r
304 var classes = ' cke_' + (
\r
305 state == CKEDITOR.TRISTATE_ON ? 'on' :
\r
306 state == CKEDITOR.TRISTATE_DISABLED ? 'disabled' :
\r
309 var htmlLabel = this.label;
\r
311 if ( this.className )
\r
312 classes += ' ' + this.className;
\r
314 var hasSubMenu = this.getItems;
\r
317 '<span class="cke_menuitem">' +
\r
318 '<a id="', id, '"' +
\r
319 ' class="', classes, '" href="javascript:void(\'', ( this.label || '' ).replace( "'", '' ), '\')"' +
\r
320 ' title="', this.label, '"' +
\r
323 ' hidefocus="true"' +
\r
324 ' role="menuitem"' +
\r
325 ( hasSubMenu ? 'aria-haspopup="true"' : '' ) +
\r
326 ( state == CKEDITOR.TRISTATE_DISABLED ? 'aria-disabled="true"' : '' ) +
\r
327 ( state == CKEDITOR.TRISTATE_ON ? 'aria-pressed="true"' : '' ) );
\r
329 // Some browsers don't cancel key events in the keydown but in the
\r
331 // TODO: Check if really needed for Gecko+Mac.
\r
332 if ( CKEDITOR.env.opera || ( CKEDITOR.env.gecko && CKEDITOR.env.mac ) )
\r
335 ' onkeypress="return false;"' );
\r
338 // With Firefox, we need to force the button to redraw, otherwise it
\r
339 // will remain in the focus state.
\r
340 if ( CKEDITOR.env.gecko )
\r
343 ' onblur="this.style.cssText = this.style.cssText;"' );
\r
346 var offset = ( this.iconOffset || 0 ) * -16;
\r
348 // ' onkeydown="return CKEDITOR.ui.button._.keydown(', index, ', event);"' +
\r
349 ' onmouseover="CKEDITOR.tools.callFunction(', menu._.itemOverFn, ',', index, ');"' +
\r
350 ' onmouseout="CKEDITOR.tools.callFunction(', menu._.itemOutFn, ',', index, ');"' +
\r
351 ' onclick="CKEDITOR.tools.callFunction(', menu._.itemClickFn, ',', index, '); return false;"' +
\r
353 '<span class="cke_icon_wrapper"><span class="cke_icon"' +
\r
354 ( this.icon ? ' style="background-image:url(' + CKEDITOR.getUrl( this.icon ) + ');background-position:0 ' + offset + 'px;"'
\r
356 '></span></span>' +
\r
357 '<span class="cke_label">' );
\r
362 '<span class="cke_menuarrow">',
\r
364 ( this.editor.lang.dir == 'rtl' ?
\r
365 '9668' : // BLACK LEFT-POINTING POINTER
\r
366 '9658' ), // BLACK RIGHT-POINTING POINTER
\r
381 * The amount of time, in milliseconds, the editor waits before showing submenu
\r
382 * options when moving the mouse over options that contains submenus, like the
\r
383 * "Cell Properties" entry for tables.
\r
387 * // Remove the submenu delay.
\r
388 * config.menu_subMenuDelay = 0;
\r
390 CKEDITOR.config.menu_subMenuDelay = 400;
\r
393 * A comma separated list of items group names to be displayed in the context
\r
394 * menu. The items order will reflect the order in this list if no priority
\r
395 * has been definted in the groups.
\r
397 * @default 'clipboard,form,tablecell,tablecellproperties,tablerow,tablecolumn,table,anchor,link,image,flash,checkbox,radio,textfield,hiddenfield,imagebutton,button,select,textarea'
\r
399 * config.menu_groups = 'clipboard,table,anchor,link,image';
\r
401 CKEDITOR.config.menu_groups =
\r
404 'tablecell,tablecellproperties,tablerow,tablecolumn,table,'+
\r
405 'anchor,link,image,flash,' +
\r
406 'checkbox,radio,textfield,hiddenfield,imagebutton,button,select,textarea,div';
\r