On prépare de quoi se concentrer sur les images incorporées.
[ckeditor.git] / skins / ckeditor / _source / plugins / menu / plugin.js
1 /*
2 Copyright (c) 2003-2011, CKSource - Frederico Knabben. All rights reserved.
3 For licensing, see LICENSE.html or http://ckeditor.com/license
4 */
5
6 CKEDITOR.plugins.add( 'menu',
7 {
8 beforeInit : function( editor )
9 {
10 var groups = editor.config.menu_groups.split( ',' ),
11 groupsOrder = editor._.menuGroups = {},
12 menuItems = editor._.menuItems = {};
13
14 for ( var i = 0 ; i < groups.length ; i++ )
15 groupsOrder[ groups[ i ] ] = i + 1;
16
17 /**
18 * Registers an item group to the editor context menu in order to make it
19 * possible to associate it with menu items later.
20 * @name CKEDITOR.editor.prototype.addMenuGroup
21 * @param {String} name Specify a group name.
22 * @param {Number} [order=100] Define the display sequence of this group
23 * inside the menu. A smaller value gets displayed first.
24 */
25 editor.addMenuGroup = function( name, order )
26 {
27 groupsOrder[ name ] = order || 100;
28 };
29
30 /**
31 * Adds an item from the specified definition to the editor context menu.
32 * @name CKEDITOR.editor.prototype.addMenuItem
33 * @param {String} name The menu item name.
34 * @param {CKEDITOR.menu.definition} definition The menu item definition.
35 */
36 editor.addMenuItem = function( name, definition )
37 {
38 if ( groupsOrder[ definition.group ] )
39 menuItems[ name ] = new CKEDITOR.menuItem( this, name, definition );
40 };
41
42 /**
43 * Adds one or more items from the specified definition array to the editor context menu.
44 * @name CKEDITOR.editor.prototype.addMenuItems
45 * @param {Array} definitions List of definitions for each menu item as if {@link CKEDITOR.editor.addMenuItem} is called.
46 */
47 editor.addMenuItems = function( definitions )
48 {
49 for ( var itemName in definitions )
50 {
51 this.addMenuItem( itemName, definitions[ itemName ] );
52 }
53 };
54
55 /**
56 * Retrieves a particular menu item definition from the editor context menu.
57 * @name CKEDITOR.editor.prototype.getMenuItem
58 * @param {String} name The name of the desired menu item.
59 * @return {CKEDITOR.menu.definition}
60 */
61 editor.getMenuItem = function( name )
62 {
63 return menuItems[ name ];
64 };
65
66 /**
67 * Removes a particular menu item added before from the editor context menu.
68 * @name CKEDITOR.editor.prototype.removeMenuItem
69 * @param {String} name The name of the desired menu item.
70 * @since 3.6.1
71 */
72 editor.removeMenuItem = function( name )
73 {
74 delete menuItems[ name ];
75 };
76 },
77
78 requires : [ 'floatpanel' ]
79 });
80
81 (function()
82 {
83 CKEDITOR.menu = CKEDITOR.tools.createClass(
84 {
85 $ : function( editor, definition )
86 {
87 definition = this._.definition = definition || {};
88 this.id = CKEDITOR.tools.getNextId();
89
90 this.editor = editor;
91 this.items = [];
92 this._.listeners = [];
93
94 this._.level = definition.level || 1;
95
96 var panelDefinition = CKEDITOR.tools.extend( {}, definition.panel,
97 {
98 css : editor.skin.editor.css,
99 level : this._.level - 1,
100 block : {}
101 } );
102
103 var attrs = panelDefinition.block.attributes = ( panelDefinition.attributes || {} );
104 // Provide default role of 'menu'.
105 !attrs.role && ( attrs.role = 'menu' );
106 this._.panelDefinition = panelDefinition;
107 },
108
109 _ :
110 {
111 onShow : function()
112 {
113 var selection = this.editor.getSelection();
114
115 // Selection will be unavailable after menu shows up
116 // in IE, lock it now.
117 if ( CKEDITOR.env.ie )
118 selection && selection.lock();
119
120 var element = selection && selection.getStartElement(),
121 listeners = this._.listeners,
122 includedItems = [];
123
124 this.removeAll();
125 // Call all listeners, filling the list of items to be displayed.
126 for ( var i = 0 ; i < listeners.length ; i++ )
127 {
128 var listenerItems = listeners[ i ]( element, selection );
129
130 if ( listenerItems )
131 {
132 for ( var itemName in listenerItems )
133 {
134 var item = this.editor.getMenuItem( itemName );
135
136 if ( item && ( !item.command || this.editor.getCommand( item.command ).state ) )
137 {
138 item.state = listenerItems[ itemName ];
139 this.add( item );
140 }
141 }
142 }
143 }
144 },
145
146 onClick : function( item )
147 {
148 this.hide( false );
149
150 if ( item.onClick )
151 item.onClick();
152 else if ( item.command )
153 this.editor.execCommand( item.command );
154 },
155
156 onEscape : function( keystroke )
157 {
158 var parent = this.parent;
159 // 1. If it's sub-menu, restore the last focused item
160 // of upper level menu.
161 // 2. In case of a top-menu, close it.
162 if ( parent )
163 {
164 parent._.panel.hideChild();
165 // Restore parent block item focus.
166 var parentBlock = parent._.panel._.panel._.currentBlock,
167 parentFocusIndex = parentBlock._.focusIndex;
168 parentBlock._.markItem( parentFocusIndex );
169 }
170 else if ( keystroke == 27 )
171 this.hide();
172
173 return false;
174 },
175
176 onHide : function()
177 {
178 if ( CKEDITOR.env.ie )
179 {
180 var selection = this.editor.getSelection();
181 selection && selection.unlock();
182 }
183
184 this.onHide && this.onHide();
185 },
186
187 showSubMenu : function( index )
188 {
189 var menu = this._.subMenu,
190 item = this.items[ index ],
191 subItemDefs = item.getItems && item.getItems();
192
193 // If this item has no subitems, we just hide the submenu, if
194 // available, and return back.
195 if ( !subItemDefs )
196 {
197 this._.panel.hideChild();
198 return;
199 }
200
201 // Record parent menu focused item first (#3389).
202 var block = this._.panel.getBlock( this.id );
203 block._.focusIndex = index;
204
205 // Create the submenu, if not available, or clean the existing
206 // one.
207 if ( menu )
208 menu.removeAll();
209 else
210 {
211 menu = this._.subMenu = new CKEDITOR.menu( this.editor,
212 CKEDITOR.tools.extend( {}, this._.definition, { level : this._.level + 1 }, true ) );
213 menu.parent = this;
214 menu._.onClick = CKEDITOR.tools.bind( this._.onClick, this );
215 }
216
217 // Add all submenu items to the menu.
218 for ( var subItemName in subItemDefs )
219 {
220 var subItem = this.editor.getMenuItem( subItemName );
221 if ( subItem )
222 {
223 subItem.state = subItemDefs[ subItemName ];
224 menu.add( subItem );
225 }
226 }
227
228 // Get the element representing the current item.
229 var element = this._.panel.getBlock( this.id ).element.getDocument().getById( this.id + String( index ) );
230
231 // Show the submenu.
232 menu.show( element, 2 );
233 }
234 },
235
236 proto :
237 {
238 add : function( item )
239 {
240 // Later we may sort the items, but Array#sort is not stable in
241 // some browsers, here we're forcing the original sequence with
242 // 'order' attribute if it hasn't been assigned. (#3868)
243 if ( !item.order )
244 item.order = this.items.length;
245
246 this.items.push( item );
247 },
248
249 removeAll : function()
250 {
251 this.items = [];
252 },
253
254 show : function( offsetParent, corner, offsetX, offsetY )
255 {
256 // Not for sub menu.
257 if ( !this.parent )
258 {
259 this._.onShow();
260 // Don't menu with zero items.
261 if ( ! this.items.length )
262 return;
263 }
264
265 corner = corner || ( this.editor.lang.dir == 'rtl' ? 2 : 1 );
266
267 var items = this.items,
268 editor = this.editor,
269 panel = this._.panel,
270 element = this._.element;
271
272 // Create the floating panel for this menu.
273 if ( !panel )
274 {
275 panel = this._.panel = new CKEDITOR.ui.floatPanel( this.editor,
276 CKEDITOR.document.getBody(),
277 this._.panelDefinition,
278 this._.level );
279
280 panel.onEscape = CKEDITOR.tools.bind( function( keystroke )
281 {
282 if ( this._.onEscape( keystroke ) === false )
283 return false;
284 },
285 this );
286
287 panel.onHide = CKEDITOR.tools.bind( function()
288 {
289 this._.onHide && this._.onHide();
290 },
291 this );
292
293 // Create an autosize block inside the panel.
294 var block = panel.addBlock( this.id, this._.panelDefinition.block );
295 block.autoSize = true;
296
297 var keys = block.keys;
298 keys[ 40 ] = 'next'; // ARROW-DOWN
299 keys[ 9 ] = 'next'; // TAB
300 keys[ 38 ] = 'prev'; // ARROW-UP
301 keys[ CKEDITOR.SHIFT + 9 ] = 'prev'; // SHIFT + TAB
302 keys[ ( editor.lang.dir == 'rtl' ? 37 : 39 ) ]= CKEDITOR.env.ie ? 'mouseup' : 'click'; // ARROW-RIGHT/ARROW-LEFT(rtl)
303 keys[ 32 ] = CKEDITOR.env.ie ? 'mouseup' : 'click'; // SPACE
304 CKEDITOR.env.ie && ( keys[ 13 ] = 'mouseup' ); // Manage ENTER, since onclick is blocked in IE (#8041).
305
306 element = this._.element = block.element;
307 element.addClass( editor.skinClass );
308
309 var elementDoc = element.getDocument();
310 elementDoc.getBody().setStyle( 'overflow', 'hidden' );
311 elementDoc.getElementsByTag( 'html' ).getItem( 0 ).setStyle( 'overflow', 'hidden' );
312
313 this._.itemOverFn = CKEDITOR.tools.addFunction( function( index )
314 {
315 clearTimeout( this._.showSubTimeout );
316 this._.showSubTimeout = CKEDITOR.tools.setTimeout( this._.showSubMenu, editor.config.menu_subMenuDelay || 400, this, [ index ] );
317 },
318 this );
319
320 this._.itemOutFn = CKEDITOR.tools.addFunction( function( index )
321 {
322 clearTimeout( this._.showSubTimeout );
323 },
324 this );
325
326 this._.itemClickFn = CKEDITOR.tools.addFunction( function( index )
327 {
328 var item = this.items[ index ];
329
330 if ( item.state == CKEDITOR.TRISTATE_DISABLED )
331 {
332 this.hide();
333 return;
334 }
335
336 if ( item.getItems )
337 this._.showSubMenu( index );
338 else
339 this._.onClick( item );
340 },
341 this );
342 }
343
344 // Put the items in the right order.
345 sortItems( items );
346
347 var chromeRoot = editor.container.getChild( 1 ),
348 mixedContentClass = chromeRoot.hasClass( 'cke_mixed_dir_content' ) ? ' cke_mixed_dir_content' : '';
349
350 // Build the HTML that composes the menu and its items.
351 var output = [ '<div class="cke_menu' + mixedContentClass + '" role="presentation">' ];
352
353 var length = items.length,
354 lastGroup = length && items[ 0 ].group;
355
356 for ( var i = 0 ; i < length ; i++ )
357 {
358 var item = items[ i ];
359 if ( lastGroup != item.group )
360 {
361 output.push( '<div class="cke_menuseparator" role="separator"></div>' );
362 lastGroup = item.group;
363 }
364
365 item.render( this, i, output );
366 }
367
368 output.push( '</div>' );
369
370 // Inject the HTML inside the panel.
371 element.setHtml( output.join( '' ) );
372
373 CKEDITOR.ui.fire( 'ready', this );
374
375 // Show the panel.
376 if ( this.parent )
377 this.parent._.panel.showAsChild( panel, this.id, offsetParent, corner, offsetX, offsetY );
378 else
379 panel.showBlock( this.id, offsetParent, corner, offsetX, offsetY );
380
381 editor.fire( 'menuShow', [ panel ] );
382 },
383
384 addListener : function( listenerFn )
385 {
386 this._.listeners.push( listenerFn );
387 },
388
389 hide : function( returnFocus )
390 {
391 this._.onHide && this._.onHide();
392 this._.panel && this._.panel.hide( returnFocus );
393 }
394 }
395 });
396
397 function sortItems( items )
398 {
399 items.sort( function( itemA, itemB )
400 {
401 if ( itemA.group < itemB.group )
402 return -1;
403 else if ( itemA.group > itemB.group )
404 return 1;
405
406 return itemA.order < itemB.order ? -1 :
407 itemA.order > itemB.order ? 1 :
408 0;
409 });
410 }
411 CKEDITOR.menuItem = CKEDITOR.tools.createClass(
412 {
413 $ : function( editor, name, definition )
414 {
415 CKEDITOR.tools.extend( this, definition,
416 // Defaults
417 {
418 order : 0,
419 className : 'cke_button_' + name
420 });
421
422 // Transform the group name into its order number.
423 this.group = editor._.menuGroups[ this.group ];
424
425 this.editor = editor;
426 this.name = name;
427 },
428
429 proto :
430 {
431 render : function( menu, index, output )
432 {
433 var id = menu.id + String( index ),
434 state = ( typeof this.state == 'undefined' ) ? CKEDITOR.TRISTATE_OFF : this.state;
435
436 var classes = ' cke_' + (
437 state == CKEDITOR.TRISTATE_ON ? 'on' :
438 state == CKEDITOR.TRISTATE_DISABLED ? 'disabled' :
439 'off' );
440
441 var htmlLabel = this.label;
442
443 if ( this.className )
444 classes += ' ' + this.className;
445
446 var hasSubMenu = this.getItems;
447
448 output.push(
449 '<span class="cke_menuitem' + ( this.icon && this.icon.indexOf( '.png' ) == -1 ? ' cke_noalphafix' : '' ) + '">' +
450 '<a id="', id, '"' +
451 ' class="', classes, '" href="javascript:void(\'', ( this.label || '' ).replace( "'", '' ), '\')"' +
452 ' title="', this.label, '"' +
453 ' tabindex="-1"' +
454 '_cke_focus=1' +
455 ' hidefocus="true"' +
456 ' role="menuitem"' +
457 ( hasSubMenu ? 'aria-haspopup="true"' : '' ) +
458 ( state == CKEDITOR.TRISTATE_DISABLED ? 'aria-disabled="true"' : '' ) +
459 ( state == CKEDITOR.TRISTATE_ON ? 'aria-pressed="true"' : '' ) );
460
461 // Some browsers don't cancel key events in the keydown but in the
462 // keypress.
463 // TODO: Check if really needed for Gecko+Mac.
464 if ( CKEDITOR.env.opera || ( CKEDITOR.env.gecko && CKEDITOR.env.mac ) )
465 {
466 output.push(
467 ' onkeypress="return false;"' );
468 }
469
470 // With Firefox, we need to force the button to redraw, otherwise it
471 // will remain in the focus state.
472 if ( CKEDITOR.env.gecko )
473 {
474 output.push(
475 ' onblur="this.style.cssText = this.style.cssText;"' );
476 }
477
478 var offset = ( this.iconOffset || 0 ) * -16;
479 output.push(
480 // ' onkeydown="return CKEDITOR.ui.button._.keydown(', index, ', event);"' +
481 ' onmouseover="CKEDITOR.tools.callFunction(', menu._.itemOverFn, ',', index, ');"' +
482 ' onmouseout="CKEDITOR.tools.callFunction(', menu._.itemOutFn, ',', index, ');" ' +
483 ( CKEDITOR.env.ie ? 'onclick="return false;" onmouseup' : 'onclick' ) + // #188
484 '="CKEDITOR.tools.callFunction(', menu._.itemClickFn, ',', index, '); return false;"' +
485 '>' +
486 '<span class="cke_icon_wrapper"><span class="cke_icon"' +
487 ( this.icon ? ' style="background-image:url(' + CKEDITOR.getUrl( this.icon ) + ');background-position:0 ' + offset + 'px;"'
488 : '' ) +
489 '></span></span>' +
490 '<span class="cke_label">' );
491
492 if ( hasSubMenu )
493 {
494 output.push(
495 '<span class="cke_menuarrow">',
496 '<span>&#',
497 ( this.editor.lang.dir == 'rtl' ?
498 '9668' : // BLACK LEFT-POINTING POINTER
499 '9658' ), // BLACK RIGHT-POINTING POINTER
500 ';</span>',
501 '</span>' );
502 }
503
504 output.push(
505 htmlLabel,
506 '</span>' +
507 '</a>' +
508 '</span>' );
509 }
510 }
511 });
512
513 })();
514
515
516 /**
517 * The amount of time, in milliseconds, the editor waits before displaying submenu
518 * options when moving the mouse over options that contain submenus, like the
519 * "Cell Properties" entry for tables.
520 * @type Number
521 * @default 400
522 * @example
523 * // Remove the submenu delay.
524 * config.menu_subMenuDelay = 0;
525 */
526
527 /**
528 * A comma separated list of items group names to be displayed in the context
529 * menu. The order of items will reflect the order specified in this list if
530 * no priority was defined in the groups.
531 * @type String
532 * @default 'clipboard,form,tablecell,tablecellproperties,tablerow,tablecolumn,table,anchor,link,image,flash,checkbox,radio,textfield,hiddenfield,imagebutton,button,select,textarea'
533 * @example
534 * config.menu_groups = 'clipboard,table,anchor,link,image';
535 */
536 CKEDITOR.config.menu_groups =
537 'clipboard,' +
538 'form,' +
539 'tablecell,tablecellproperties,tablerow,tablecolumn,table,'+
540 'anchor,link,image,flash,' +
541 'checkbox,radio,textfield,hiddenfield,imagebutton,button,select,textarea,div';