2012-07-19 00:11:26 +00:00
|
|
|
/**
|
|
|
|
* VisualEditor Factory class.
|
2012-07-19 21:25:16 +00:00
|
|
|
*
|
2012-07-19 00:11:26 +00:00
|
|
|
* @copyright 2011-2012 VisualEditor Team and others; see AUTHORS.txt
|
|
|
|
* @license The MIT License (MIT); see LICENSE.txt
|
|
|
|
*/
|
|
|
|
|
2012-04-20 21:44:41 +00:00
|
|
|
/**
|
2012-05-31 23:50:16 +00:00
|
|
|
* Generic object factory.
|
2012-05-14 22:05:09 +00:00
|
|
|
*
|
2012-04-20 21:44:41 +00:00
|
|
|
* @class
|
2012-05-31 23:50:16 +00:00
|
|
|
* @abstract
|
2012-04-20 21:44:41 +00:00
|
|
|
* @constructor
|
2012-05-31 23:50:16 +00:00
|
|
|
* @extends {ve.EventEmitter}
|
2012-04-20 21:44:41 +00:00
|
|
|
*/
|
2012-08-07 01:50:44 +00:00
|
|
|
ve.Factory = function () {
|
2012-05-31 23:50:16 +00:00
|
|
|
// Inheritance
|
|
|
|
ve.EventEmitter.call( this );
|
|
|
|
|
|
|
|
// Properties
|
2012-04-23 18:46:13 +00:00
|
|
|
this.registry = [];
|
2012-04-20 21:44:41 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Methods */
|
|
|
|
|
2012-04-23 18:46:13 +00:00
|
|
|
/**
|
2012-05-31 23:50:16 +00:00
|
|
|
* Register a constructor with the factory.
|
2012-05-14 22:05:09 +00:00
|
|
|
*
|
2012-04-30 22:42:36 +00:00
|
|
|
* Arguments will be passed through directly to the constructor.
|
2012-05-31 23:50:16 +00:00
|
|
|
* @see {ve.Factory.prototype.create}
|
2012-05-14 22:05:09 +00:00
|
|
|
*
|
2012-05-04 20:31:14 +00:00
|
|
|
* @method
|
2012-05-31 23:50:16 +00:00
|
|
|
* @param {String} type Object type
|
|
|
|
* @param {Function} constructor Constructor to use when creating object
|
2012-04-30 22:42:36 +00:00
|
|
|
* @throws 'Constructor must be a function, cannot be a string'
|
2012-04-23 18:46:13 +00:00
|
|
|
*/
|
2012-08-07 01:50:44 +00:00
|
|
|
ve.Factory.prototype.register = function ( type, constructor ) {
|
2012-04-23 18:46:13 +00:00
|
|
|
if ( typeof constructor !== 'function' ) {
|
2012-08-08 17:48:53 +00:00
|
|
|
throw new Error( 'Constructor must be a function, cannot be a ' + typeof constructor );
|
2012-04-23 18:46:13 +00:00
|
|
|
}
|
|
|
|
this.registry[type] = constructor;
|
2012-05-31 23:50:16 +00:00
|
|
|
this.emit( 'register', type, constructor );
|
2012-04-23 18:46:13 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2012-05-31 23:50:16 +00:00
|
|
|
* Create an object based on a type.
|
2012-05-14 22:05:09 +00:00
|
|
|
*
|
2012-04-30 22:42:36 +00:00
|
|
|
* Type 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.
|
2012-05-14 22:05:09 +00:00
|
|
|
*
|
2012-08-16 17:53:33 +00:00
|
|
|
* WARNING: JavaScript does not allow using new and .apply together, so we just pass through 3
|
2012-04-30 22:42:36 +00:00
|
|
|
* arguments here since we know we only need that many at this time. If we need more in the future
|
|
|
|
* we should change this to suit that use case. Because of undefined pass through, there's no harm
|
|
|
|
* in adding more.
|
2012-05-14 22:05:09 +00:00
|
|
|
*
|
2012-05-04 20:31:14 +00:00
|
|
|
* @method
|
2012-05-31 23:50:16 +00:00
|
|
|
* @param {String} type Object type
|
2012-08-16 17:53:33 +00:00
|
|
|
* @param {Mixed} [...] Up to 3 additional arguments to pass through to the constructor
|
2012-05-31 23:50:16 +00:00
|
|
|
* @returns {Object} The new object
|
|
|
|
* @throws 'Unknown object type'
|
2012-04-23 18:46:13 +00:00
|
|
|
*/
|
2012-08-16 17:53:33 +00:00
|
|
|
ve.Factory.prototype.create = function ( type, a, b, c ) {
|
2012-04-23 18:46:13 +00:00
|
|
|
if ( type in this.registry ) {
|
2012-08-16 17:53:33 +00:00
|
|
|
return new this.registry[type]( a, b, c );
|
2012-04-23 18:46:13 +00:00
|
|
|
}
|
2012-08-08 17:48:53 +00:00
|
|
|
throw new Error( 'Unknown object type: ' + type );
|
2012-04-20 21:44:41 +00:00
|
|
|
};
|
2012-04-30 23:25:44 +00:00
|
|
|
|
2012-05-04 20:31:14 +00:00
|
|
|
/**
|
|
|
|
* Gets a constructor for a given type.
|
2012-05-14 22:05:09 +00:00
|
|
|
*
|
2012-05-04 20:31:14 +00:00
|
|
|
* @method
|
2012-05-31 23:50:16 +00:00
|
|
|
* @param {String} type Object type
|
2012-05-04 20:31:14 +00:00
|
|
|
* @returns {Function|undefined} Constructor for type
|
|
|
|
*/
|
2012-08-07 01:50:44 +00:00
|
|
|
ve.Factory.prototype.lookup = function ( type ) {
|
2012-05-04 20:31:14 +00:00
|
|
|
return this.registry[type];
|
|
|
|
};
|
2012-05-31 23:50:16 +00:00
|
|
|
|
|
|
|
/* Inheritance */
|
|
|
|
|
|
|
|
ve.extendClass( ve.Factory, ve.EventEmitter );
|