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