Merge "Make ve.Factory require static name property"

This commit is contained in:
jenkins-bot 2013-10-03 22:03:26 +00:00 committed by Gerrit Code Review
commit c21c8556fc
22 changed files with 71 additions and 93 deletions

View file

@ -161,10 +161,6 @@
"ve.Range", "ve.Element", "ve.EventSequencer"
]
},
{
"name": "Factories",
"classes": ["ve.NamedClassFactory"]
},
{
"name": "Nodes",
"classes": ["ve.Node", "ve.BranchNode", "ve.LeafNode", "ve.Document"]

View file

@ -265,7 +265,6 @@ $wgResourceModules += array(
've/ve.Factory.js',
've/ve.Range.js',
've/ve.Node.js',
've/ve.NamedClassFactory.js',
've/ve.BranchNode.js',
've/ve.LeafNode.js',
've/ve.Element.js',

View file

@ -120,7 +120,6 @@ $html = file_get_contents( $page );
<script src="../../modules/ve/ve.Factory.js"></script>
<script src="../../modules/ve/ve.Range.js"></script>
<script src="../../modules/ve/ve.Node.js"></script>
<script src="../../modules/ve/ve.NamedClassFactory.js"></script>
<script src="../../modules/ve/ve.BranchNode.js"></script>
<script src="../../modules/ve/ve.LeafNode.js"></script>
<script src="../../modules/ve/ve.Element.js"></script>

View file

@ -9,17 +9,17 @@
* ContentEditable annotation factory.
*
* @class
* @extends ve.NamedClassFactory
* @extends ve.Factory
* @constructor
*/
ve.ce.AnnotationFactory = function VeCeAnnotationFactory() {
// Parent constructor
ve.NamedClassFactory.call( this );
ve.Factory.call( this );
};
/* Inheritance */
ve.inheritClass( ve.ce.AnnotationFactory, ve.NamedClassFactory );
ve.inheritClass( ve.ce.AnnotationFactory, ve.Factory );
/* Methods */

View file

@ -9,17 +9,17 @@
* ContentEditable node factory.
*
* @class
* @extends ve.NamedClassFactory
* @extends ve.Factory
* @constructor
*/
ve.ce.NodeFactory = function VeCeNodeFactory() {
// Parent constructor
ve.NamedClassFactory.call( this );
ve.Factory.call( this );
};
/* Inheritance */
ve.inheritClass( ve.ce.NodeFactory, ve.NamedClassFactory );
ve.inheritClass( ve.ce.NodeFactory, ve.Factory );
/* Methods */

View file

@ -9,17 +9,17 @@
* DataModel annotation factory.
*
* @class
* @extends ve.NamedClassFactory
* @extends ve.Factory
* @constructor
*/
ve.dm.AnnotationFactory = function VeDmAnnotationFactory() {
// Parent constructor
ve.NamedClassFactory.call( this );
ve.Factory.call( this );
};
/* Inheritance */
ve.inheritClass( ve.dm.AnnotationFactory, ve.NamedClassFactory );
ve.inheritClass( ve.dm.AnnotationFactory, ve.Factory );
/* Initialization */

View file

@ -9,17 +9,17 @@
* DataModel meta item factory.
*
* @class
* @extends ve.NamedClassFactory
* @extends ve.Factory
* @constructor
*/
ve.dm.MetaItemFactory = function VeDmMetaItemFactory() {
// Parent constructor
ve.NamedClassFactory.call( this );
ve.Factory.call( this );
};
/* Inheritance */
ve.inheritClass( ve.dm.MetaItemFactory, ve.NamedClassFactory );
ve.inheritClass( ve.dm.MetaItemFactory, ve.Factory );
/* Methods */

View file

@ -9,17 +9,17 @@
* DataModel node factory.
*
* @class
* @extends ve.NamedClassFactory
* @extends ve.Factory
* @constructor
*/
ve.dm.NodeFactory = function VeDmNodeFactory() {
// Parent constructor
ve.NamedClassFactory.call( this );
ve.Factory.call( this );
};
/* Inheritance */
ve.inheritClass( ve.dm.NodeFactory, ve.NamedClassFactory );
ve.inheritClass( ve.dm.NodeFactory, ve.Factory );
/* Methods */

View file

@ -65,7 +65,6 @@
<script src="../../ve/ve.Factory.js"></script>
<script src="../../ve/ve.Range.js"></script>
<script src="../../ve/ve.Node.js"></script>
<script src="../../ve/ve.NamedClassFactory.js"></script>
<script src="../../ve/ve.BranchNode.js"></script>
<script src="../../ve/ve.LeafNode.js"></script>
<script src="../../ve/ve.Element.js"></script>

View file

@ -16,21 +16,24 @@ ve.FactoryObjectStub = function VeFactoryObjectStub( a, b, c, d ) {
this.d = d;
};
ve.FactoryObjectStub.static = {};
ve.FactoryObjectStub.static.name = 'factory-object-stub';
/* Tests */
QUnit.test( 'register', 3, function ( assert ) {
QUnit.test( 'register', 2, function ( assert ) {
var factory = new ve.Factory();
assert.throws(
function () {
factory.register( 'factory-object-stub', 'not-a-function' );
factory.register( 'not-a-function' );
},
Error,
'Throws an exception when trying to register a non-function value as a constructor'
);
factory.register( ['factory-object-stub-1', 'factory-object-stub-2'], ve.FactoryObjectStub );
assert.strictEqual( factory.lookup( 'factory-object-stub-1' ), ve.FactoryObjectStub );
assert.strictEqual( factory.lookup( 'factory-object-stub-2' ), ve.FactoryObjectStub );
factory.register( ve.FactoryObjectStub );
assert.strictEqual( factory.lookup( 'factory-object-stub' ), ve.FactoryObjectStub );
} );
QUnit.test( 'create', 3, function ( assert ) {
@ -45,7 +48,7 @@ QUnit.test( 'create', 3, function ( assert ) {
'Throws an exception when trying to create a object of an unregistered type'
);
factory.register( 'factory-object-stub', ve.FactoryObjectStub );
factory.register( ve.FactoryObjectStub );
obj = factory.create( 'factory-object-stub', 16, 'foo', { 'baz': 'quux' }, 5 );
@ -64,6 +67,6 @@ QUnit.test( 'create', 3, function ( assert ) {
QUnit.test( 'lookup', 1, function ( assert ) {
var factory = new ve.Factory();
factory.register( 'factory-object-stub', ve.FactoryObjectStub );
factory.register( ve.FactoryObjectStub );
assert.strictEqual( factory.lookup( 'factory-object-stub' ), ve.FactoryObjectStub );
} );

View file

@ -24,6 +24,8 @@ ve.inheritClass( ve.ui.AnnotationAction, ve.ui.Action );
/* Static Properties */
ve.ui.AnnotationAction.static.name = 'annotation';
/**
* List of allowed methods for the action.
*
@ -109,4 +111,4 @@ ve.ui.AnnotationAction.prototype.clearAll = function () {
/* Registration */
ve.ui.actionFactory.register( 'annotation', ve.ui.AnnotationAction );
ve.ui.actionFactory.register( ve.ui.AnnotationAction );

View file

@ -24,6 +24,8 @@ ve.inheritClass( ve.ui.ContentAction, ve.ui.Action );
/* Static Properties */
ve.ui.ContentAction.static.name = 'content';
/**
* List of allowed methods for the action.
*
@ -66,4 +68,4 @@ ve.ui.ContentAction.prototype.select = function ( range ) {
/* Registration */
ve.ui.actionFactory.register( 'content', ve.ui.ContentAction );
ve.ui.actionFactory.register( ve.ui.ContentAction );

View file

@ -24,6 +24,8 @@ ve.inheritClass( ve.ui.FormatAction, ve.ui.Action );
/* Static Properties */
ve.ui.FormatAction.static.name = 'format';
/**
* List of allowed methods for this action.
*
@ -74,4 +76,4 @@ ve.ui.FormatAction.prototype.convert = function ( type, attributes ) {
/* Registration */
ve.ui.actionFactory.register( 'format', ve.ui.FormatAction );
ve.ui.actionFactory.register( ve.ui.FormatAction );

View file

@ -24,6 +24,8 @@ ve.inheritClass( ve.ui.HistoryAction, ve.ui.Action );
/* Static Properties */
ve.ui.HistoryAction.static.name = 'history';
/**
* List of allowed methods for the action.
*
@ -60,4 +62,4 @@ ve.ui.HistoryAction.prototype.redo = function () {
/* Registration */
ve.ui.actionFactory.register( 'history', ve.ui.HistoryAction );
ve.ui.actionFactory.register( ve.ui.HistoryAction );

View file

@ -24,6 +24,8 @@ ve.inheritClass( ve.ui.IndentationAction, ve.ui.Action );
/* Static Properties */
ve.ui.IndentationAction.static.name = 'indentation';
/**
* List of allowed methods for the action.
*
@ -312,4 +314,4 @@ ve.ui.IndentationAction.prototype.unindentListItem = function ( listItem ) {
/* Registration */
ve.ui.actionFactory.register( 'indentation', ve.ui.IndentationAction );
ve.ui.actionFactory.register( ve.ui.IndentationAction );

View file

@ -24,6 +24,8 @@ ve.inheritClass( ve.ui.InspectorAction, ve.ui.Action );
/* Static Properties */
ve.ui.InspectorAction.static.name = 'inspector';
/**
* List of allowed methods for the action.
*
@ -46,4 +48,4 @@ ve.ui.InspectorAction.prototype.open = function ( name ) {
/* Registration */
ve.ui.actionFactory.register( 'inspector', ve.ui.InspectorAction );
ve.ui.actionFactory.register( ve.ui.InspectorAction );

View file

@ -24,6 +24,8 @@ ve.inheritClass( ve.ui.ListAction, ve.ui.Action );
/* Static Properties */
ve.ui.ListAction.static.name = 'list';
/**
* List of allowed methods for the action.
*
@ -143,4 +145,4 @@ ve.ui.ListAction.prototype.unwrap = function () {
/* Registration */
ve.ui.actionFactory.register( 'list', ve.ui.ListAction );
ve.ui.actionFactory.register( ve.ui.ListAction );

View file

@ -9,17 +9,17 @@
* UserInterface Dialog factory.
*
* @class
* @extends ve.NamedClassFactory
* @extends ve.Factory
* @constructor
*/
ve.ui.DialogFactory = function VeUiDialogFactory() {
// Parent constructor
ve.NamedClassFactory.call( this );
ve.Factory.call( this );
};
/* Inheritance */
ve.inheritClass( ve.ui.DialogFactory, ve.NamedClassFactory );
ve.inheritClass( ve.ui.DialogFactory, ve.Factory );
/* Initialization */

View file

@ -9,17 +9,17 @@
* UserInterface inspector factory.
*
* @class
* @extends ve.NamedClassFactory
* @extends ve.Factory
* @constructor
*/
ve.ui.InspectorFactory = function VeUiInspectorFactory() {
// Parent constructor
ve.NamedClassFactory.call( this );
ve.Factory.call( this );
};
/* Inheritance */
ve.inheritClass( ve.ui.InspectorFactory, ve.NamedClassFactory );
ve.inheritClass( ve.ui.InspectorFactory, ve.Factory );
/* Initialization */

View file

@ -9,17 +9,17 @@
* UserInterface tool factory.
*
* @class
* @extends ve.NamedClassFactory
* @extends ve.Factory
* @constructor
*/
ve.ui.ToolFactory = function VeUiToolFactory() {
// Parent constructor
ve.NamedClassFactory.call( this );
ve.Factory.call( this );
};
/* Inheritance */
ve.inheritClass( ve.ui.ToolFactory, ve.NamedClassFactory );
ve.inheritClass( ve.ui.ToolFactory, ve.Factory );
/* Methods */

View file

@ -29,24 +29,31 @@ ve.inheritClass( ve.Factory, ve.Registry );
/**
* Register a constructor with the factory.
*
* Classes must have a static `name` property to be registered.
*
* @example
* function MyClass() {};
* // Adds a static property to the class defining a symbolic name
* MyClass.static = { 'name': 'mine' };
* // Registers class with factory, available via symbolic name 'mine'
* factory.register( MyClass );
*
* @method
* @param {string|string[]} name Symbolic name or list of symbolic names
* @param {Function} constructor Constructor to use when creating object
* @throws {Error} Name must be a string and must not be empty
* @throws {Error} Constructor must be a function
*/
ve.Factory.prototype.register = function ( name, constructor ) {
var i, len;
ve.Factory.prototype.register = function ( constructor ) {
var name;
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] );
}
name = constructor.static && constructor.static.name;
if ( typeof name !== 'string' || name === '' ) {
throw new Error( 'Name must be a string and must not be empty' );
}
this.entries.push( name );
ve.Registry.prototype.register.call( this, name, constructor );
};

View file

@ -1,39 +0,0 @@
/*!
* VisualEditor NamedClassFactory class.
*
* @copyright 2011-2013 VisualEditor Team and others; see AUTHORS.txt
* @license The MIT License (MIT); see LICENSE.txt
*/
/**
* Generic factory for classes with a .static.name property.
*
* @abstract
* @extends ve.Factory
* @constructor
*/
ve.NamedClassFactory = function VeNamedClassFactory() {
// Parent constructor
ve.Factory.call( this );
};
/* Inheritance */
ve.inheritClass( ve.NamedClassFactory, ve.Factory );
/* Methods */
/**
* Register a constructor with the factory.
*
* @method
* @param {Function} constructor Constructor to use when creating object
* @throws {Error} Names must be strings and must not be empty
*/
ve.NamedClassFactory.prototype.register = function ( constructor ) {
var name = constructor.static && constructor.static.name;
if ( typeof name !== 'string' || name === '' ) {
throw new Error( 'Names must be strings and must not be empty' );
}
ve.Factory.prototype.register.call( this, name, constructor );
};