1 /*
  2 Copyright (c) 2003-2012, CKSource - Frederico Knabben. All rights reserved.
  3 For licensing, see LICENSE.html or http://ckeditor.com/license
  4 */
  5 
  6 /**
  7  * @fileOverview Contains the second part of the {@link CKEDITOR} object
  8  *		definition, which defines the basic editor features to be available in
  9  *		the root ckeditor_basic.js file.
 10  */
 11 
 12 if ( CKEDITOR.status == 'unloaded' )
 13 {
 14 	(function()
 15 	{
 16 		CKEDITOR.event.implementOn( CKEDITOR );
 17 
 18 		/**
 19 		 * Forces the full CKEditor core code, in the case only the basic code has been
 20 		 * loaded (ckeditor_basic.js). This method self-destroys (becomes undefined) in
 21 		 * the first call or as soon as the full code is available.
 22 		 * @example
 23 		 * // Check if the full core code has been loaded and load it.
 24 		 * if ( CKEDITOR.loadFullCore )
 25 		 *     <b>CKEDITOR.loadFullCore()</b>;
 26 		 */
 27 		CKEDITOR.loadFullCore = function()
 28 		{
 29 			// If not the basic code is not ready it, just mark it to be loaded.
 30 			if ( CKEDITOR.status != 'basic_ready' )
 31 			{
 32 				CKEDITOR.loadFullCore._load = 1;
 33 				return;
 34 			}
 35 
 36 			// Destroy this function.
 37 			delete CKEDITOR.loadFullCore;
 38 
 39 			// Append the script to the head.
 40 			var script = document.createElement( 'script' );
 41 			script.type = 'text/javascript';
 42 			script.src = CKEDITOR.basePath + 'ckeditor.js';
 43 			script.src = CKEDITOR.basePath + 'ckeditor_source.js';		// %REMOVE_LINE%
 44 
 45 			document.getElementsByTagName( 'head' )[0].appendChild( script );
 46 		};
 47 
 48 		/**
 49 		 * The time to wait (in seconds) to load the full editor code after the
 50 		 * page load, if the "ckeditor_basic" file is used. If set to zero, the
 51 		 * editor is loaded on demand, as soon as an instance is created.
 52 		 *
 53 		 * This value must be set on the page before the page load completion.
 54 		 * @type Number
 55 		 * @default 0 (zero)
 56 		 * @example
 57 		 * // Loads the full source after five seconds.
 58 		 * CKEDITOR.loadFullCoreTimeout = 5;
 59 		 */
 60 		CKEDITOR.loadFullCoreTimeout = 0;
 61 
 62 		/**
 63 		 * The class name used to identify <textarea> elements to be replace
 64 		 * by CKEditor instances.
 65 		 * @type String
 66 		 * @default 'ckeditor'
 67 		 * @example
 68 		 * <b>CKEDITOR.replaceClass</b> = 'rich_editor';
 69 		 */
 70 		CKEDITOR.replaceClass = 'ckeditor';
 71 
 72 		/**
 73 		 * Enables the replacement of all textareas with class name matching
 74 		 * {@link CKEDITOR.replaceClass}.
 75 		 * @type Boolean
 76 		 * @default true
 77 		 * @example
 78 		 * // Disable the auto-replace feature.
 79 		 * <b>CKEDITOR.replaceByClassEnabled</b> = false;
 80 		 */
 81 		CKEDITOR.replaceByClassEnabled = 1;
 82 
 83 		var createInstance = function( elementOrIdOrName, config, creationFunction, data )
 84 		{
 85 			if ( CKEDITOR.env.isCompatible )
 86 			{
 87 				// Load the full core.
 88 				if ( CKEDITOR.loadFullCore )
 89 					CKEDITOR.loadFullCore();
 90 
 91 				var editor = creationFunction( elementOrIdOrName, config, data );
 92 				CKEDITOR.add( editor );
 93 				return editor;
 94 			}
 95 
 96 			return null;
 97 		};
 98 
 99 		/**
100 		 * Replaces a <textarea> or a DOM element (DIV) with a CKEditor
101 		 * instance. For textareas, the initial value in the editor will be the
102 		 * textarea value. For DOM elements, their innerHTML will be used
103 		 * instead. We recommend using TEXTAREA and DIV elements only.
104 		 * @param {Object|String} elementOrIdOrName The DOM element (textarea), its
105 		 *		ID or name.
106 		 * @param {Object} [config] The specific configurations to apply to this
107 		 *		editor instance. Configurations set here will override global CKEditor
108 		 *		settings.
109 		 * @returns {CKEDITOR.editor} The editor instance created.
110 		 * @example
111 		 * <textarea id="myfield" name="myfield"><:/textarea>
112 		 * ...
113 		 * <b>CKEDITOR.replace( 'myfield' )</b>;
114 		 * @example
115 		 * var textarea = document.body.appendChild( document.createElement( 'textarea' ) );
116 		 * <b>CKEDITOR.replace( textarea )</b>;
117 		 */
118 		CKEDITOR.replace = function( elementOrIdOrName, config )
119 		{
120 			return createInstance( elementOrIdOrName, config, CKEDITOR.editor.replace );
121 		};
122 
123 		/**
124 		 * Creates a new editor instance inside a specific DOM element.
125 		 * @param {Object|String} elementOrId The DOM element or its ID.
126 		 * @param {Object} [config] The specific configurations to apply to this
127 		 *		editor instance. Configurations set here will override global CKEditor
128 		 *		settings.
129 		 * @param {String} [data] Since 3.3. Initial value for the instance.
130 		 * @returns {CKEDITOR.editor} The editor instance created.
131 		 * @example
132 		 * <div id="editorSpace"><:/div>
133 		 * ...
134 		 * <b>CKEDITOR.appendTo( 'editorSpace' )</b>;
135 		 */
136 		CKEDITOR.appendTo = function( elementOrId, config, data )
137 		{
138 			return createInstance( elementOrId, config, CKEDITOR.editor.appendTo, data );
139 		};
140 
141 		// Documented at ckeditor.js.
142 		CKEDITOR.add = function( editor )
143 		{
144 			// For now, just put the editor in the pending list. It will be
145 			// processed as soon as the full code gets loaded.
146 			var pending = this._.pending || ( this._.pending = [] );
147 			pending.push( editor );
148 		};
149 
150 		/**
151 		 * Replace all <textarea> elements available in the document with
152 		 * editor instances.
153 		 * @example
154 		 * // Replace all <textarea> elements in the page.
155 		 * CKEDITOR.replaceAll();
156 		 * @example
157 		 * // Replace all <textarea class="myClassName"> elements in the page.
158 		 * CKEDITOR.replaceAll( 'myClassName' );
159 		 * @example
160 		 * // Selectively replace <textarea> elements, based on custom assertions.
161 		 * CKEDITOR.replaceAll( function( textarea, config )
162 		 *     {
163 		 *         // Custom code to evaluate the replace, returning false
164 		 *         // if it must not be done.
165 		 *         // It also passes the "config" parameter, so the
166 		 *         // developer can customize the instance.
167 		 *     } );
168 		 */
169 		CKEDITOR.replaceAll = function()
170 		{
171 			var textareas = document.getElementsByTagName( 'textarea' );
172 
173 			for ( var i = 0 ; i < textareas.length ; i++ )
174 			{
175 				var config = null,
176 					textarea = textareas[i];
177 
178 				// The "name" and/or "id" attribute must exist.
179 				if ( !textarea.name && !textarea.id )
180 					continue;
181 
182 				if ( typeof arguments[0] == 'string' )
183 				{
184 					// The textarea class name could be passed as the function
185 					// parameter.
186 
187 					var classRegex = new RegExp( '(?:^|\\s)' + arguments[0] + '(?:$|\\s)' );
188 
189 					if ( !classRegex.test( textarea.className ) )
190 						continue;
191 				}
192 				else if ( typeof arguments[0] == 'function' )
193 				{
194 					// An assertion function could be passed as the function parameter.
195 					// It must explicitly return "false" to ignore a specific <textarea>.
196 					config = {};
197 					if ( arguments[0]( textarea, config ) === false )
198 						continue;
199 				}
200 
201 				this.replace( textarea, config );
202 			}
203 		};
204 
205 		(function()
206 		{
207 			var onload = function()
208 			{
209 				var loadFullCore = CKEDITOR.loadFullCore,
210 					loadFullCoreTimeout = CKEDITOR.loadFullCoreTimeout;
211 
212 				// Replace all textareas with the default class name.
213 				if ( CKEDITOR.replaceByClassEnabled )
214 					CKEDITOR.replaceAll( CKEDITOR.replaceClass );
215 
216 				CKEDITOR.status = 'basic_ready';
217 
218 				if ( loadFullCore && loadFullCore._load )
219 					loadFullCore();
220 				else if ( loadFullCoreTimeout )
221 				{
222 					setTimeout( function()
223 						{
224 							if ( CKEDITOR.loadFullCore )
225 								CKEDITOR.loadFullCore();
226 						}
227 						, loadFullCoreTimeout * 1000 );
228 				}
229 			};
230 
231 			if ( window.addEventListener )
232 				window.addEventListener( 'load', onload, false );
233 			else if ( window.attachEvent )
234 				window.attachEvent( 'onload', onload );
235 		})();
236 
237 		CKEDITOR.status = 'basic_loaded';
238 	})();
239 }
240