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