JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
vanilla ckeditor-3.1
[ckeditor.git] / _source / core / loader.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 Defines the {@link CKEDITOR.loader} objects, which is used to\r
8  *              load core scripts and their dependencies from _source.\r
9  */\r
10 \r
11 if ( typeof CKEDITOR == 'undefined' )\r
12         CKEDITOR = {};\r
13 \r
14 if ( !CKEDITOR.loader )\r
15 {\r
16         /**\r
17          * Load core scripts and their dependencies from _source.\r
18          * @namespace\r
19          * @example\r
20          */\r
21         CKEDITOR.loader = (function()\r
22         {\r
23                 // Table of script names and their dependencies.\r
24                 var scripts =\r
25                 {\r
26                         'core/_bootstrap'               : [ 'core/config', 'core/ckeditor', 'core/plugins', 'core/scriptloader', 'core/tools', /* The following are entries that we want to force loading at the end to avoid dependence recursion */ 'core/dom/comment', 'core/dom/elementpath', 'core/dom/text', 'core/dom/range' ],\r
27                         'core/ajax'                             : [ 'core/xml' ],\r
28                         'core/ckeditor'                 : [ 'core/ckeditor_basic', 'core/dom', 'core/dtd', 'core/dom/document', 'core/dom/element', 'core/editor', 'core/event', 'core/htmlparser', 'core/htmlparser/element', 'core/htmlparser/fragment', 'core/htmlparser/filter', 'core/htmlparser/basicwriter', 'core/tools' ],\r
29                         'core/ckeditor_base'    : [],\r
30                         'core/ckeditor_basic'   : [ 'core/editor_basic', 'core/env', 'core/event' ],\r
31                         'core/command'                  : [],\r
32                         'core/config'                   : [ 'core/ckeditor_base' ],\r
33                         'core/dom'                              : [],\r
34                         'core/dom/comment'              : [ 'core/dom/node' ],\r
35                         'core/dom/document'             : [ 'core/dom', 'core/dom/domobject', 'core/dom/window' ],\r
36                         'core/dom/documentfragment'     : [ 'core/dom/element' ],\r
37                         'core/dom/element'              : [ 'core/dom', 'core/dom/document', 'core/dom/domobject', 'core/dom/node', 'core/dom/nodelist', 'core/tools' ],\r
38                         'core/dom/elementpath'  : [ 'core/dom/element' ],\r
39                         'core/dom/event'                : [],\r
40                         'core/dom/node'                 : [ 'core/dom/domobject', 'core/tools' ],\r
41                         'core/dom/nodelist'             : [ 'core/dom/node' ],\r
42                         'core/dom/domobject'    : [ 'core/dom/event' ],\r
43                         'core/dom/range'                : [ 'core/dom/document', 'core/dom/documentfragment', 'core/dom/element', 'core/dom/walker' ],\r
44                         'core/dom/text'                 : [ 'core/dom/node', 'core/dom/domobject' ],\r
45                         'core/dom/walker'               : [ 'core/dom/node' ],\r
46                         'core/dom/window'               : [ 'core/dom/domobject' ],\r
47                         'core/dtd'                              : [ 'core/tools' ],\r
48                         'core/editor'                   : [ 'core/command', 'core/config', 'core/editor_basic', 'core/focusmanager', 'core/lang', 'core/plugins', 'core/skins', 'core/themes', 'core/tools', 'core/ui' ],\r
49                         'core/editor_basic'             : [ 'core/event' ],\r
50                         'core/env'                              : [],\r
51                         'core/event'                    : [],\r
52                         'core/focusmanager'             : [],\r
53                         'core/htmlparser'               : [],\r
54                         'core/htmlparser/comment'       : [ 'core/htmlparser' ],\r
55                         'core/htmlparser/element'       : [ 'core/htmlparser', 'core/htmlparser/fragment' ],\r
56                         'core/htmlparser/fragment'      : [ 'core/htmlparser', 'core/htmlparser/comment', 'core/htmlparser/text', 'core/htmlparser/cdata' ],\r
57                         'core/htmlparser/text'          : [ 'core/htmlparser' ],\r
58                         'core/htmlparser/cdata'         : [ 'core/htmlparser' ],\r
59                         'core/htmlparser/filter'        : [ 'core/htmlparser' ],\r
60                         'core/htmlparser/basicwriter': [ 'core/htmlparser' ],\r
61                         'core/imagecacher'              : [ 'core/dom/element' ],\r
62                         'core/lang'                             : [],\r
63                         'core/plugins'                  : [ 'core/resourcemanager' ],\r
64                         'core/resourcemanager'  : [ 'core/scriptloader', 'core/tools' ],\r
65                         'core/scriptloader'             : [ 'core/dom/element', 'core/env' ],\r
66                         'core/skins'                    : [ 'core/imagecacher', 'core/scriptloader' ],\r
67                         'core/themes'                   : [ 'core/resourcemanager' ],\r
68                         'core/tools'                    : [ 'core/env' ],\r
69                         'core/ui'                               : [],\r
70                         'core/xml'                              : [ 'core/env' ]\r
71                 };\r
72 \r
73                 var basePath = (function()\r
74                 {\r
75                         // This is a copy of CKEDITOR.basePath, but requires the script having\r
76                         // "_source/core/loader.js".\r
77                         if ( CKEDITOR && CKEDITOR.basePath )\r
78                                 return CKEDITOR.basePath;\r
79 \r
80                         // Find out the editor directory path, based on its <script> tag.\r
81                         var path = '';\r
82                         var scripts = document.getElementsByTagName( 'script' );\r
83 \r
84                         for ( var i = 0 ; i < scripts.length ; i++ )\r
85                         {\r
86                                 var match = scripts[i].src.match( /(^|.*[\\\/])core\/loader.js(?:\?.*)?$/i );\r
87 \r
88                                 if ( match )\r
89                                 {\r
90                                         path = match[1];\r
91                                         break;\r
92                                 }\r
93                         }\r
94 \r
95                         // In IE (only) the script.src string is the raw valued entered in the\r
96                         // HTML. Other browsers return the full resolved URL instead.\r
97                         if ( path.indexOf('://') == -1 )\r
98                         {\r
99                                 // Absolute path.\r
100                                 if ( path.indexOf( '/' ) === 0 )\r
101                                         path = location.href.match( /^.*?:\/\/[^\/]*/ )[0] + path;\r
102                                 // Relative path.\r
103                                 else\r
104                                         path = location.href.match( /^[^\?]*\// )[0] + path;\r
105                         }\r
106 \r
107                         return path;\r
108                 })();\r
109 \r
110                 var timestamp = 'A06B';\r
111 \r
112                 var getUrl = function( resource )\r
113                 {\r
114                         if ( CKEDITOR && CKEDITOR.getUrl )\r
115                                 return CKEDITOR.getUrl( resource );\r
116 \r
117                         return basePath + resource +\r
118                                 ( resource.indexOf( '?' ) >= 0 ? '&' : '?' ) +\r
119                                 't=' + timestamp;\r
120                 };\r
121 \r
122                 var pendingLoad = [];\r
123 \r
124                 /** @lends CKEDITOR.loader */\r
125                 return {\r
126                         /**\r
127                          * The list of loaded scripts in their loading order.\r
128                          * @type Array\r
129                          * @example\r
130                          * // Alert the loaded script names.\r
131                          * alert( <b>CKEDITOR.loader.loadedScripts</b> );\r
132                          */\r
133                         loadedScripts : [],\r
134 \r
135                         loadPending : function()\r
136                         {\r
137                                 var scriptName = pendingLoad.shift();\r
138 \r
139                                 if ( !scriptName )\r
140                                         return;\r
141 \r
142                                 var scriptSrc = getUrl( '_source/' + scriptName + '.js' );\r
143 \r
144                                 var script = document.createElement( 'script' );\r
145                                 script.type = 'text/javascript';\r
146                                 script.src = scriptSrc;\r
147 \r
148                                 function onScriptLoaded()\r
149                                 {\r
150                                         // Append this script to the list of loaded scripts.\r
151                                         CKEDITOR.loader.loadedScripts.push( scriptName );\r
152 \r
153                                         // Load the next.\r
154                                         CKEDITOR.loader.loadPending();\r
155                                 }\r
156 \r
157                                 // We must guarantee the execution order of the scripts, so we\r
158                                 // need to load them one by one. (#4145)\r
159                                 // The followin if/else block has been taken from the scriptloader core code.\r
160                                 if ( CKEDITOR.env.ie )\r
161                                 {\r
162                                         /** @ignore */\r
163                                         script.onreadystatechange = function()\r
164                                         {\r
165                                                 if ( script.readyState == 'loaded' || script.readyState == 'complete' )\r
166                                                 {\r
167                                                         script.onreadystatechange = null;\r
168                                                         onScriptLoaded();\r
169                                                 }\r
170                                         };\r
171                                 }\r
172                                 else\r
173                                 {\r
174                                         /** @ignore */\r
175                                         script.onload = function()\r
176                                         {\r
177                                                 // Some browsers, such as Safari, may call the onLoad function\r
178                                                 // immediately. Which will break the loading sequence. (#3661)\r
179                                                 setTimeout( function() { onScriptLoaded( scriptName ); }, 0 );\r
180                                         };\r
181                                 }\r
182 \r
183                                 document.body.appendChild( script );\r
184                         },\r
185 \r
186                         /**\r
187                          * Loads a specific script, including its dependencies. This is not a\r
188                          * synchronous loading, which means that the code the be loaded will\r
189                          * not necessarily be available after this call.\r
190                          * @example\r
191                          * CKEDITOR.loader.load( 'core/dom/element' );\r
192                          */\r
193                         load : function( scriptName, defer )\r
194                         {\r
195                                 // Check if the script has already been loaded.\r
196                                 if ( scriptName in this.loadedScripts )\r
197                                         return;\r
198 \r
199                                 // Get the script dependencies list.\r
200                                 var dependencies = scripts[ scriptName ];\r
201                                 if ( !dependencies )\r
202                                         throw 'The script name"' + scriptName + '" is not defined.';\r
203 \r
204                                 // Mark the script as loaded, even before really loading it, to\r
205                                 // avoid cross references recursion.\r
206                                 this.loadedScripts[ scriptName ] = true;\r
207 \r
208                                 // Load all dependencies first.\r
209                                 for ( var i = 0 ; i < dependencies.length ; i++ )\r
210                                         this.load( dependencies[ i ], true );\r
211 \r
212                                 var scriptSrc = getUrl( '_source/' + scriptName + '.js' );\r
213 \r
214                                 // Append the <script> element to the DOM.\r
215                                 if ( document.body )\r
216                                 {\r
217                                         pendingLoad.push( scriptName );\r
218 \r
219                                         if ( !defer )\r
220                                                 this.loadPending();\r
221                                 }\r
222                                 else\r
223                                 {\r
224                                         // Append this script to the list of loaded scripts.\r
225                                         this.loadedScripts.push( scriptName );\r
226 \r
227                                         document.write( '<script src="' + scriptSrc + '" type="text/javascript"><\/script>' );\r
228                                 }\r
229                         }\r
230                 };\r
231         })();\r
232 }\r
233 \r
234 // Check if any script has been defined for autoload.\r
235 if ( CKEDITOR._autoLoad )\r
236 {\r
237         CKEDITOR.loader.load( CKEDITOR._autoLoad );\r
238         delete CKEDITOR._autoLoad;\r
239 }\r