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