2 Copyright (c) 2003-2010, CKSource - Frederico Knabben. All rights reserved.
\r
3 For licensing, see LICENSE.html or http://ckeditor.com/license
\r
7 * @fileOverview Defines the {@link CKEDITOR.resourceManager} class, which is
\r
8 * the base for resource managers, like plugins and themes.
\r
12 * Base class for resource managers, like plugins and themes. This class is not
\r
13 * intended to be used out of the CKEditor core code.
\r
14 * @param {String} basePath The path for the resources folder.
\r
15 * @param {String} fileName The name used for resource files.
\r
19 CKEDITOR.resourceManager = function( basePath, fileName )
\r
22 * The base directory containing all resources.
\r
23 * @name CKEDITOR.resourceManager.prototype.basePath
\r
27 this.basePath = basePath;
\r
30 * The name used for resource files.
\r
31 * @name CKEDITOR.resourceManager.prototype.fileName
\r
35 this.fileName = fileName;
\r
38 * Contains references to all resources that have already been registered
\r
39 * with {@link #add}.
\r
40 * @name CKEDITOR.resourceManager.prototype.registered
\r
44 this.registered = {};
\r
47 * Contains references to all resources that have already been loaded
\r
48 * with {@link #load}.
\r
49 * @name CKEDITOR.resourceManager.prototype.loaded
\r
56 * Contains references to all resources that have already been registered
\r
57 * with {@link #addExternal}.
\r
58 * @name CKEDITOR.resourceManager.prototype.externals
\r
62 this.externals = {};
\r
69 // List of callbacks waiting for plugins to be loaded.
\r
74 CKEDITOR.resourceManager.prototype =
\r
77 * Registers a resource.
\r
78 * @param {String} name The resource name.
\r
79 * @param {Object} [definition] The resource definition.
\r
81 * CKEDITOR.plugins.add( 'sample', { ... plugin definition ... } );
\r
82 * @see CKEDITOR.pluginDefinition
\r
84 add : function( name, definition )
\r
86 if ( this.registered[ name ] )
\r
87 throw '[CKEDITOR.resourceManager.add] The resource name "' + name + '" is already registered.';
\r
89 CKEDITOR.fire( name + CKEDITOR.tools.capitalize( this.fileName ) + 'Ready',
\r
90 this.registered[ name ] = definition || {} );
\r
94 * Gets the definition of a specific resource.
\r
95 * @param {String} name The resource name.
\r
98 * var definition = <b>CKEDITOR.plugins.get( 'sample' )</b>;
\r
100 get : function( name )
\r
102 return this.registered[ name ] || null;
\r
106 * Get the folder path for a specific loaded resource.
\r
107 * @param {String} name The resource name.
\r
110 * alert( <b>CKEDITOR.plugins.getPath( 'sample' )</b> ); // "<editor path>/plugins/sample/"
\r
112 getPath : function( name )
\r
114 var external = this.externals[ name ];
\r
115 return CKEDITOR.getUrl( ( external && external.dir ) || this.basePath + name + '/' );
\r
119 * Get the file path for a specific loaded resource.
\r
120 * @param {String} name The resource name.
\r
123 * alert( <b>CKEDITOR.plugins.getFilePath( 'sample' )</b> ); // "<editor path>/plugins/sample/plugin.js"
\r
125 getFilePath : function( name )
\r
127 var external = this.externals[ name ];
\r
128 return CKEDITOR.getUrl(
\r
129 this.getPath( name ) +
\r
130 ( ( external && ( typeof external.file == 'string' ) ) ? external.file : this.fileName + '.js' ) );
\r
134 * Registers one or more resources to be loaded from an external path
\r
135 * instead of the core base path.
\r
136 * @param {String} names The resource names, separated by commas.
\r
137 * @param {String} path The path of the folder containing the resource.
\r
138 * @param {String} [fileName] The resource file name. If not provided, the
\r
139 * default name is used; If provided with a empty string, will implicitly indicates that {@param path}
\r
140 * is already the full path.
\r
142 * // Loads a plugin from '/myplugin/samples/plugin.js'.
\r
143 * CKEDITOR.plugins.addExternal( 'sample', '/myplugins/sample/' );
\r
145 * // Loads a plugin from '/myplugin/samples/my_plugin.js'.
\r
146 * CKEDITOR.plugins.addExternal( 'sample', '/myplugins/sample/', 'my_plugin.js' );
\r
148 * // Loads a plugin from '/myplugin/samples/my_plugin.js'.
\r
149 * CKEDITOR.plugins.addExternal( 'sample', '/myplugins/sample/my_plugin.js', '' );
\r
151 addExternal : function( names, path, fileName )
\r
153 names = names.split( ',' );
\r
154 for ( var i = 0 ; i < names.length ; i++ )
\r
156 var name = names[ i ];
\r
158 this.externals[ name ] =
\r
167 * Loads one or more resources.
\r
168 * @param {String|Array} name The name of the resource to load. It may be a
\r
169 * string with a single resource name, or an array with several names.
\r
170 * @param {Function} callback A function to be called when all resources
\r
171 * are loaded. The callback will receive an array containing all
\r
173 * @param {Object} [scope] The scope object to be used for the callback
\r
176 * <b>CKEDITOR.plugins.load</b>( 'myplugin', function( plugins )
\r
178 * alert( plugins['myplugin'] ); // "object"
\r
181 load : function( names, callback, scope )
\r
183 // Ensure that we have an array of names.
\r
184 if ( !CKEDITOR.tools.isArray( names ) )
\r
185 names = names ? [ names ] : [];
\r
187 var loaded = this.loaded,
\r
188 registered = this.registered,
\r
193 // Loop through all names.
\r
194 for ( var i = 0 ; i < names.length ; i++ )
\r
196 var name = names[ i ];
\r
201 // If not available yet.
\r
202 if ( !loaded[ name ] && !registered[ name ] )
\r
204 var url = this.getFilePath( name );
\r
206 if ( !( url in urlsNames ) )
\r
207 urlsNames[ url ] = [];
\r
208 urlsNames[ url ].push( name );
\r
211 resources[ name ] = this.get( name );
\r
214 CKEDITOR.scriptLoader.load( urls, function( completed, failed )
\r
216 if ( failed.length )
\r
218 throw '[CKEDITOR.resourceManager.load] Resource name "' + urlsNames[ failed[ 0 ] ].join( ',' )
\r
219 + '" was not found at "' + failed[ 0 ] + '".';
\r
222 for ( var i = 0 ; i < completed.length ; i++ )
\r
224 var nameList = urlsNames[ completed[ i ] ];
\r
225 for ( var j = 0 ; j < nameList.length ; j++ )
\r
227 var name = nameList[ j ];
\r
228 resources[ name ] = this.get( name );
\r
230 loaded[ name ] = 1;
\r
234 callback.call( scope, resources );
\r