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