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