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