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