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 Contains the second part of the {@link CKEDITOR} object
 24  *		definition, which defines the basic editor features to be available in
 25  *		the root ckeditor_basic.js file.
 26  */
 27
 28 if ( CKEDITOR.status == 'unloaded' )
 29 {
 30 	CKEDITOR.event.implementOn( CKEDITOR );
 31
 32 	/**
 33 	 * Forces the full CKEditor core code, in the case only the basic code has been
 34 	 * loaded (ckeditor_basic.js). This method self-destroys (becomes undefined) in
 35 	 * the first call or as soon as the full code is available.
 36 	 * @example
 37 	 * // Check if the full core code has been loaded and load it.
 38 	 * if ( CKEDITOR.loadFullCore )
 39 	 *     <b>CKEDITOR.loadFullCore()</b>;
 40 	 */
 41 	CKEDITOR.loadFullCore = function()
 42 	{
 43 		delete CKEDITOR.loadFullCore;
 44
 45 		var script = document.createElement( 'script' );
 46 		script.type = 'text/javascript';
 47 		script.src = CKEDITOR.basePath + 'ckeditor.js';
 48 		script.src = CKEDITOR.basePath + 'ckeditor_source.js';		// @Packager.RemoveLine
 49
 50 		document.getElementsByTagName( 'head' )[0].appendChild( script );
 51 	};
 52
 53 	/**
 54 	 * The class name used to identify <textarea> elements to be replace
 55 	 * by CKEditor instances.
 56 	 * @type String
 57 	 * @default 'ckeditor'
 58 	 * @example
 59 	 * <b>CKEDITOR.replaceClass</b> = 'rich_editor';
 60 	 */
 61 	CKEDITOR.replaceClass = 'ckeditor';
 62
 63 	/**
 64 	 * Enables the replacement of all textareas with class name matching
 65 	 * {@link CKEDITOR.replaceClass}.
 66 	 * @type Boolean
 67 	 * @default true
 68 	 * @example
 69 	 * // Disable the auto-replace feature.
 70 	 * <b>CKEDITOR.replaceByClassEnabled</b> = false;
 71 	 */
 72 	CKEDITOR.replaceByClassEnabled = true;
 73
 74 	/**
 75 	 * Replaces a specific <textarea> with a CKEditor instance.
 76 	 * @param {Object|String} elementOrIdOrName The DOM element (textarea), its
 77 	 *		ID or name.
 78 	 * @param {Object} [config] The specific configurations to apply to this
 79 	 *		editor instance. Configurations set here will override global CKEditor
 80 	 *		settings.
 81 	 * @example
 82 	 * <textarea id="myfield" name="myfield"><:/textarea>
 83 	 * ...
 84 	 * <b>CKEDITOR.replace( 'myfield' )</b>;
 85 	 * @example
 86 	 * var textarea = document.body.appendChild( document.createElement( 'textarea' ) );
 87 	 * <b>CKEDITOR.replace( textarea )</b>;
 88 	 */
 89 	CKEDITOR.replace = function( elementOrIdOrName, config )
 90 	{
 91 		if ( CKEDITOR.env.isCompatible )
 92 		{
 93 			var textarea = elementOrIdOrName;
 94
 95 			if ( typeof textarea != 'object' )
 96 			{
 97 				// First look for the element id, then the name.
 98 				var i = 0;
 99 				var textareasByName	= document.getElementsByName( elementOrIdOrName );
100 				textarea = document.getElementById( elementOrIdOrName ) || textareasByName[ i++ ];
101
102 				while ( textarea )
103 				{
104 					if ( textarea.tagName.toLowerCase() == 'textarea' )
105 						break;
106 					textarea = textareasByName[ i++ ];
107 				}
108
109 				if ( !textarea )
110 					throw '[CKEDITOR.replace] The <textarea> with id or name "' + elementOrIdOrName + '" was not found.';
111 			}
112
113 			CKEDITOR.replace._replaceElement( textarea, config );
114 		}
115 	};
116
117 	// This function will be overwritten by the full core code implementation.
118 	CKEDITOR.replace._replaceElement = function( textarea, config )
119 	{
120 		// Do not replace the textarea right now, just hide it. the
121 		// effective replacement will be done by the full core code.
122 		textarea.style.visibility = 'hidden';
123
124 		// Push the textarea in the array of pending replacements.
125 		var pending = CKEDITOR.replace._pending || ( CKEDITOR.replace._pending = [] );
126 		pending.push( [ textarea, config ] );
127
128 		// Check if it is time to load the full core code.
129 		if ( CKEDITOR.loadFullCore && CKEDITOR.status == 'basic_ready' )
130 			CKEDITOR.loadFullCore();
131 	};
132
133 	/**
134 	 * Replace all <textarea> elements available in the document with
135 	 * editor instances.
136 	 * @example
137 	 * // Replace all <textarea> elements in the page.
138 	 * CKEDITOR.replaceAll();
139 	 * @example
140 	 * // Replace all <textarea class="myClassName"> elements in the page.
141 	 * CKEDITOR.replaceAll( 'myClassName' );
142 	 * @example
143 	 * // Selectively replace <textarea> elements, based on custom assertions.
144 	 * CKEDITOR.replaceAll( function( textarea, config )
145 	 *     {
146 	 *         // Custom code to evaluate the replace, returning false
147 	 *         // if it must not be done.
148 	 *         // It also passes the "config" parameter, so the
149 	 *         // developer can customize the instance.
150 	 *     } );
151 	 */
152 	CKEDITOR.replaceAll = function()
153 	{
154 		var textareas = document.getElementsByTagName( 'textarea' );
155
156 		for ( var i = 0 ; i < textareas.length ; i++ )
157 		{
158 			var config = null;
159 			var textarea = textareas[i];
160 			var name = textarea.name;
161
162 			// The "name" and/or "id" attribute must exist.
163 			if ( !textarea.name && !textarea.id )
164 				continue;
165
166 			if ( typeof arguments[0] == 'string' )
167 			{
168 				// The textarea class name could be passed as the function
169 				// parameter.
170
171 				var classRegex = new RegExp( '(?:^| )' + arguments[0] + '(?:$| )' );
172
173 				if ( !classRegex.test( textarea.className ) )
174 					continue;
175 			}
176 			else if ( typeof arguments[0] == 'function' )
177 			{
178 				// An assertion function could be passed as the function parameter.
179 				// It must explicitly return "false" to ignore a specific <textarea>.
180 				config = {};
181 				if ( arguments[0]( textarea, config ) === false )
182 					continue;
183 			}
184
185 			this.replace( textarea, config );
186 		}
187 	};
188
189 	(function()
190 	{
191 		var onload = function()
192 		{
193 			// Replace all textareas with the default class name.
194 			if ( CKEDITOR.replaceByClassEnabled )
195 				CKEDITOR.replaceAll( CKEDITOR.replaceClass );
196
197 			if ( CKEDITOR.status == 'basic_loaded' )
198 			{
199 				if ( CKEDITOR.replace._pending && CKEDITOR.loadFullCore )
200 					CKEDITOR.loadFullCore();
201
202 				CKEDITOR.status = 'basic_ready';
203 			}
204 		};
205
206 		if ( window.addEventListener )
207 			window.addEventListener( 'load', onload, false );
208 		else if ( window.attachEvent )
209 			window.attachEvent( 'onload', onload );
210 	})();
211
212 	CKEDITOR.status = 'basic_loaded';
213 }
214