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