JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
vanilla ckeditor-3.6.3
[ckeditor.git] / _source / plugins / editingblock / plugin.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  * @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._.previousMode = this.mode;\r
167 \r
168                         this.fire( 'beforeModeUnload' );\r
169 \r
170                         var currentMode = this.getMode();\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 = this.getMode( 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          * Gets the current or any of the objects that represent the editing\r
197          * area modes. The two most common editing modes are "wysiwyg" and "source".\r
198          * @param {String} [mode] The mode to be retrieved. If not specified, the\r
199          *              current one is returned.\r
200          */\r
201         CKEDITOR.editor.prototype.getMode = function( mode )\r
202         {\r
203                 return this._.modes && this._.modes[ mode || this.mode ];\r
204         };\r
205 \r
206         /**\r
207          * Moves the selection focus to the editing are space in the editor.\r
208          */\r
209         CKEDITOR.editor.prototype.focus = function()\r
210         {\r
211                 this.forceNextSelectionCheck();\r
212                 var mode = this.getMode();\r
213                 if ( mode )\r
214                         mode.focus();\r
215         };\r
216 })();\r
217 \r
218 /**\r
219  * The mode to load at the editor startup. It depends on the plugins\r
220  * loaded. By default, the "wysiwyg" and "source" modes are available.\r
221  * @type String\r
222  * @default 'wysiwyg'\r
223  * @example\r
224  * config.startupMode = 'source';\r
225  */\r
226 CKEDITOR.config.startupMode = 'wysiwyg';\r
227 \r
228 /**\r
229  * Sets whether the editor should have the focus when the page loads.\r
230  * @name CKEDITOR.config.startupFocus\r
231  * @type Boolean\r
232  * @default false\r
233  * @example\r
234  * config.startupFocus = true;\r
235  */\r
236 \r
237 /**\r
238  * Whether to render or not the editing block area in the editor interface.\r
239  * @type Boolean\r
240  * @default true\r
241  * @example\r
242  * config.editingBlock = false;\r
243  */\r
244 CKEDITOR.config.editingBlock = true;\r
245 \r
246 /**\r
247  * Fired when a CKEDITOR instance is created, fully initialized and ready for interaction.\r
248  * @name CKEDITOR#instanceReady\r
249  * @event\r
250  * @param {CKEDITOR.editor} editor The editor instance that has been created.\r
251  */\r
252 \r
253 /**\r
254  * Fired when the CKEDITOR instance is created, fully initialized and ready for interaction.\r
255  * @name CKEDITOR.editor#instanceReady\r
256  * @event\r
257  */\r
258 \r
259 /**\r
260  * Fired before changing the editing mode. See also CKEDITOR.editor#beforeSetMode and CKEDITOR.editor#mode\r
261  * @name CKEDITOR.editor#beforeModeUnload\r
262  * @event\r
263  */\r
264 \r
265  /**\r
266  * Fired before the editor mode is set. See also CKEDITOR.editor#mode and CKEDITOR.editor#beforeModeUnload\r
267  * @name CKEDITOR.editor#beforeSetMode\r
268  * @event\r
269  * @since 3.5.3\r
270  * @param {String} newMode The name of the mode which is about to be set.\r
271  */\r
272 \r
273 /**\r
274  * Fired after setting the editing mode. See also CKEDITOR.editor#beforeSetMode and CKEDITOR.editor#beforeModeUnload\r
275  * @name CKEDITOR.editor#mode\r
276  * @event\r
277  * @param {String} previousMode The previous mode of the editor.\r
278  */\r