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