2 Copyright (c) 2003-2011, CKSource - Frederico Knabben. All rights reserved.
3 For licensing, see LICENSE.html or http://ckeditor.com/license
7 * @fileOverview Defines the {@link CKEDITOR.loader} objects, which is used to
8 * load core scripts and their dependencies from _source.
11 if ( typeof CKEDITOR
== 'undefined' )
14 if ( !CKEDITOR
.loader
)
17 * Load core scripts and their dependencies from _source.
21 CKEDITOR
.loader
= (function()
23 // Table of script names and their dependencies.
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' ],
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' ],
28 'core/ckeditor_base' : [],
29 'core/ckeditor_basic' : [ 'core/editor_basic', 'core/env', 'core/event' ],
31 'core/config' : [ 'core/ckeditor_base' ],
33 'core/dom/comment' : [ 'core/dom/node' ],
34 'core/dom/document' : [ 'core/dom', 'core/dom/domobject', 'core/dom/window' ],
35 'core/dom/documentfragment' : [ 'core/dom/element' ],
36 'core/dom/element' : [ 'core/dom', 'core/dom/document', 'core/dom/domobject', 'core/dom/node', 'core/dom/nodelist', 'core/tools' ],
37 'core/dom/elementpath' : [ 'core/dom/element' ],
38 'core/dom/event' : [],
39 'core/dom/node' : [ 'core/dom/domobject', 'core/tools' ],
40 'core/dom/nodelist' : [ 'core/dom/node' ],
41 'core/dom/domobject' : [ 'core/dom/event' ],
42 'core/dom/range' : [ 'core/dom/document', 'core/dom/documentfragment', 'core/dom/element', 'core/dom/walker' ],
43 'core/dom/rangelist' : [ 'core/dom/range' ],
44 'core/dom/text' : [ 'core/dom/node', 'core/dom/domobject' ],
45 'core/dom/walker' : [ 'core/dom/node' ],
46 'core/dom/window' : [ 'core/dom/domobject' ],
47 'core/dtd' : [ 'core/tools' ],
48 'core/editor' : [ 'core/command', 'core/config', 'core/editor_basic', 'core/focusmanager', 'core/lang', 'core/plugins', 'core/skins', 'core/themes', 'core/tools', 'core/ui' ],
49 'core/editor_basic' : [ 'core/event' ],
52 'core/focusmanager' : [],
53 'core/htmlparser' : [],
54 'core/htmlparser/comment' : [ 'core/htmlparser' ],
55 'core/htmlparser/element' : [ 'core/htmlparser', 'core/htmlparser/fragment' ],
56 'core/htmlparser/fragment' : [ 'core/htmlparser', 'core/htmlparser/comment', 'core/htmlparser/text', 'core/htmlparser/cdata' ],
57 'core/htmlparser/text' : [ 'core/htmlparser' ],
58 'core/htmlparser/cdata' : [ 'core/htmlparser' ],
59 'core/htmlparser/filter' : [ 'core/htmlparser' ],
60 'core/htmlparser/basicwriter': [ 'core/htmlparser' ],
62 'core/plugins' : [ 'core/resourcemanager' ],
63 'core/resourcemanager' : [ 'core/scriptloader', 'core/tools' ],
64 'core/scriptloader' : [ 'core/dom/element', 'core/env' ],
65 'core/skins' : [ 'core/scriptloader' ],
66 'core/themes' : [ 'core/resourcemanager' ],
67 'core/tools' : [ 'core/env' ],
71 var basePath
= (function()
73 // This is a copy of CKEDITOR.basePath, but requires the script having
74 // "_source/core/loader.js".
75 if ( CKEDITOR
&& CKEDITOR
.basePath
)
76 return CKEDITOR
.basePath
;
78 // Find out the editor directory path, based on its <script> tag.
80 var scripts
= document
.getElementsByTagName( 'script' );
82 for ( var i
= 0 ; i
< scripts
.length
; i
++ )
84 var match
= scripts
[i
].src
.match( /(^|.*?[\\\/])(?:_source\/)?core\/loader.js(?:\?.*)?$/i );
93 // In IE (only) the script.src string is the raw valued entered in the
94 // HTML. Other browsers return the full resolved URL instead.
95 if ( path
.indexOf('://') == -1 )
98 if ( path
.indexOf( '/' ) === 0 )
99 path
= location
.href
.match( /^.*?:\/\/[^\/]*/ )[0] + path
;
102 path
= location
.href
.match( /^[^\?]*\// )[0] + path
;
108 var timestamp
= 'B5GJ5GG';
110 var getUrl = function( resource
)
112 if ( CKEDITOR
&& CKEDITOR
.getUrl
)
113 return CKEDITOR
.getUrl( resource
);
115 return basePath
+ resource
+
116 ( resource
.indexOf( '?' ) >= 0 ? '&' : '?' ) +
120 var pendingLoad
= [];
122 /** @lends CKEDITOR.loader */
125 * The list of loaded scripts in their loading order.
128 * // Alert the loaded script names.
129 * alert( <b>CKEDITOR.loader.loadedScripts</b> );
133 loadPending : function()
135 var scriptName
= pendingLoad
.shift();
140 var scriptSrc
= getUrl( '_source/' + scriptName
+ '.js' );
142 var script
= document
.createElement( 'script' );
143 script
.type
= 'text/javascript';
144 script
.src
= scriptSrc
;
146 function onScriptLoaded()
148 // Append this script to the list of loaded scripts.
149 CKEDITOR
.loader
.loadedScripts
.push( scriptName
);
152 CKEDITOR
.loader
.loadPending();
155 // We must guarantee the execution order of the scripts, so we
156 // need to load them one by one. (#4145)
157 // The following if/else block has been taken from the scriptloader core code.
158 if ( typeof(script
.onreadystatechange
) !== "undefined" )
161 script
.onreadystatechange = function()
163 if ( script
.readyState
== 'loaded' || script
.readyState
== 'complete' )
165 script
.onreadystatechange
= null;
173 script
.onload = function()
175 // Some browsers, such as Safari, may call the onLoad function
176 // immediately. Which will break the loading sequence. (#3661)
177 setTimeout( function() { onScriptLoaded( scriptName
); }, 0 );
181 document
.body
.appendChild( script
);
185 * Loads a specific script, including its dependencies. This is not a
186 * synchronous loading, which means that the code to be loaded will
187 * not necessarily be available after this call.
189 * CKEDITOR.loader.load( 'core/dom/element' );
191 load : function( scriptName
, defer
)
193 // Check if the script has already been loaded.
194 if ( scriptName
in this.loadedScripts
)
197 // Get the script dependencies list.
198 var dependencies
= scripts
[ scriptName
];
200 throw 'The script name"' + scriptName
+ '" is not defined.';
202 // Mark the script as loaded, even before really loading it, to
203 // avoid cross references recursion.
204 this.loadedScripts
[ scriptName
] = true;
206 // Load all dependencies first.
207 for ( var i
= 0 ; i
< dependencies
.length
; i
++ )
208 this.load( dependencies
[ i
], true );
210 var scriptSrc
= getUrl( '_source/' + scriptName
+ '.js' );
212 // Append the <script> element to the DOM.
213 // If the page is fully loaded, we can't use document.write
214 // but if the script is run while the body is loading then it's safe to use it
215 // Unfortunately, Firefox <3.6 doesn't support document.readyState, so it won't get this improvement
216 if ( document
.body
&& (!document
.readyState
|| document
.readyState
== 'complete') )
218 pendingLoad
.push( scriptName
);
225 // Append this script to the list of loaded scripts.
226 this.loadedScripts
.push( scriptName
);
228 document
.write( '<script src="' + scriptSrc
+ '" type="text/javascript"><\/script>' );
235 // Check if any script has been defined for autoload.
236 if ( CKEDITOR
._autoLoad
)
238 CKEDITOR
.loader
.load( CKEDITOR
._autoLoad
);
239 delete CKEDITOR
._autoLoad
;