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