JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
vanilla ckeditor-3.6.3
[ckeditor.git] / _source / themes / default / theme.js
1 /*\r
2 Copyright (c) 2003-2012, 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                         // IFrame shim for dialog that masks activeX in IE. (#7619)\r
246                         if ( CKEDITOR.env.ie && !CKEDITOR.env.ie6Compat )\r
247                         {\r
248                                 var isCustomDomain = CKEDITOR.env.isCustomDomain(),\r
249                                         src = 'javascript:void(function(){' + encodeURIComponent( 'document.open();' + ( isCustomDomain ? ( 'document.domain="' + document.domain + '";' ) : '' ) + 'document.close();' ) + '}())',\r
250                                         iframe = CKEDITOR.dom.element.createFromHtml( '<iframe' +\r
251                                                         ' frameBorder="0"' +\r
252                                                         ' class="cke_iframe_shim"' +\r
253                                                         ' src="' + src + '"' +\r
254                                                         ' tabIndex="-1"' +\r
255                                                         '></iframe>' );\r
256                                 iframe.appendTo( body.getParent() );\r
257                         }\r
258 \r
259                         // Make the Title and Close Button unselectable.\r
260                         title.unselectable();\r
261                         close.unselectable();\r
262 \r
263 \r
264                         return {\r
265                                 element : element,\r
266                                 parts :\r
267                                 {\r
268                                         dialog          : element.getChild( 0 ),\r
269                                         title           : title,\r
270                                         close           : close,\r
271                                         tabs            : body.getChild( 2 ),\r
272                                         contents        : body.getChild( [ 3, 0, 0, 0 ] ),\r
273                                         footer          : body.getChild( [ 3, 0, 1, 0 ] )\r
274                                 }\r
275                         };\r
276                 },\r
277 \r
278                 destroy : function( editor )\r
279                 {\r
280                         var container = editor.container,\r
281                                 element = editor.element;\r
282 \r
283                         if ( container )\r
284                         {\r
285                                 container.clearCustomData();\r
286                                 container.remove();\r
287                         }\r
288 \r
289                         if ( element )\r
290                         {\r
291                                 element.clearCustomData();\r
292                                 editor.elementMode == CKEDITOR.ELEMENT_MODE_REPLACE && element.show();\r
293                                 delete editor.element;\r
294                         }\r
295                 }\r
296         };\r
297 })() );\r
298 \r
299 /**\r
300  * Returns the DOM element that represents a theme space. The default theme defines\r
301  * three spaces, namely "top", "contents" and "bottom", representing the main\r
302  * blocks that compose the editor interface.\r
303  * @param {String} spaceName The space name.\r
304  * @returns {CKEDITOR.dom.element} The element that represents the space.\r
305  * @example\r
306  * // Hide the bottom space in the UI.\r
307  * var bottom = editor.getThemeSpace( 'bottom' );\r
308  * bottom.setStyle( 'display', 'none' );\r
309  */\r
310 CKEDITOR.editor.prototype.getThemeSpace = function( spaceName )\r
311 {\r
312         var spacePrefix = 'cke_' + spaceName;\r
313         var space = this._[ spacePrefix ] ||\r
314                 ( this._[ spacePrefix ] = CKEDITOR.document.getById( spacePrefix + '_' + this.name ) );\r
315         return space;\r
316 };\r
317 \r
318 /**\r
319  * Resizes the editor interface.\r
320  * @param {Number|String} width The new width. It can be an pixels integer or a\r
321  *              CSS size value.\r
322  * @param {Number|String} height The new height. It can be an pixels integer or\r
323  *              a CSS size value.\r
324  * @param {Boolean} [isContentHeight] Indicates that the provided height is to\r
325  *              be applied to the editor contents space, not to the entire editor\r
326  *              interface. Defaults to false.\r
327  * @param {Boolean} [resizeInner] Indicates that the first inner interface\r
328  *              element must receive the size, not the outer element. The default theme\r
329  *              defines the interface inside a pair of span elements\r
330  *              (&lt;span&gt;&lt;span&gt;...&lt;/span&gt;&lt;/span&gt;). By default the\r
331  *              first span element receives the sizes. If this parameter is set to\r
332  *              true, the second span is sized instead.\r
333  * @example\r
334  * editor.resize( 900, 300 );\r
335  * @example\r
336  * editor.resize( '100%', 450, true );\r
337  */\r
338 CKEDITOR.editor.prototype.resize = function( width, height, isContentHeight, resizeInner )\r
339 {\r
340         var container = this.container,\r
341                 contents = CKEDITOR.document.getById( 'cke_contents_' + this.name ),\r
342                 contentsFrame = CKEDITOR.env.webkit && this.document && this.document.getWindow().$.frameElement,\r
343                 outer = resizeInner ? container.getChild( 1 ) : container;\r
344 \r
345         // Set as border box width. (#5353)\r
346         outer.setSize( 'width',  width, true );\r
347 \r
348         // WebKit needs to refresh the iframe size to avoid rendering issues. (1/2) (#8348)\r
349         contentsFrame && ( contentsFrame.style.width = '1%' );\r
350 \r
351         // Get the height delta between the outer table and the content area.\r
352         // If we're setting the content area's height, then we don't need the delta.\r
353         var delta = isContentHeight ? 0 : ( outer.$.offsetHeight || 0 ) - ( contents.$.clientHeight || 0 );\r
354         contents.setStyle( 'height', Math.max( height - delta, 0 ) + 'px' );\r
355 \r
356         // WebKit needs to refresh the iframe size to avoid rendering issues. (2/2) (#8348)\r
357         contentsFrame && ( contentsFrame.style.width = '100%' );\r
358 \r
359         // Emit a resize event.\r
360         this.fire( 'resize' );\r
361 };\r
362 \r
363 /**\r
364  * Gets the element that can be freely used to check the editor size. This method\r
365  * is mainly used by the resize plugin, which adds a UI handle that can be used\r
366  * to resize the editor.\r
367  * @param {Boolean} forContents Whether to return the "contents" part of the theme instead of the container.\r
368  * @returns {CKEDITOR.dom.element} The resizable element.\r
369  * @example\r
370  */\r
371 CKEDITOR.editor.prototype.getResizable = function( forContents )\r
372 {\r
373         return forContents ? CKEDITOR.document.getById( 'cke_contents_' + this.name ) : this.container;\r
374 };\r
375 \r
376 /**\r
377  * Makes it possible to place some of the editor UI blocks, like the toolbar\r
378  * and the elements path, into any element in the page.\r
379  * The elements used to hold the UI blocks can be shared among several editor\r
380  * instances. In that case, only the blocks of the active editor instance will\r
381  * display.\r
382  * @name CKEDITOR.config.sharedSpaces\r
383  * @type Object\r
384  * @default undefined\r
385  * @example\r
386  * // Place the toolbar inside the element with ID "someElementId" and the\r
387  * // elements path into the element with ID "anotherId".\r
388  * config.sharedSpaces =\r
389  * {\r
390  *     top : 'someElementId',\r
391  *     bottom : 'anotherId'\r
392  * };\r
393  * @example\r
394  * // Place the toolbar inside the element with ID "someElementId". The\r
395  * // elements path will remain attached to the editor UI.\r
396  * config.sharedSpaces =\r
397  * {\r
398  *     top : 'someElementId'\r
399  * };\r
400  */\r
401 \r
402 /**\r
403  * Fired after the editor instance is resized through\r
404  * the {@link CKEDITOR.editor.prototype.resize} method.\r
405  * @name CKEDITOR.editor#resize\r
406  * @event\r
407  */\r