1 /*
  2 Copyright (c) 2003-2009, 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 	var getMode = function( editor, mode )
 14 	{
 15 		return editor._.modes && editor._.modes[ mode || editor.mode ];
 16 	};
 17
 18 	// This is a semaphore used to avoid recursive calls between
 19 	// the following data handling functions.
 20 	var isHandlingData;
 21
 22 	CKEDITOR.plugins.add( 'editingblock',
 23 	{
 24 		init : function( editor )
 25 		{
 26 			if ( !editor.config.editingBlock )
 27 				return;
 28
 29 			editor.on( 'themeSpace', function( event )
 30 				{
 31 					if ( event.data.space == 'contents' )
 32 						event.data.html += '<br>';
 33 				});
 34
 35 			editor.on( 'themeLoaded', function()
 36 				{
 37 					editor.fireOnce( 'editingBlockReady' );
 38 				});
 39
 40 			editor.on( 'uiReady', function()
 41 				{
 42 					editor.setMode( editor.config.startupMode );
 43
 44 					if ( editor.config.startupFocus )
 45 						editor.focus();
 46 				});
 47
 48 			editor.on( 'afterSetData', function()
 49 				{
 50 					if ( !isHandlingData )
 51 					{
 52 						function setData()
 53 						{
 54 							isHandlingData = true;
 55 							getMode( editor ).loadData( editor.getData() );
 56 							isHandlingData = false;
 57 						}
 58
 59 						if ( editor.mode )
 60 							setData();
 61 						else
 62 						{
 63 							editor.on( 'mode', function()
 64 								{
 65 									setData();
 66 									editor.removeListener( 'mode', arguments.callee );
 67 								});
 68 						}
 69 					}
 70 				});
 71
 72 			editor.on( 'beforeGetData', function()
 73 				{
 74 					if ( !isHandlingData && editor.mode )
 75 					{
 76 						isHandlingData = true;
 77 						editor.setData( getMode( editor ).getData() );
 78 						isHandlingData = false;
 79 					}
 80 				});
 81
 82 			editor.on( 'getSnapshot', function( event )
 83 				{
 84 					if ( editor.mode )
 85 						event.data = getMode( editor ).getSnapshotData();
 86 				});
 87
 88 			editor.on( 'loadSnapshot', function( event )
 89 				{
 90 					if ( editor.mode )
 91 						getMode( editor ).loadSnapshotData( event.data );
 92 				});
 93
 94 			// For the first "mode" call, we'll also fire the "instanceReady"
 95 			// event.
 96 			editor.on( 'mode', function( event )
 97 				{
 98 					// Do that once only.
 99 					event.removeListener();
100
101 					// Grab editor focus if the editor container is focused. (#3104)
102 					editor.container.on( 'focus', function()
103 						{
104 							editor.focus();
105 						});
106
107 					// Fire instanceReady for both the editor and CKEDITOR.
108 					editor.fireOnce( 'instanceReady' );
109 					CKEDITOR.fire( 'instanceReady', null, editor );
110 				});
111 		}
112 	});
113
114 	/**
115 	 * The current editing mode. An editing mode is basically a viewport for
116 	 * editing or content viewing. By default the possible values for this
117 	 * property are "wysiwyg" and "source".
118 	 * @type String
119 	 * @example
120 	 * alert( CKEDITOR.instances.editor1.mode );  // "wysiwyg" (e.g.)
121 	 */
122 	CKEDITOR.editor.prototype.mode = '';
123
124 	/**
125 	 * Registers an editing mode. This function is to be used mainly by plugins.
126 	 * @param {String} mode The mode name.
127 	 * @param {Object} modeEditor The mode editor definition.
128 	 * @example
129 	 */
130 	CKEDITOR.editor.prototype.addMode = function( mode, modeEditor )
131 	{
132 		modeEditor.name = mode;
133 		( this._.modes || ( this._.modes = {} ) )[ mode ] = modeEditor;
134 	};
135
136 	/**
137 	 * Sets the current editing mode in this editor instance.
138 	 * @param {String} mode A registered mode name.
139 	 * @example
140 	 * // Switch to "source" view.
141 	 * CKEDITOR.instances.editor1.setMode( 'source' );
142 	 */
143 	CKEDITOR.editor.prototype.setMode = function( mode )
144 	{
145 		var data,
146 			holderElement = this.getThemeSpace( 'contents' ),
147 			isDirty = this.checkDirty();
148
149 		// Unload the previous mode.
150 		if ( this.mode )
151 		{
152 			if ( mode == this.mode )
153 				return;
154
155 			var currentMode = getMode( this );
156 			data = currentMode.getData();
157 			currentMode.unload( holderElement );
158 			this.mode = '';
159 		}
160
161 		holderElement.setHtml( '' );
162
163 		// Load required mode.
164 		var modeEditor = getMode( this, mode );
165 		if ( !modeEditor )
166 			throw '[CKEDITOR.editor.setMode] Unknown mode "' + mode + '".';
167
168 		if ( !isDirty )
169 		{
170 			this.on( 'mode', function()
171 				{
172 					this.resetDirty();
173 					this.removeListener( 'mode', arguments.callee );
174 				});
175 		}
176
177 		modeEditor.load( holderElement, ( typeof data ) != 'string'  ? this.getData() : data);
178 	};
179
180 	/**
181 	 * Moves the selection focus to the editing are space in the editor.
182 	 */
183 	CKEDITOR.editor.prototype.focus = function()
184 	{
185 		var mode = getMode( this );
186 		if ( mode )
187 			mode.focus();
188 	};
189 })();
190
191 /**
192  * The mode to load at the editor startup. It depends on the plugins
193  * loaded. By default, the "wysiwyg" and "source" modes are available.
194  * @type String
195  * @default 'wysiwyg'
196  * @example
197  * config.toolbarLocation = 'source';
198  */
199 CKEDITOR.config.startupMode = 'wysiwyg';
200
201 /**
202  * Sets whether the editor should have the focus when the page loads.
203  * @type Boolean
204  * @default false
205  */
206 CKEDITOR.config.startupFocus = false;
207
208 CKEDITOR.config.editingBlock = true;
209