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