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.loader} objects, which is used to
 24  *		load core scripts and their dependencies from _source.
 25  */
 26
 27 if ( typeof CKEDITOR == 'undefined' )
 28 	CKEDITOR = {};
 29
 30 /**
 31  * Load core scripts and their dependencies from _source.
 32  * @namespace
 33  * @example
 34  */
 35 CKEDITOR.loader = (function()
 36 {
 37 	// Table of script names and their dependencies.
 38 	var scripts =
 39 	{
 40 		'lang/en'				: [],	// TODO: Remove me
 41
 42 		'core/_bootstrap'		: [ 'core/config', 'core/ckeditor', 'core/plugins', 'core/scriptLoader', 'core/tools', /* The following are entries that we wnat to force loading to at the end to avoid dependence recursion */ 'core/dom/text' ],
 43 		'core/ajax'				: [ 'core/xml' ],
 44 		'core/ckeditor'			: [ 'core/ajax', 'core/ckeditor_basic', 'core/dom', 'core/dom/document', 'core/dom/element', 'core/editor', 'core/event', 'core/tools' ],
 45 		'core/ckeditor_base'	: [],
 46 		'core/ckeditor_basic'	: [ 'core/env', 'core/event' ],
 47 		'core/config'			: [ 'core/ckeditor_base' ],
 48 		'core/dom'				: [],
 49 		'core/dom/document'		: [ 'core/dom','core/dom/element', 'core/dom/window' ],
 50 		'core/dom/element'		: [ 'core/dom', 'core/dom/document', 'core/dom/node', 'core/tools' ],
 51 		'core/dom/node'			: [],
 52 		'core/dom/text'			: [ 'core/dom/node' ],
 53 		'core/dom/window'		: [],
 54 		'core/editor'			: [ 'core/config', 'core/event', 'core/plugins', 'core/skins', 'core/themes', 'core/tools' ],
 55 		'core/env'				: [],
 56 		'core/event'			: [],
 57 		'core/plugins'			: [ 'core/resourceManager' ],
 58 		'core/resourceManager'	: [ 'core/scriptLoader', 'core/tools' ],
 59 		'core/scriptLoader'		: [ 'core/dom/element', 'core/env' ],
 60 		'core/skins'			: [],
 61 		'core/themes'			: [ 'lang/en', 'core/resourceManager' ],
 62 		'core/tools'			: [ 'core/env' ],
 63 		'core/xml'				: [ 'core/env' ]
 64 	};
 65
 66 	var basePath = (function()
 67 	{
 68 		// This is a copy of CKEDITOR.basePath, but requires the script having
 69 		// "_source/core/loader.js".
 70 		if ( CKEDITOR && CKEDITOR.basePath )
 71 			return CKEDITOR.basePath;
 72
 73 		// Find out the editor directory path, based on its <script> tag.
 74 		var path = '';
 75 		var scripts = document.getElementsByTagName( 'script' );
 76
 77 		for ( var i = 0 ; i < scripts.length ; i++ )
 78 		{
 79 			var match = scripts[i].src.match( /(^|.*[\\\/])core\/loader.js(?:\?.*)?$/i );
 80
 81 			if ( match )
 82 			{
 83 				path = match[1];
 84 				break;
 85 			}
 86 		}
 87
 88 		// In IE (only) the script.src string is the raw valued entered in the
 89 		// HTML. Other browsers return the full resolved URL instead.
 90 		if ( path.indexOf('://') == -1 )
 91 		{
 92 			// Absolute path.
 93 			if ( path.indexOf( '/' ) === 0 )
 94 				path = location.href.match( /^.*?:\/\/[^\/]*/ )[0] + path;
 95 			// Relative path.
 96 			else
 97 				path = location.href.match( /^[^\?]*\// )[0] + path;
 98 		}
 99
100 		return path;
101 	})();
102
103 	/** @lends CKEDITOR.loader */
104 	return {
105 		/**
106 		 * The list of loaded scripts in their loading order.
107 		 * @type Array
108 		 * @example
109 		 * // Alert the loaded script names.
110 		 * alert( <b>CKEDITOR.loader.loadedScripts</b> );
111 		 */
112 		loadedScripts : [],
113
114 		/**
115 		 * Loads a specific script, including its dependencies. This is not a
116 		 * synchronous loading, which means that the code the be loaded will
117 		 * not necessarily be available after this call.
118 		 * @example
119 		 * CKEDITOR.loader.load( 'core/dom/element' );
120 		 */
121 		load : function( scriptName )
122 		{
123 			// Check if the script has already been loaded.
124 			if ( scriptName in this.loadedScripts )
125 				return;
126
127 			// Get the script dependencies list.
128 			var dependencies = scripts[ scriptName ];
129 			if ( !dependencies )
130 				throw 'The script name"' + scriptName + '" is not defined.';
131
132 			// Mark the script as loaded, even before really loading it, to
133 			// avoid cross references recursion.
134 			this.loadedScripts[ scriptName ] = true;
135
136 			// Load all dependencies first.
137 			for ( var i = 0 ; i < dependencies.length ; i++ )
138 				this.load( dependencies[ i ] );
139
140 			// Append this script to the list of loaded scripts.
141 			this.loadedScripts.push( scriptName );
142
143 			var scriptSrc = basePath + '_source/' + scriptName + '.js';
144
145 			// Append the <script> element to the DOM.
146 			if ( document.body )
147 			{
148 				var script = document.createElement( 'script' );
149 				script.type = 'text/javascript';
150 				script.src = scriptSrc;
151
152 				document.body.appendChild( script );
153 			}
154 			else
155 				document.write( '<script src="' + scriptSrc + '" type="text/javascript"><\/script>' );
156 		}
157 	};
158 })();
159
160 // Check if any script has been defined for autoload.
161 if ( CKEDITOR._autoLoad )
162 {
163 	CKEDITOR.loader.load( CKEDITOR._autoLoad );
164 	delete CKEDITOR._autoLoad;
165 }
166