JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
vanilla ckeditor-3.4.2
[ckeditor.git] / _source / core / ckeditor_basic.js
1 /*\r
2 Copyright (c) 2003-2010, CKSource - Frederico Knabben. All rights reserved.\r
3 For licensing, see LICENSE.html or http://ckeditor.com/license\r
4 */\r
5 \r
6 /**\r
7  * @fileOverview Contains the second part of the {@link CKEDITOR} object\r
8  *              definition, which defines the basic editor features to be available in\r
9  *              the root ckeditor_basic.js file.\r
10  */\r
11 \r
12 if ( CKEDITOR.status == 'unloaded' )\r
13 {\r
14         (function()\r
15         {\r
16                 CKEDITOR.event.implementOn( CKEDITOR );\r
17 \r
18                 /**\r
19                  * Forces the full CKEditor core code, in the case only the basic code has been\r
20                  * loaded (ckeditor_basic.js). This method self-destroys (becomes undefined) in\r
21                  * the first call or as soon as the full code is available.\r
22                  * @example\r
23                  * // Check if the full core code has been loaded and load it.\r
24                  * if ( CKEDITOR.loadFullCore )\r
25                  *     <b>CKEDITOR.loadFullCore()</b>;\r
26                  */\r
27                 CKEDITOR.loadFullCore = function()\r
28                 {\r
29                         // If not the basic code is not ready it, just mark it to be loaded.\r
30                         if ( CKEDITOR.status != 'basic_ready' )\r
31                         {\r
32                                 CKEDITOR.loadFullCore._load = 1;\r
33                                 return;\r
34                         }\r
35 \r
36                         // Destroy this function.\r
37                         delete CKEDITOR.loadFullCore;\r
38 \r
39                         // Append the script to the head.\r
40                         var script = document.createElement( 'script' );\r
41                         script.type = 'text/javascript';\r
42                         script.src = CKEDITOR.basePath + 'ckeditor.js';\r
43 \r
44                         document.getElementsByTagName( 'head' )[0].appendChild( script );\r
45                 };\r
46 \r
47                 /**\r
48                  * The time to wait (in seconds) to load the full editor code after the\r
49                  * page load, if the "ckeditor_basic" file is used. If set to zero, the\r
50                  * editor is loaded on demand, as soon as an instance is created.\r
51                  *\r
52                  * This value must be set on the page before the page load completion.\r
53                  * @type Number\r
54                  * @default 0 (zero)\r
55                  * @example\r
56                  * // Loads the full source after five seconds.\r
57                  * CKEDITOR.loadFullCoreTimeout = 5;\r
58                  */\r
59                 CKEDITOR.loadFullCoreTimeout = 0;\r
60 \r
61                 /**\r
62                  * The class name used to identify &lt;textarea&gt; elements to be replace\r
63                  * by CKEditor instances.\r
64                  * @type String\r
65                  * @default 'ckeditor'\r
66                  * @example\r
67                  * <b>CKEDITOR.replaceClass</b> = 'rich_editor';\r
68                  */\r
69                 CKEDITOR.replaceClass = 'ckeditor';\r
70 \r
71                 /**\r
72                  * Enables the replacement of all textareas with class name matching\r
73                  * {@link CKEDITOR.replaceClass}.\r
74                  * @type Boolean\r
75                  * @default true\r
76                  * @example\r
77                  * // Disable the auto-replace feature.\r
78                  * <b>CKEDITOR.replaceByClassEnabled</b> = false;\r
79                  */\r
80                 CKEDITOR.replaceByClassEnabled = 1;\r
81 \r
82                 var createInstance = function( elementOrIdOrName, config, creationFunction, data )\r
83                 {\r
84                         if ( CKEDITOR.env.isCompatible )\r
85                         {\r
86                                 // Load the full core.\r
87                                 if ( CKEDITOR.loadFullCore )\r
88                                         CKEDITOR.loadFullCore();\r
89 \r
90                                 var editor = creationFunction( elementOrIdOrName, config, data );\r
91                                 CKEDITOR.add( editor );\r
92                                 return editor;\r
93                         }\r
94 \r
95                         return null;\r
96                 };\r
97 \r
98                 /**\r
99                  * Replaces a &lt;textarea&gt; or a DOM element (DIV) with a CKEditor\r
100                  * instance. For textareas, the initial value in the editor will be the\r
101                  * textarea value. For DOM elements, their innerHTML will be used\r
102                  * instead. We recommend using TEXTAREA and DIV elements only.\r
103                  * @param {Object|String} elementOrIdOrName The DOM element (textarea), its\r
104                  *              ID or name.\r
105                  * @param {Object} [config] The specific configurations to apply to this\r
106                  *              editor instance. Configurations set here will override global CKEditor\r
107                  *              settings.\r
108                  * @returns {CKEDITOR.editor} The editor instance created.\r
109                  * @example\r
110                  * &lt;textarea id="myfield" name="myfield"&gt;&lt:/textarea&gt;\r
111                  * ...\r
112                  * <b>CKEDITOR.replace( 'myfield' )</b>;\r
113                  * @example\r
114                  * var textarea = document.body.appendChild( document.createElement( 'textarea' ) );\r
115                  * <b>CKEDITOR.replace( textarea )</b>;\r
116                  */\r
117                 CKEDITOR.replace = function( elementOrIdOrName, config )\r
118                 {\r
119                         return createInstance( elementOrIdOrName, config, CKEDITOR.editor.replace );\r
120                 };\r
121 \r
122                 /**\r
123                  * Creates a new editor instance inside a specific DOM element.\r
124                  * @param {Object|String} elementOrId The DOM element or its ID.\r
125                  * @param {Object} [config] The specific configurations to apply to this\r
126                  *              editor instance. Configurations set here will override global CKEditor\r
127                  *              settings.\r
128                  * @param {String} [data] Since 3.3. Initial value for the instance.\r
129                  * @returns {CKEDITOR.editor} The editor instance created.\r
130                  * @example\r
131                  * &lt;div id="editorSpace"&gt;&lt:/div&gt;\r
132                  * ...\r
133                  * <b>CKEDITOR.appendTo( 'editorSpace' )</b>;\r
134                  */\r
135                 CKEDITOR.appendTo = function( elementOrId, config, data )\r
136                 {\r
137                         return createInstance( elementOrId, config, CKEDITOR.editor.appendTo, data );\r
138                 };\r
139 \r
140                 // Documented at ckeditor.js.\r
141                 CKEDITOR.add = function( editor )\r
142                 {\r
143                         // For now, just put the editor in the pending list. It will be\r
144                         // processed as soon as the full code gets loaded.\r
145                         var pending = this._.pending || ( this._.pending = [] );\r
146                         pending.push( editor );\r
147                 };\r
148 \r
149                 /**\r
150                  * Replace all &lt;textarea&gt; elements available in the document with\r
151                  * editor instances.\r
152                  * @example\r
153                  * // Replace all &lt;textarea&gt; elements in the page.\r
154                  * CKEDITOR.replaceAll();\r
155                  * @example\r
156                  * // Replace all &lt;textarea class="myClassName"&gt; elements in the page.\r
157                  * CKEDITOR.replaceAll( 'myClassName' );\r
158                  * @example\r
159                  * // Selectively replace &lt;textarea&gt; elements, based on custom assertions.\r
160                  * CKEDITOR.replaceAll( function( textarea, config )\r
161                  *     {\r
162                  *         // Custom code to evaluate the replace, returning false\r
163                  *         // if it must not be done.\r
164                  *         // It also passes the "config" parameter, so the\r
165                  *         // developer can customize the instance.\r
166                  *     } );\r
167                  */\r
168                 CKEDITOR.replaceAll = function()\r
169                 {\r
170                         var textareas = document.getElementsByTagName( 'textarea' );\r
171 \r
172                         for ( var i = 0 ; i < textareas.length ; i++ )\r
173                         {\r
174                                 var config = null,\r
175                                         textarea = textareas[i],\r
176                                         name = textarea.name;\r
177 \r
178                                 // The "name" and/or "id" attribute must exist.\r
179                                 if ( !textarea.name && !textarea.id )\r
180                                         continue;\r
181 \r
182                                 if ( typeof arguments[0] == 'string' )\r
183                                 {\r
184                                         // The textarea class name could be passed as the function\r
185                                         // parameter.\r
186 \r
187                                         var classRegex = new RegExp( '(?:^|\\s)' + arguments[0] + '(?:$|\\s)' );\r
188 \r
189                                         if ( !classRegex.test( textarea.className ) )\r
190                                                 continue;\r
191                                 }\r
192                                 else if ( typeof arguments[0] == 'function' )\r
193                                 {\r
194                                         // An assertion function could be passed as the function parameter.\r
195                                         // It must explicitly return "false" to ignore a specific <textarea>.\r
196                                         config = {};\r
197                                         if ( arguments[0]( textarea, config ) === false )\r
198                                                 continue;\r
199                                 }\r
200 \r
201                                 this.replace( textarea, config );\r
202                         }\r
203                 };\r
204 \r
205                 (function()\r
206                 {\r
207                         var onload = function()\r
208                         {\r
209                                 var loadFullCore = CKEDITOR.loadFullCore,\r
210                                         loadFullCoreTimeout = CKEDITOR.loadFullCoreTimeout;\r
211 \r
212                                 // Replace all textareas with the default class name.\r
213                                 if ( CKEDITOR.replaceByClassEnabled )\r
214                                         CKEDITOR.replaceAll( CKEDITOR.replaceClass );\r
215 \r
216                                 CKEDITOR.status = 'basic_ready';\r
217 \r
218                                 if ( loadFullCore && loadFullCore._load )\r
219                                         loadFullCore();\r
220                                 else if ( loadFullCoreTimeout )\r
221                                 {\r
222                                         setTimeout( function()\r
223                                                 {\r
224                                                         if ( CKEDITOR.loadFullCore )\r
225                                                                 CKEDITOR.loadFullCore();\r
226                                                 }\r
227                                                 , loadFullCoreTimeout * 1000 );\r
228                                 }\r
229                         };\r
230 \r
231                         if ( window.addEventListener )\r
232                                 window.addEventListener( 'load', onload, false );\r
233                         else if ( window.attachEvent )\r
234                                 window.attachEvent( 'onload', onload );\r
235                 })();\r
236 \r
237                 CKEDITOR.status = 'basic_loaded';\r
238         })();\r
239 }\r