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