JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
05d78b6a35f0ca50d213e6f79d9f59db7b6cb605
[ckeditor.git] / _source / plugins / editingblock / plugin.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  * @fileOverview The default editing block plugin, which holds the editing area\r
8  *              and source view.\r
9  */\r
10 \r
11 (function()\r
12 {\r
13         var getMode = function( editor, mode )\r
14         {\r
15                 return editor._.modes && editor._.modes[ mode || editor.mode ];\r
16         };\r
17 \r
18         // This is a semaphore used to avoid recursive calls between\r
19         // the following data handling functions.\r
20         var isHandlingData;\r
21 \r
22         CKEDITOR.plugins.add( 'editingblock',\r
23         {\r
24                 init : function( editor )\r
25                 {\r
26                         if ( !editor.config.editingBlock )\r
27                                 return;\r
28 \r
29                         editor.on( 'themeSpace', function( event )\r
30                                 {\r
31                                         if ( event.data.space == 'contents' )\r
32                                                 event.data.html += '<br>';\r
33                                 });\r
34 \r
35                         editor.on( 'themeLoaded', function()\r
36                                 {\r
37                                         editor.fireOnce( 'editingBlockReady' );\r
38                                 });\r
39 \r
40                         editor.on( 'uiReady', function()\r
41                                 {\r
42                                         editor.setMode( editor.config.startupMode );\r
43                                 });\r
44 \r
45                         editor.on( 'afterSetData', function()\r
46                                 {\r
47                                         if ( !isHandlingData )\r
48                                         {\r
49                                                 function setData()\r
50                                                 {\r
51                                                         isHandlingData = true;\r
52                                                         getMode( editor ).loadData( editor.getData() );\r
53                                                         isHandlingData = false;\r
54                                                 }\r
55 \r
56                                                 if ( editor.mode )\r
57                                                         setData();\r
58                                                 else\r
59                                                 {\r
60                                                         editor.on( 'mode', function()\r
61                                                                 {\r
62                                                                         setData();\r
63                                                                         editor.removeListener( 'mode', arguments.callee );\r
64                                                                 });\r
65                                                 }\r
66                                         }\r
67                                 });\r
68 \r
69                         editor.on( 'beforeGetData', function()\r
70                                 {\r
71                                         if ( !isHandlingData && editor.mode )\r
72                                         {\r
73                                                 isHandlingData = true;\r
74                                                 editor.setData( getMode( editor ).getData(), null, 1 );\r
75                                                 isHandlingData = false;\r
76                                         }\r
77                                 });\r
78 \r
79                         editor.on( 'getSnapshot', function( event )\r
80                                 {\r
81                                         if ( editor.mode )\r
82                                                 event.data = getMode( editor ).getSnapshotData();\r
83                                 });\r
84 \r
85                         editor.on( 'loadSnapshot', function( event )\r
86                                 {\r
87                                         if ( editor.mode )\r
88                                                 getMode( editor ).loadSnapshotData( event.data );\r
89                                 });\r
90 \r
91                         // For the first "mode" call, we'll also fire the "instanceReady"\r
92                         // event.\r
93                         editor.on( 'mode', function( event )\r
94                                 {\r
95                                         // Do that once only.\r
96                                         event.removeListener();\r
97 \r
98                                         // Redirect the focus into editor for webkit. (#5713)\r
99                                         CKEDITOR.env.webkit && editor.container.on( 'focus', function()\r
100                                                 {\r
101                                                         editor.focus();\r
102                                                 });\r
103 \r
104                                         if ( editor.config.startupFocus )\r
105                                                 editor.focus();\r
106 \r
107                                         // Fire instanceReady for both the editor and CKEDITOR, but\r
108                                         // defer this until the whole execution has completed\r
109                                         // to guarantee the editor is fully responsible.\r
110                                         setTimeout( function(){\r
111                                                 editor.fireOnce( 'instanceReady' );\r
112                                                 CKEDITOR.fire( 'instanceReady', null, editor );\r
113                                         }, 0 );\r
114                                 });\r
115 \r
116                         editor.on( 'destroy', function ()\r
117                         {\r
118                                 // ->           currentMode.unload( holderElement );\r
119                                 if ( this.mode )\r
120                                         this._.modes[ this.mode ].unload( this.getThemeSpace( 'contents' ) );\r
121                         });\r
122                 }\r
123         });\r
124 \r
125         /**\r
126          * The current editing mode. An editing mode is basically a viewport for\r
127          * editing or content viewing. By default the possible values for this\r
128          * property are "wysiwyg" and "source".\r
129          * @type String\r
130          * @example\r
131          * alert( CKEDITOR.instances.editor1.mode );  // "wysiwyg" (e.g.)\r
132          */\r
133         CKEDITOR.editor.prototype.mode = '';\r
134 \r
135         /**\r
136          * Registers an editing mode. This function is to be used mainly by plugins.\r
137          * @param {String} mode The mode name.\r
138          * @param {Object} modeEditor The mode editor definition.\r
139          * @example\r
140          */\r
141         CKEDITOR.editor.prototype.addMode = function( mode, modeEditor )\r
142         {\r
143                 modeEditor.name = mode;\r
144                 ( this._.modes || ( this._.modes = {} ) )[ mode ] = modeEditor;\r
145         };\r
146 \r
147         /**\r
148          * Sets the current editing mode in this editor instance.\r
149          * @param {String} mode A registered mode name.\r
150          * @example\r
151          * // Switch to "source" view.\r
152          * CKEDITOR.instances.editor1.setMode( 'source' );\r
153          */\r
154         CKEDITOR.editor.prototype.setMode = function( mode )\r
155         {\r
156                 this.fire( 'beforeSetMode', { newMode : mode } );\r
157 \r
158                 var data,\r
159                         holderElement = this.getThemeSpace( 'contents' ),\r
160                         isDirty = this.checkDirty();\r
161 \r
162                 // Unload the previous mode.\r
163                 if ( this.mode )\r
164                 {\r
165                         if ( mode == this.mode )\r
166                                 return;\r
167 \r
168                         this.fire( 'beforeModeUnload' );\r
169 \r
170                         var currentMode = getMode( this );\r
171                         data = currentMode.getData();\r
172                         currentMode.unload( holderElement );\r
173                         this.mode = '';\r
174                 }\r
175 \r
176                 holderElement.setHtml( '' );\r
177 \r
178                 // Load required mode.\r
179                 var modeEditor = getMode( this, mode );\r
180                 if ( !modeEditor )\r
181                         throw '[CKEDITOR.editor.setMode] Unknown mode "' + mode + '".';\r
182 \r
183                 if ( !isDirty )\r
184                 {\r
185                         this.on( 'mode', function()\r
186                                 {\r
187                                         this.resetDirty();\r
188                                         this.removeListener( 'mode', arguments.callee );\r
189                                 });\r
190                 }\r
191 \r
192                 modeEditor.load( holderElement, ( typeof data ) != 'string'  ? this.getData() : data);\r
193         };\r
194 \r
195         /**\r
196          * Moves the selection focus to the editing are space in the editor.\r
197          */\r
198         CKEDITOR.editor.prototype.focus = function()\r
199         {\r
200                 this.forceNextSelectionCheck();\r
201                 var mode = getMode( this );\r
202                 if ( mode )\r
203                         mode.focus();\r
204         };\r
205 })();\r
206 \r
207 /**\r
208  * The mode to load at the editor startup. It depends on the plugins\r
209  * loaded. By default, the "wysiwyg" and "source" modes are available.\r
210  * @type String\r
211  * @default 'wysiwyg'\r
212  * @example\r
213  * config.startupMode = 'source';\r
214  */\r
215 CKEDITOR.config.startupMode = 'wysiwyg';\r
216 \r
217 /**\r
218  * Sets whether the editor should have the focus when the page loads.\r
219  * @name CKEDITOR.config.startupFocus\r
220  * @type Boolean\r
221  * @default false\r
222  * @example\r
223  * config.startupFocus = true;\r
224  */\r
225 \r
226 /**\r
227  * Whether to render or not the editing block area in the editor interface.\r
228  * @type Boolean\r
229  * @default true\r
230  * @example\r
231  * config.editingBlock = false;\r
232  */\r
233 CKEDITOR.config.editingBlock = true;\r
234 \r
235 /**\r
236  * Fired when a CKEDITOR instance is created, fully initialized and ready for interaction.\r
237  * @name CKEDITOR#instanceReady\r
238  * @event\r
239  * @param {CKEDITOR.editor} editor The editor instance that has been created.\r
240  */\r
241 \r
242 /**\r
243  * Fired when the CKEDITOR instance is created, fully initialized and ready for interaction.\r
244  * @name CKEDITOR.editor#instanceReady\r
245  * @event\r
246  */\r
247 \r
248 /**\r
249  * Fired before changing the editing mode.\r
250  * @name CKEDITOR.editor#beforeModeUnload\r
251  * @event\r
252  */\r
253 \r
254  /**\r
255  * Fired before the editor mode is set.\r
256  * @name CKEDITOR.editor#beforeSetMode\r
257  * @event\r
258  * @since 3.5.3\r
259  * @param {String} newMode The name of the mode which is about to be set.\r
260  */\r