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