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, pluginPath )
 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 && editor.mode )
 51 					{
 52 						isHandlingData = true;
 53 						getMode( editor ).loadData( editor.getData() );
 54 						isHandlingData = false;
 55 					}
 56 				});
 57
 58 			editor.on( 'beforeGetData', function()
 59 				{
 60 					if ( !isHandlingData && editor.mode )
 61 					{
 62 						isHandlingData = true;
 63 						editor.setData( getMode( editor ).getData() );
 64 						isHandlingData = false;
 65 					}
 66 				});
 67
 68 			editor.on( 'getSnapshot', function( event )
 69 				{
 70 					if ( editor.mode )
 71 						event.data = getMode( editor ).getSnapshotData();
 72 				});
 73 		}
 74 	});
 75
 76 	/**
 77 	 * The current editing mode. An editing mode is basically a viewport for
 78 	 * editing or content viewing. By default the possible values for this
 79 	 * property are "wysiwyg" and "source".
 80 	 * @type String
 81 	 * @example
 82 	 * alert( CKEDITOR.instances.editor1.mode );  // "wysiwyg" (e.g.)
 83 	 */
 84 	CKEDITOR.editor.prototype.mode = '';
 85
 86 	/**
 87 	 * Registers an editing mode. This function is to be used mainly by plugins.
 88 	 * @param {String} mode The mode name.
 89 	 * @param {Object} modeEditor The mode editor definition.
 90 	 * @example
 91 	 */
 92 	CKEDITOR.editor.prototype.addMode = function( mode, modeEditor )
 93 	{
 94 		modeEditor.name = mode;
 95 		( this._.modes || ( this._.modes = {} ) )[ mode ] = modeEditor;
 96 	};
 97
 98 	/**
 99 	 * Sets the current editing mode in this editor instance.
100 	 * @param {String} mode A registered mode name.
101 	 * @example
102 	 * // Switch to "source" view.
103 	 * CKEDITOR.instances.editor1.setMode( 'source' );
104 	 */
105 	CKEDITOR.editor.prototype.setMode = function( mode )
106 	{
107 		var data,
108 			holderElement = this.getThemeSpace( 'contents' ),
109 			isDirty = this.checkDirty();
110
111 		// Unload the previous mode.
112 		if ( this.mode )
113 		{
114 			if ( mode == this.mode )
115 				return;
116
117 			var currentMode = getMode( this );
118 			data = currentMode.getData();
119 			currentMode.unload( holderElement );
120 			this.mode = '';
121 		}
122
123 		holderElement.setHtml( '' );
124
125 		// Load required mode.
126 		var modeEditor = getMode( this, mode );
127 		if ( !modeEditor )
128 			throw '[CKEDITOR.editor.setMode] Unknown mode "' + mode + '".';
129
130 		if ( !isDirty )
131 		{
132 			this.on( 'mode', function()
133 				{
134 					this.resetDirty();
135 					this.removeListener( 'mode', arguments.callee );
136 				});
137 		}
138
139 		modeEditor.load( holderElement, data || this.getData() );
140 	};
141
142 	/**
143 	 * Moves the selection focus to the editing are space in the editor.
144 	 */
145 	CKEDITOR.editor.prototype.focus = function()
146 	{
147 		var mode = getMode( this );
148 		if ( mode )
149 			mode.focus();
150 	};
151 })();
152
153 /**
154  * The mode to load at the editor startup. It depends on the plugins
155  * loaded. By default, the "wysiwyg" and "source" modes are available.
156  * @type String
157  * @default 'wysiwyg'
158  * @example
159  * config.toolbarLocation = 'source';
160  */
161 CKEDITOR.config.startupMode = 'wysiwyg';
162
163 /**
164  * Sets whether the editor should have the focus when the page loads.
165  * @type Boolean
166  * @default false
167  */
168 CKEDITOR.config.startupFocus = false;
169
170 CKEDITOR.config.editingBlock = true;
171