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( 'menu',
\r
8 beforeInit : function( editor )
\r
10 var groups = editor.config.menu_groups.split( ',' ),
\r
13 for ( var i = 0 ; i < groups.length ; i++ )
\r
14 groupsOrder[ groups[ i ] ] = i + 1;
\r
16 editor._.menuGroups = groupsOrder;
\r
17 editor._.menuItems = {};
\r
20 requires : [ 'floatpanel' ]
\r
23 CKEDITOR.tools.extend( CKEDITOR.editor.prototype,
\r
25 addMenuGroup : function( name, order )
\r
27 this._.menuGroups[ name ] = order || 100;
\r
30 addMenuItem : function( name, definition )
\r
32 if ( this._.menuGroups[ definition.group ] )
\r
33 this._.menuItems[ name ] = new CKEDITOR.menuItem( this, name, definition );
\r
36 addMenuItems : function( definitions )
\r
38 for ( var itemName in definitions )
\r
40 this.addMenuItem( itemName, definitions[ itemName ] );
\r
44 getMenuItem : function( name )
\r
46 return this._.menuItems[ name ];
\r
52 CKEDITOR.menu = CKEDITOR.tools.createClass(
\r
54 $ : function( editor, level )
\r
56 this.id = 'cke_' + CKEDITOR.tools.getNextNumber();
\r
58 this.editor = editor;
\r
61 this._.level = level || 1;
\r
66 showSubMenu : function( index )
\r
68 var menu = this._.subMenu,
\r
69 item = this.items[ index ],
\r
70 subItems = item.getItems && item.getItems();
\r
72 // If this item has no subitems, we just hide the submenu, if
\r
73 // available, and return back.
\r
76 this._.panel.hideChild();
\r
80 // Create the submenu, if not available, or clean the existing
\r
86 menu = this._.subMenu = new CKEDITOR.menu( this.editor, this._.level + 1 );
\r
88 menu.onClick = CKEDITOR.tools.bind( this.onClick, this );
\r
91 // Add all submenu items to the menu.
\r
92 for ( var itemName in subItems )
\r
94 menu.add( this.editor.getMenuItem( itemName ) );
\r
97 // Get the element representing the current item.
\r
98 var element = this._.panel.getBlock( this.id ).element.getDocument().getById( this.id + String( index ) );
\r
100 // Show the submenu.
\r
101 menu.show( element, 2 );
\r
107 add : function( item )
\r
109 // Later we may sort the items, but Array#sort is not stable in
\r
110 // some browsers, here we're forcing the original sequence with
\r
111 // 'order' attribute if it hasn't been assigned. (#3868)
\r
113 item.order = this.items.length;
\r
115 this.items.push( item );
\r
118 removeAll : function()
\r
123 show : function( offsetParent, corner, offsetX, offsetY )
\r
125 var items = this.items,
\r
126 editor = this.editor,
\r
127 panel = this._.panel,
\r
128 element = this._.element;
\r
130 // Create the floating panel for this menu.
\r
133 panel = this._.panel = new CKEDITOR.ui.floatPanel( this.editor, CKEDITOR.document.getBody(),
\r
135 css : [ CKEDITOR.getUrl( editor.skinPath + 'editor.css' ) ],
\r
136 level : this._.level - 1,
\r
137 className : editor.skinClass + ' cke_contextmenu'
\r
141 panel.onEscape = CKEDITOR.tools.bind( function()
\r
143 this.onEscape && this.onEscape();
\r
148 panel.onHide = CKEDITOR.tools.bind( function()
\r
150 this.onHide && this.onHide();
\r
154 // Create an autosize block inside the panel.
\r
155 var block = panel.addBlock( this.id );
\r
156 block.autoSize = true;
\r
158 var keys = block.keys;
\r
159 keys[ 40 ] = 'next'; // ARROW-DOWN
\r
160 keys[ 9 ] = 'next'; // TAB
\r
161 keys[ 38 ] = 'prev'; // ARROW-UP
\r
162 keys[ CKEDITOR.SHIFT + 9 ] = 'prev'; // SHIFT + TAB
\r
163 keys[ 32 ] = 'click'; // SPACE
\r
164 keys[ 39 ] = 'click'; // ARROW-RIGHT
\r
166 element = this._.element = block.element;
\r
167 element.addClass( editor.skinClass );
\r
169 var elementDoc = element.getDocument();
\r
170 elementDoc.getBody().setStyle( 'overflow', 'hidden' );
\r
171 elementDoc.getElementsByTag( 'html' ).getItem( 0 ).setStyle( 'overflow', 'hidden' );
\r
173 this._.itemOverFn = CKEDITOR.tools.addFunction( function( index )
\r
175 clearTimeout( this._.showSubTimeout );
\r
176 this._.showSubTimeout = CKEDITOR.tools.setTimeout( this._.showSubMenu, editor.config.menu_subMenuDelay, this, [ index ] );
\r
180 this._.itemOutFn = CKEDITOR.tools.addFunction( function( index )
\r
182 clearTimeout( this._.showSubTimeout );
\r
186 this._.itemClickFn = CKEDITOR.tools.addFunction( function( index )
\r
188 var item = this.items[ index ];
\r
190 if ( item.state == CKEDITOR.TRISTATE_DISABLED )
\r
196 if ( item.getItems )
\r
197 this._.showSubMenu( index );
\r
199 this.onClick && this.onClick( item );
\r
204 // Put the items in the right order.
\r
205 sortItems( items );
\r
207 // Build the HTML that composes the menu and its items.
\r
208 var output = [ '<div class="cke_menu">' ];
\r
210 var length = items.length,
\r
211 lastGroup = length && items[ 0 ].group;
\r
213 for ( var i = 0 ; i < length ; i++ )
\r
215 var item = items[ i ];
\r
216 if ( lastGroup != item.group )
\r
218 output.push( '<div class="cke_menuseparator"></div>' );
\r
219 lastGroup = item.group;
\r
222 item.render( this, i, output );
\r
225 output.push( '</div>' );
\r
227 // Inject the HTML inside the panel.
\r
228 element.setHtml( output.join( '' ) );
\r
232 this.parent._.panel.showAsChild( panel, this.id, offsetParent, corner, offsetX, offsetY );
\r
234 panel.showBlock( this.id, offsetParent, corner, offsetX, offsetY );
\r
236 editor.fire( 'menuShow', [ panel ] );
\r
241 this._.panel && this._.panel.hide();
\r
246 function sortItems( items )
\r
248 items.sort( function( itemA, itemB )
\r
250 if ( itemA.group < itemB.group )
\r
252 else if ( itemA.group > itemB.group )
\r
255 return itemA.order < itemB.order ? -1 :
\r
256 itemA.order > itemB.order ? 1 :
\r
262 CKEDITOR.menuItem = CKEDITOR.tools.createClass(
\r
264 $ : function( editor, name, definition )
\r
266 CKEDITOR.tools.extend( this, definition,
\r
270 className : 'cke_button_' + name
\r
273 // Transform the group name into its order number.
\r
274 this.group = editor._.menuGroups[ this.group ];
\r
276 this.editor = editor;
\r
282 render : function( menu, index, output )
\r
284 var id = menu.id + String( index ),
\r
285 state = ( typeof this.state == 'undefined' ) ? CKEDITOR.TRISTATE_OFF : this.state;
\r
287 var classes = ' cke_' + (
\r
288 state == CKEDITOR.TRISTATE_ON ? 'on' :
\r
289 state == CKEDITOR.TRISTATE_DISABLED ? 'disabled' :
\r
292 var htmlLabel = this.label;
\r
293 if ( state == CKEDITOR.TRISTATE_DISABLED )
\r
294 htmlLabel = this.editor.lang.common.unavailable.replace( '%1', htmlLabel );
\r
296 if ( this.className )
\r
297 classes += ' ' + this.className;
\r
300 '<span class="cke_menuitem">' +
\r
301 '<a id="', id, '"' +
\r
302 ' class="', classes, '" href="javascript:void(\'', ( this.label || '' ).replace( "'", '' ), '\')"' +
\r
303 ' title="', this.label, '"' +
\r
306 ' hidefocus="true"' );
\r
308 // Some browsers don't cancel key events in the keydown but in the
\r
310 // TODO: Check if really needed for Gecko+Mac.
\r
311 if ( CKEDITOR.env.opera || ( CKEDITOR.env.gecko && CKEDITOR.env.mac ) )
\r
314 ' onkeypress="return false;"' );
\r
317 // With Firefox, we need to force the button to redraw, otherwise it
\r
318 // will remain in the focus state.
\r
319 if ( CKEDITOR.env.gecko )
\r
322 ' onblur="this.style.cssText = this.style.cssText;"' );
\r
325 var offset = ( this.iconOffset || 0 ) * -16;
\r
327 // ' onkeydown="return CKEDITOR.ui.button._.keydown(', index, ', event);"' +
\r
328 ' onmouseover="CKEDITOR.tools.callFunction(', menu._.itemOverFn, ',', index, ');"' +
\r
329 ' onmouseout="CKEDITOR.tools.callFunction(', menu._.itemOutFn, ',', index, ');"' +
\r
330 ' onclick="CKEDITOR.tools.callFunction(', menu._.itemClickFn, ',', index, '); return false;"' +
\r
332 '<span class="cke_icon_wrapper"><span class="cke_icon"' +
\r
333 ( this.icon ? ' style="background-image:url(' + CKEDITOR.getUrl( this.icon ) + ');background-position:0 ' + offset + 'px;"'
\r
335 '></span></span>' +
\r
336 '<span class="cke_label">' );
\r
338 if ( this.getItems )
\r
341 '<span class="cke_menuarrow"></span>' );
\r
354 * The amount of time, in milliseconds, the editor waits before showing submenu
\r
355 * options when moving the mouse over options that contains submenus, like the
\r
356 * "Cell Properties" entry for tables.
\r
360 * // Remove the submenu delay.
\r
361 * config.menu_subMenuDelay = 0;
\r
363 CKEDITOR.config.menu_subMenuDelay = 400;
\r
366 * A comma separated list of items group names to be displayed in the context
\r
367 * menu. The items order will reflect the order in this list if no priority
\r
368 * has been definted in the groups.
\r
370 * @default 'clipboard,form,tablecell,tablecellproperties,tablerow,tablecolumn,table,anchor,link,image,flash,checkbox,radio,textfield,hiddenfield,imagebutton,button,select,textarea'
\r
372 * config.menu_groups = 'clipboard,table,anchor,link,image';
\r
374 CKEDITOR.config.menu_groups =
\r
377 'tablecell,tablecellproperties,tablerow,tablecolumn,table,'+
\r
378 'anchor,link,image,flash,' +
\r
379 'checkbox,radio,textfield,hiddenfield,imagebutton,button,select,textarea';
\r