Mimimum syndical pour en faire un produit zope / cmf.
[ckeditor.git] / _source / core / loader.js
1 /*
2 Copyright (c) 2003-2011, CKSource - Frederico Knabben. All rights reserved.
3 For licensing, see LICENSE.html or http://ckeditor.com/license
4 */
5
6 /**
7 * @fileOverview Defines the {@link CKEDITOR.loader} objects, which is used to
8 * load core scripts and their dependencies from _source.
9 */
10
11 if ( typeof CKEDITOR == 'undefined' )
12 CKEDITOR = {};
13
14 if ( !CKEDITOR.loader )
15 {
16 /**
17 * Load core scripts and their dependencies from _source.
18 * @namespace
19 * @example
20 */
21 CKEDITOR.loader = (function()
22 {
23 // Table of script names and their dependencies.
24 var scripts =
25 {
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' ],
30 'core/command' : [],
31 'core/config' : [ 'core/ckeditor_base' ],
32 'core/dom' : [],
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' ],
50 'core/env' : [],
51 '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' ],
61 'core/lang' : [],
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' ],
68 'core/ui' : []
69 };
70
71 var basePath = (function()
72 {
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;
77
78 // Find out the editor directory path, based on its <script> tag.
79 var path = '';
80 var scripts = document.getElementsByTagName( 'script' );
81
82 for ( var i = 0 ; i < scripts.length ; i++ )
83 {
84 var match = scripts[i].src.match( /(^|.*?[\\\/])(?:_source\/)?core\/loader.js(?:\?.*)?$/i );
85
86 if ( match )
87 {
88 path = match[1];
89 break;
90 }
91 }
92
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 )
96 {
97 // Absolute path.
98 if ( path.indexOf( '/' ) === 0 )
99 path = location.href.match( /^.*?:\/\/[^\/]*/ )[0] + path;
100 // Relative path.
101 else
102 path = location.href.match( /^[^\?]*\// )[0] + path;
103 }
104
105 return path;
106 })();
107
108 var timestamp = 'B5GJ5GG';
109
110 var getUrl = function( resource )
111 {
112 if ( CKEDITOR && CKEDITOR.getUrl )
113 return CKEDITOR.getUrl( resource );
114
115 return basePath + resource +
116 ( resource.indexOf( '?' ) >= 0 ? '&' : '?' ) +
117 't=' + timestamp;
118 };
119
120 var pendingLoad = [];
121
122 /** @lends CKEDITOR.loader */
123 return {
124 /**
125 * The list of loaded scripts in their loading order.
126 * @type Array
127 * @example
128 * // Alert the loaded script names.
129 * alert( <b>CKEDITOR.loader.loadedScripts</b> );
130 */
131 loadedScripts : [],
132
133 loadPending : function()
134 {
135 var scriptName = pendingLoad.shift();
136
137 if ( !scriptName )
138 return;
139
140 var scriptSrc = getUrl( '_source/' + scriptName + '.js' );
141
142 var script = document.createElement( 'script' );
143 script.type = 'text/javascript';
144 script.src = scriptSrc;
145
146 function onScriptLoaded()
147 {
148 // Append this script to the list of loaded scripts.
149 CKEDITOR.loader.loadedScripts.push( scriptName );
150
151 // Load the next.
152 CKEDITOR.loader.loadPending();
153 }
154
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" )
159 {
160 /** @ignore */
161 script.onreadystatechange = function()
162 {
163 if ( script.readyState == 'loaded' || script.readyState == 'complete' )
164 {
165 script.onreadystatechange = null;
166 onScriptLoaded();
167 }
168 };
169 }
170 else
171 {
172 /** @ignore */
173 script.onload = function()
174 {
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 );
178 };
179 }
180
181 document.body.appendChild( script );
182 },
183
184 /**
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.
188 * @example
189 * CKEDITOR.loader.load( 'core/dom/element' );
190 */
191 load : function( scriptName, defer )
192 {
193 // Check if the script has already been loaded.
194 if ( scriptName in this.loadedScripts )
195 return;
196
197 // Get the script dependencies list.
198 var dependencies = scripts[ scriptName ];
199 if ( !dependencies )
200 throw 'The script name"' + scriptName + '" is not defined.';
201
202 // Mark the script as loaded, even before really loading it, to
203 // avoid cross references recursion.
204 this.loadedScripts[ scriptName ] = true;
205
206 // Load all dependencies first.
207 for ( var i = 0 ; i < dependencies.length ; i++ )
208 this.load( dependencies[ i ], true );
209
210 var scriptSrc = getUrl( '_source/' + scriptName + '.js' );
211
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') )
217 {
218 pendingLoad.push( scriptName );
219
220 if ( !defer )
221 this.loadPending();
222 }
223 else
224 {
225 // Append this script to the list of loaded scripts.
226 this.loadedScripts.push( scriptName );
227
228 document.write( '<script src="' + scriptSrc + '" type="text/javascript"><\/script>' );
229 }
230 }
231 };
232 })();
233 }
234
235 // Check if any script has been defined for autoload.
236 if ( CKEDITOR._autoLoad )
237 {
238 CKEDITOR.loader.load( CKEDITOR._autoLoad );
239 delete CKEDITOR._autoLoad;
240 }