2 Copyright (c) 2003-2010, CKSource - Frederico Knabben. All rights reserved.
\r
3 For licensing, see LICENSE.html or http://ckeditor.com/license
\r
7 * @fileOverview Defines the {@link CKEDITOR.scriptLoader} object, used to load scripts
\r
12 * Load scripts asynchronously.
\r
16 CKEDITOR.scriptLoader = (function()
\r
18 var uniqueScripts = {},
\r
21 return /** @lends CKEDITOR.scriptLoader */ {
\r
23 * Loads one or more external script checking if not already loaded
\r
24 * previously by this function.
\r
25 * @param {String|Array} scriptUrl One or more URLs pointing to the
\r
26 * scripts to be loaded.
\r
27 * @param {Function} [callback] A function to be called when the script
\r
28 * is loaded and executed. If a string is passed to "scriptUrl", a
\r
29 * boolean parameter is passed to the callback, indicating the
\r
30 * success of the load. If an array is passed instead, two array
\r
31 * parameters are passed to the callback; the first contains the
\r
32 * URLs that have been properly loaded, and the second the failed
\r
34 * @param {Object} [scope] The scope ("this" reference) to be used for
\r
35 * the callback call. Default to {@link CKEDITOR}.
\r
36 * @param {Boolean} [noCheck] Indicates that the script must be loaded
\r
37 * anyway, not checking if it has already loaded.
\r
38 * @param {Boolean} [showBusy] Changes the cursor of the document while
\r
39 + * the script is loaded.
\r
41 * CKEDITOR.scriptLoader.load( '/myscript.js' );
\r
43 * CKEDITOR.scriptLoader.load( '/myscript.js', function( success )
\r
45 * // Alerts "true" if the script has been properly loaded.
\r
46 * // HTTP error 404 should return "false".
\r
50 * CKEDITOR.scriptLoader.load( [ '/myscript1.js', '/myscript2.js' ], function( completed, failed )
\r
52 * alert( 'Number of scripts loaded: ' + completed.length );
\r
53 * alert( 'Number of failures: ' + failed.length );
\r
56 load : function( scriptUrl, callback, scope, noCheck, showBusy )
\r
58 var isString = ( typeof scriptUrl == 'string' );
\r
61 scriptUrl = [ scriptUrl ];
\r
66 var scriptCount = scriptUrl.length,
\r
70 var doCallback = function( success )
\r
75 callback.call( scope, success );
\r
77 callback.call( scope, completed, failed );
\r
81 if ( scriptCount === 0 )
\r
87 var checkLoaded = function( url, success )
\r
89 ( success ? completed : failed ).push( url );
\r
91 if ( --scriptCount <= 0 )
\r
93 showBusy && CKEDITOR.document.getDocumentElement().removeStyle( 'cursor' );
\r
94 doCallback( success );
\r
98 var onLoad = function( url, success )
\r
100 // Mark this script as loaded.
\r
101 uniqueScripts[ url ] = 1;
\r
103 // Get the list of callback checks waiting for this file.
\r
104 var waitingInfo = waitingList[ url ];
\r
105 delete waitingList[ url ];
\r
107 // Check all callbacks waiting for this file.
\r
108 for ( var i = 0 ; i < waitingInfo.length ; i++ )
\r
109 waitingInfo[ i ]( url, success );
\r
112 var loadScript = function( url )
\r
114 if ( noCheck !== true && uniqueScripts[ url ] )
\r
116 checkLoaded( url, true );
\r
120 var waitingInfo = waitingList[ url ] || ( waitingList[ url ] = [] );
\r
121 waitingInfo.push( checkLoaded );
\r
123 // Load it only for the first request.
\r
124 if ( waitingInfo.length > 1 )
\r
127 // Create the <script> element.
\r
128 var script = new CKEDITOR.dom.element( 'script' );
\r
129 script.setAttributes( {
\r
130 type : 'text/javascript',
\r
135 if ( CKEDITOR.env.ie )
\r
137 // FIXME: For IE, we are not able to return false on error (like 404).
\r
140 script.$.onreadystatechange = function ()
\r
142 if ( script.$.readyState == 'loaded' || script.$.readyState == 'complete' )
\r
144 script.$.onreadystatechange = null;
\r
145 onLoad( url, true );
\r
152 script.$.onload = function()
\r
154 // Some browsers, such as Safari, may call the onLoad function
\r
155 // immediately. Which will break the loading sequence. (#3661)
\r
156 setTimeout( function() { onLoad( url, true ); }, 0 );
\r
159 // FIXME: Opera and Safari will not fire onerror.
\r
162 script.$.onerror = function()
\r
164 onLoad( url, false );
\r
169 // Append it to <head>.
\r
170 script.appendTo( CKEDITOR.document.getHead() );
\r
172 CKEDITOR.fire( 'download', url ); // @Packager.RemoveLine
\r
175 showBusy && CKEDITOR.document.getDocumentElement().setStyle( 'cursor', 'wait' );
\r
176 for ( var i = 0 ; i < scriptCount ; i++ )
\r
178 loadScript( scriptUrl[ i ] );
\r
183 * Executes a JavaScript code into the current document.
\r
184 * @param {String} code The code to be executed.
\r
186 * CKEDITOR.scriptLoader.loadCode( 'var x = 10;' );
\r
187 * alert( x ); // "10"
\r
189 loadCode : function( code )
\r
191 // Create the <script> element.
\r
192 var script = new CKEDITOR.dom.element( 'script' );
\r
193 script.setAttribute( 'type', 'text/javascript' );
\r
194 script.appendText( code );
\r
196 // Append it to <head>.
\r
197 script.appendTo( CKEDITOR.document.getHead() );
\r