1 /*
  2 Copyright (c) 2003-2009, CKSource - Frederico Knabben. All rights reserved.
  3 For licensing, see LICENSE.html or http://ckeditor.com/license
  4 */
  5
  6 if ( !CKEDITOR.editor )
  7 {
  8 	/**
  9 	 * No element is linked to the editor instance.
 10 	 * @constant
 11 	 * @example
 12 	 */
 13 	CKEDITOR.ELEMENT_MODE_NONE = 0;
 14
 15 	/**
 16 	 * The element is to be replaced by the editor instance.
 17 	 * @constant
 18 	 * @example
 19 	 */
 20 	CKEDITOR.ELEMENT_MODE_REPLACE = 1;
 21
 22 	/**
 23 	 * The editor is to be created inside the element.
 24 	 * @constant
 25 	 * @example
 26 	 */
 27 	CKEDITOR.ELEMENT_MODE_APPENDTO = 2;
 28
 29 	/**
 30 	 * Represents an editor instance. This constructor should be rarely used,
 31 	 * being the {@link CKEDITOR} methods preferible.
 32 	 * @constructor
 33 	 * @param {Object} instanceConfig Configuration values for this specific
 34 	 *		instance.
 35 	 * @param {CKEDITOR.dom.element} [element] The element linked to this
 36 	 *		instance.
 37 	 * @param {Number} [mode] The mode in which the element is linked to this
 38 	 *		instance.
 39 	 * @augments CKEDITOR.event
 40 	 * @example
 41 	 */
 42 	CKEDITOR.editor = function( instanceConfig, element, mode )
 43 	{
 44 		this._ =
 45 		{
 46 			// Save the config to be processed later by the full core code.
 47 			instanceConfig : instanceConfig,
 48 			element : element
 49 		};
 50
 51 		/**
 52 		 * The mode in which the {@link #element} is linked to this editor
 53 		 * instance. It can be any of the following values:
 54 		 * <ul>
 55 		 * <li><b>CKEDITOR.ELEMENT_MODE_NONE</b>: No element is linked to the
 56 		 *		editor instance.</li>
 57 		 * <li><b>CKEDITOR.ELEMENT_MODE_REPLACE</b>: The element is to be
 58 		 *		replaced by the editor instance.</li>
 59 		 * <li><b>CKEDITOR.ELEMENT_MODE_APPENDTO</b>: The editor is to be
 60 		 *		created inside the element.</li>
 61 		 * </ul>
 62 		 * @name CKEDITOR.editor.prototype.elementMode
 63 		 * @type Number
 64 		 * @example
 65 		 * var editor = CKEDITOR.replace( 'editor1' );
 66 		 * alert( <b>editor.elementMode</b> );  "1"
 67 		 */
 68 		this.elementMode = mode || CKEDITOR.ELEMENT_MODE_NONE;
 69
 70 		// Call the CKEDITOR.event constructor to initialize this instance.
 71 		CKEDITOR.event.call( this );
 72
 73 		this._init();
 74 	};
 75
 76 	/**
 77 	 * Replaces a <textarea> or a DOM element (DIV) with a CKEditor
 78 	 * instance. For textareas, the initial value in the editor will be the
 79 	 * textarea value. For DOM elements, their innerHTML will be used
 80 	 * instead. We recommend using TEXTAREA and DIV elements only. Do not use
 81 	 * this function directly. Use {@link CKEDITOR.replace} instead.
 82 	 * @param {Object|String} elementOrIdOrName The DOM element (textarea), its
 83 	 *		ID or name.
 84 	 * @param {Object} [config] The specific configurations to apply to this
 85 	 *		editor instance. Configurations set here will override global CKEditor
 86 	 *		settings.
 87 	 * @returns {CKEDITOR.editor} The editor instance created.
 88 	 * @example
 89 	 */
 90 	CKEDITOR.editor.replace = function( elementOrIdOrName, config )
 91 	{
 92 		var element = elementOrIdOrName;
 93
 94 		if ( typeof element != 'object' )
 95 		{
 96 			// Look for the element by id. We accept any kind of element here.
 97 			element = document.getElementById( elementOrIdOrName );
 98
 99 			// If not found, look for elements by name. In this case we accept only
100 			// textareas.
101 			if ( !element )
102 			{
103 				var i = 0,
104 					textareasByName	= document.getElementsByName( elementOrIdOrName );
105
106 				while ( ( element = textareasByName[ i++ ] ) && element.tagName.toLowerCase() != 'textarea' )
107 				{ /*jsl:pass*/ }
108 			}
109
110 			if ( !element )
111 				throw '[CKEDITOR.editor.replace] The element with id or name "' + elementOrIdOrName + '" was not found.';
112 		}
113
114 		// Do not replace the textarea right now, just hide it. The effective
115 		// replacement will be done by the _init function.
116 		element.style.visibility = 'hidden';
117
118 		// Create the editor instance.
119 		return new CKEDITOR.editor( config, element, CKEDITOR.ELEMENT_MODE_REPLACE );
120 	};
121
122 	/**
123 	 * Creates a new editor instance inside a specific DOM element. Do not use
124 	 * this function directly. Use {@link CKEDITOR.appendTo} instead.
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 	 * @returns {CKEDITOR.editor} The editor instance created.
130 	 * @example
131 	 */
132 	CKEDITOR.editor.appendTo = function( elementOrId, config )
133 	{
134 		if ( typeof elementOrId != 'object' )
135 		{
136 			elementOrId = document.getElementById( elementOrId );
137
138 			if ( !elementOrId )
139 				throw '[CKEDITOR.editor.appendTo] The element with id "' + elementOrId + '" was not found.';
140 		}
141
142 		// Create the editor instance.
143 		return new CKEDITOR.editor( config, elementOrId, CKEDITOR.ELEMENT_MODE_APPENDTO );
144 	};
145
146 	CKEDITOR.editor.prototype =
147 	{
148 		/**
149 		 * Initializes the editor instance. This function will be overriden by the
150 		 * full CKEDITOR.editor implementation (editor.js).
151 		 * @private
152 		 */
153 		_init : function()
154 		{
155 			var pending = CKEDITOR.editor._pending || ( CKEDITOR.editor._pending = [] );
156 			pending.push( this );
157 		},
158
159 		// Both fire and fireOnce will always pass this editor instance as the
160 		// "editor" param in CKEDITOR.event.fire. So, we override it to do that
161 		// automaticaly.
162
163 		/** @ignore */
164 		fire : function( eventName, data )
165 		{
166 			return CKEDITOR.event.prototype.fire.call( this, eventName, data, this );
167 		},
168
169 		/** @ignore */
170 		fireOnce : function( eventName, data )
171 		{
172 			return CKEDITOR.event.prototype.fireOnce.call( this, eventName, data, this );
173 		}
174 	};
175
176 	// "Inherit" (copy actually) from CKEDITOR.event.
177 	CKEDITOR.event.implementOn( CKEDITOR.editor.prototype, true );
178 }
179