JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
vanilla ckeditor-3.5.3
[ckeditor.git] / _source / themes / default / theme.js
1 /*\r
2 Copyright (c) 2003-2011, 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  * @name CKEDITOR.theme\r
8  * @class\r
9  */\r
10 \r
11 CKEDITOR.themes.add( 'default', (function()\r
12 {\r
13         function checkSharedSpace( editor, spaceName )\r
14         {\r
15                 var container,\r
16                         element;\r
17 \r
18                 // Try to retrieve the target element from the sharedSpaces settings.\r
19                 element = editor.config.sharedSpaces;\r
20                 element = element && element[ spaceName ];\r
21                 element = element && CKEDITOR.document.getById( element );\r
22 \r
23                 // If the element is available, we'll then create the container for\r
24                 // the space.\r
25                 if ( element )\r
26                 {\r
27                         // Creates an HTML structure that reproduces the editor class hierarchy.\r
28                         var html =\r
29                                 '<span class="cke_shared "' +\r
30                                 ' dir="'+ editor.lang.dir + '"' +\r
31                                 '>' +\r
32                                 '<span class="' + editor.skinClass + ' ' + editor.id + ' cke_editor_' + editor.name + '">' +\r
33                                 '<span class="' + CKEDITOR.env.cssClass + '">' +\r
34                                 '<span class="cke_wrapper cke_' + editor.lang.dir + '">' +\r
35                                 '<span class="cke_editor">' +\r
36                                 '<div class="cke_' + spaceName + '">' +\r
37                                 '</div></span></span></span></span></span>';\r
38 \r
39                         var mainContainer = element.append( CKEDITOR.dom.element.createFromHtml( html, element.getDocument() ) );\r
40 \r
41                         // Only the first container starts visible. Others get hidden.\r
42                         if ( element.getCustomData( 'cke_hasshared' ) )\r
43                                 mainContainer.hide();\r
44                         else\r
45                                 element.setCustomData( 'cke_hasshared', 1 );\r
46 \r
47                         // Get the deeper inner <div>.\r
48                         container = mainContainer.getChild( [0,0,0,0] );\r
49 \r
50                         // Save a reference to the shared space container.\r
51                         !editor.sharedSpaces && ( editor.sharedSpaces = {} );\r
52                         editor.sharedSpaces[ spaceName ] = container;\r
53 \r
54                         // When the editor gets focus, we show the space container, hiding others.\r
55                         editor.on( 'focus', function()\r
56                                 {\r
57                                         for ( var i = 0, sibling, children = element.getChildren() ; ( sibling = children.getItem( i ) ) ; i++ )\r
58                                         {\r
59                                                 if ( sibling.type == CKEDITOR.NODE_ELEMENT\r
60                                                         && !sibling.equals( mainContainer )\r
61                                                         && sibling.hasClass( 'cke_shared' ) )\r
62                                                 {\r
63                                                         sibling.hide();\r
64                                                 }\r
65                                         }\r
66 \r
67                                         mainContainer.show();\r
68                                 });\r
69 \r
70                         editor.on( 'destroy', function()\r
71                                 {\r
72                                         mainContainer.remove();\r
73                                 });\r
74                 }\r
75 \r
76                 return container;\r
77         }\r
78 \r
79         return /** @lends CKEDITOR.theme */ {\r
80                 build : function( editor, themePath )\r
81                 {\r
82                         var name = editor.name,\r
83                                 element = editor.element,\r
84                                 elementMode = editor.elementMode;\r
85 \r
86                         if ( !element || elementMode == CKEDITOR.ELEMENT_MODE_NONE )\r
87                                 return;\r
88 \r
89                         if ( elementMode == CKEDITOR.ELEMENT_MODE_REPLACE )\r
90                                 element.hide();\r
91 \r
92                         // Get the HTML for the predefined spaces.\r
93                         var topHtml                     = editor.fire( 'themeSpace', { space : 'top', html : '' } ).html;\r
94                         var contentsHtml        = editor.fire( 'themeSpace', { space : 'contents', html : '' } ).html;\r
95                         var bottomHtml          = editor.fireOnce( 'themeSpace', { space : 'bottom', html : '' } ).html;\r
96 \r
97                         var height      = contentsHtml && editor.config.height;\r
98 \r
99                         var tabIndex = editor.config.tabIndex || editor.element.getAttribute( 'tabindex' ) || 0;\r
100 \r
101                         // The editor height is considered only if the contents space got filled.\r
102                         if ( !contentsHtml )\r
103                                 height = 'auto';\r
104                         else if ( !isNaN( height ) )\r
105                                 height += 'px';\r
106 \r
107                         var style = '';\r
108                         var width       = editor.config.width;\r
109 \r
110                         if ( width )\r
111                         {\r
112                                 if ( !isNaN( width ) )\r
113                                         width += 'px';\r
114 \r
115                                 style += "width: " + width + ";";\r
116                         }\r
117 \r
118                         var sharedTop           = topHtml && checkSharedSpace( editor, 'top' ),\r
119                                 sharedBottoms   = checkSharedSpace( editor, 'bottom' );\r
120 \r
121                         sharedTop               && ( sharedTop.setHtml( topHtml )               , topHtml = '' );\r
122                         sharedBottoms   && ( sharedBottoms.setHtml( bottomHtml ), bottomHtml = '' );\r
123 \r
124                         var container = CKEDITOR.dom.element.createFromHtml( [\r
125                                 '<span' +\r
126                                         ' id="cke_', name, '"' +\r
127                                         ' class="', editor.skinClass, ' ', editor.id, ' cke_editor_', name, '"' +\r
128                                         ' dir="', editor.lang.dir, '"' +\r
129                                         ' title="', ( CKEDITOR.env.gecko ? ' ' : '' ), '"' +\r
130                                         ' lang="', editor.langCode, '"' +\r
131                                                 ( CKEDITOR.env.webkit? ' tabindex="' + tabIndex + '"' : '' ) +\r
132                                         ' role="application"' +\r
133                                         ' aria-labelledby="cke_', name, '_arialbl"' +\r
134                                         ( style ? ' style="' + style + '"' : '' ) +\r
135                                         '>' +\r
136                                         '<span id="cke_', name, '_arialbl" class="cke_voice_label">' + editor.lang.editor + '</span>' +\r
137                                         '<span class="' , CKEDITOR.env.cssClass, '" role="presentation">' +\r
138                                                 '<span class="cke_wrapper cke_', editor.lang.dir, '" role="presentation">' +\r
139                                                         '<table class="cke_editor" border="0" cellspacing="0" cellpadding="0" role="presentation"><tbody>' +\r
140                                                                 '<tr', topHtml          ? '' : ' style="display:none"', ' role="presentation"><td id="cke_top_'         , name, '" class="cke_top" role="presentation">'        , topHtml               , '</td></tr>' +\r
141                                                                 '<tr', contentsHtml     ? '' : ' style="display:none"', ' role="presentation"><td id="cke_contents_', name, '" class="cke_contents" style="height:', height, '" role="presentation">', contentsHtml, '</td></tr>' +\r
142                                                                 '<tr', bottomHtml       ? '' : ' style="display:none"', ' role="presentation"><td id="cke_bottom_'      , name, '" class="cke_bottom" role="presentation">'     , bottomHtml    , '</td></tr>' +\r
143                                                         '</tbody></table>' +\r
144                                                         //Hide the container when loading skins, later restored by skin css.\r
145                                                         '<style>.', editor.skinClass, '{visibility:hidden;}</style>' +\r
146                                                 '</span>' +\r
147                                         '</span>' +\r
148                                 '</span>' ].join( '' ) );\r
149 \r
150                         container.getChild( [1, 0, 0, 0, 0] ).unselectable();\r
151                         container.getChild( [1, 0, 0, 0, 2] ).unselectable();\r
152 \r
153                         if ( elementMode == CKEDITOR.ELEMENT_MODE_REPLACE )\r
154                                 container.insertAfter( element );\r
155                         else\r
156                                 element.append( container );\r
157 \r
158                         /**\r
159                          * The DOM element that holds the main editor interface.\r
160                          * @name CKEDITOR.editor.prototype.container\r
161                          * @type CKEDITOR.dom.element\r
162                          * @example\r
163                          * var editor = CKEDITOR.instances.editor1;\r
164                          * alert( <b>editor.container</b>.getName() );  "span"\r
165                          */\r
166                         editor.container = container;\r
167 \r
168                         // Disable browser context menu for editor's chrome.\r
169                         container.disableContextMenu();\r
170 \r
171                         // Use a class to indicate that the current selection is in different direction than the UI.\r
172                         editor.on( 'contentDirChanged', function( evt )\r
173                         {\r
174                                 var func = ( editor.lang.dir != evt.data ? 'add' : 'remove' ) + 'Class';\r
175 \r
176                                 container.getChild( 1 )[ func ]( 'cke_mixed_dir_content' );\r
177 \r
178                                 // Put the mixed direction class on the respective element also for shared spaces.\r
179                                 var toolbarSpace = this.sharedSpaces && this.sharedSpaces[ this.config.toolbarLocation ];\r
180                                 toolbarSpace && toolbarSpace.getParent().getParent()[ func ]( 'cke_mixed_dir_content' );\r
181                         });\r
182 \r
183                         editor.fireOnce( 'themeLoaded' );\r
184                         editor.fireOnce( 'uiReady' );\r
185                 },\r
186 \r
187                 buildDialog : function( editor )\r
188                 {\r
189                         var baseIdNumber = CKEDITOR.tools.getNextNumber();\r
190 \r
191                         var element = CKEDITOR.dom.element.createFromHtml( [\r
192                                         '<div class="', editor.id, '_dialog cke_editor_', editor.name.replace('.', '\\.'), '_dialog cke_skin_', editor.skinName,\r
193                                                 '" dir="', editor.lang.dir, '"' +\r
194                                                 ' lang="', editor.langCode, '"' +\r
195                                                 ' role="dialog"' +\r
196                                                 ' aria-labelledby="%title#"' +\r
197                                                 '>' +\r
198                                                 '<table class="cke_dialog', ' ' + CKEDITOR.env.cssClass,\r
199                                                         ' cke_', editor.lang.dir, '" style="position:absolute" role="presentation">' +\r
200                                                         '<tr><td role="presentation">' +\r
201                                                         '<div class="%body" role="presentation">' +\r
202                                                                 '<div id="%title#" class="%title" role="presentation"></div>' +\r
203                                                                 '<a id="%close_button#" class="%close_button" href="javascript:void(0)" title="' +  editor.lang.common.close+'" role="button"><span class="cke_label">X</span></a>' +\r
204                                                                 '<div id="%tabs#" class="%tabs" role="tablist"></div>' +\r
205                                                                 '<table class="%contents" role="presentation">' +\r
206                                                                 '<tr>' +\r
207                                                                   '<td id="%contents#" class="%contents" role="presentation"></td>' +\r
208                                                                 '</tr>' +\r
209                                                                 '<tr>' +\r
210                                                                   '<td id="%footer#" class="%footer" role="presentation"></td>' +\r
211                                                                 '</tr>' +\r
212                                                                 '</table>' +\r
213                                                         '</div>' +\r
214                                                         '<div id="%tl#" class="%tl"></div>' +\r
215                                                         '<div id="%tc#" class="%tc"></div>' +\r
216                                                         '<div id="%tr#" class="%tr"></div>' +\r
217                                                         '<div id="%ml#" class="%ml"></div>' +\r
218                                                         '<div id="%mr#" class="%mr"></div>' +\r
219                                                         '<div id="%bl#" class="%bl"></div>' +\r
220                                                         '<div id="%bc#" class="%bc"></div>' +\r
221                                                         '<div id="%br#" class="%br"></div>' +\r
222                                                         '</td></tr>' +\r
223                                                 '</table>',\r
224 \r
225                                                 //Hide the container when loading skins, later restored by skin css.\r
226                                                 ( CKEDITOR.env.ie ? '' : '<style>.cke_dialog{visibility:hidden;}</style>' ),\r
227 \r
228                                         '</div>'\r
229                                 ].join( '' )\r
230                                         .replace( /#/g, '_' + baseIdNumber )\r
231                                         .replace( /%/g, 'cke_dialog_' ) );\r
232 \r
233                         var body = element.getChild( [ 0, 0, 0, 0, 0 ] ),\r
234                                 title = body.getChild( 0 ),\r
235                                 close = body.getChild( 1 );\r
236 \r
237                         // Make the Title and Close Button unselectable.\r
238                         title.unselectable();\r
239                         close.unselectable();\r
240 \r
241 \r
242                         return {\r
243                                 element : element,\r
244                                 parts :\r
245                                 {\r
246                                         dialog          : element.getChild( 0 ),\r
247                                         title           : title,\r
248                                         close           : close,\r
249                                         tabs            : body.getChild( 2 ),\r
250                                         contents        : body.getChild( [ 3, 0, 0, 0 ] ),\r
251                                         footer          : body.getChild( [ 3, 0, 1, 0 ] )\r
252                                 }\r
253                         };\r
254                 },\r
255 \r
256                 destroy : function( editor )\r
257                 {\r
258                         var container = editor.container,\r
259                                 element = editor.element;\r
260 \r
261                         if ( container )\r
262                         {\r
263                                 container.clearCustomData();\r
264                                 container.remove();\r
265                         }\r
266 \r
267                         if ( element )\r
268                         {\r
269                                 element.clearCustomData();\r
270                                 editor.elementMode == CKEDITOR.ELEMENT_MODE_REPLACE && element.show();\r
271                                 delete editor.element;\r
272                         }\r
273                 }\r
274         };\r
275 })() );\r
276 \r
277 /**\r
278  * Returns the DOM element that represents a theme space. The default theme defines\r
279  * three spaces, namely "top", "contents" and "bottom", representing the main\r
280  * blocks that compose the editor interface.\r
281  * @param {String} spaceName The space name.\r
282  * @returns {CKEDITOR.dom.element} The element that represents the space.\r
283  * @example\r
284  * // Hide the bottom space in the UI.\r
285  * var bottom = editor.getThemeSpace( 'bottom' );\r
286  * bottom.setStyle( 'display', 'none' );\r
287  */\r
288 CKEDITOR.editor.prototype.getThemeSpace = function( spaceName )\r
289 {\r
290         var spacePrefix = 'cke_' + spaceName;\r
291         var space = this._[ spacePrefix ] ||\r
292                 ( this._[ spacePrefix ] = CKEDITOR.document.getById( spacePrefix + '_' + this.name ) );\r
293         return space;\r
294 };\r
295 \r
296 /**\r
297  * Resizes the editor interface.\r
298  * @param {Number|String} width The new width. It can be an pixels integer or a\r
299  *              CSS size value.\r
300  * @param {Number|String} height The new height. It can be an pixels integer or\r
301  *              a CSS size value.\r
302  * @param {Boolean} [isContentHeight] Indicates that the provided height is to\r
303  *              be applied to the editor contents space, not to the entire editor\r
304  *              interface. Defaults to false.\r
305  * @param {Boolean} [resizeInner] Indicates that the first inner interface\r
306  *              element must receive the size, not the outer element. The default theme\r
307  *              defines the interface inside a pair of span elements\r
308  *              (&lt;span&gt;&lt;span&gt;...&lt;/span&gt;&lt;/span&gt;). By default the\r
309  *              first span element receives the sizes. If this parameter is set to\r
310  *              true, the second span is sized instead.\r
311  * @example\r
312  * editor.resize( 900, 300 );\r
313  * @example\r
314  * editor.resize( '100%', 450, true );\r
315  */\r
316 CKEDITOR.editor.prototype.resize = function( width, height, isContentHeight, resizeInner )\r
317 {\r
318         var container = this.container,\r
319                 contents = CKEDITOR.document.getById( 'cke_contents_' + this.name ),\r
320                 outer = resizeInner ? container.getChild( 1 ) : container;\r
321 \r
322         // Resize the width first.\r
323         // WEBKIT BUG: Webkit requires that we put the editor off from display when we\r
324         // resize it. If we don't, the browser crashes!\r
325         CKEDITOR.env.webkit && outer.setStyle( 'display', 'none' );\r
326         // Set as border box width. (#5353)\r
327         outer.setSize( 'width',  width, true );\r
328         if ( CKEDITOR.env.webkit )\r
329         {\r
330                 outer.$.offsetWidth;\r
331                 outer.setStyle( 'display', '' );\r
332         }\r
333 \r
334         // Get the height delta between the outer table and the content area.\r
335         // If we're setting the content area's height, then we don't need the delta.\r
336         var delta = isContentHeight ? 0 : ( outer.$.offsetHeight || 0 ) - ( contents.$.clientHeight || 0 );\r
337         contents.setStyle( 'height', Math.max( height - delta, 0 ) + 'px' );\r
338 \r
339         // Emit a resize event.\r
340         this.fire( 'resize' );\r
341 };\r
342 \r
343 /**\r
344  * Gets the element that can be freely used to check the editor size. This method\r
345  * is mainly used by the resize plugin, which adds a UI handle that can be used\r
346  * to resize the editor.\r
347  * @returns {CKEDITOR.dom.element} The resizable element.\r
348  * @example\r
349  */\r
350 CKEDITOR.editor.prototype.getResizable = function()\r
351 {\r
352         return this.container;\r
353 };\r
354 \r
355 /**\r
356  * Makes it possible to place some of the editor UI blocks, like the toolbar\r
357  * and the elements path, into any element in the page.\r
358  * The elements used to hold the UI blocks can be shared among several editor\r
359  * instances. In that case, only the blocks of the active editor instance will\r
360  * display.\r
361  * @name CKEDITOR.config.sharedSpaces\r
362  * @type Object\r
363  * @default undefined\r
364  * @example\r
365  * // Place the toolbar inside the element with ID "someElementId" and the\r
366  * // elements path into the element with ID "anotherId".\r
367  * config.sharedSpaces =\r
368  * {\r
369  *     top : 'someElementId',\r
370  *     bottom : 'anotherId'\r
371  * };\r
372  * @example\r
373  * // Place the toolbar inside the element with ID "someElementId". The\r
374  * // elements path will remain attached to the editor UI.\r
375  * config.sharedSpaces =\r
376  * {\r
377  *     top : 'someElementId'\r
378  * };\r
379  */\r
380 \r
381 /**\r
382  * Fired after the editor instance is resized through\r
383  * the {@link CKEDITOR.editor.prototype.resize} method.\r
384  * @name CKEDITOR.editor#resize\r
385  * @event\r
386  */\r