JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
vanilla ckeditor-3.2
[ckeditor.git] / _source / plugins / menu / plugin.js
1 /*\r
2 Copyright (c) 2003-2010, 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                 editor.addMenuGroup = function( name, order )\r
18                         {\r
19                                 groupsOrder[ name ] = order || 100;\r
20                         };\r
21 \r
22                 editor.addMenuItem = function( name, definition )\r
23                         {\r
24                                 if ( groupsOrder[ definition.group ] )\r
25                                         menuItems[ name ] = new CKEDITOR.menuItem( this, name, definition );\r
26                         };\r
27 \r
28                 editor.addMenuItems = function( definitions )\r
29                         {\r
30                                 for ( var itemName in definitions )\r
31                                 {\r
32                                         this.addMenuItem( itemName, definitions[ itemName ] );\r
33                                 }\r
34                         };\r
35 \r
36                 editor.getMenuItem = function( name )\r
37                         {\r
38                                 return menuItems[ name ];\r
39                         };\r
40         },\r
41 \r
42         requires : [ 'floatpanel' ]\r
43 });\r
44 \r
45 (function()\r
46 {\r
47         CKEDITOR.menu = CKEDITOR.tools.createClass(\r
48         {\r
49                 $ : function( editor, definition )\r
50                 {\r
51                         definition = this._.definition = definition || {};\r
52                         this.id = 'cke_' + CKEDITOR.tools.getNextNumber();\r
53 \r
54                         this.editor = editor;\r
55                         this.items = [];\r
56 \r
57                         this._.level = definition.level || 1;\r
58 \r
59                         var panelDefinition = CKEDITOR.tools.extend( {}, definition.panel,\r
60                         {\r
61                                 css : editor.skin.editor.css,\r
62                                 level : this._.level - 1,\r
63                                 block : {}\r
64                         } );\r
65 \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
70                 },\r
71 \r
72                 _ :\r
73                 {\r
74                         showSubMenu : function( index )\r
75                         {\r
76                                 var menu = this._.subMenu,\r
77                                         item = this.items[ index ],\r
78                                         subItemDefs = item.getItems && item.getItems();\r
79 \r
80                                 // If this item has no subitems, we just hide the submenu, if\r
81                                 // available, and return back.\r
82                                 if ( !subItemDefs )\r
83                                 {\r
84                                         this._.panel.hideChild();\r
85                                         return;\r
86                                 }\r
87 \r
88                                 // Record parent menu focused item first (#3389).\r
89                                 var block = this._.panel.getBlock( this.id );\r
90                                 block._.focusIndex = index;\r
91 \r
92                                 // Create the submenu, if not available, or clean the existing\r
93                                 // one.\r
94                                 if ( menu )\r
95                                         menu.removeAll();\r
96                                 else\r
97                                 {\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
104                                 }\r
105 \r
106                                 // Add all submenu items to the menu.\r
107                                 for ( var subItemName in subItemDefs )\r
108                                 {\r
109                                         var subItem = this.editor.getMenuItem( subItemName );\r
110                                         if ( subItem )\r
111                                         {\r
112                                                 subItem.state = subItemDefs[ subItemName ];\r
113                                                 menu.add( subItem );\r
114                                         }\r
115                                 }\r
116 \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
119 \r
120                                 // Show the submenu.\r
121                                 menu.show( element, 2 );\r
122                         }\r
123                 },\r
124 \r
125                 proto :\r
126                 {\r
127                         add : function( item )\r
128                         {\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
132                                 if ( !item.order )\r
133                                         item.order = this.items.length;\r
134 \r
135                                 this.items.push( item );\r
136                         },\r
137 \r
138                         removeAll : function()\r
139                         {\r
140                                 this.items = [];\r
141                         },\r
142 \r
143                         show : function( offsetParent, corner, offsetX, offsetY )\r
144                         {\r
145                                 var items = this.items,\r
146                                         editor = this.editor,\r
147                                         panel = this._.panel,\r
148                                         element = this._.element;\r
149 \r
150                                 // Create the floating panel for this menu.\r
151                                 if ( !panel )\r
152                                 {\r
153                                         panel = this._.panel = new CKEDITOR.ui.floatPanel( this.editor,\r
154                                                 CKEDITOR.document.getBody(),\r
155                                                 this._.panelDefinition,\r
156                                                 this._.level );\r
157 \r
158                                         panel.onEscape = CKEDITOR.tools.bind( function( keystroke )\r
159                                         {\r
160                                                 if ( this.onEscape && this.onEscape( keystroke ) === false )\r
161                                                         return false;\r
162                                         },\r
163                                         this );\r
164 \r
165                                         panel.onHide = CKEDITOR.tools.bind( function()\r
166                                         {\r
167                                                 this.onHide && this.onHide();\r
168                                         },\r
169                                         this );\r
170 \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
174 \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
182 \r
183                                         element = this._.element = block.element;\r
184                                         element.addClass( editor.skinClass );\r
185 \r
186                                         var elementDoc = element.getDocument();\r
187                                         elementDoc.getBody().setStyle( 'overflow', 'hidden' );\r
188                                         elementDoc.getElementsByTag( 'html' ).getItem( 0 ).setStyle( 'overflow', 'hidden' );\r
189 \r
190                                         this._.itemOverFn = CKEDITOR.tools.addFunction( function( index )\r
191                                                 {\r
192                                                         clearTimeout( this._.showSubTimeout );\r
193                                                         this._.showSubTimeout = CKEDITOR.tools.setTimeout( this._.showSubMenu, editor.config.menu_subMenuDelay, this, [ index ] );\r
194                                                 },\r
195                                                 this);\r
196 \r
197                                         this._.itemOutFn = CKEDITOR.tools.addFunction( function( index )\r
198                                                 {\r
199                                                         clearTimeout( this._.showSubTimeout );\r
200                                                 },\r
201                                                 this);\r
202 \r
203                                         this._.itemClickFn = CKEDITOR.tools.addFunction( function( index )\r
204                                                 {\r
205                                                         var item = this.items[ index ];\r
206 \r
207                                                         if ( item.state == CKEDITOR.TRISTATE_DISABLED )\r
208                                                         {\r
209                                                                 this.hide();\r
210                                                                 return;\r
211                                                         }\r
212 \r
213                                                         if ( item.getItems )\r
214                                                                 this._.showSubMenu( index );\r
215                                                         else\r
216                                                                 this.onClick && this.onClick( item );\r
217                                                 },\r
218                                                 this);\r
219                                 }\r
220 \r
221                                 // Put the items in the right order.\r
222                                 sortItems( items );\r
223 \r
224                                 // Build the HTML that composes the menu and its items.\r
225                                 var output = [ '<div class="cke_menu" role="presentation">' ];\r
226 \r
227                                 var length = items.length,\r
228                                         lastGroup = length && items[ 0 ].group;\r
229 \r
230                                 for ( var i = 0 ; i < length ; i++ )\r
231                                 {\r
232                                         var item = items[ i ];\r
233                                         if ( lastGroup != item.group )\r
234                                         {\r
235                                                 output.push( '<div class="cke_menuseparator" role="separator"></div>' );\r
236                                                 lastGroup = item.group;\r
237                                         }\r
238 \r
239                                         item.render( this, i, output );\r
240                                 }\r
241 \r
242                                 output.push( '</div>' );\r
243 \r
244                                 // Inject the HTML inside the panel.\r
245                                 element.setHtml( output.join( '' ) );\r
246 \r
247                                 // Show the panel.\r
248                                 if ( this.parent )\r
249                                         this.parent._.panel.showAsChild( panel, this.id, offsetParent, corner, offsetX, offsetY );\r
250                                 else\r
251                                         panel.showBlock( this.id, offsetParent, corner, offsetX, offsetY );\r
252 \r
253                                 editor.fire( 'menuShow', [ panel ] );\r
254                         },\r
255 \r
256                         hide : function()\r
257                         {\r
258                                 this._.panel && this._.panel.hide();\r
259                         }\r
260                 }\r
261         });\r
262 \r
263         function sortItems( items )\r
264         {\r
265                 items.sort( function( itemA, itemB )\r
266                         {\r
267                                 if ( itemA.group < itemB.group )\r
268                                         return -1;\r
269                                 else if ( itemA.group > itemB.group )\r
270                                         return 1;\r
271 \r
272                                 return itemA.order < itemB.order ? -1 :\r
273                                         itemA.order > itemB.order ? 1 :\r
274                                         0;\r
275                         });\r
276         }\r
277 })();\r
278 \r
279 CKEDITOR.menuItem = CKEDITOR.tools.createClass(\r
280 {\r
281         $ : function( editor, name, definition )\r
282         {\r
283                 CKEDITOR.tools.extend( this, definition,\r
284                         // Defaults\r
285                         {\r
286                                 order : 0,\r
287                                 className : 'cke_button_' + name\r
288                         });\r
289 \r
290                 // Transform the group name into its order number.\r
291                 this.group = editor._.menuGroups[ this.group ];\r
292 \r
293                 this.editor = editor;\r
294                 this.name = name;\r
295         },\r
296 \r
297         proto :\r
298         {\r
299                 render : function( menu, index, output )\r
300                 {\r
301                         var id = menu.id + String( index ),\r
302                                 state = ( typeof this.state == 'undefined' ) ? CKEDITOR.TRISTATE_OFF : this.state;\r
303 \r
304                         var classes = ' cke_' + (\r
305                                 state == CKEDITOR.TRISTATE_ON ? 'on' :\r
306                                 state == CKEDITOR.TRISTATE_DISABLED ? 'disabled' :\r
307                                 'off' );\r
308 \r
309                         var htmlLabel = this.label;\r
310 \r
311                         if ( this.className )\r
312                                 classes += ' ' + this.className;\r
313 \r
314                         var hasSubMenu = this.getItems;\r
315 \r
316                         output.push(\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
321                                         ' tabindex="-1"' +\r
322                                         '_cke_focus=1' +\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
328 \r
329                         // Some browsers don't cancel key events in the keydown but in the\r
330                         // keypress.\r
331                         // TODO: Check if really needed for Gecko+Mac.\r
332                         if ( CKEDITOR.env.opera || ( CKEDITOR.env.gecko && CKEDITOR.env.mac ) )\r
333                         {\r
334                                 output.push(\r
335                                         ' onkeypress="return false;"' );\r
336                         }\r
337 \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
341                         {\r
342                                 output.push(\r
343                                         ' onblur="this.style.cssText = this.style.cssText;"' );\r
344                         }\r
345 \r
346                         var offset = ( this.iconOffset || 0 ) * -16;\r
347                         output.push(\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
352                                         '>' +\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
355                                                         : '' ) +\r
356                                                         '></span></span>' +\r
357                                                 '<span class="cke_label">' );\r
358 \r
359                         if ( hasSubMenu )\r
360                         {\r
361                                 output.push(\r
362                                                         '<span class="cke_menuarrow">',\r
363                                                                 '<span>&#',\r
364                                                                         ( this.editor.lang.dir == 'rtl' ?\r
365                                                                                 '9668' :        // BLACK LEFT-POINTING POINTER\r
366                                                                                 '9658' ),       // BLACK RIGHT-POINTING POINTER\r
367                                                                 ';</span>',\r
368                                                         '</span>' );\r
369                         }\r
370 \r
371                         output.push(\r
372                                                         htmlLabel,\r
373                                                 '</span>' +\r
374                                 '</a>' +\r
375                                 '</span>' );\r
376                 }\r
377         }\r
378 });\r
379 \r
380 /**\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
384  * @type Number\r
385  * @default 400\r
386  * @example\r
387  * // Remove the submenu delay.\r
388  * config.menu_subMenuDelay = 0;\r
389  */\r
390 CKEDITOR.config.menu_subMenuDelay = 400;\r
391 \r
392 /**\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
396  * @type String\r
397  * @default 'clipboard,form,tablecell,tablecellproperties,tablerow,tablecolumn,table,anchor,link,image,flash,checkbox,radio,textfield,hiddenfield,imagebutton,button,select,textarea'\r
398  * @example\r
399  * config.menu_groups = 'clipboard,table,anchor,link,image';\r
400  */\r
401 CKEDITOR.config.menu_groups =\r
402         'clipboard,' +\r
403         'form,' +\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