JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
vanilla ckeditor-3.6.6.1
[ckeditor.git] / _source / plugins / menu / plugin.js
1 /*\r
2 Copyright (c) 2003-2013, CKSource - Frederico Knabben. All rights reserved.\r
3 For licensing, see LICENSE.html or http://ckeditor.com/license\r
4 */\r
5 \r
6 CKEDITOR.plugins.add( 'menu',\r
7 {\r
8         beforeInit : function( editor )\r
9         {\r
10                 var groups = editor.config.menu_groups.split( ',' ),\r
11                         groupsOrder = editor._.menuGroups = {},\r
12                         menuItems = editor._.menuItems = {};\r
13 \r
14                 for ( var i = 0 ; i < groups.length ; i++ )\r
15                         groupsOrder[ groups[ i ] ] = i + 1;\r
16 \r
17                 /**\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
24                  */\r
25                 editor.addMenuGroup = function( name, order )\r
26                         {\r
27                                 groupsOrder[ name ] = order || 100;\r
28                         };\r
29 \r
30                 /**\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
35                  */\r
36                 editor.addMenuItem = function( name, definition )\r
37                         {\r
38                                 if ( groupsOrder[ definition.group ] )\r
39                                         menuItems[ name ] = new CKEDITOR.menuItem( this, name, definition );\r
40                         };\r
41 \r
42                 /**\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
46                  */\r
47                 editor.addMenuItems = function( definitions )\r
48                         {\r
49                                 for ( var itemName in definitions )\r
50                                 {\r
51                                         this.addMenuItem( itemName, definitions[ itemName ] );\r
52                                 }\r
53                         };\r
54 \r
55                 /**\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
60                  */\r
61                 editor.getMenuItem = function( name )\r
62                         {\r
63                                 return menuItems[ name ];\r
64                         };\r
65 \r
66                 /**\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
70                  * @since 3.6.1\r
71                  */\r
72                 editor.removeMenuItem = function( name )\r
73                         {\r
74                                 delete menuItems[ name ];\r
75                         };\r
76         },\r
77 \r
78         requires : [ 'floatpanel' ]\r
79 });\r
80 \r
81 (function()\r
82 {\r
83         CKEDITOR.menu = CKEDITOR.tools.createClass(\r
84         {\r
85                 $ : function( editor, definition )\r
86                 {\r
87                         definition = this._.definition = definition || {};\r
88                         this.id = CKEDITOR.tools.getNextId();\r
89 \r
90                         this.editor = editor;\r
91                         this.items = [];\r
92                         this._.listeners = [];\r
93 \r
94                         this._.level = definition.level || 1;\r
95 \r
96                         var panelDefinition = CKEDITOR.tools.extend( {}, definition.panel,\r
97                         {\r
98                                 css : editor.skin.editor.css,\r
99                                 level : this._.level - 1,\r
100                                 block : {}\r
101                         } );\r
102 \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
107                 },\r
108 \r
109                 _ :\r
110                 {\r
111                         onShow : function()\r
112                         {\r
113                                 var selection = this.editor.getSelection();\r
114 \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
119 \r
120                                 var element = selection && selection.getStartElement(),\r
121                                         listeners = this._.listeners,\r
122                                         includedItems = [];\r
123 \r
124                                 this.removeAll();\r
125                                 // Call all listeners, filling the list of items to be displayed.\r
126                                 for ( var i = 0 ; i < listeners.length ; i++ )\r
127                                 {\r
128                                         var listenerItems = listeners[ i ]( element, selection );\r
129 \r
130                                         if ( listenerItems )\r
131                                         {\r
132                                                 for ( var itemName in listenerItems )\r
133                                                 {\r
134                                                         var item = this.editor.getMenuItem( itemName );\r
135 \r
136                                                         if ( item && ( !item.command || this.editor.getCommand( item.command ).state ) )\r
137                                                         {\r
138                                                                 item.state = listenerItems[ itemName ];\r
139                                                                 this.add( item );\r
140                                                         }\r
141                                                 }\r
142                                         }\r
143                                 }\r
144                         },\r
145 \r
146                         onClick : function( item )\r
147                         {\r
148                                 this.hide( false );\r
149 \r
150                                 if ( item.onClick )\r
151                                         item.onClick();\r
152                                 else if ( item.command )\r
153                                         this.editor.execCommand( item.command );\r
154                         },\r
155 \r
156                         onEscape : function( keystroke )\r
157                         {\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
162                                 if ( parent )\r
163                                 {\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
169                                 }\r
170                                 else if ( keystroke == 27 )\r
171                                         this.hide();\r
172 \r
173                                 return false;\r
174                         },\r
175 \r
176                         onHide : function()\r
177                         {\r
178                                 this._.unlockSelection();\r
179                                 this.onHide && this.onHide();\r
180                         },\r
181 \r
182                         unlockSelection : function() {\r
183                                 // Unlock the selection upon first panel closing.\r
184                                 if ( CKEDITOR.env.ie && !this.parent )\r
185                                 {\r
186                                         var selection = this.editor.getSelection();\r
187                                         selection && selection.unlock( true );\r
188                                 }\r
189                         },\r
190 \r
191                         showSubMenu : function( index )\r
192                         {\r
193                                 var menu = this._.subMenu,\r
194                                         item = this.items[ index ],\r
195                                         subItemDefs = item.getItems && item.getItems();\r
196 \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
200                                 {\r
201                                         this._.panel.hideChild();\r
202                                         return;\r
203                                 }\r
204 \r
205                                 // Record parent menu focused item first (#3389).\r
206                                 var block = this._.panel.getBlock( this.id );\r
207                                 block._.focusIndex = index;\r
208 \r
209                                 // Create the submenu, if not available, or clean the existing\r
210                                 // one.\r
211                                 if ( menu )\r
212                                         menu.removeAll();\r
213                                 else\r
214                                 {\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
219                                 }\r
220 \r
221                                 // Add all submenu items to the menu.\r
222                                 for ( var subItemName in subItemDefs )\r
223                                 {\r
224                                         var subItem = this.editor.getMenuItem( subItemName );\r
225                                         if ( subItem )\r
226                                         {\r
227                                                 subItem.state = subItemDefs[ subItemName ];\r
228                                                 menu.add( subItem );\r
229                                         }\r
230                                 }\r
231 \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
234 \r
235                                 // Show the submenu.\r
236                                 menu.show( element, 2 );\r
237                         }\r
238                 },\r
239 \r
240                 proto :\r
241                 {\r
242                         add : function( item )\r
243                         {\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
247                                 if ( !item.order )\r
248                                         item.order = this.items.length;\r
249 \r
250                                 this.items.push( item );\r
251                         },\r
252 \r
253                         removeAll : function()\r
254                         {\r
255                                 this.items = [];\r
256                         },\r
257 \r
258                         show : function( offsetParent, corner, offsetX, offsetY )\r
259                         {\r
260                                 // Not for sub menu.\r
261                                 if ( !this.parent )\r
262                                 {\r
263                                         this._.onShow();\r
264                                         // Don't menu with zero items.\r
265                                         if ( ! this.items.length ) {\r
266                                                 this._.unlockSelection();\r
267                                                 return;\r
268                                         }\r
269                                 }\r
270 \r
271                                 corner = corner || ( this.editor.lang.dir == 'rtl' ? 2 : 1 );\r
272 \r
273                                 var items = this.items,\r
274                                         editor = this.editor,\r
275                                         panel = this._.panel,\r
276                                         element = this._.element;\r
277 \r
278                                 // Create the floating panel for this menu.\r
279                                 if ( !panel )\r
280                                 {\r
281                                         panel = this._.panel = new CKEDITOR.ui.floatPanel( this.editor,\r
282                                                 CKEDITOR.document.getBody(),\r
283                                                 this._.panelDefinition,\r
284                                                 this._.level );\r
285 \r
286                                         panel.onEscape = CKEDITOR.tools.bind( function( keystroke )\r
287                                         {\r
288                                                 if ( this._.onEscape( keystroke ) === false )\r
289                                                         return false;\r
290                                         },\r
291                                         this );\r
292 \r
293                                         panel.onHide = CKEDITOR.tools.bind( function()\r
294                                         {\r
295                                                 this._.onHide && this._.onHide();\r
296                                         },\r
297                                         this );\r
298 \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
302 \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
311 \r
312                                         element = this._.element = block.element;\r
313                                         element.addClass( editor.skinClass );\r
314 \r
315                                         var elementDoc = element.getDocument();\r
316                                         elementDoc.getBody().setStyle( 'overflow', 'hidden' );\r
317                                         elementDoc.getElementsByTag( 'html' ).getItem( 0 ).setStyle( 'overflow', 'hidden' );\r
318 \r
319                                         this._.itemOverFn = CKEDITOR.tools.addFunction( function( index )\r
320                                                 {\r
321                                                         clearTimeout( this._.showSubTimeout );\r
322                                                         this._.showSubTimeout = CKEDITOR.tools.setTimeout( this._.showSubMenu, editor.config.menu_subMenuDelay || 400, this, [ index ] );\r
323                                                 },\r
324                                                 this );\r
325 \r
326                                         this._.itemOutFn = CKEDITOR.tools.addFunction( function( index )\r
327                                                 {\r
328                                                         clearTimeout( this._.showSubTimeout );\r
329                                                 },\r
330                                                 this );\r
331 \r
332                                         this._.itemClickFn = CKEDITOR.tools.addFunction( function( index )\r
333                                                 {\r
334                                                         var item = this.items[ index ];\r
335 \r
336                                                         if ( item.state == CKEDITOR.TRISTATE_DISABLED )\r
337                                                         {\r
338                                                                 this.hide();\r
339                                                                 return;\r
340                                                         }\r
341 \r
342                                                         if ( item.getItems )\r
343                                                                 this._.showSubMenu( index );\r
344                                                         else\r
345                                                                 this._.onClick( item );\r
346                                                 },\r
347                                                 this );\r
348                                 }\r
349 \r
350                                 // Put the items in the right order.\r
351                                 sortItems( items );\r
352 \r
353                                 var chromeRoot = editor.container.getChild( 1 ),\r
354                                         mixedContentClass = chromeRoot.hasClass( 'cke_mixed_dir_content' ) ? ' cke_mixed_dir_content' : '';\r
355 \r
356                                 // Build the HTML that composes the menu and its items.\r
357                                 var output = [ '<div class="cke_menu' + mixedContentClass + '" role="presentation">' ];\r
358 \r
359                                 var length = items.length,\r
360                                         lastGroup = length && items[ 0 ].group;\r
361 \r
362                                 for ( var i = 0 ; i < length ; i++ )\r
363                                 {\r
364                                         var item = items[ i ];\r
365                                         if ( lastGroup != item.group )\r
366                                         {\r
367                                                 output.push( '<div class="cke_menuseparator" role="separator"></div>' );\r
368                                                 lastGroup = item.group;\r
369                                         }\r
370 \r
371                                         item.render( this, i, output );\r
372                                 }\r
373 \r
374                                 output.push( '</div>' );\r
375 \r
376                                 // Inject the HTML inside the panel.\r
377                                 element.setHtml( output.join( '' ) );\r
378 \r
379                                 CKEDITOR.ui.fire( 'ready', this );\r
380 \r
381                                 // Show the panel.\r
382                                 if ( this.parent )\r
383                                         this.parent._.panel.showAsChild( panel, this.id, offsetParent, corner, offsetX, offsetY );\r
384                                 else\r
385                                         panel.showBlock( this.id, offsetParent, corner, offsetX, offsetY );\r
386 \r
387                                 editor.fire( 'menuShow', [ panel ] );\r
388                         },\r
389 \r
390                         addListener : function( listenerFn )\r
391                         {\r
392                                 this._.listeners.push( listenerFn );\r
393                         },\r
394 \r
395                         hide : function( returnFocus )\r
396                         {\r
397                                 this._.onHide && this._.onHide();\r
398                                 this._.panel && this._.panel.hide( returnFocus );\r
399                         }\r
400                 }\r
401         });\r
402 \r
403         function sortItems( items )\r
404         {\r
405                 items.sort( function( itemA, itemB )\r
406                         {\r
407                                 if ( itemA.group < itemB.group )\r
408                                         return -1;\r
409                                 else if ( itemA.group > itemB.group )\r
410                                         return 1;\r
411 \r
412                                 return itemA.order < itemB.order ? -1 :\r
413                                         itemA.order > itemB.order ? 1 :\r
414                                         0;\r
415                         });\r
416         }\r
417         CKEDITOR.menuItem = CKEDITOR.tools.createClass(\r
418         {\r
419                 $ : function( editor, name, definition )\r
420                 {\r
421                         CKEDITOR.tools.extend( this, definition,\r
422                                 // Defaults\r
423                                 {\r
424                                         order : 0,\r
425                                         className : 'cke_button_' + name\r
426                                 });\r
427 \r
428                         // Transform the group name into its order number.\r
429                         this.group = editor._.menuGroups[ this.group ];\r
430 \r
431                         this.editor = editor;\r
432                         this.name = name;\r
433                 },\r
434 \r
435                 proto :\r
436                 {\r
437                         render : function( menu, index, output )\r
438                         {\r
439                                 var id = menu.id + String( index ),\r
440                                         state = ( typeof this.state == 'undefined' ) ? CKEDITOR.TRISTATE_OFF : this.state;\r
441 \r
442                                 var classes = ' cke_' + (\r
443                                         state == CKEDITOR.TRISTATE_ON ? 'on' :\r
444                                         state == CKEDITOR.TRISTATE_DISABLED ? 'disabled' :\r
445                                         'off' );\r
446 \r
447                                 var htmlLabel = this.label;\r
448 \r
449                                 if ( this.className )\r
450                                         classes += ' ' + this.className;\r
451 \r
452                         var hasSubMenu = this.getItems;\r
453 \r
454                         output.push(\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
459                                         ' tabindex="-1"' +\r
460                                         '_cke_focus=1' +\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
466 \r
467                                 // Some browsers don't cancel key events in the keydown but in the\r
468                                 // keypress.\r
469                                 // TODO: Check if really needed for Gecko+Mac.\r
470                                 if ( CKEDITOR.env.opera || ( CKEDITOR.env.gecko && CKEDITOR.env.mac ) )\r
471                                 {\r
472                                         output.push(\r
473                                                 ' onkeypress="return false;"' );\r
474                                 }\r
475 \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
479                                 {\r
480                                         output.push(\r
481                                                 ' onblur="this.style.cssText = this.style.cssText;"' );\r
482                                 }\r
483 \r
484                                 var offset = ( this.iconOffset || 0 ) * -16;\r
485                                 output.push(\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
491                                         '>' +\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
494                                                         : '' ) +\r
495                                                         '></span></span>' +\r
496                                                 '<span class="cke_label">' );\r
497 \r
498                         if ( hasSubMenu )\r
499                         {\r
500                                 output.push(\r
501                                                         '<span class="cke_menuarrow">',\r
502                                                                 '<span>&#',\r
503                                                                         ( this.editor.lang.dir == 'rtl' ?\r
504                                                                                 '9668' :        // BLACK LEFT-POINTING POINTER\r
505                                                                                 '9658' ),       // BLACK RIGHT-POINTING POINTER\r
506                                                                 ';</span>',\r
507                                                         '</span>' );\r
508                         }\r
509 \r
510                                 output.push(\r
511                                                                 htmlLabel,\r
512                                                         '</span>' +\r
513                                         '</a>' +\r
514                                         '</span>' );\r
515                 }\r
516                 }\r
517         });\r
518 \r
519 })();\r
520 \r
521 \r
522 /**\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
526  * @type Number\r
527  * @default 400\r
528  * @example\r
529  * // Remove the submenu delay.\r
530  * config.menu_subMenuDelay = 0;\r
531  */\r
532 \r
533 /**\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
537  * @type String\r
538  * @default 'clipboard,form,tablecell,tablecellproperties,tablerow,tablecolumn,table,anchor,link,image,flash,checkbox,radio,textfield,hiddenfield,imagebutton,button,select,textarea'\r
539  * @example\r
540  * config.menu_groups = 'clipboard,table,anchor,link,image';\r
541  */\r
542 CKEDITOR.config.menu_groups =\r
543         'clipboard,' +\r
544         'form,' +\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