JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
960ee0b507ccf4c0f43d8ef0ecb4e70581584e79
[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                         editor.fireOnce( 'themeLoaded' );\r
172                         editor.fireOnce( 'uiReady' );\r
173                 },\r
174 \r
175                 buildDialog : function( editor )\r
176                 {\r
177                         var baseIdNumber = CKEDITOR.tools.getNextNumber();\r
178 \r
179                         var element = CKEDITOR.dom.element.createFromHtml( [\r
180                                         '<div class="', editor.id, '_dialog cke_editor_', editor.name.replace('.', '\\.'), '_dialog cke_skin_', editor.skinName,\r
181                                                 '" dir="', editor.lang.dir, '"' +\r
182                                                 ' lang="', editor.langCode, '"' +\r
183                                                 ' role="dialog"' +\r
184                                                 ' aria-labelledby="%title#"' +\r
185                                                 '>' +\r
186                                                 '<table class="cke_dialog', ' ' + CKEDITOR.env.cssClass,\r
187                                                         ' cke_', editor.lang.dir, '" style="position:absolute" role="presentation">' +\r
188                                                         '<tr><td role="presentation">' +\r
189                                                         '<div class="%body" role="presentation">' +\r
190                                                                 '<div id="%title#" class="%title" role="presentation"></div>' +\r
191                                                                 '<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
192                                                                 '<div id="%tabs#" class="%tabs" role="tablist"></div>' +\r
193                                                                 '<table class="%contents" role="presentation">' +\r
194                                                                 '<tr>' +\r
195                                                                   '<td id="%contents#" class="%contents" role="presentation"></td>' +\r
196                                                                 '</tr>' +\r
197                                                                 '<tr>' +\r
198                                                                   '<td id="%footer#" class="%footer" role="presentation"></td>' +\r
199                                                                 '</tr>' +\r
200                                                                 '</table>' +\r
201                                                         '</div>' +\r
202                                                         '<div id="%tl#" class="%tl"></div>' +\r
203                                                         '<div id="%tc#" class="%tc"></div>' +\r
204                                                         '<div id="%tr#" class="%tr"></div>' +\r
205                                                         '<div id="%ml#" class="%ml"></div>' +\r
206                                                         '<div id="%mr#" class="%mr"></div>' +\r
207                                                         '<div id="%bl#" class="%bl"></div>' +\r
208                                                         '<div id="%bc#" class="%bc"></div>' +\r
209                                                         '<div id="%br#" class="%br"></div>' +\r
210                                                         '</td></tr>' +\r
211                                                 '</table>',\r
212 \r
213                                                 //Hide the container when loading skins, later restored by skin css.\r
214                                                 ( CKEDITOR.env.ie ? '' : '<style>.cke_dialog{visibility:hidden;}</style>' ),\r
215 \r
216                                         '</div>'\r
217                                 ].join( '' )\r
218                                         .replace( /#/g, '_' + baseIdNumber )\r
219                                         .replace( /%/g, 'cke_dialog_' ) );\r
220 \r
221                         var body = element.getChild( [ 0, 0, 0, 0, 0 ] ),\r
222                                 title = body.getChild( 0 ),\r
223                                 close = body.getChild( 1 );\r
224 \r
225                         // Make the Title and Close Button unselectable.\r
226                         title.unselectable();\r
227                         close.unselectable();\r
228 \r
229 \r
230                         return {\r
231                                 element : element,\r
232                                 parts :\r
233                                 {\r
234                                         dialog          : element.getChild( 0 ),\r
235                                         title           : title,\r
236                                         close           : close,\r
237                                         tabs            : body.getChild( 2 ),\r
238                                         contents        : body.getChild( [ 3, 0, 0, 0 ] ),\r
239                                         footer          : body.getChild( [ 3, 0, 1, 0 ] )\r
240                                 }\r
241                         };\r
242                 },\r
243 \r
244                 destroy : function( editor )\r
245                 {\r
246                         var container = editor.container,\r
247                                 element = editor.element;\r
248 \r
249                         if ( container )\r
250                         {\r
251                                 container.clearCustomData();\r
252                                 container.remove();\r
253                         }\r
254 \r
255                         if ( element )\r
256                         {\r
257                                 element.clearCustomData();\r
258                                 editor.elementMode == CKEDITOR.ELEMENT_MODE_REPLACE && element.show();\r
259                                 delete editor.element;\r
260                         }\r
261                 }\r
262         };\r
263 })() );\r
264 \r
265 /**\r
266  * Returns the DOM element that represents a theme space. The default theme defines\r
267  * three spaces, namely "top", "contents" and "bottom", representing the main\r
268  * blocks that compose the editor interface.\r
269  * @param {String} spaceName The space name.\r
270  * @returns {CKEDITOR.dom.element} The element that represents the space.\r
271  * @example\r
272  * // Hide the bottom space in the UI.\r
273  * var bottom = editor.getThemeSpace( 'bottom' );\r
274  * bottom.setStyle( 'display', 'none' );\r
275  */\r
276 CKEDITOR.editor.prototype.getThemeSpace = function( spaceName )\r
277 {\r
278         var spacePrefix = 'cke_' + spaceName;\r
279         var space = this._[ spacePrefix ] ||\r
280                 ( this._[ spacePrefix ] = CKEDITOR.document.getById( spacePrefix + '_' + this.name ) );\r
281         return space;\r
282 };\r
283 \r
284 /**\r
285  * Resizes the editor interface.\r
286  * @param {Number|String} width The new width. It can be an pixels integer or a\r
287  *              CSS size value.\r
288  * @param {Number|String} height The new height. It can be an pixels integer or\r
289  *              a CSS size value.\r
290  * @param {Boolean} [isContentHeight] Indicates that the provided height is to\r
291  *              be applied to the editor contents space, not to the entire editor\r
292  *              interface. Defaults to false.\r
293  * @param {Boolean} [resizeInner] Indicates that the first inner interface\r
294  *              element must receive the size, not the outer element. The default theme\r
295  *              defines the interface inside a pair of span elements\r
296  *              (&lt;span&gt;&lt;span&gt;...&lt;/span&gt;&lt;/span&gt;). By default the\r
297  *              first span element receives the sizes. If this parameter is set to\r
298  *              true, the second span is sized instead.\r
299  * @example\r
300  * editor.resize( 900, 300 );\r
301  * @example\r
302  * editor.resize( '100%', 450, true );\r
303  */\r
304 CKEDITOR.editor.prototype.resize = function( width, height, isContentHeight, resizeInner )\r
305 {\r
306         var container = this.container,\r
307                 contents = CKEDITOR.document.getById( 'cke_contents_' + this.name ),\r
308                 outer = resizeInner ? container.getChild( 1 ) : container;\r
309 \r
310         // Resize the width first.\r
311         // WEBKIT BUG: Webkit requires that we put the editor off from display when we\r
312         // resize it. If we don't, the browser crashes!\r
313         CKEDITOR.env.webkit && outer.setStyle( 'display', 'none' );\r
314         // Set as border box width. (#5353)\r
315         outer.setSize( 'width',  width, true );\r
316         if ( CKEDITOR.env.webkit )\r
317         {\r
318                 outer.$.offsetWidth;\r
319                 outer.setStyle( 'display', '' );\r
320         }\r
321 \r
322         // Get the height delta between the outer table and the content area.\r
323         // If we're setting the content area's height, then we don't need the delta.\r
324         var delta = isContentHeight ? 0 : ( outer.$.offsetHeight || 0 ) - ( contents.$.clientHeight || 0 );\r
325         contents.setStyle( 'height', Math.max( height - delta, 0 ) + 'px' );\r
326 \r
327         // Emit a resize event.\r
328         this.fire( 'resize' );\r
329 };\r
330 \r
331 /**\r
332  * Gets the element that can be freely used to check the editor size. This method\r
333  * is mainly used by the resize plugin, which adds a UI handle that can be used\r
334  * to resize the editor.\r
335  * @returns {CKEDITOR.dom.element} The resizable element.\r
336  * @example\r
337  */\r
338 CKEDITOR.editor.prototype.getResizable = function()\r
339 {\r
340         return this.container;\r
341 };\r
342 \r
343 /**\r
344  * Makes it possible to place some of the editor UI blocks, like the toolbar\r
345  * and the elements path, into any element in the page.\r
346  * The elements used to hold the UI blocks can be shared among several editor\r
347  * instances. In that case, only the blocks of the active editor instance will\r
348  * display.\r
349  * @name CKEDITOR.config.sharedSpaces\r
350  * @type Object\r
351  * @default undefined\r
352  * @example\r
353  * // Place the toolbar inside the element with ID "someElementId" and the\r
354  * // elements path into the element with ID "anotherId".\r
355  * config.sharedSpaces =\r
356  * {\r
357  *     top : 'someElementId',\r
358  *     bottom : 'anotherId'\r
359  * };\r
360  * @example\r
361  * // Place the toolbar inside the element with ID "someElementId". The\r
362  * // elements path will remain attached to the editor UI.\r
363  * config.sharedSpaces =\r
364  * {\r
365  *     top : 'someElementId'\r
366  * };\r
367  */\r
368 \r
369 /**\r
370  * Fired after the editor instance is resized through\r
371  * the {@link CKEDITOR.editor.prototype.resize} method.\r
372  * @name CKEDITOR.editor#resize\r
373  * @event\r
374  */\r