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