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