1 /*
  2  * CKEditor - The text editor for Internet - http://ckeditor.com
  3  * Copyright (C) 2003-2008 Frederico Caldeira Knabben
  4  *
  5  * == BEGIN LICENSE ==
  6  *
  7  * Licensed under the terms of any of the following licenses at your
  8  * choice:
  9  *
 10  *  - GNU General Public License Version 2 or later (the "GPL")
 11  *    http://www.gnu.org/licenses/gpl.html
 12  *
 13  *  - GNU Lesser General Public License Version 2.1 or later (the "LGPL")
 14  *    http://www.gnu.org/licenses/lgpl.html
 15  *
 16  *  - Mozilla Public License Version 1.1 or later (the "MPL")
 17  *    http://www.mozilla.org/MPL/MPL-1.1.html
 18  *
 19  * == END LICENSE ==
 20  */
 21
 22 /**
 23  * @fileOverview The default editing block plugin, which holds the editing area
 24  *		and source view.
 25  */
 26
 27 (function()
 28 {
 29 	var getMode = function( editor, mode )
 30 	{
 31 		return editor._.modes && editor._.modes[ mode || editor.mode ];
 32 	};
 33
 34 	// This is a semaphore used to avoid recursive calls between
 35 	// the following data handling functions.
 36 	var isHandlingData;
 37
 38 	CKEDITOR.plugins.add( 'editingblock',
 39 	{
 40 		init : function( editor, pluginPath )
 41 		{
 42 			editor.on( 'themeSpace', function( event )
 43 				{
 44 					if ( event.data.space == 'contents' )
 45 						event.data.html += '<br>';
 46 				});
 47
 48 			editor.on( 'themeLoaded', function()
 49 				{
 50 					editor.fireOnce( 'editingBlockReady' );
 51 				});
 52
 53 			editor.on( 'uiReady', function()
 54 				{
 55 					editor.setMode( editor.config.startupMode );
 56
 57 					if ( editor.config.startupFocus )
 58 						editor.focus();
 59 				});
 60
 61 			editor.on( 'afterSetData', function()
 62 				{
 63 					if ( !isHandlingData && editor.mode )
 64 					{
 65 						isHandlingData = true;
 66 						getMode( editor ).loadData( editor.getData() );
 67 						isHandlingData = false;
 68 					}
 69 				});
 70
 71 			editor.on( 'beforeGetData', function()
 72 				{
 73 					if ( !isHandlingData && editor.mode )
 74 					{
 75 						isHandlingData = true;
 76 						editor.setData( getMode( editor ).getData() );
 77 						isHandlingData = false;
 78 					}
 79 				});
 80 		}
 81 	});
 82
 83 	/**
 84 	 * The current editing mode. An editing mode is basically a viewport for
 85 	 * editing or content viewing. By default the possible values for this
 86 	 * property are "wysiwyg" and "source".
 87 	 * @type String
 88 	 * @example
 89 	 * alert( CKEDITOR.instances.editor1.mode );  // "wysiwyg" (e.g.)
 90 	 */
 91 	CKEDITOR.editor.prototype.mode = '';
 92
 93 	/**
 94 	 * Registers an editing mode. This function is to be used mainly by plugins.
 95 	 * @param {String} mode The mode name.
 96 	 * @param {Object} modeEditor The mode editor definition.
 97 	 * @example
 98 	 */
 99 	CKEDITOR.editor.prototype.addMode = function( mode, modeEditor )
100 	{
101 		modeEditor.name = mode;
102 		( this._.modes || ( this._.modes = {} ) )[ mode ] = modeEditor;
103 	};
104
105 	/**
106 	 * Sets the current editing mode in this editor instance.
107 	 * @param {String} mode A registered mode name.
108 	 * @example
109 	 * // Switch to "source" view.
110 	 * CKEDITOR.instances.editor1.setMode( 'source' );
111 	 */
112 	CKEDITOR.editor.prototype.setMode = function( mode )
113 	{
114 		var data,
115 			holderElement = this.getThemeSpace( 'contents' );
116
117 		// Unload the previous mode.
118 		if ( this.mode )
119 		{
120 			if ( mode == this.mode )
121 				return;
122
123 			var currentMode = getMode( this );
124 			data = currentMode.getData();
125 			currentMode.unload( holderElement );
126 			this.mode = '';
127 		}
128
129 		holderElement.setHtml( '' );
130
131 		// Load required mode.
132 		var modeEditor = getMode( this, mode );
133 		if ( !modeEditor )
134 			throw '[CKEDITOR.editor.setMode] Unknown mode "' + mode + '".';
135
136 		modeEditor.load( holderElement, data || this.getData() );
137
138 		this.mode = mode;
139 	};
140
141 	/**
142 	 * Moves the selection focus to the editing are space in the editor.
143 	 */
144 	CKEDITOR.editor.prototype.focus = function()
145 	{
146 		var mode = getMode( this );
147 		if ( mode )
148 			mode.focus();
149 	};
150 })();
151
152 /**
153  * The mode to load at the editor startup. It depends on the plugins
154  * loaded. By default, the "wysiwyg" and "source" modes are available.
155  * @type String
156  * @default 'wysiwyg'
157  * @example
158  * config.toolbarLocation = 'source';
159  */
160 CKEDITOR.config.startupMode = 'wysiwyg';
161
162 /**
163  * Sets whether the editor should have the focus when the page loads.
164  * @type Boolean
165  * @default false
166  */
167 CKEDITOR.config.startupFocus = false;
168