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 subItemDefs = 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 subItemName in subItemDefs )
\r
94 var subItem = this.editor.getMenuItem( subItemName );
\r
97 subItem.state = subItemDefs[ subItemName ];
\r
98 menu.add( subItem );
\r
102 // Get the element representing the current item.
\r
103 var element = this._.panel.getBlock( this.id ).element.getDocument().getById( this.id + String( index ) );
\r
105 // Show the submenu.
\r
106 menu.show( element, 2 );
\r
112 add : function( item )
\r
114 // Later we may sort the items, but Array#sort is not stable in
\r
115 // some browsers, here we're forcing the original sequence with
\r
116 // 'order' attribute if it hasn't been assigned. (#3868)
\r
118 item.order = this.items.length;
\r
120 this.items.push( item );
\r
123 removeAll : function()
\r
128 show : function( offsetParent, corner, offsetX, offsetY )
\r
130 var items = this.items,
\r
131 editor = this.editor,
\r
132 panel = this._.panel,
\r
133 element = this._.element;
\r
135 // Create the floating panel for this menu.
\r
138 panel = this._.panel = new CKEDITOR.ui.floatPanel( this.editor, CKEDITOR.document.getBody(),
\r
140 css : [ CKEDITOR.getUrl( editor.skinPath + 'editor.css' ) ],
\r
141 level : this._.level - 1,
\r
142 className : editor.skinClass + ' cke_contextmenu'
\r
146 panel.onEscape = CKEDITOR.tools.bind( function()
\r
148 this.onEscape && this.onEscape();
\r
153 panel.onHide = CKEDITOR.tools.bind( function()
\r
155 this.onHide && this.onHide();
\r
159 // Create an autosize block inside the panel.
\r
160 var block = panel.addBlock( this.id );
\r
161 block.autoSize = true;
\r
163 var keys = block.keys;
\r
164 keys[ 40 ] = 'next'; // ARROW-DOWN
\r
165 keys[ 9 ] = 'next'; // TAB
\r
166 keys[ 38 ] = 'prev'; // ARROW-UP
\r
167 keys[ CKEDITOR.SHIFT + 9 ] = 'prev'; // SHIFT + TAB
\r
168 keys[ 32 ] = 'click'; // SPACE
\r
169 keys[ 39 ] = 'click'; // ARROW-RIGHT
\r
171 element = this._.element = block.element;
\r
172 element.addClass( editor.skinClass );
\r
174 var elementDoc = element.getDocument();
\r
175 elementDoc.getBody().setStyle( 'overflow', 'hidden' );
\r
176 elementDoc.getElementsByTag( 'html' ).getItem( 0 ).setStyle( 'overflow', 'hidden' );
\r
178 this._.itemOverFn = CKEDITOR.tools.addFunction( function( index )
\r
180 clearTimeout( this._.showSubTimeout );
\r
181 this._.showSubTimeout = CKEDITOR.tools.setTimeout( this._.showSubMenu, editor.config.menu_subMenuDelay, this, [ index ] );
\r
185 this._.itemOutFn = CKEDITOR.tools.addFunction( function( index )
\r
187 clearTimeout( this._.showSubTimeout );
\r
191 this._.itemClickFn = CKEDITOR.tools.addFunction( function( index )
\r
193 var item = this.items[ index ];
\r
195 if ( item.state == CKEDITOR.TRISTATE_DISABLED )
\r
201 if ( item.getItems )
\r
202 this._.showSubMenu( index );
\r
204 this.onClick && this.onClick( item );
\r
209 // Put the items in the right order.
\r
210 sortItems( items );
\r
212 // Build the HTML that composes the menu and its items.
\r
213 var output = [ '<div class="cke_menu">' ];
\r
215 var length = items.length,
\r
216 lastGroup = length && items[ 0 ].group;
\r
218 for ( var i = 0 ; i < length ; i++ )
\r
220 var item = items[ i ];
\r
221 if ( lastGroup != item.group )
\r
223 output.push( '<div class="cke_menuseparator"></div>' );
\r
224 lastGroup = item.group;
\r
227 item.render( this, i, output );
\r
230 output.push( '</div>' );
\r
232 // Inject the HTML inside the panel.
\r
233 element.setHtml( output.join( '' ) );
\r
237 this.parent._.panel.showAsChild( panel, this.id, offsetParent, corner, offsetX, offsetY );
\r
239 panel.showBlock( this.id, offsetParent, corner, offsetX, offsetY );
\r
241 editor.fire( 'menuShow', [ panel ] );
\r
246 this._.panel && this._.panel.hide();
\r
251 function sortItems( items )
\r
253 items.sort( function( itemA, itemB )
\r
255 if ( itemA.group < itemB.group )
\r
257 else if ( itemA.group > itemB.group )
\r
260 return itemA.order < itemB.order ? -1 :
\r
261 itemA.order > itemB.order ? 1 :
\r
267 CKEDITOR.menuItem = CKEDITOR.tools.createClass(
\r
269 $ : function( editor, name, definition )
\r
271 CKEDITOR.tools.extend( this, definition,
\r
275 className : 'cke_button_' + name
\r
278 // Transform the group name into its order number.
\r
279 this.group = editor._.menuGroups[ this.group ];
\r
281 this.editor = editor;
\r
287 render : function( menu, index, output )
\r
289 var id = menu.id + String( index ),
\r
290 state = ( typeof this.state == 'undefined' ) ? CKEDITOR.TRISTATE_OFF : this.state;
\r
292 var classes = ' cke_' + (
\r
293 state == CKEDITOR.TRISTATE_ON ? 'on' :
\r
294 state == CKEDITOR.TRISTATE_DISABLED ? 'disabled' :
\r
297 var htmlLabel = this.label;
\r
298 if ( state == CKEDITOR.TRISTATE_DISABLED )
\r
299 htmlLabel = this.editor.lang.common.unavailable.replace( '%1', htmlLabel );
\r
301 if ( this.className )
\r
302 classes += ' ' + this.className;
\r
305 '<span class="cke_menuitem">' +
\r
306 '<a id="', id, '"' +
\r
307 ' class="', classes, '" href="javascript:void(\'', ( this.label || '' ).replace( "'", '' ), '\')"' +
\r
308 ' title="', this.label, '"' +
\r
311 ' hidefocus="true"' );
\r
313 // Some browsers don't cancel key events in the keydown but in the
\r
315 // TODO: Check if really needed for Gecko+Mac.
\r
316 if ( CKEDITOR.env.opera || ( CKEDITOR.env.gecko && CKEDITOR.env.mac ) )
\r
319 ' onkeypress="return false;"' );
\r
322 // With Firefox, we need to force the button to redraw, otherwise it
\r
323 // will remain in the focus state.
\r
324 if ( CKEDITOR.env.gecko )
\r
327 ' onblur="this.style.cssText = this.style.cssText;"' );
\r
330 var offset = ( this.iconOffset || 0 ) * -16;
\r
332 // ' onkeydown="return CKEDITOR.ui.button._.keydown(', index, ', event);"' +
\r
333 ' onmouseover="CKEDITOR.tools.callFunction(', menu._.itemOverFn, ',', index, ');"' +
\r
334 ' onmouseout="CKEDITOR.tools.callFunction(', menu._.itemOutFn, ',', index, ');"' +
\r
335 ' onclick="CKEDITOR.tools.callFunction(', menu._.itemClickFn, ',', index, '); return false;"' +
\r
337 '<span class="cke_icon_wrapper"><span class="cke_icon"' +
\r
338 ( this.icon ? ' style="background-image:url(' + CKEDITOR.getUrl( this.icon ) + ');background-position:0 ' + offset + 'px;"'
\r
340 '></span></span>' +
\r
341 '<span class="cke_label">' );
\r
343 if ( this.getItems )
\r
346 '<span class="cke_menuarrow"></span>' );
\r
359 * The amount of time, in milliseconds, the editor waits before showing submenu
\r
360 * options when moving the mouse over options that contains submenus, like the
\r
361 * "Cell Properties" entry for tables.
\r
365 * // Remove the submenu delay.
\r
366 * config.menu_subMenuDelay = 0;
\r
368 CKEDITOR.config.menu_subMenuDelay = 400;
\r
371 * A comma separated list of items group names to be displayed in the context
\r
372 * menu. The items order will reflect the order in this list if no priority
\r
373 * has been definted in the groups.
\r
375 * @default 'clipboard,form,tablecell,tablecellproperties,tablerow,tablecolumn,table,anchor,link,image,flash,checkbox,radio,textfield,hiddenfield,imagebutton,button,select,textarea'
\r
377 * config.menu_groups = 'clipboard,table,anchor,link,image';
\r
379 CKEDITOR.config.menu_groups =
\r
382 'tablecell,tablecellproperties,tablerow,tablecolumn,table,'+
\r
383 'anchor,link,image,flash,' +
\r
384 'checkbox,radio,textfield,hiddenfield,imagebutton,button,select,textarea';
\r