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