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