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