JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
a6cd8d080fc82936ff8c2f4f9cd47790f29da5dc
[ckeditor.git] / _source / plugins / toolbar / 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 /**\r
7  * @fileOverview The "toolbar" plugin. Renders the default toolbar interface in\r
8  * the editor.\r
9  */\r
10 \r
11 (function()\r
12 {\r
13         var toolbox = function()\r
14         {\r
15                 this.toolbars = [];\r
16                 this.focusCommandExecuted = false;\r
17         };\r
18 \r
19         toolbox.prototype.focus = function()\r
20         {\r
21                 for ( var t = 0, toolbar ; toolbar = this.toolbars[ t++ ] ; )\r
22                 {\r
23                         for ( var i = 0, item ; item = toolbar.items[ i++ ] ; )\r
24                         {\r
25                                 if ( item.focus )\r
26                                 {\r
27                                         item.focus();\r
28                                         return;\r
29                                 }\r
30                         }\r
31                 }\r
32         };\r
33 \r
34         var commands =\r
35         {\r
36                 toolbarFocus :\r
37                 {\r
38                         modes : { wysiwyg : 1, source : 1 },\r
39                         readOnly : 1,\r
40 \r
41                         exec : function( editor )\r
42                         {\r
43                                 if ( editor.toolbox )\r
44                                 {\r
45                                         editor.toolbox.focusCommandExecuted = true;\r
46 \r
47                                         // Make the first button focus accessible for IE. (#3417)\r
48                                         // Adobe AIR instead need while of delay.\r
49                                         if ( CKEDITOR.env.ie || CKEDITOR.env.air )\r
50                                                 setTimeout( function(){ editor.toolbox.focus(); }, 100 );\r
51                                         else\r
52                                                 editor.toolbox.focus();\r
53                                 }\r
54                         }\r
55                 }\r
56         };\r
57 \r
58         CKEDITOR.plugins.add( 'toolbar',\r
59         {\r
60                 requires : [ 'button' ],\r
61                 init : function( editor )\r
62                 {\r
63                         var endFlag;\r
64 \r
65                         var itemKeystroke = function( item, keystroke )\r
66                         {\r
67                                 var next, toolbar;\r
68                                 var rtl = editor.lang.dir == 'rtl',\r
69                                         toolbarGroupCycling = editor.config.toolbarGroupCycling;\r
70 \r
71                                 toolbarGroupCycling = toolbarGroupCycling === undefined || toolbarGroupCycling;\r
72 \r
73                                 switch ( keystroke )\r
74                                 {\r
75                                         case 9 :                                        // TAB\r
76                                         case CKEDITOR.SHIFT + 9 :       // SHIFT + TAB\r
77                                                 // Cycle through the toolbars, starting from the one\r
78                                                 // closest to the current item.\r
79                                                 while ( !toolbar || !toolbar.items.length )\r
80                                                 {\r
81                                                         toolbar = keystroke == 9 ?\r
82                                                                 ( ( toolbar ? toolbar.next : item.toolbar.next ) || editor.toolbox.toolbars[ 0 ] ) :\r
83                                                                 ( ( toolbar ? toolbar.previous : item.toolbar.previous ) || editor.toolbox.toolbars[ editor.toolbox.toolbars.length - 1 ] );\r
84 \r
85                                                         // Look for the first item that accepts focus.\r
86                                                         if ( toolbar.items.length )\r
87                                                         {\r
88                                                                 item = toolbar.items[ endFlag ? ( toolbar.items.length - 1 ) : 0 ];\r
89                                                                 while ( item && !item.focus )\r
90                                                                 {\r
91                                                                         item = endFlag ? item.previous : item.next;\r
92 \r
93                                                                         if ( !item )\r
94                                                                                 toolbar = 0;\r
95                                                                 }\r
96                                                         }\r
97                                                 }\r
98 \r
99                                                 if ( item )\r
100                                                         item.focus();\r
101 \r
102                                                 return false;\r
103 \r
104                                         case rtl ? 37 : 39 :            // RIGHT-ARROW\r
105                                         case 40 :                                       // DOWN-ARROW\r
106                                                 next = item;\r
107                                                 do\r
108                                                 {\r
109                                                         // Look for the next item in the toolbar.\r
110                                                         next = next.next;\r
111 \r
112                                                         // If it's the last item, cycle to the first one.\r
113                                                         if ( !next && toolbarGroupCycling )\r
114                                                                 next = item.toolbar.items[ 0 ];\r
115                                                 }\r
116                                                 while ( next && !next.focus )\r
117 \r
118                                                 // If available, just focus it, otherwise focus the\r
119                                                 // first one.\r
120                                                 if ( next )\r
121                                                         next.focus();\r
122                                                 else\r
123                                                         // Send a TAB.\r
124                                                         itemKeystroke( item, 9 );\r
125 \r
126                                                 return false;\r
127 \r
128                                         case rtl ? 39 : 37 :            // LEFT-ARROW\r
129                                         case 38 :                                       // UP-ARROW\r
130                                                 next = item;\r
131                                                 do\r
132                                                 {\r
133                                                         // Look for the previous item in the toolbar.\r
134                                                         next = next.previous;\r
135 \r
136                                                         // If it's the first item, cycle to the last one.\r
137                                                         if ( !next && toolbarGroupCycling )\r
138                                                                 next = item.toolbar.items[ item.toolbar.items.length - 1 ];\r
139                                                 }\r
140                                                 while ( next && !next.focus )\r
141 \r
142                                                 // If available, just focus it, otherwise focus the\r
143                                                 // last one.\r
144                                                 if ( next )\r
145                                                         next.focus();\r
146                                                 else\r
147                                                 {\r
148                                                         endFlag = 1;\r
149                                                         // Send a SHIFT + TAB.\r
150                                                         itemKeystroke( item, CKEDITOR.SHIFT + 9 );\r
151                                                         endFlag = 0;\r
152                                                 }\r
153 \r
154                                                 return false;\r
155 \r
156                                         case 27 :                                       // ESC\r
157                                                 editor.focus();\r
158                                                 return false;\r
159 \r
160                                         case 13 :                                       // ENTER\r
161                                         case 32 :                                       // SPACE\r
162                                                 item.execute();\r
163                                                 return false;\r
164                                 }\r
165                                 return true;\r
166                         };\r
167 \r
168                         editor.on( 'themeSpace', function( event )\r
169                                 {\r
170                                         if ( event.data.space == editor.config.toolbarLocation )\r
171                                         {\r
172                                                 editor.toolbox = new toolbox();\r
173 \r
174                                                 var labelId = CKEDITOR.tools.getNextId();\r
175 \r
176                                                 var output = [ '<div class="cke_toolbox" role="group" aria-labelledby="', labelId, '" onmousedown="return false;"' ],\r
177                                                         expanded =  editor.config.toolbarStartupExpanded !== false,\r
178                                                         groupStarted;\r
179 \r
180                                                 output.push( expanded ? '>' : ' style="display:none">' );\r
181 \r
182                                                 // Sends the ARIA label.\r
183                                                 output.push( '<span id="', labelId, '" class="cke_voice_label">', editor.lang.toolbars, '</span>' );\r
184 \r
185                                                 var toolbars = editor.toolbox.toolbars,\r
186                                                         toolbar =\r
187                                                                         ( editor.config.toolbar instanceof Array ) ?\r
188                                                                                 editor.config.toolbar\r
189                                                                         :\r
190                                                                                 editor.config[ 'toolbar_' + editor.config.toolbar ];\r
191 \r
192                                                 for ( var r = 0 ; r < toolbar.length ; r++ )\r
193                                                 {\r
194                                                         var toolbarId,\r
195                                                                 toolbarObj = 0,\r
196                                                                 toolbarName,\r
197                                                                 row = toolbar[ r ],\r
198                                                                 items;\r
199 \r
200                                                         // It's better to check if the row object is really\r
201                                                         // available because it's a common mistake to leave\r
202                                                         // an extra comma in the toolbar definition\r
203                                                         // settings, which leads on the editor not loading\r
204                                                         // at all in IE. (#3983)\r
205                                                         if ( !row )\r
206                                                                 continue;\r
207 \r
208                                                         if ( groupStarted )\r
209                                                         {\r
210                                                                 output.push( '</div>' );\r
211                                                                 groupStarted = 0;\r
212                                                         }\r
213 \r
214                                                         if ( row === '/' )\r
215                                                         {\r
216                                                                 output.push( '<div class="cke_break"></div>' );\r
217                                                                 continue;\r
218                                                         }\r
219 \r
220                                                         items = row.items || row;\r
221 \r
222                                                         // Create all items defined for this toolbar.\r
223                                                         for ( var i = 0 ; i < items.length ; i++ )\r
224                                                         {\r
225                                                                 var item,\r
226                                                                         itemName = items[ i ],\r
227                                                                         canGroup;\r
228 \r
229                                                                 item = editor.ui.create( itemName );\r
230 \r
231                                                                 if ( item )\r
232                                                                 {\r
233                                                                         canGroup = item.canGroup !== false;\r
234 \r
235                                                                         // Initialize the toolbar first, if needed.\r
236                                                                         if ( !toolbarObj )\r
237                                                                         {\r
238                                                                                 // Create the basic toolbar object.\r
239                                                                                 toolbarId = CKEDITOR.tools.getNextId();\r
240                                                                                 toolbarObj = { id : toolbarId, items : [] };\r
241                                                                                 toolbarName = row.name && ( editor.lang.toolbarGroups[ row.name ] || row.name );\r
242 \r
243                                                                                 // Output the toolbar opener.\r
244                                                                                 output.push( '<span id="', toolbarId, '" class="cke_toolbar"',\r
245                                                                                         ( toolbarName ? ' aria-labelledby="'+ toolbarId +  '_label"' : '' ),\r
246                                                                                         ' role="toolbar">' );\r
247 \r
248                                                                                 // If a toolbar name is available, send the voice label.\r
249                                                                                 toolbarName && output.push( '<span id="', toolbarId, '_label" class="cke_voice_label">', toolbarName, '</span>' );\r
250 \r
251                                                                                 output.push( '<span class="cke_toolbar_start"></span>' );\r
252 \r
253                                                                                 // Add the toolbar to the "editor.toolbox.toolbars"\r
254                                                                                 // array.\r
255                                                                                 var index = toolbars.push( toolbarObj ) - 1;\r
256 \r
257                                                                                 // Create the next/previous reference.\r
258                                                                                 if ( index > 0 )\r
259                                                                                 {\r
260                                                                                         toolbarObj.previous = toolbars[ index - 1 ];\r
261                                                                                         toolbarObj.previous.next = toolbarObj;\r
262                                                                                 }\r
263                                                                         }\r
264 \r
265                                                                         if ( canGroup )\r
266                                                                         {\r
267                                                                                 if ( !groupStarted )\r
268                                                                                 {\r
269                                                                                         output.push( '<span class="cke_toolgroup" role="presentation">' );\r
270                                                                                         groupStarted = 1;\r
271                                                                                 }\r
272                                                                         }\r
273                                                                         else if ( groupStarted )\r
274                                                                         {\r
275                                                                                 output.push( '</span>' );\r
276                                                                                 groupStarted = 0;\r
277                                                                         }\r
278 \r
279                                                                         var itemObj = item.render( editor, output );\r
280                                                                         index = toolbarObj.items.push( itemObj ) - 1;\r
281 \r
282                                                                         if ( index > 0 )\r
283                                                                         {\r
284                                                                                 itemObj.previous = toolbarObj.items[ index - 1 ];\r
285                                                                                 itemObj.previous.next = itemObj;\r
286                                                                         }\r
287 \r
288                                                                         itemObj.toolbar = toolbarObj;\r
289                                                                         itemObj.onkey = itemKeystroke;\r
290 \r
291                                                                         /*\r
292                                                                          * Fix for #3052:\r
293                                                                          * Prevent JAWS from focusing the toolbar after document load.\r
294                                                                          */\r
295                                                                         itemObj.onfocus = function()\r
296                                                                         {\r
297                                                                                 if ( !editor.toolbox.focusCommandExecuted )\r
298                                                                                         editor.focus();\r
299                                                                         };\r
300                                                                 }\r
301                                                         }\r
302 \r
303                                                         if ( groupStarted )\r
304                                                         {\r
305                                                                 output.push( '</span>' );\r
306                                                                 groupStarted = 0;\r
307                                                         }\r
308 \r
309                                                         if ( toolbarObj )\r
310                                                                 output.push( '<span class="cke_toolbar_end"></span></span>' );\r
311                                                 }\r
312 \r
313                                                 output.push( '</div>' );\r
314 \r
315                                                 if ( editor.config.toolbarCanCollapse )\r
316                                                 {\r
317                                                         var collapserFn = CKEDITOR.tools.addFunction(\r
318                                                                 function()\r
319                                                                 {\r
320                                                                         editor.execCommand( 'toolbarCollapse' );\r
321                                                                 });\r
322 \r
323                                                         editor.on( 'destroy', function () {\r
324                                                                         CKEDITOR.tools.removeFunction( collapserFn );\r
325                                                                 });\r
326 \r
327                                                         var collapserId = CKEDITOR.tools.getNextId();\r
328 \r
329                                                         editor.addCommand( 'toolbarCollapse',\r
330                                                                 {\r
331                                                                         readOnly : 1,\r
332                                                                         exec : function( editor )\r
333                                                                         {\r
334                                                                                 var collapser = CKEDITOR.document.getById( collapserId ),\r
335                                                                                         toolbox = collapser.getPrevious(),\r
336                                                                                         contents = editor.getThemeSpace( 'contents' ),\r
337                                                                                         toolboxContainer = toolbox.getParent(),\r
338                                                                                         contentHeight = parseInt( contents.$.style.height, 10 ),\r
339                                                                                         previousHeight = toolboxContainer.$.offsetHeight,\r
340                                                                                         collapsed = !toolbox.isVisible();\r
341 \r
342                                                                                 if ( !collapsed )\r
343                                                                                 {\r
344                                                                                         toolbox.hide();\r
345                                                                                         collapser.addClass( 'cke_toolbox_collapser_min' );\r
346                                                                                         collapser.setAttribute( 'title', editor.lang.toolbarExpand );\r
347                                                                                 }\r
348                                                                                 else\r
349                                                                                 {\r
350                                                                                         toolbox.show();\r
351                                                                                         collapser.removeClass( 'cke_toolbox_collapser_min' );\r
352                                                                                         collapser.setAttribute( 'title', editor.lang.toolbarCollapse );\r
353                                                                                 }\r
354 \r
355                                                                                 // Update collapser symbol.\r
356                                                                                 collapser.getFirst().setText( collapsed ?\r
357                                                                                         '\u25B2' :              // BLACK UP-POINTING TRIANGLE\r
358                                                                                         '\u25C0' );             // BLACK LEFT-POINTING TRIANGLE\r
359 \r
360                                                                                 var dy = toolboxContainer.$.offsetHeight - previousHeight;\r
361                                                                                 contents.setStyle( 'height', ( contentHeight - dy ) + 'px' );\r
362 \r
363                                                                                 editor.fire( 'resize' );\r
364                                                                         },\r
365 \r
366                                                                         modes : { wysiwyg : 1, source : 1 }\r
367                                                                 } );\r
368 \r
369                                                         output.push( '<a title="' + ( expanded ? editor.lang.toolbarCollapse : editor.lang.toolbarExpand )\r
370                                                                                                           + '" id="' + collapserId + '" tabIndex="-1" class="cke_toolbox_collapser' );\r
371 \r
372                                                         if ( !expanded )\r
373                                                                 output.push( ' cke_toolbox_collapser_min' );\r
374 \r
375                                                         output.push( '" onclick="CKEDITOR.tools.callFunction(' + collapserFn + ')">',\r
376                                                                                 '<span>&#9650;</span>',         // BLACK UP-POINTING TRIANGLE\r
377                                                                                 '</a>' );\r
378                                                 }\r
379 \r
380                                                 event.data.html += output.join( '' );\r
381                                         }\r
382                                 });\r
383 \r
384                         editor.on( 'destroy', function()\r
385                         {\r
386                                 var toolbars, index = 0, i,\r
387                                                 items, instance;\r
388                                 toolbars = this.toolbox.toolbars;\r
389                                 for ( ; index < toolbars.length; index++ )\r
390                                 {\r
391                                         items = toolbars[ index ].items;\r
392                                         for ( i = 0; i < items.length; i++ )\r
393                                         {\r
394                                                 instance = items[ i ];\r
395                                                 if ( instance.clickFn ) CKEDITOR.tools.removeFunction( instance.clickFn );\r
396                                                 if ( instance.keyDownFn ) CKEDITOR.tools.removeFunction( instance.keyDownFn );\r
397                                         }\r
398                                 }\r
399                         });\r
400 \r
401                         editor.addCommand( 'toolbarFocus', commands.toolbarFocus );\r
402 \r
403                         editor.ui.add( '-', CKEDITOR.UI_SEPARATOR, {} );\r
404                         editor.ui.addHandler( CKEDITOR.UI_SEPARATOR,\r
405                         {\r
406                                 create: function()\r
407                                 {\r
408                                         return {\r
409                                                 render : function( editor, output )\r
410                                                 {\r
411                                                         output.push( '<span class="cke_separator" role="separator"></span>' );\r
412                                                         return {};\r
413                                                 }\r
414                                         };\r
415                                 }\r
416                         });\r
417                 }\r
418         });\r
419 })();\r
420 \r
421 CKEDITOR.UI_SEPARATOR = 'separator';\r
422 \r
423 /**\r
424  * The "theme space" to which rendering the toolbar. For the default theme,\r
425  * the recommended options are "top" and "bottom".\r
426  * @type String\r
427  * @default 'top'\r
428  * @see CKEDITOR.config.theme\r
429  * @example\r
430  * config.toolbarLocation = 'bottom';\r
431  */\r
432 CKEDITOR.config.toolbarLocation = 'top';\r
433 \r
434 /**\r
435  * The toolbar definition. It is an array of toolbars (strips),\r
436  * each one being also an array, containing a list of UI items.\r
437  * Note that this setting is composed by "toolbar_" added by the toolbar name,\r
438  * which in this case is called "Basic". This second part of the setting name\r
439  * can be anything. You must use this name in the\r
440  * {@link CKEDITOR.config.toolbar} setting, so you instruct the editor which\r
441  * toolbar_(name) setting to you.\r
442  * @type Array\r
443  * @example\r
444  * // Defines a toolbar with only one strip containing the "Source" button, a\r
445  * // separator and the "Bold" and "Italic" buttons.\r
446  * <b>config.toolbar_Basic =\r
447  * [\r
448  *     [ 'Source', '-', 'Bold', 'Italic' ]\r
449  * ]</b>;\r
450  * config.toolbar = 'Basic';\r
451  */\r
452 CKEDITOR.config.toolbar_Basic =\r
453 [\r
454         ['Bold', 'Italic', '-', 'NumberedList', 'BulletedList', '-', 'Link', 'Unlink','-','About']\r
455 ];\r
456 \r
457 /**\r
458  * This is the default toolbar definition used by the editor. It contains all\r
459  * editor features.\r
460  * @type Array\r
461  * @default (see example)\r
462  * @example\r
463  * // This is actually the default value.\r
464  * config.toolbar_Full =\r
465  * [\r
466  *     { name: 'document',    items : [ 'Source','-','Save','NewPage','DocProps','Preview','Print','-','Templates' ] },\r
467  *     { name: 'clipboard',   items : [ 'Cut','Copy','Paste','PasteText','PasteFromWord','-','Undo','Redo' ] },\r
468  *     { name: 'editing',     items : [ 'Find','Replace','-','SelectAll','-','SpellChecker', 'Scayt' ] },\r
469  *     { name: 'forms',       items : [ 'Form', 'Checkbox', 'Radio', 'TextField', 'Textarea', 'Select', 'Button', 'ImageButton', 'HiddenField' ] },\r
470  *     '/',\r
471  *     { name: 'basicstyles', items : [ 'Bold','Italic','Underline','Strike','Subscript','Superscript','-','RemoveFormat' ] },\r
472  *     { name: 'paragraph',   items : [ 'NumberedList','BulletedList','-','Outdent','Indent','-','Blockquote','CreateDiv','-','JustifyLeft','JustifyCenter','JustifyRight','JustifyBlock','-','BidiLtr','BidiRtl' ] },\r
473  *     { name: 'links',       items : [ 'Link','Unlink','Anchor' ] },\r
474  *     { name: 'insert',      items : [ 'Image','Flash','Table','HorizontalRule','Smiley','SpecialChar','PageBreak' ] },\r
475  *     '/',\r
476  *     { name: 'styles',      items : [ 'Styles','Format','Font','FontSize' ] },\r
477  *     { name: 'colors',      items : [ 'TextColor','BGColor' ] },\r
478  *     { name: 'tools',       items : [ 'Maximize', 'ShowBlocks','-','About' ] }\r
479  * ];\r
480  */\r
481 CKEDITOR.config.toolbar_Full =\r
482 [\r
483         { name: 'document',             items : [ 'Source','-','Save','NewPage','DocProps','Preview','Print','-','Templates' ] },\r
484         { name: 'clipboard',    items : [ 'Cut','Copy','Paste','PasteText','PasteFromWord','-','Undo','Redo' ] },\r
485         { name: 'editing',              items : [ 'Find','Replace','-','SelectAll','-','SpellChecker', 'Scayt' ] },\r
486         { name: 'forms',                items : [ 'Form', 'Checkbox', 'Radio', 'TextField', 'Textarea', 'Select', 'Button', 'ImageButton', 'HiddenField' ] },\r
487         '/',\r
488         { name: 'basicstyles',  items : [ 'Bold','Italic','Underline','Strike','Subscript','Superscript','-','RemoveFormat' ] },\r
489         { name: 'paragraph',    items : [ 'NumberedList','BulletedList','-','Outdent','Indent','-','Blockquote','CreateDiv','-','JustifyLeft','JustifyCenter','JustifyRight','JustifyBlock','-','BidiLtr','BidiRtl' ] },\r
490         { name: 'links',                items : [ 'Link','Unlink','Anchor' ] },\r
491         { name: 'insert',               items : [ 'Image','Flash','Table','HorizontalRule','Smiley','SpecialChar','PageBreak','Iframe' ] },\r
492         '/',\r
493         { name: 'styles',               items : [ 'Styles','Format','Font','FontSize' ] },\r
494         { name: 'colors',               items : [ 'TextColor','BGColor' ] },\r
495         { name: 'tools',                items : [ 'Maximize', 'ShowBlocks','-','About' ] }\r
496 ];\r
497 \r
498 /**\r
499  * The toolbox (alias toolbar) definition. It is a toolbar name or an array of\r
500  * toolbars (strips), each one being also an array, containing a list of UI items.\r
501  * @type Array|String\r
502  * @default 'Full'\r
503  * @example\r
504  * // Defines a toolbar with only one strip containing the "Source" button, a\r
505  * // separator and the "Bold" and "Italic" buttons.\r
506  * config.toolbar =\r
507  * [\r
508  *     [ 'Source', '-', 'Bold', 'Italic' ]\r
509  * ];\r
510  * @example\r
511  * // Load toolbar_Name where Name = Basic.\r
512  * config.toolbar = 'Basic';\r
513  */\r
514 CKEDITOR.config.toolbar = 'Full';\r
515 \r
516 /**\r
517  * Whether the toolbar can be collapsed by the user. If disabled, the collapser\r
518  * button will not be displayed.\r
519  * @type Boolean\r
520  * @default true\r
521  * @example\r
522  * config.toolbarCanCollapse = false;\r
523  */\r
524 CKEDITOR.config.toolbarCanCollapse = true;\r
525 \r
526 /**\r
527  * Whether the toolbar must start expanded when the editor is loaded.\r
528  * @name CKEDITOR.config.toolbarStartupExpanded\r
529  * @type Boolean\r
530  * @default true\r
531  * @example\r
532  * config.toolbarStartupExpanded = false;\r
533  */\r
534 \r
535 /**\r
536  * When enabled, makes the arrow keys navigation cycle within the current\r
537  * toolbar group. Otherwise the arrows will move trought all items available in\r
538  * the toolbar. The TAB key will still be used to quickly jump among the\r
539  * toolbar groups.\r
540  * @name CKEDITOR.config.toolbarGroupCycling\r
541  * @since 3.6\r
542  * @type Boolean\r
543  * @default true\r
544  * @example\r
545  * config.toolbarGroupCycling = false;\r
546  */\r