JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
vanilla ckeditor-4.3_full
[ckeditor.git] / samples / plugins / toolbar / toolbar.html
1 <!DOCTYPE html>\r
2 <!--\r
3 Copyright (c) 2003-2013, CKSource - Frederico Knabben. All rights reserved.\r
4 For licensing, see LICENSE.md or http://ckeditor.com/license\r
5 -->\r
6 <html>\r
7 <head>\r
8         <title>Toolbar Configuration &mdash; CKEditor Sample</title>\r
9         <meta charset="utf-8">\r
10         <meta name="ckeditor-sample-name" content="Toolbar Configurations">\r
11         <meta name="ckeditor-sample-group" content="Advanced Samples">\r
12         <meta name="ckeditor-sample-description" content="Configuring CKEditor to display full or custom toolbar layout.">\r
13         <script src="../../../ckeditor.js"></script>\r
14         <link href="../../../samples/sample.css" rel="stylesheet">\r
15 </head>\r
16 <body>\r
17         <h1 class="samples">\r
18                 <a href="../../../samples/index.html">CKEditor Samples</a> &raquo; Toolbar Configuration\r
19         </h1>\r
20         <div class="description">\r
21                 <p>\r
22                         This sample page demonstrates editor with loaded <a href="#fullToolbar">full toolbar</a> (all registered buttons) and, if\r
23                         current editor's configuration modifies default settings, also editor with <a href="#currentToolbar">modified toolbar</a>.\r
24                 </p>\r
25 \r
26                 <p>Since CKEditor 4 there are two ways to configure toolbar buttons.</p>\r
27 \r
28                 <h2 class="samples">By <a href="http://docs.ckeditor.com/#!/api/CKEDITOR.config-cfg-toolbar">config.toolbar</a></h2>\r
29 \r
30                 <p>\r
31                         You can explicitly define which buttons are displayed in which groups and in which order.\r
32                         This is the more precise setting, but less flexible. If newly added plugin adds its\r
33                         own button you'll have to add it manually to your <code>config.toolbar</code> setting as well.\r
34                 </p>\r
35 \r
36                 <p>To add a CKEditor instance with custom toolbar setting, insert the following JavaScript call to your code:</p>\r
37 \r
38                 <pre class="samples">\r
39 CKEDITOR.replace( <em>'textarea_id'</em>, {\r
40         <strong>toolbar:</strong> [\r
41                 { name: 'document', items: [ 'Source', '-', 'NewPage', 'Preview', '-', 'Templates' ] }, // Defines toolbar group with name (used to create voice label) and items in 3 subgroups.\r
42                 [ 'Cut', 'Copy', 'Paste', 'PasteText', 'PasteFromWord', '-', 'Undo', 'Redo' ],                  // Defines toolbar group without name.\r
43                 '/',                                                                                                                                                                    // Line break - next group will be placed in new line.\r
44                 { name: 'basicstyles', items: [ 'Bold', 'Italic' ] }\r
45         ]\r
46 });</pre>\r
47 \r
48                 <h2 class="samples">By <a href="http://docs.ckeditor.com/#!/api/CKEDITOR.config-cfg-toolbarGroups">config.toolbarGroups</a></h2>\r
49 \r
50                 <p>\r
51                         You can define which groups of buttons (like e.g. <code>basicstyles</code>, <code>clipboard</code>\r
52                         and <code>forms</code>) are displayed and in which order. Registered buttons are associated\r
53                         with toolbar groups by <code>toolbar</code> property in their definition.\r
54                         This setting's advantage is that you don't have to modify toolbar configuration\r
55                         when adding/removing plugins which register their own buttons.\r
56                 </p>\r
57 \r
58                 <p>To add a CKEditor instance with custom toolbar groups setting, insert the following JavaScript call to your code:</p>\r
59 \r
60                 <pre class="samples">\r
61 CKEDITOR.replace( <em>'textarea_id'</em>, {\r
62         <strong>toolbarGroups:</strong> [\r
63                 { name: 'document',        groups: [ 'mode', 'document' ] },                    // Displays document group with its two subgroups.\r
64                 { name: 'clipboard',   groups: [ 'clipboard', 'undo' ] },                       // Group's name will be used to create voice label.\r
65                 '/',                                                                                                                            // Line break - next group will be placed in new line.\r
66                 { name: 'basicstyles', groups: [ 'basicstyles', 'cleanup' ] },\r
67                 { name: 'links' }\r
68         ]\r
69 \r
70         // NOTE: Remember to leave 'toolbar' property with the default value (null).\r
71 });</pre>\r
72         </div>\r
73 \r
74         <div id="currentToolbar" style="display: none">\r
75                 <h2 class="samples">Current toolbar configuration</h2>\r
76                 <p>Below you can see editor with current toolbar definition.</p>\r
77                 <textarea cols="80" id="editorCurrent" name="editorCurrent" rows="10">&lt;p&gt;This is some &lt;strong&gt;sample text&lt;/strong&gt;. You are using &lt;a href="http://ckeditor.com/"&gt;CKEditor&lt;/a&gt;.&lt;/p&gt;</textarea>\r
78                 <pre id="editorCurrentCfg" class="samples"></pre>\r
79         </div>\r
80 \r
81         <div id="fullToolbar">\r
82                 <h2 class="samples">Full toolbar configuration</h2>\r
83                 <p>Below you can see editor with full toolbar, generated automatically by the editor.</p>\r
84                 <p>\r
85                         <strong>Note</strong>: To create editor instance with full toolbar you don't have to set anything.\r
86                         Just leave <code>toolbar</code> and <code>toolbarGroups</code> with the default, <code>null</code> values.\r
87                 </p>\r
88                 <textarea cols="80" id="editorFull" name="editorFull" rows="10">&lt;p&gt;This is some &lt;strong&gt;sample text&lt;/strong&gt;. You are using &lt;a href="http://ckeditor.com/"&gt;CKEditor&lt;/a&gt;.&lt;/p&gt;</textarea>\r
89                 <pre id="editorFullCfg" class="samples"></pre>\r
90         </div>\r
91 \r
92         <script>\r
93 \r
94 (function() {\r
95         'use strict';\r
96 \r
97         var buttonsNames;\r
98 \r
99         CKEDITOR.config.extraPlugins = 'toolbar';\r
100 \r
101         CKEDITOR.on( 'instanceReady', function( evt ) {\r
102                 var editor = evt.editor,\r
103                         editorCurrent = editor.name == 'editorCurrent',\r
104                         defaultToolbar = !( editor.config.toolbar || editor.config.toolbarGroups ),\r
105                         pre = CKEDITOR.document.getById( editor.name + 'Cfg' ),\r
106                         output = '';\r
107 \r
108                 if ( editorCurrent ) {\r
109                         // If default toolbar configuration has been modified, show "current toolbar" section.\r
110                         if ( !defaultToolbar )\r
111                                 CKEDITOR.document.getById( 'currentToolbar' ).show();\r
112                         else\r
113                                 return;\r
114                 }\r
115 \r
116                 if ( !buttonsNames )\r
117                         buttonsNames = createButtonsNamesHash( editor.ui.items );\r
118 \r
119                 // Toolbar isn't set explicitly, so it was created automatically from toolbarGroups.\r
120                 if ( !editor.config.toolbar ) {\r
121                         output +=\r
122                                 '// Toolbar configuration generated automatically by the editor based on config.toolbarGroups.\n' +\r
123                                 dumpToolbarConfiguration( editor ) +\r
124                                 '\n\n' +\r
125                                 '// Toolbar groups configuration.\n' +\r
126                                 dumpToolbarConfiguration( editor, true )\r
127                 }\r
128                 // Toolbar groups doesn't count in this case - print only toolbar.\r
129                 else {\r
130                         output += '// Toolbar configuration.\n' +\r
131                                 dumpToolbarConfiguration( editor );\r
132                 }\r
133 \r
134                 // Recreate to avoid old IE from loosing whitespaces on filling <pre> content.\r
135                 var preOutput = pre.getOuterHtml().replace( /(?=<\/)/, output );\r
136                 CKEDITOR.dom.element.createFromHtml( preOutput ).replace( pre );\r
137         } );\r
138 \r
139         CKEDITOR.replace( 'editorCurrent', { height: 100 } );\r
140         CKEDITOR.replace( 'editorFull', {\r
141                 // Reset toolbar settings, so full toolbar will be generated automatically.\r
142                 toolbar: null,\r
143                 toolbarGroups: null,\r
144                 height: 100\r
145         } );\r
146 \r
147         function dumpToolbarConfiguration( editor, printGroups ) {\r
148                 var output = [],\r
149                         toolbar = editor.toolbar;\r
150 \r
151                 for ( var i = 0; i < toolbar.length; ++i ) {\r
152                         var group = dumpToolbarGroup( toolbar[ i ], printGroups );\r
153                         if ( group )\r
154                                 output.push( group );\r
155                 }\r
156 \r
157                 return 'config.toolbar' + ( printGroups ? 'Groups' : '' ) + ' = [\n\t' + output.join( ',\n\t' ) + '\n];';\r
158         }\r
159 \r
160         function dumpToolbarGroup( group, printGroups ) {\r
161                 var output = [];\r
162 \r
163                 if ( typeof group == 'string' )\r
164                         return '\'' + group + '\'';\r
165                 if ( CKEDITOR.tools.isArray( group ) )\r
166                         return dumpToolbarItems( group );\r
167                 // Skip group when printing entire toolbar configuration and there are no items in this group.\r
168                 if ( !printGroups && !group.items )\r
169                         return;\r
170 \r
171                 if ( group.name )\r
172                         output.push( 'name: \'' + group.name + '\'' );\r
173 \r
174                 if ( group.groups )\r
175                         output.push( 'groups: ' + dumpToolbarItems( group.groups ) );\r
176 \r
177                 if ( !printGroups )\r
178                         output.push( 'items: ' + dumpToolbarItems( group.items ) );\r
179 \r
180                 return '{ ' + output.join( ', ' ) + ' }';\r
181         }\r
182 \r
183         function dumpToolbarItems( items ) {\r
184                 if ( typeof items == 'string' )\r
185                         return '\'' + items + '\'';\r
186 \r
187                 var names = [],\r
188                         i, item;\r
189 \r
190                 for ( var i = 0; i < items.length; ++i ) {\r
191                         item = items[ i ];\r
192                         if ( typeof item == 'string' )\r
193                                 names.push( item );\r
194                         else {\r
195                                 if ( item.type == CKEDITOR.UI_SEPARATOR )\r
196                                         names.push( '-' );\r
197                                 else\r
198                                         names.push( buttonsNames[ item.name ] );\r
199                         }\r
200                 }\r
201 \r
202                 return '[ \'' + names.join( '\', \'' ) + '\' ]';\r
203         }\r
204 \r
205         // Creates { 'lowercased': 'LowerCased' } buttons names hash.\r
206         function createButtonsNamesHash( items ) {\r
207                 var hash = {},\r
208                         name;\r
209 \r
210                 for ( name in items ) {\r
211                         hash[ items[ name ].name ] = name;\r
212                 }\r
213 \r
214                 return hash;\r
215         }\r
216 \r
217 })();\r
218         </script>\r
219 \r
220         <div id="footer">\r
221                 <hr>\r
222                 <p>\r
223                         CKEditor - The text editor for the Internet - <a class="samples" href="http://ckeditor.com/">http://ckeditor.com</a>\r
224                 </p>\r
225                 <p id="copy">\r
226                         Copyright &copy; 2003-2013, <a class="samples" href="http://cksource.com/">CKSource</a> - Frederico\r
227                         Knabben. All rights reserved.\r
228                 </p>\r
229         </div>\r
230 </body>\r
231 </html>\r