1 /*
  2  * CKEditor - The text editor for Internet - http://ckeditor.com
  3  * Copyright (C) 2003-2008 Frederico Caldeira Knabben
  4  *
  5  * == BEGIN LICENSE ==
  6  *
  7  * Licensed under the terms of any of the following licenses at your
  8  * choice:
  9  *
 10  *  - GNU General Public License Version 2 or later (the "GPL")
 11  *    http://www.gnu.org/licenses/gpl.html
 12  *
 13  *  - GNU Lesser General Public License Version 2.1 or later (the "LGPL")
 14  *    http://www.gnu.org/licenses/lgpl.html
 15  *
 16  *  - Mozilla Public License Version 1.1 or later (the "MPL")
 17  *    http://www.mozilla.org/MPL/MPL-1.1.html
 18  *
 19  * == END LICENSE ==
 20  */
 21
 22 /**
 23  * @fileOverview Defines the {@link CKEDITOR.scriptLoader} object, used to load scripts
 24  *		asynchronously.
 25  */
 26
 27 /**
 28  * Load scripts asynchronously.
 29  * @namespace
 30  * @example
 31  */
 32 CKEDITOR.scriptLoader = (function()
 33 {
 34 	var uniqueScripts = {};
 35
 36 	return /** @lends CKEDITOR.scriptLoader */ {
 37 		/**
 38 		 * Loads an external script checking if it hasn't been already loaded
 39 		 * previously by this function.
 40 		 * @param {String} scriptUrl The URL pointing to the external script to
 41 		 *		be loaded.
 42 		 * @param {Function} [callback] A function to be called when the script
 43 		 *		is loaded and executed
 44 		 * @param {Object} [scope] The scope ("this" reference) to be used for
 45 		 *		the callback call. Default to {@link CKEDITOR}.
 46 		 * @param {Boolean} [noCheck] Indicates that the script must be loaded
 47 		 *		anyway, not checking if it has already loaded.
 48 		 * @returns {Boolean} A boolean indicating that the script has been
 49 		 *		loaded. Returns false if it has already been loaded previously.
 50 		 * @example
 51 		 * CKEDITOR.scriptLoader.load( '/myscript.js' );
 52 		 * @example
 53 		 * CKEDITOR.scriptLoader.load( '/myscript.js', function( success )
 54 		 *     {
 55 		 *         // Alerts "true" if the script has been properly loaded.
 56 		 *         // HTTP error 404 should return "false".
 57 		 *         alert( success );
 58 		 *     });
 59 		 */
 60 		load : function( scriptUrl, callback, scope, noCheck )
 61 		{
 62 			if ( noCheck !== true )
 63 			{
 64 				if ( uniqueScripts[ scriptUrl ] )
 65 					return false;
 66
 67 				uniqueScripts[ scriptUrl ] = true;
 68 			}
 69
 70 			// Create the <script> element.
 71 			var script = new CKEDITOR.dom.element( 'script' );
 72 			script.setAttributes( {
 73 				type : 'text/javascript',
 74 				src : scriptUrl } );
 75
 76 			if ( callback )
 77 			{
 78 				if ( !scope )
 79 					scope = CKEDITOR;
 80
 81 				if ( CKEDITOR.env.ie )
 82 				{
 83 					// FIXME: For IE, we are not able to return false on error (like 404).
 84
 85 					/** @ignore */
 86 					script.$.onreadystatechange = function ()
 87 					{
 88 						if ( script.$.readyState == 'loaded' || script.$.readyState == 'complete' )
 89 						{
 90 							script.$.onreadystatechange = null;
 91 							callback.call( scope, true );
 92 						}
 93 					};
 94 				}
 95 				else
 96 				{
 97 					/** @ignore */
 98 					script.$.onload = function()
 99 					{
100 						callback.call( scope, true );
101 					};
102
103 					// FIXME: Opera and Safari will not fire onerror.
104
105 					/** @ignore */
106 					script.$.onerror = function()
107 					{
108 						callback.call( scope, false );
109 					};
110 				}
111 			}
112
113 			// Append it to <head>.
114 			script.appendTo( CKEDITOR.dom.element.getHead() );
115
116 			return true;
117 		},
118
119 		/**
120 		 * Executes a JavaScript code into the current document.
121 		 * @param {String} code The code to be executed.
122 		 * @type undefined
123 		 * @example
124 		 * CKEDITOR.scriptLoader.loadCode( 'var x = 10;' );
125 		 * alert( x );  // "10"
126 		 */
127 		loadCode : function( code )
128 		{
129 			// Create the <script> element.
130 			var script = new CKEDITOR.dom.element( 'script' );
131 			script.setAttribute( 'type', 'text/javascript' );
132 			script.appendText( code );
133
134 			// Append it to <head>.
135 			script.appendTo( CKEDITOR.dom.element.getHead() );
136 		}
137 	};
138 })();
139