JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
76b8f0142acbbb7975edb549f8517007a8569cc5
[ckeditor.git] / _source / core / ckeditor_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 /**\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 = true;\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 = true;\r
81 \r
82                 var createInstance = function( elementOrIdOrName, config, creationFunction )\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 );\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                  * @returns {CKEDITOR.editor} The editor instance created.\r
129                  * @example\r
130                  * &lt;div id="editorSpace"&gt;&lt:/div&gt;\r
131                  * ...\r
132                  * <b>CKEDITOR.appendTo( 'editorSpace' )</b>;\r
133                  */\r
134                 CKEDITOR.appendTo = function( elementOrId, config )\r
135                 {\r
136                         return createInstance( elementOrId, config, CKEDITOR.editor.appendTo );\r
137                 };\r
138 \r
139                 /**\r
140                  * @ignore\r
141                  * Documented at ckeditor.js.\r
142                  */\r
143                 CKEDITOR.add = function( editor )\r
144                 {\r
145                         // For now, just put the editor in the pending list. It will be\r
146                         // processed as soon as the full code gets loaded.\r
147                         var pending = this._.pending || ( this._.pending = [] );\r
148                         pending.push( editor );\r
149                 };\r
150 \r
151                 /**\r
152                  * Replace all &lt;textarea&gt; elements available in the document with\r
153                  * editor instances.\r
154                  * @example\r
155                  * // Replace all &lt;textarea&gt; elements in the page.\r
156                  * CKEDITOR.replaceAll();\r
157                  * @example\r
158                  * // Replace all &lt;textarea class="myClassName"&gt; elements in the page.\r
159                  * CKEDITOR.replaceAll( 'myClassName' );\r
160                  * @example\r
161                  * // Selectively replace &lt;textarea&gt; elements, based on custom assertions.\r
162                  * CKEDITOR.replaceAll( function( textarea, config )\r
163                  *     {\r
164                  *         // Custom code to evaluate the replace, returning false\r
165                  *         // if it must not be done.\r
166                  *         // It also passes the "config" parameter, so the\r
167                  *         // developer can customize the instance.\r
168                  *     } );\r
169                  */\r
170                 CKEDITOR.replaceAll = function()\r
171                 {\r
172                         var textareas = document.getElementsByTagName( 'textarea' );\r
173 \r
174                         for ( var i = 0 ; i < textareas.length ; i++ )\r
175                         {\r
176                                 var config = null;\r
177                                 var textarea = textareas[i];\r
178                                 var name = textarea.name;\r
179 \r
180                                 // The "name" and/or "id" attribute must exist.\r
181                                 if ( !textarea.name && !textarea.id )\r
182                                         continue;\r
183 \r
184                                 if ( typeof arguments[0] == 'string' )\r
185                                 {\r
186                                         // The textarea class name could be passed as the function\r
187                                         // parameter.\r
188 \r
189                                         var classRegex = new RegExp( '(?:^| )' + arguments[0] + '(?:$| )' );\r
190 \r
191                                         if ( !classRegex.test( textarea.className ) )\r
192                                                 continue;\r
193                                 }\r
194                                 else if ( typeof arguments[0] == 'function' )\r
195                                 {\r
196                                         // An assertion function could be passed as the function parameter.\r
197                                         // It must explicitly return "false" to ignore a specific <textarea>.\r
198                                         config = {};\r
199                                         if ( arguments[0]( textarea, config ) === false )\r
200                                                 continue;\r
201                                 }\r
202 \r
203                                 this.replace( textarea, config );\r
204                         }\r
205                 };\r
206 \r
207                 (function()\r
208                 {\r
209                         var onload = function()\r
210                         {\r
211                                 var loadFullCore = CKEDITOR.loadFullCore,\r
212                                         loadFullCoreTimeout = CKEDITOR.loadFullCoreTimeout;\r
213 \r
214                                 // Replace all textareas with the default class name.\r
215                                 if ( CKEDITOR.replaceByClassEnabled )\r
216                                         CKEDITOR.replaceAll( CKEDITOR.replaceClass );\r
217 \r
218                                 CKEDITOR.status = 'basic_ready';\r
219 \r
220                                 if ( loadFullCore && loadFullCore._load )\r
221                                         loadFullCore();\r
222                                 else if ( loadFullCoreTimeout )\r
223                                 {\r
224                                         setTimeout( function()\r
225                                                 {\r
226                                                         if ( CKEDITOR.loadFullCore )\r
227                                                                 CKEDITOR.loadFullCore();\r
228                                                 }\r
229                                                 , loadFullCoreTimeout * 1000 );\r
230                                 }\r
231                         };\r
232 \r
233                         if ( window.addEventListener )\r
234                                 window.addEventListener( 'load', onload, false );\r
235                         else if ( window.attachEvent )\r
236                                 window.attachEvent( 'onload', onload );\r
237                 })();\r
238 \r
239                 CKEDITOR.status = 'basic_loaded';\r
240         })();\r
241 }\r