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