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