JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
vanilla ckeditor-3.2.2
[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                                         ' role="application"' +\r
122                                         ' aria-labelledby="cke_', name, '_arialbl"' +\r
123                                         ( style ? ' style="' + style + '"' : '' ) +\r
124                                         '>' +\r
125                                         '<span id="cke_', name, '_arialbl" class="cke_voice_label">' + editor.lang.editor + '</span>' +\r
126                                         '<span class="' , CKEDITOR.env.cssClass, '" role="presentation">' +\r
127                                                 '<span class="cke_wrapper cke_', editor.lang.dir, '" role="presentation">' +\r
128                                                         '<table class="cke_editor" border="0" cellspacing="0" cellpadding="0" role="presentation"><tbody>' +\r
129                                                                 '<tr', topHtml          ? '' : ' style="display:none"', ' role="presentation"><td id="cke_top_'         , name, '" class="cke_top" role="presentation">'        , topHtml               , '</td></tr>' +\r
130                                                                 '<tr', contentsHtml     ? '' : ' style="display:none"', ' role="presentation"><td id="cke_contents_', name, '" class="cke_contents" style="height:', height, '" role="presentation">', contentsHtml, '</td></tr>' +\r
131                                                                 '<tr', bottomHtml       ? '' : ' style="display:none"', ' role="presentation"><td id="cke_bottom_'      , name, '" class="cke_bottom" role="presentation">'     , bottomHtml    , '</td></tr>' +\r
132                                                         '</tbody></table>' +\r
133                                                         //Hide the container when loading skins, later restored by skin css.\r
134                                                         '<style>.', editor.skinClass, '{visibility:hidden;}</style>' +\r
135                                                 '</span>' +\r
136                                         '</span>' +\r
137                                 '</span>' ].join( '' ) );\r
138 \r
139                         container.getChild( [1, 0, 0, 0, 0] ).unselectable();\r
140                         container.getChild( [1, 0, 0, 0, 2] ).unselectable();\r
141 \r
142                         if ( elementMode == CKEDITOR.ELEMENT_MODE_REPLACE )\r
143                                 container.insertAfter( element );\r
144                         else\r
145                                 element.append( container );\r
146 \r
147                         /**\r
148                          * The DOM element that holds the main editor interface.\r
149                          * @name CKEDITOR.editor.prototype.container\r
150                          * @type CKEDITOR.dom.element\r
151                          * @example\r
152                          * var editor = CKEDITOR.instances.editor1;\r
153                          * alert( <b>editor.container</b>.getName() );  "span"\r
154                          */\r
155                         editor.container = container;\r
156 \r
157                         // Disable browser context menu for editor's chrome.\r
158                         container.disableContextMenu();\r
159 \r
160                         editor.fireOnce( 'themeLoaded' );\r
161                         editor.fireOnce( 'uiReady' );\r
162                 },\r
163 \r
164                 buildDialog : function( editor )\r
165                 {\r
166                         var baseIdNumber = CKEDITOR.tools.getNextNumber();\r
167 \r
168                         var element = CKEDITOR.dom.element.createFromHtml( [\r
169                                         '<div class="cke_editor_' + editor.name.replace('.', '\\.') + '_dialog cke_skin_', editor.skinName,\r
170                                                 '" dir="', editor.lang.dir, '"' +\r
171                                                 ' lang="', editor.langCode, '"' +\r
172                                                 ' role="dialog"' +\r
173                                                 ' aria-labelledby="%title#"' +\r
174                                                 '>' +\r
175                                                 '<table class="cke_dialog', ' ' + CKEDITOR.env.cssClass,\r
176                                                         ' cke_', editor.lang.dir, '" style="position:absolute" role="presentation">' +\r
177                                                         '<tr><td role="presentation">' +\r
178                                                         '<div class="%body" role="presentation">' +\r
179                                                                 '<div id="%title#" class="%title" role="presentation"></div>' +\r
180                                                                 '<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
181                                                                 '<div id="%tabs#" class="%tabs" role="tablist"></div>' +\r
182                                                                 '<table class="%contents" role="presentation"><tr>' +\r
183                                                                   '<td id="%contents#" class="%contents" role="presentation"></td>' +\r
184                                                                 '</tr></table>' +\r
185                                                                 '<div id="%footer#" class="%footer" role="presentation"></div>' +\r
186                                                         '</div>' +\r
187                                                         '<div id="%tl#" class="%tl"></div>' +\r
188                                                         '<div id="%tc#" class="%tc"></div>' +\r
189                                                         '<div id="%tr#" class="%tr"></div>' +\r
190                                                         '<div id="%ml#" class="%ml"></div>' +\r
191                                                         '<div id="%mr#" class="%mr"></div>' +\r
192                                                         '<div id="%bl#" class="%bl"></div>' +\r
193                                                         '<div id="%bc#" class="%bc"></div>' +\r
194                                                         '<div id="%br#" class="%br"></div>' +\r
195                                                         '</td></tr>' +\r
196                                                 '</table>',\r
197 \r
198                                                 //Hide the container when loading skins, later restored by skin css.\r
199                                                 ( CKEDITOR.env.ie ? '' : '<style>.cke_dialog{visibility:hidden;}</style>' ),\r
200 \r
201                                         '</div>'\r
202                                 ].join( '' )\r
203                                         .replace( /#/g, '_' + baseIdNumber )\r
204                                         .replace( /%/g, 'cke_dialog_' ) );\r
205 \r
206                         var body = element.getChild( [ 0, 0, 0, 0, 0 ] ),\r
207                                 title = body.getChild( 0 ),\r
208                                 close = body.getChild( 1 );\r
209 \r
210                         // Make the Title and Close Button unselectable.\r
211                         title.unselectable();\r
212                         close.unselectable();\r
213 \r
214 \r
215                         return {\r
216                                 element : element,\r
217                                 parts :\r
218                                 {\r
219                                         dialog          : element.getChild( 0 ),\r
220                                         title           : title,\r
221                                         close           : close,\r
222                                         tabs            : body.getChild( 2 ),\r
223                                         contents        : body.getChild( [ 3, 0, 0, 0 ] ),\r
224                                         footer          : body.getChild( 4 )\r
225                                 }\r
226                         };\r
227                 },\r
228 \r
229                 destroy : function( editor )\r
230                 {\r
231                         var container = editor.container;\r
232                         container.clearCustomData();\r
233                         editor.element.clearCustomData();\r
234 \r
235                         /*\r
236                          * IE BUG: Removing the editor DOM elements while the selection is inside\r
237                          * the editing area would break IE7/8's selection system. So we need to put\r
238                          * the selection back to the parent document without scrolling the window.\r
239                          * (#3812)\r
240                          */\r
241                         if ( CKEDITOR.env.ie )\r
242                         {\r
243                                 container.setStyle( 'display', 'none' );\r
244 \r
245                                 var $range = document.body.createTextRange();\r
246                                 $range.moveToElementText( container.$ );\r
247                                 try\r
248                                 {\r
249                                         // Putting the selection to a display:none element - this will certainly\r
250                                         // fail. But! We've just put the selection document back to the parent\r
251                                         // document without scrolling the window!\r
252                                         $range.select();\r
253                                 }\r
254                                 catch ( e ) {}\r
255                         }\r
256 \r
257                         if ( container )\r
258                                 container.remove();\r
259 \r
260                         if ( editor.elementMode == CKEDITOR.ELEMENT_MODE_REPLACE )\r
261                                 editor.element.show();\r
262 \r
263                         delete editor.element;\r
264                 }\r
265         };\r
266 })() );\r
267 \r
268 CKEDITOR.editor.prototype.getThemeSpace = function( spaceName )\r
269 {\r
270         var spacePrefix = 'cke_' + spaceName;\r
271         var space = this._[ spacePrefix ] ||\r
272                 ( this._[ spacePrefix ] = CKEDITOR.document.getById( spacePrefix + '_' + this.name ) );\r
273         return space;\r
274 };\r
275 \r
276 CKEDITOR.editor.prototype.resize = function( width, height, isContentHeight, resizeInner )\r
277 {\r
278         var numberRegex = /^\d+$/;\r
279         if ( numberRegex.test( width ) )\r
280                 width += 'px';\r
281 \r
282         var container = this.container,\r
283                 contents = CKEDITOR.document.getById( 'cke_contents_' + this.name ),\r
284                 outer = resizeInner ? container.getChild( 1 ) : container;\r
285 \r
286         // Resize the width first.\r
287         // WEBKIT BUG: Webkit requires that we put the editor off from display when we\r
288         // resize it. If we don't, the browser crashes!\r
289         CKEDITOR.env.webkit && outer.setStyle( 'display', 'none' );\r
290         outer.setStyle( 'width', width );\r
291         if ( CKEDITOR.env.webkit )\r
292         {\r
293                 outer.$.offsetWidth;\r
294                 outer.setStyle( 'display', '' );\r
295         }\r
296 \r
297         // Get the height delta between the outer table and the content area.\r
298         // If we're setting the content area's height, then we don't need the delta.\r
299         var delta = isContentHeight ? 0 : ( outer.$.offsetHeight || 0 ) - ( contents.$.clientHeight || 0 );\r
300         contents.setStyle( 'height', Math.max( height - delta, 0 ) + 'px' );\r
301 \r
302         // Emit a resize event.\r
303         this.fire( 'resize' );\r
304 };\r
305 \r
306 CKEDITOR.editor.prototype.getResizable = function()\r
307 {\r
308         return this.container.getChild( 1 );\r
309 };\r
310 \r
311 /**\r
312  * Makes it possible to place some of the editor UI blocks, like the toolbar\r
313  * and the elements path, into any element in the page.\r
314  * The elements used to hold the UI blocks can be shared among several editor\r
315  * instances. In that case, only the blocks of the active editor instance will\r
316  * display.\r
317  * @name CKEDITOR.config.sharedSpaces\r
318  * @type Object\r
319  * @default undefined\r
320  * @example\r
321  * // Place the toolbar inside the element with ID "someElementId" and the\r
322  * // elements path into the element with ID "anotherId".\r
323  * config.sharedSpaces =\r
324  * {\r
325  *     top : 'someElementId',\r
326  *     bottom : 'anotherId'\r
327  * };\r
328  * @example\r
329  * // Place the toolbar inside the element with ID "someElementId". The\r
330  * // elements path will remain attached to the editor UI.\r
331  * config.sharedSpaces =\r
332  * {\r
333  *     top : 'someElementId'\r
334  * };\r
335  */\r