JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
vanilla ckeditor-4.3.1_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 || editor.config.removeButtons ),\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                 removeButtons: null,\r
145                 height: 100\r
146         } );\r
147 \r
148         function dumpToolbarConfiguration( editor, printGroups ) {\r
149                 var output = [],\r
150                         toolbar = editor.toolbar;\r
151 \r
152                 for ( var i = 0; i < toolbar.length; ++i ) {\r
153                         var group = dumpToolbarGroup( toolbar[ i ], printGroups );\r
154                         if ( group )\r
155                                 output.push( group );\r
156                 }\r
157 \r
158                 return 'config.toolbar' + ( printGroups ? 'Groups' : '' ) + ' = [\n\t' + output.join( ',\n\t' ) + '\n];';\r
159         }\r
160 \r
161         function dumpToolbarGroup( group, printGroups ) {\r
162                 var output = [];\r
163 \r
164                 if ( typeof group == 'string' )\r
165                         return '\'' + group + '\'';\r
166                 if ( CKEDITOR.tools.isArray( group ) )\r
167                         return dumpToolbarItems( group );\r
168                 // Skip group when printing entire toolbar configuration and there are no items in this group.\r
169                 if ( !printGroups && !group.items )\r
170                         return;\r
171 \r
172                 if ( group.name )\r
173                         output.push( 'name: \'' + group.name + '\'' );\r
174 \r
175                 if ( group.groups )\r
176                         output.push( 'groups: ' + dumpToolbarItems( group.groups ) );\r
177 \r
178                 if ( !printGroups )\r
179                         output.push( 'items: ' + dumpToolbarItems( group.items ) );\r
180 \r
181                 return '{ ' + output.join( ', ' ) + ' }';\r
182         }\r
183 \r
184         function dumpToolbarItems( items ) {\r
185                 if ( typeof items == 'string' )\r
186                         return '\'' + items + '\'';\r
187 \r
188                 var names = [],\r
189                         i, item;\r
190 \r
191                 for ( var i = 0; i < items.length; ++i ) {\r
192                         item = items[ i ];\r
193                         if ( typeof item == 'string' )\r
194                                 names.push( item );\r
195                         else {\r
196                                 if ( item.type == CKEDITOR.UI_SEPARATOR )\r
197                                         names.push( '-' );\r
198                                 else\r
199                                         names.push( buttonsNames[ item.name ] );\r
200                         }\r
201                 }\r
202 \r
203                 return '[ \'' + names.join( '\', \'' ) + '\' ]';\r
204         }\r
205 \r
206         // Creates { 'lowercased': 'LowerCased' } buttons names hash.\r
207         function createButtonsNamesHash( items ) {\r
208                 var hash = {},\r
209                         name;\r
210 \r
211                 for ( name in items ) {\r
212                         hash[ items[ name ].name ] = name;\r
213                 }\r
214 \r
215                 return hash;\r
216         }\r
217 \r
218 })();\r
219         </script>\r
220 \r
221         <div id="footer">\r
222                 <hr>\r
223                 <p>\r
224                         CKEditor - The text editor for the Internet - <a class="samples" href="http://ckeditor.com/">http://ckeditor.com</a>\r
225                 </p>\r
226                 <p id="copy">\r
227                         Copyright &copy; 2003-2013, <a class="samples" href="http://cksource.com/">CKSource</a> - Frederico\r
228                         Knabben. All rights reserved.\r
229                 </p>\r
230         </div>\r
231 </body>\r
232 </html>\r