mediawiki-extensions-Visual.../modules/ve/ve.Factory.js
Trevor Parscal 8dfbc5baa5 Make tools generic and add fancy tool groups
Objectives:

* Got rid of mw prefixing in tools, inspectors and dialogs
* Simplify tool classes so they can be generically used as items in bars, lists and menus
* Add support for a catch-all toolbar group
* Simplify tool registration, leaning on tool classes' static name property
* Move default commands to command registry
* Move default triggers to trigger registry
* Get language tool working in standalone

Change-Id: Ic97a636f9a193374728629931b6702bee1b3416a
2013-09-03 11:27:39 -07:00

85 lines
2.4 KiB
JavaScript

/*!
* VisualEditor Factory class.
*
* @copyright 2011-2013 VisualEditor Team and others; see AUTHORS.txt
* @license The MIT License (MIT); see LICENSE.txt
*/
/**
* Generic object factory.
*
* @abstract
* @extends ve.Registry
* @constructor
*/
ve.Factory = function VeFactory() {
// Parent constructor
ve.Registry.call( this );
// Properties
this.entries = [];
};
/* Inheritance */
ve.inheritClass( ve.Factory, ve.Registry );
/* Methods */
/**
* Register a constructor with the factory.
*
* @method
* @param {string|string[]} name Symbolic name or list of symbolic names
* @param {Function} constructor Constructor to use when creating object
* @throws {Error} Constructor must be a function
*/
ve.Factory.prototype.register = function ( name, constructor ) {
var i, len;
if ( typeof constructor !== 'function' ) {
throw new Error( 'constructor must be a function, cannot be a ' + typeof constructor );
}
if ( typeof name === 'string' ) {
this.entries.push( name );
} else if ( ve.isArray( name ) ) {
for ( i = 0, len = name.length; i < len; i++ ) {
this.entries.push( name[i] );
}
}
ve.Registry.prototype.register.call( this, name, constructor );
};
/**
* Create an object based on a name.
*
* Name is used to look up the constructor to use, while all additional arguments are passed to the
* constructor directly, so leaving one out will pass an undefined to the constructor.
*
* @method
* @param {string} name Object name
* @param {Mixed...} [args] Arguments to pass to the constructor
* @returns {Object} The new object
* @throws {Error} Unknown object name
*/
ve.Factory.prototype.create = function ( name ) {
var args, obj, constructor;
constructor = this.registry[name];
if ( constructor === undefined ) {
throw new Error( 'No class registered by that name: ' + name );
}
// Convert arguments to array and shift the first argument (name) off
args = Array.prototype.slice.call( arguments, 1 );
// We can't use the "new" operator with .apply directly because apply needs a
// context. So instead just do what "new" does: create an object that inherits from
// the constructor's prototype (which also makes it an "instanceof" the constructor),
// then invoke the constructor with the object as context, and return it (ignoring
// the constructor's return value).
obj = ve.createObject( constructor.prototype );
constructor.apply( obj, args );
return obj;
};