2013-12-03 19:48:24 +00:00
|
|
|
|
/*!
|
2017-04-05 15:40:22 +00:00
|
|
|
|
* OOjs v2.0.0 optimised for jQuery
|
2013-12-13 01:28:50 +00:00
|
|
|
|
* https://www.mediawiki.org/wiki/OOjs
|
2013-12-03 19:48:24 +00:00
|
|
|
|
*
|
2017-04-05 15:40:22 +00:00
|
|
|
|
* Copyright 2011-2017 OOjs Team and other contributors.
|
2013-12-03 19:48:24 +00:00
|
|
|
|
* Released under the MIT license
|
2017-04-05 15:40:22 +00:00
|
|
|
|
* https://oojs.mit-license.org
|
2013-12-03 19:48:24 +00:00
|
|
|
|
*
|
2017-04-05 15:40:22 +00:00
|
|
|
|
* Date: 2017-04-05T02:18:04Z
|
2013-12-03 19:48:24 +00:00
|
|
|
|
*/
|
|
|
|
|
|
( function ( global ) {
|
|
|
|
|
|
|
|
|
|
|
|
'use strict';
|
2014-07-23 20:53:29 +00:00
|
|
|
|
|
2017-04-05 15:40:22 +00:00
|
|
|
|
/* exported toString */
|
2013-12-03 19:48:24 +00:00
|
|
|
|
var
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Namespace for all classes, static methods and static properties.
|
|
|
|
|
|
* @class OO
|
|
|
|
|
|
* @singleton
|
|
|
|
|
|
*/
|
|
|
|
|
|
oo = {},
|
2014-11-17 19:26:17 +00:00
|
|
|
|
// Optimisation: Local reference to Object.prototype.hasOwnProperty
|
2013-12-03 19:48:24 +00:00
|
|
|
|
hasOwn = oo.hasOwnProperty,
|
2017-04-05 15:40:22 +00:00
|
|
|
|
toString = oo.toString;
|
2013-12-03 19:48:24 +00:00
|
|
|
|
|
|
|
|
|
|
/* Class Methods */
|
|
|
|
|
|
|
2014-04-02 21:40:24 +00:00
|
|
|
|
/**
|
|
|
|
|
|
* Utility to initialize a class for OO inheritance.
|
|
|
|
|
|
*
|
|
|
|
|
|
* Currently this just initializes an empty static object.
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param {Function} fn
|
|
|
|
|
|
*/
|
|
|
|
|
|
oo.initClass = function ( fn ) {
|
|
|
|
|
|
fn.static = fn.static || {};
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2013-12-03 19:48:24 +00:00
|
|
|
|
/**
|
2014-08-31 21:26:08 +00:00
|
|
|
|
* Inherit from prototype to another using Object#create.
|
2013-12-03 19:48:24 +00:00
|
|
|
|
*
|
|
|
|
|
|
* Beware: This redefines the prototype, call before setting your prototypes.
|
2014-08-31 21:26:08 +00:00
|
|
|
|
*
|
2013-12-03 19:48:24 +00:00
|
|
|
|
* Beware: This redefines the prototype, can only be called once on a function.
|
2014-08-31 21:26:08 +00:00
|
|
|
|
* If called multiple times on the same function, the previous prototype is lost.
|
|
|
|
|
|
* This is how prototypal inheritance works, it can only be one straight chain
|
|
|
|
|
|
* (just like classical inheritance in PHP for example). If you need to work with
|
|
|
|
|
|
* multiple constructors consider storing an instance of the other constructor in a
|
|
|
|
|
|
* property instead, or perhaps use a mixin (see OO.mixinClass).
|
2013-12-03 19:48:24 +00:00
|
|
|
|
*
|
2014-02-15 01:54:19 +00:00
|
|
|
|
* function Thing() {}
|
|
|
|
|
|
* Thing.prototype.exists = function () {};
|
2013-12-03 19:48:24 +00:00
|
|
|
|
*
|
2014-02-15 01:54:19 +00:00
|
|
|
|
* function Person() {
|
2014-03-08 01:36:25 +00:00
|
|
|
|
* Person.super.apply( this, arguments );
|
2014-02-15 01:54:19 +00:00
|
|
|
|
* }
|
2014-03-08 01:36:25 +00:00
|
|
|
|
* OO.inheritClass( Person, Thing );
|
2014-02-15 01:54:19 +00:00
|
|
|
|
* Person.static.defaultEyeCount = 2;
|
|
|
|
|
|
* Person.prototype.walk = function () {};
|
2013-12-03 19:48:24 +00:00
|
|
|
|
*
|
2014-02-15 01:54:19 +00:00
|
|
|
|
* function Jumper() {
|
2014-03-08 01:36:25 +00:00
|
|
|
|
* Jumper.super.apply( this, arguments );
|
2014-02-15 01:54:19 +00:00
|
|
|
|
* }
|
|
|
|
|
|
* OO.inheritClass( Jumper, Person );
|
|
|
|
|
|
* Jumper.prototype.jump = function () {};
|
2013-12-03 19:48:24 +00:00
|
|
|
|
*
|
2014-02-15 01:54:19 +00:00
|
|
|
|
* Jumper.static.defaultEyeCount === 2;
|
|
|
|
|
|
* var x = new Jumper();
|
|
|
|
|
|
* x.jump();
|
|
|
|
|
|
* x.walk();
|
|
|
|
|
|
* x instanceof Thing && x instanceof Person && x instanceof Jumper;
|
2013-12-03 19:48:24 +00:00
|
|
|
|
*
|
|
|
|
|
|
* @param {Function} targetFn
|
|
|
|
|
|
* @param {Function} originFn
|
|
|
|
|
|
* @throws {Error} If target already inherits from origin
|
|
|
|
|
|
*/
|
|
|
|
|
|
oo.inheritClass = function ( targetFn, originFn ) {
|
2015-11-11 17:02:06 +00:00
|
|
|
|
var targetConstructor;
|
|
|
|
|
|
|
2017-04-05 15:40:22 +00:00
|
|
|
|
if ( !originFn ) {
|
|
|
|
|
|
throw new Error( 'inheritClass: Origin is not a function (actually ' + originFn + ')' );
|
|
|
|
|
|
}
|
2013-12-03 19:48:24 +00:00
|
|
|
|
if ( targetFn.prototype instanceof originFn ) {
|
2017-04-05 15:40:22 +00:00
|
|
|
|
throw new Error( 'inheritClass: Target already inherits from origin' );
|
2013-12-03 19:48:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2015-11-11 17:02:06 +00:00
|
|
|
|
targetConstructor = targetFn.prototype.constructor;
|
2013-12-03 19:48:24 +00:00
|
|
|
|
|
2014-06-26 23:55:15 +00:00
|
|
|
|
// Using ['super'] instead of .super because 'super' is not supported
|
|
|
|
|
|
// by IE 8 and below (bug 63303).
|
|
|
|
|
|
// Provide .parent as alias for code supporting older browsers which
|
|
|
|
|
|
// allows people to comply with their style guide.
|
2017-04-05 15:40:22 +00:00
|
|
|
|
// eslint-disable-next-line dot-notation
|
2015-11-11 17:02:06 +00:00
|
|
|
|
targetFn[ 'super' ] = targetFn.parent = originFn;
|
2014-06-26 23:55:15 +00:00
|
|
|
|
|
2017-04-05 15:40:22 +00:00
|
|
|
|
targetFn.prototype = Object.create( originFn.prototype, {
|
2013-12-03 19:48:24 +00:00
|
|
|
|
// Restore constructor property of targetFn
|
|
|
|
|
|
constructor: {
|
|
|
|
|
|
value: targetConstructor,
|
|
|
|
|
|
enumerable: false,
|
|
|
|
|
|
writable: true,
|
|
|
|
|
|
configurable: true
|
|
|
|
|
|
}
|
|
|
|
|
|
} );
|
|
|
|
|
|
|
|
|
|
|
|
// Extend static properties - always initialize both sides
|
2014-04-02 21:40:24 +00:00
|
|
|
|
oo.initClass( originFn );
|
2017-04-05 15:40:22 +00:00
|
|
|
|
targetFn.static = Object.create( originFn.static );
|
2013-12-03 19:48:24 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2014-08-31 21:26:08 +00:00
|
|
|
|
* Copy over *own* prototype properties of a mixin.
|
|
|
|
|
|
*
|
2013-12-03 19:48:24 +00:00
|
|
|
|
* The 'constructor' (whether implicit or explicit) is not copied over.
|
|
|
|
|
|
*
|
2015-04-29 01:24:59 +00:00
|
|
|
|
* This does not create inheritance to the origin. If you need inheritance,
|
|
|
|
|
|
* use OO.inheritClass instead.
|
2013-12-03 19:48:24 +00:00
|
|
|
|
*
|
|
|
|
|
|
* Beware: This can redefine a prototype property, call before setting your prototypes.
|
2014-08-31 21:26:08 +00:00
|
|
|
|
*
|
2015-04-29 01:24:59 +00:00
|
|
|
|
* Beware: Don't call before OO.inheritClass.
|
2013-12-03 19:48:24 +00:00
|
|
|
|
*
|
|
|
|
|
|
* function Foo() {}
|
|
|
|
|
|
* function Context() {}
|
|
|
|
|
|
*
|
|
|
|
|
|
* // Avoid repeating this code
|
|
|
|
|
|
* function ContextLazyLoad() {}
|
|
|
|
|
|
* ContextLazyLoad.prototype.getContext = function () {
|
|
|
|
|
|
* if ( !this.context ) {
|
|
|
|
|
|
* this.context = new Context();
|
|
|
|
|
|
* }
|
|
|
|
|
|
* return this.context;
|
|
|
|
|
|
* };
|
|
|
|
|
|
*
|
|
|
|
|
|
* function FooBar() {}
|
|
|
|
|
|
* OO.inheritClass( FooBar, Foo );
|
|
|
|
|
|
* OO.mixinClass( FooBar, ContextLazyLoad );
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param {Function} targetFn
|
|
|
|
|
|
* @param {Function} originFn
|
|
|
|
|
|
*/
|
|
|
|
|
|
oo.mixinClass = function ( targetFn, originFn ) {
|
|
|
|
|
|
var key;
|
|
|
|
|
|
|
2017-04-05 15:40:22 +00:00
|
|
|
|
if ( !originFn ) {
|
|
|
|
|
|
throw new Error( 'mixinClass: Origin is not a function (actually ' + originFn + ')' );
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2013-12-03 19:48:24 +00:00
|
|
|
|
// Copy prototype properties
|
|
|
|
|
|
for ( key in originFn.prototype ) {
|
|
|
|
|
|
if ( key !== 'constructor' && hasOwn.call( originFn.prototype, key ) ) {
|
2015-11-11 17:02:06 +00:00
|
|
|
|
targetFn.prototype[ key ] = originFn.prototype[ key ];
|
2013-12-03 19:48:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Copy static properties - always initialize both sides
|
2014-04-02 21:40:24 +00:00
|
|
|
|
oo.initClass( targetFn );
|
2013-12-03 19:48:24 +00:00
|
|
|
|
if ( originFn.static ) {
|
|
|
|
|
|
for ( key in originFn.static ) {
|
|
|
|
|
|
if ( hasOwn.call( originFn.static, key ) ) {
|
2015-11-11 17:02:06 +00:00
|
|
|
|
targetFn.static[ key ] = originFn.static[ key ];
|
2013-12-03 19:48:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
} else {
|
2014-04-02 21:40:24 +00:00
|
|
|
|
oo.initClass( originFn );
|
2013-12-03 19:48:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2017-04-05 15:40:22 +00:00
|
|
|
|
/**
|
|
|
|
|
|
* Test whether one class is a subclass of another, without instantiating it.
|
|
|
|
|
|
*
|
|
|
|
|
|
* Every class is considered a subclass of Object and of itself.
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param {Function} testFn The class to be tested
|
|
|
|
|
|
* @param {Function} baseFn The base class
|
|
|
|
|
|
* @return {boolean} Whether testFn is a subclass of baseFn (or equal to it)
|
|
|
|
|
|
*/
|
|
|
|
|
|
oo.isSubclass = function ( testFn, baseFn ) {
|
|
|
|
|
|
return testFn === baseFn || testFn.prototype instanceof baseFn;
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2013-12-03 19:48:24 +00:00
|
|
|
|
/* Object Methods */
|
|
|
|
|
|
|
2014-11-17 19:26:17 +00:00
|
|
|
|
/**
|
|
|
|
|
|
* Get a deeply nested property of an object using variadic arguments, protecting against
|
|
|
|
|
|
* undefined property errors.
|
|
|
|
|
|
*
|
2017-04-05 15:40:22 +00:00
|
|
|
|
* `quux = OO.getProp( obj, 'foo', 'bar', 'baz' );` is equivalent to `quux = obj.foo.bar.baz;`
|
2014-11-17 19:26:17 +00:00
|
|
|
|
* except that the former protects against JS errors if one of the intermediate properties
|
|
|
|
|
|
* is undefined. Instead of throwing an error, this function will return undefined in
|
|
|
|
|
|
* that case.
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param {Object} obj
|
2015-11-11 17:02:06 +00:00
|
|
|
|
* @param {...Mixed} [keys]
|
|
|
|
|
|
* @return {Object|undefined} obj[arguments[1]][arguments[2]].... or undefined
|
2014-11-17 19:26:17 +00:00
|
|
|
|
*/
|
|
|
|
|
|
oo.getProp = function ( obj ) {
|
|
|
|
|
|
var i,
|
|
|
|
|
|
retval = obj;
|
|
|
|
|
|
for ( i = 1; i < arguments.length; i++ ) {
|
|
|
|
|
|
if ( retval === undefined || retval === null ) {
|
|
|
|
|
|
// Trying to access a property of undefined or null causes an error
|
|
|
|
|
|
return undefined;
|
|
|
|
|
|
}
|
2015-11-11 17:02:06 +00:00
|
|
|
|
retval = retval[ arguments[ i ] ];
|
2014-11-17 19:26:17 +00:00
|
|
|
|
}
|
|
|
|
|
|
return retval;
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Set a deeply nested property of an object using variadic arguments, protecting against
|
|
|
|
|
|
* undefined property errors.
|
|
|
|
|
|
*
|
|
|
|
|
|
* `oo.setProp( obj, 'foo', 'bar', 'baz' );` is equivalent to `obj.foo.bar = baz;` except that
|
|
|
|
|
|
* the former protects against JS errors if one of the intermediate properties is
|
|
|
|
|
|
* undefined. Instead of throwing an error, undefined intermediate properties will be
|
|
|
|
|
|
* initialized to an empty object. If an intermediate property is not an object, or if obj itself
|
|
|
|
|
|
* is not an object, this function will silently abort.
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param {Object} obj
|
2015-11-11 17:02:06 +00:00
|
|
|
|
* @param {...Mixed} [keys]
|
2014-11-17 19:26:17 +00:00
|
|
|
|
* @param {Mixed} [value]
|
|
|
|
|
|
*/
|
|
|
|
|
|
oo.setProp = function ( obj ) {
|
|
|
|
|
|
var i,
|
|
|
|
|
|
prop = obj;
|
2017-04-05 15:40:22 +00:00
|
|
|
|
if ( Object( obj ) !== obj || arguments.length < 2 ) {
|
2014-11-17 19:26:17 +00:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
for ( i = 1; i < arguments.length - 2; i++ ) {
|
2015-11-11 17:02:06 +00:00
|
|
|
|
if ( prop[ arguments[ i ] ] === undefined ) {
|
|
|
|
|
|
prop[ arguments[ i ] ] = {};
|
2014-11-17 19:26:17 +00:00
|
|
|
|
}
|
2015-11-11 17:02:06 +00:00
|
|
|
|
if ( Object( prop[ arguments[ i ] ] ) !== prop[ arguments[ i ] ] ) {
|
2014-11-17 19:26:17 +00:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
2015-11-11 17:02:06 +00:00
|
|
|
|
prop = prop[ arguments[ i ] ];
|
2014-11-17 19:26:17 +00:00
|
|
|
|
}
|
2015-11-11 17:02:06 +00:00
|
|
|
|
prop[ arguments[ arguments.length - 2 ] ] = arguments[ arguments.length - 1 ];
|
2014-11-17 19:26:17 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
2017-04-05 15:40:22 +00:00
|
|
|
|
/**
|
|
|
|
|
|
* Delete a deeply nested property of an object using variadic arguments, protecting against
|
|
|
|
|
|
* undefined property errors, and deleting resulting empty objects.
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param {Object} obj
|
|
|
|
|
|
* @param {...Mixed} [keys]
|
|
|
|
|
|
*/
|
|
|
|
|
|
oo.deleteProp = function ( obj ) {
|
|
|
|
|
|
var i,
|
|
|
|
|
|
prop = obj,
|
|
|
|
|
|
props = [ prop ];
|
|
|
|
|
|
if ( Object( obj ) !== obj || arguments.length < 2 ) {
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
for ( i = 1; i < arguments.length - 1; i++ ) {
|
|
|
|
|
|
if ( prop[ arguments[ i ] ] === undefined || Object( prop[ arguments[ i ] ] ) !== prop[ arguments[ i ] ] ) {
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
prop = prop[ arguments[ i ] ];
|
|
|
|
|
|
props.push( prop );
|
|
|
|
|
|
}
|
|
|
|
|
|
delete prop[ arguments[ i ] ];
|
|
|
|
|
|
// Walk back through props removing any plain empty objects
|
|
|
|
|
|
while ( ( prop = props.pop() ) && oo.isPlainObject( prop ) && !Object.keys( prop ).length ) {
|
|
|
|
|
|
delete props[ props.length - 1 ][ arguments[ props.length ] ];
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2013-12-03 19:48:24 +00:00
|
|
|
|
/**
|
|
|
|
|
|
* Create a new object that is an instance of the same
|
|
|
|
|
|
* constructor as the input, inherits from the same object
|
|
|
|
|
|
* and contains the same own properties.
|
|
|
|
|
|
*
|
|
|
|
|
|
* This makes a shallow non-recursive copy of own properties.
|
|
|
|
|
|
* To create a recursive copy of plain objects, use #copy.
|
|
|
|
|
|
*
|
|
|
|
|
|
* var foo = new Person( mom, dad );
|
|
|
|
|
|
* foo.setAge( 21 );
|
|
|
|
|
|
* var foo2 = OO.cloneObject( foo );
|
|
|
|
|
|
* foo.setAge( 22 );
|
|
|
|
|
|
*
|
|
|
|
|
|
* // Then
|
|
|
|
|
|
* foo2 !== foo; // true
|
|
|
|
|
|
* foo2 instanceof Person; // true
|
|
|
|
|
|
* foo2.getAge(); // 21
|
|
|
|
|
|
* foo.getAge(); // 22
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param {Object} origin
|
|
|
|
|
|
* @return {Object} Clone of origin
|
|
|
|
|
|
*/
|
|
|
|
|
|
oo.cloneObject = function ( origin ) {
|
|
|
|
|
|
var key, r;
|
|
|
|
|
|
|
2017-04-05 15:40:22 +00:00
|
|
|
|
r = Object.create( origin.constructor.prototype );
|
2013-12-03 19:48:24 +00:00
|
|
|
|
|
|
|
|
|
|
for ( key in origin ) {
|
|
|
|
|
|
if ( hasOwn.call( origin, key ) ) {
|
2015-11-11 17:02:06 +00:00
|
|
|
|
r[ key ] = origin[ key ];
|
2013-12-03 19:48:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return r;
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2014-03-08 01:36:25 +00:00
|
|
|
|
* Get an array of all property values in an object.
|
2013-12-03 19:48:24 +00:00
|
|
|
|
*
|
2015-11-11 17:02:06 +00:00
|
|
|
|
* @param {Object} obj Object to get values from
|
2014-03-08 01:36:25 +00:00
|
|
|
|
* @return {Array} List of object values
|
2013-12-03 19:48:24 +00:00
|
|
|
|
*/
|
|
|
|
|
|
oo.getObjectValues = function ( obj ) {
|
|
|
|
|
|
var key, values;
|
|
|
|
|
|
|
|
|
|
|
|
if ( obj !== Object( obj ) ) {
|
|
|
|
|
|
throw new TypeError( 'Called on non-object' );
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
values = [];
|
|
|
|
|
|
for ( key in obj ) {
|
|
|
|
|
|
if ( hasOwn.call( obj, key ) ) {
|
2015-11-11 17:02:06 +00:00
|
|
|
|
values[ values.length ] = obj[ key ];
|
2013-12-03 19:48:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return values;
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2015-11-11 17:02:06 +00:00
|
|
|
|
/**
|
|
|
|
|
|
* Use binary search to locate an element in a sorted array.
|
|
|
|
|
|
*
|
|
|
|
|
|
* searchFunc is given an element from the array. `searchFunc(elem)` must return a number
|
|
|
|
|
|
* above 0 if the element we're searching for is to the right of (has a higher index than) elem,
|
|
|
|
|
|
* below 0 if it is to the left of elem, or zero if it's equal to elem.
|
|
|
|
|
|
*
|
|
|
|
|
|
* To search for a specific value with a comparator function (a `function cmp(a,b)` that returns
|
|
|
|
|
|
* above 0 if `a > b`, below 0 if `a < b`, and 0 if `a == b`), you can use
|
|
|
|
|
|
* `searchFunc = cmp.bind( null, value )`.
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param {Array} arr Array to search in
|
|
|
|
|
|
* @param {Function} searchFunc Search function
|
|
|
|
|
|
* @param {boolean} [forInsertion] If not found, return index where val could be inserted
|
|
|
|
|
|
* @return {number|null} Index where val was found, or null if not found
|
|
|
|
|
|
*/
|
|
|
|
|
|
oo.binarySearch = function ( arr, searchFunc, forInsertion ) {
|
|
|
|
|
|
var mid, cmpResult,
|
|
|
|
|
|
left = 0,
|
|
|
|
|
|
right = arr.length;
|
|
|
|
|
|
while ( left < right ) {
|
|
|
|
|
|
// Equivalent to Math.floor( ( left + right ) / 2 ) but much faster
|
2017-04-05 15:40:22 +00:00
|
|
|
|
// eslint-disable-next-line no-bitwise
|
2015-11-11 17:02:06 +00:00
|
|
|
|
mid = ( left + right ) >> 1;
|
|
|
|
|
|
cmpResult = searchFunc( arr[ mid ] );
|
|
|
|
|
|
if ( cmpResult < 0 ) {
|
|
|
|
|
|
right = mid;
|
|
|
|
|
|
} else if ( cmpResult > 0 ) {
|
|
|
|
|
|
left = mid + 1;
|
|
|
|
|
|
} else {
|
|
|
|
|
|
return mid;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return forInsertion ? right : null;
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2013-12-03 19:48:24 +00:00
|
|
|
|
/**
|
2014-08-31 21:26:08 +00:00
|
|
|
|
* Recursively compare properties between two objects.
|
2013-12-03 19:48:24 +00:00
|
|
|
|
*
|
|
|
|
|
|
* A false result may be caused by property inequality or by properties in one object missing from
|
|
|
|
|
|
* the other. An asymmetrical test may also be performed, which checks only that properties in the
|
|
|
|
|
|
* first object are present in the second object, but not the inverse.
|
|
|
|
|
|
*
|
2014-08-20 22:35:43 +00:00
|
|
|
|
* If either a or b is null or undefined it will be treated as an empty object.
|
|
|
|
|
|
*
|
2014-08-31 21:26:08 +00:00
|
|
|
|
* @param {Object|undefined|null} a First object to compare
|
|
|
|
|
|
* @param {Object|undefined|null} b Second object to compare
|
2014-11-17 19:26:17 +00:00
|
|
|
|
* @param {boolean} [asymmetrical] Whether to check only that a's values are equal to b's
|
|
|
|
|
|
* (i.e. a is a subset of b)
|
2014-03-08 01:36:25 +00:00
|
|
|
|
* @return {boolean} If the objects contain the same values as each other
|
2013-12-03 19:48:24 +00:00
|
|
|
|
*/
|
|
|
|
|
|
oo.compare = function ( a, b, asymmetrical ) {
|
|
|
|
|
|
var aValue, bValue, aType, bType, k;
|
|
|
|
|
|
|
|
|
|
|
|
if ( a === b ) {
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2014-08-20 22:35:43 +00:00
|
|
|
|
a = a || {};
|
|
|
|
|
|
b = b || {};
|
|
|
|
|
|
|
2015-02-26 01:59:38 +00:00
|
|
|
|
if ( typeof a.nodeType === 'number' && typeof a.isEqualNode === 'function' ) {
|
|
|
|
|
|
return a.isEqualNode( b );
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2013-12-03 19:48:24 +00:00
|
|
|
|
for ( k in a ) {
|
2015-11-11 17:02:06 +00:00
|
|
|
|
if ( !hasOwn.call( a, k ) || a[ k ] === undefined || a[ k ] === b[ k ] ) {
|
2014-11-17 19:26:17 +00:00
|
|
|
|
// Support es3-shim: Without the hasOwn filter, comparing [] to {} will be false in ES3
|
2014-06-26 23:55:15 +00:00
|
|
|
|
// because the shimmed "forEach" is enumerable and shows up in Array but not Object.
|
2014-11-17 19:26:17 +00:00
|
|
|
|
// Also ignore undefined values, because there is no conceptual difference between
|
|
|
|
|
|
// a key that is absent and a key that is present but whose value is undefined.
|
2014-06-26 23:55:15 +00:00
|
|
|
|
continue;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2015-11-11 17:02:06 +00:00
|
|
|
|
aValue = a[ k ];
|
|
|
|
|
|
bValue = b[ k ];
|
2013-12-03 19:48:24 +00:00
|
|
|
|
aType = typeof aValue;
|
|
|
|
|
|
bType = typeof bValue;
|
|
|
|
|
|
if ( aType !== bType ||
|
2014-09-11 00:43:02 +00:00
|
|
|
|
(
|
|
|
|
|
|
( aType === 'string' || aType === 'number' || aType === 'boolean' ) &&
|
|
|
|
|
|
aValue !== bValue
|
|
|
|
|
|
) ||
|
2015-02-26 01:59:38 +00:00
|
|
|
|
( aValue === Object( aValue ) && !oo.compare( aValue, bValue, true ) ) ) {
|
2013-12-03 19:48:24 +00:00
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
// If the check is not asymmetrical, recursing with the arguments swapped will verify our result
|
|
|
|
|
|
return asymmetrical ? true : oo.compare( b, a, true );
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Create a plain deep copy of any kind of object.
|
|
|
|
|
|
*
|
|
|
|
|
|
* Copies are deep, and will either be an object or an array depending on `source`.
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param {Object} source Object to copy
|
2014-08-20 22:35:43 +00:00
|
|
|
|
* @param {Function} [leafCallback] Applied to leaf values after they are cloned but before they are added to the clone
|
|
|
|
|
|
* @param {Function} [nodeCallback] Applied to all values before they are cloned. If the nodeCallback returns a value other than undefined, the returned value is used instead of attempting to clone.
|
2014-03-08 01:36:25 +00:00
|
|
|
|
* @return {Object} Copy of source object
|
2013-12-03 19:48:24 +00:00
|
|
|
|
*/
|
2014-08-20 22:35:43 +00:00
|
|
|
|
oo.copy = function ( source, leafCallback, nodeCallback ) {
|
|
|
|
|
|
var key, destination;
|
|
|
|
|
|
|
|
|
|
|
|
if ( nodeCallback ) {
|
|
|
|
|
|
// Extensibility: check before attempting to clone source.
|
|
|
|
|
|
destination = nodeCallback( source );
|
|
|
|
|
|
if ( destination !== undefined ) {
|
|
|
|
|
|
return destination;
|
|
|
|
|
|
}
|
2013-12-03 19:48:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2014-08-20 22:35:43 +00:00
|
|
|
|
if ( Array.isArray( source ) ) {
|
|
|
|
|
|
// Array (fall through)
|
|
|
|
|
|
destination = new Array( source.length );
|
|
|
|
|
|
} else if ( source && typeof source.clone === 'function' ) {
|
|
|
|
|
|
// Duck type object with custom clone method
|
|
|
|
|
|
return leafCallback ? leafCallback( source.clone() ) : source.clone();
|
|
|
|
|
|
} else if ( source && typeof source.cloneNode === 'function' ) {
|
|
|
|
|
|
// DOM Node
|
|
|
|
|
|
return leafCallback ?
|
|
|
|
|
|
leafCallback( source.cloneNode( true ) ) :
|
|
|
|
|
|
source.cloneNode( true );
|
|
|
|
|
|
} else if ( oo.isPlainObject( source ) ) {
|
|
|
|
|
|
// Plain objects (fall through)
|
|
|
|
|
|
destination = {};
|
|
|
|
|
|
} else {
|
|
|
|
|
|
// Non-plain objects (incl. functions) and primitive values
|
|
|
|
|
|
return leafCallback ? leafCallback( source ) : source;
|
|
|
|
|
|
}
|
2013-12-03 19:48:24 +00:00
|
|
|
|
|
2014-08-20 22:35:43 +00:00
|
|
|
|
// source is an array or a plain object
|
2013-12-03 19:48:24 +00:00
|
|
|
|
for ( key in source ) {
|
2015-11-11 17:02:06 +00:00
|
|
|
|
destination[ key ] = oo.copy( source[ key ], leafCallback, nodeCallback );
|
2013-12-03 19:48:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2014-08-20 22:35:43 +00:00
|
|
|
|
// This is an internal node, so we don't apply the leafCallback.
|
2013-12-03 19:48:24 +00:00
|
|
|
|
return destination;
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2014-03-08 01:36:25 +00:00
|
|
|
|
* Generate a hash of an object based on its name and data.
|
|
|
|
|
|
*
|
|
|
|
|
|
* Performance optimization: <http://jsperf.com/ve-gethash-201208#/toJson_fnReplacerIfAoForElse>
|
2013-12-03 19:48:24 +00:00
|
|
|
|
*
|
|
|
|
|
|
* To avoid two objects with the same values generating different hashes, we utilize the replacer
|
|
|
|
|
|
* argument of JSON.stringify and sort the object by key as it's being serialized. This may or may
|
|
|
|
|
|
* not be the fastest way to do this; we should investigate this further.
|
|
|
|
|
|
*
|
|
|
|
|
|
* Objects and arrays are hashed recursively. When hashing an object that has a .getHash()
|
|
|
|
|
|
* function, we call that function and use its return value rather than hashing the object
|
|
|
|
|
|
* ourselves. This allows classes to define custom hashing.
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param {Object} val Object to generate hash for
|
2014-03-08 01:36:25 +00:00
|
|
|
|
* @return {string} Hash of object
|
2013-12-03 19:48:24 +00:00
|
|
|
|
*/
|
|
|
|
|
|
oo.getHash = function ( val ) {
|
|
|
|
|
|
return JSON.stringify( val, oo.getHash.keySortReplacer );
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2014-08-31 21:26:08 +00:00
|
|
|
|
* Sort objects by key (helper function for OO.getHash).
|
2013-12-03 19:48:24 +00:00
|
|
|
|
*
|
|
|
|
|
|
* This is a callback passed into JSON.stringify.
|
|
|
|
|
|
*
|
2014-03-08 01:36:25 +00:00
|
|
|
|
* @method getHash_keySortReplacer
|
2013-12-03 19:48:24 +00:00
|
|
|
|
* @param {string} key Property name of value being replaced
|
|
|
|
|
|
* @param {Mixed} val Property value to replace
|
2014-03-08 01:36:25 +00:00
|
|
|
|
* @return {Mixed} Replacement value
|
2013-12-03 19:48:24 +00:00
|
|
|
|
*/
|
|
|
|
|
|
oo.getHash.keySortReplacer = function ( key, val ) {
|
|
|
|
|
|
var normalized, keys, i, len;
|
|
|
|
|
|
if ( val && typeof val.getHashObject === 'function' ) {
|
|
|
|
|
|
// This object has its own custom hash function, use it
|
|
|
|
|
|
val = val.getHashObject();
|
|
|
|
|
|
}
|
|
|
|
|
|
if ( !Array.isArray( val ) && Object( val ) === val ) {
|
|
|
|
|
|
// Only normalize objects when the key-order is ambiguous
|
|
|
|
|
|
// (e.g. any object not an array).
|
|
|
|
|
|
normalized = {};
|
|
|
|
|
|
keys = Object.keys( val ).sort();
|
|
|
|
|
|
i = 0;
|
|
|
|
|
|
len = keys.length;
|
|
|
|
|
|
for ( ; i < len; i += 1 ) {
|
2015-11-11 17:02:06 +00:00
|
|
|
|
normalized[ keys[ i ] ] = val[ keys[ i ] ];
|
2013-12-03 19:48:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
return normalized;
|
|
|
|
|
|
} else {
|
2017-04-05 15:40:22 +00:00
|
|
|
|
// Primitive values and arrays get stable hashes
|
|
|
|
|
|
// by default. Lets those be stringified as-is.
|
2013-12-03 19:48:24 +00:00
|
|
|
|
return val;
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2015-03-19 01:47:12 +00:00
|
|
|
|
/**
|
|
|
|
|
|
* Get the unique values of an array, removing duplicates
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param {Array} arr Array
|
|
|
|
|
|
* @return {Array} Unique values in array
|
|
|
|
|
|
*/
|
|
|
|
|
|
oo.unique = function ( arr ) {
|
|
|
|
|
|
return arr.reduce( function ( result, current ) {
|
|
|
|
|
|
if ( result.indexOf( current ) === -1 ) {
|
|
|
|
|
|
result.push( current );
|
|
|
|
|
|
}
|
|
|
|
|
|
return result;
|
|
|
|
|
|
}, [] );
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2013-12-03 19:48:24 +00:00
|
|
|
|
/**
|
|
|
|
|
|
* Compute the union (duplicate-free merge) of a set of arrays.
|
|
|
|
|
|
*
|
2014-03-08 01:36:25 +00:00
|
|
|
|
* Arrays values must be convertable to object keys (strings).
|
2013-12-03 19:48:24 +00:00
|
|
|
|
*
|
|
|
|
|
|
* By building an object (with the values for keys) in parallel with
|
2014-03-08 01:36:25 +00:00
|
|
|
|
* the array, a new item's existence in the union can be computed faster.
|
2013-12-03 19:48:24 +00:00
|
|
|
|
*
|
2015-11-11 17:02:06 +00:00
|
|
|
|
* @param {...Array} arrays Arrays to union
|
2014-03-08 01:36:25 +00:00
|
|
|
|
* @return {Array} Union of the arrays
|
2013-12-03 19:48:24 +00:00
|
|
|
|
*/
|
|
|
|
|
|
oo.simpleArrayUnion = function () {
|
|
|
|
|
|
var i, ilen, arr, j, jlen,
|
|
|
|
|
|
obj = {},
|
|
|
|
|
|
result = [];
|
|
|
|
|
|
|
|
|
|
|
|
for ( i = 0, ilen = arguments.length; i < ilen; i++ ) {
|
2015-11-11 17:02:06 +00:00
|
|
|
|
arr = arguments[ i ];
|
2013-12-03 19:48:24 +00:00
|
|
|
|
for ( j = 0, jlen = arr.length; j < jlen; j++ ) {
|
2015-11-11 17:02:06 +00:00
|
|
|
|
if ( !obj[ arr[ j ] ] ) {
|
|
|
|
|
|
obj[ arr[ j ] ] = true;
|
|
|
|
|
|
result.push( arr[ j ] );
|
2013-12-03 19:48:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Combine arrays (intersection or difference).
|
|
|
|
|
|
*
|
|
|
|
|
|
* An intersection checks the item exists in 'b' while difference checks it doesn't.
|
|
|
|
|
|
*
|
2014-03-08 01:36:25 +00:00
|
|
|
|
* Arrays values must be convertable to object keys (strings).
|
2013-12-03 19:48:24 +00:00
|
|
|
|
*
|
|
|
|
|
|
* By building an object (with the values for keys) of 'b' we can
|
2014-03-08 01:36:25 +00:00
|
|
|
|
* compute the result faster.
|
2013-12-03 19:48:24 +00:00
|
|
|
|
*
|
|
|
|
|
|
* @private
|
|
|
|
|
|
* @param {Array} a First array
|
|
|
|
|
|
* @param {Array} b Second array
|
|
|
|
|
|
* @param {boolean} includeB Whether to items in 'b'
|
2014-03-08 01:36:25 +00:00
|
|
|
|
* @return {Array} Combination (intersection or difference) of arrays
|
2013-12-03 19:48:24 +00:00
|
|
|
|
*/
|
|
|
|
|
|
function simpleArrayCombine( a, b, includeB ) {
|
|
|
|
|
|
var i, ilen, isInB,
|
|
|
|
|
|
bObj = {},
|
|
|
|
|
|
result = [];
|
|
|
|
|
|
|
|
|
|
|
|
for ( i = 0, ilen = b.length; i < ilen; i++ ) {
|
2015-11-11 17:02:06 +00:00
|
|
|
|
bObj[ b[ i ] ] = true;
|
2013-12-03 19:48:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
for ( i = 0, ilen = a.length; i < ilen; i++ ) {
|
2015-11-11 17:02:06 +00:00
|
|
|
|
isInB = !!bObj[ a[ i ] ];
|
2013-12-03 19:48:24 +00:00
|
|
|
|
if ( isInB === includeB ) {
|
2015-11-11 17:02:06 +00:00
|
|
|
|
result.push( a[ i ] );
|
2013-12-03 19:48:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Compute the intersection of two arrays (items in both arrays).
|
|
|
|
|
|
*
|
2014-03-08 01:36:25 +00:00
|
|
|
|
* Arrays values must be convertable to object keys (strings).
|
2013-12-03 19:48:24 +00:00
|
|
|
|
*
|
|
|
|
|
|
* @param {Array} a First array
|
|
|
|
|
|
* @param {Array} b Second array
|
2014-03-08 01:36:25 +00:00
|
|
|
|
* @return {Array} Intersection of arrays
|
2013-12-03 19:48:24 +00:00
|
|
|
|
*/
|
|
|
|
|
|
oo.simpleArrayIntersection = function ( a, b ) {
|
|
|
|
|
|
return simpleArrayCombine( a, b, true );
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Compute the difference of two arrays (items in 'a' but not 'b').
|
|
|
|
|
|
*
|
2014-03-08 01:36:25 +00:00
|
|
|
|
* Arrays values must be convertable to object keys (strings).
|
2013-12-03 19:48:24 +00:00
|
|
|
|
*
|
|
|
|
|
|
* @param {Array} a First array
|
|
|
|
|
|
* @param {Array} b Second array
|
2014-03-08 01:36:25 +00:00
|
|
|
|
* @return {Array} Intersection of arrays
|
2013-12-03 19:48:24 +00:00
|
|
|
|
*/
|
|
|
|
|
|
oo.simpleArrayDifference = function ( a, b ) {
|
|
|
|
|
|
return simpleArrayCombine( a, b, false );
|
|
|
|
|
|
};
|
2014-07-23 20:53:29 +00:00
|
|
|
|
|
2017-04-05 15:40:22 +00:00
|
|
|
|
/* global $ */
|
2014-06-26 23:55:15 +00:00
|
|
|
|
|
2014-07-23 19:57:40 +00:00
|
|
|
|
oo.isPlainObject = $.isPlainObject;
|
2014-07-23 20:53:29 +00:00
|
|
|
|
|
2017-04-05 15:40:22 +00:00
|
|
|
|
/* global hasOwn */
|
2014-07-23 20:53:29 +00:00
|
|
|
|
|
2014-08-31 21:26:08 +00:00
|
|
|
|
( function () {
|
2014-03-08 01:36:25 +00:00
|
|
|
|
|
|
|
|
|
|
/**
|
2014-08-31 21:26:08 +00:00
|
|
|
|
* @class OO.EventEmitter
|
2014-03-08 01:36:25 +00:00
|
|
|
|
*
|
2014-08-31 21:26:08 +00:00
|
|
|
|
* @constructor
|
2014-03-08 01:36:25 +00:00
|
|
|
|
*/
|
2014-08-31 21:26:08 +00:00
|
|
|
|
oo.EventEmitter = function OoEventEmitter() {
|
|
|
|
|
|
// Properties
|
2013-12-03 19:48:24 +00:00
|
|
|
|
|
2014-08-31 21:26:08 +00:00
|
|
|
|
/**
|
|
|
|
|
|
* Storage of bound event handlers by event name.
|
|
|
|
|
|
*
|
|
|
|
|
|
* @property
|
|
|
|
|
|
*/
|
|
|
|
|
|
this.bindings = {};
|
|
|
|
|
|
};
|
2014-08-20 22:35:43 +00:00
|
|
|
|
|
2014-08-31 21:26:08 +00:00
|
|
|
|
oo.initClass( oo.EventEmitter );
|
2013-12-03 19:48:24 +00:00
|
|
|
|
|
2014-08-31 21:26:08 +00:00
|
|
|
|
/* Private helper functions */
|
2014-08-20 22:35:43 +00:00
|
|
|
|
|
2014-08-31 21:26:08 +00:00
|
|
|
|
/**
|
|
|
|
|
|
* Validate a function or method call in a context
|
|
|
|
|
|
*
|
|
|
|
|
|
* For a method name, check that it names a function in the context object
|
|
|
|
|
|
*
|
|
|
|
|
|
* @private
|
|
|
|
|
|
* @param {Function|string} method Function or method name
|
|
|
|
|
|
* @param {Mixed} context The context of the call
|
|
|
|
|
|
* @throws {Error} A method name is given but there is no context
|
|
|
|
|
|
* @throws {Error} In the context object, no property exists with the given name
|
|
|
|
|
|
* @throws {Error} In the context object, the named property is not a function
|
|
|
|
|
|
*/
|
|
|
|
|
|
function validateMethod( method, context ) {
|
|
|
|
|
|
// Validate method and context
|
|
|
|
|
|
if ( typeof method === 'string' ) {
|
|
|
|
|
|
// Validate method
|
|
|
|
|
|
if ( context === undefined || context === null ) {
|
|
|
|
|
|
throw new Error( 'Method name "' + method + '" has no context.' );
|
|
|
|
|
|
}
|
2015-11-11 17:02:06 +00:00
|
|
|
|
if ( typeof context[ method ] !== 'function' ) {
|
2014-08-31 21:26:08 +00:00
|
|
|
|
// Technically the property could be replaced by a function before
|
|
|
|
|
|
// call time. But this probably signals a typo.
|
|
|
|
|
|
throw new Error( 'Property "' + method + '" is not a function' );
|
|
|
|
|
|
}
|
|
|
|
|
|
} else if ( typeof method !== 'function' ) {
|
|
|
|
|
|
throw new Error( 'Invalid callback. Function or method name expected.' );
|
|
|
|
|
|
}
|
2013-12-03 19:48:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2014-08-31 21:26:08 +00:00
|
|
|
|
/* Methods */
|
2013-12-03 19:48:24 +00:00
|
|
|
|
|
2014-08-31 21:26:08 +00:00
|
|
|
|
/**
|
|
|
|
|
|
* Add a listener to events of a specific event.
|
|
|
|
|
|
*
|
|
|
|
|
|
* The listener can be a function or the string name of a method; if the latter, then the
|
|
|
|
|
|
* name lookup happens at the time the listener is called.
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param {string} event Type of event to listen to
|
|
|
|
|
|
* @param {Function|string} method Function or method name to call when event occurs
|
|
|
|
|
|
* @param {Array} [args] Arguments to pass to listener, will be prepended to emitted arguments
|
|
|
|
|
|
* @param {Object} [context=null] Context object for function or method call
|
|
|
|
|
|
* @throws {Error} Listener argument is not a function or a valid method name
|
|
|
|
|
|
* @chainable
|
|
|
|
|
|
*/
|
|
|
|
|
|
oo.EventEmitter.prototype.on = function ( event, method, args, context ) {
|
|
|
|
|
|
var bindings;
|
2013-12-03 19:48:24 +00:00
|
|
|
|
|
2014-08-31 21:26:08 +00:00
|
|
|
|
validateMethod( method, context );
|
|
|
|
|
|
|
|
|
|
|
|
if ( hasOwn.call( this.bindings, event ) ) {
|
2015-11-11 17:02:06 +00:00
|
|
|
|
bindings = this.bindings[ event ];
|
2014-08-31 21:26:08 +00:00
|
|
|
|
} else {
|
|
|
|
|
|
// Auto-initialize bindings list
|
2015-11-11 17:02:06 +00:00
|
|
|
|
bindings = this.bindings[ event ] = [];
|
2014-08-31 21:26:08 +00:00
|
|
|
|
}
|
|
|
|
|
|
// Add binding
|
|
|
|
|
|
bindings.push( {
|
|
|
|
|
|
method: method,
|
|
|
|
|
|
args: args,
|
|
|
|
|
|
context: ( arguments.length < 4 ) ? null : context
|
|
|
|
|
|
} );
|
2014-08-20 22:35:43 +00:00
|
|
|
|
return this;
|
2014-08-31 21:26:08 +00:00
|
|
|
|
};
|
2014-08-20 22:35:43 +00:00
|
|
|
|
|
2014-08-31 21:26:08 +00:00
|
|
|
|
/**
|
|
|
|
|
|
* Add a one-time listener to a specific event.
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param {string} event Type of event to listen to
|
|
|
|
|
|
* @param {Function} listener Listener to call when event occurs
|
|
|
|
|
|
* @chainable
|
|
|
|
|
|
*/
|
|
|
|
|
|
oo.EventEmitter.prototype.once = function ( event, listener ) {
|
|
|
|
|
|
var eventEmitter = this,
|
2015-02-26 01:59:38 +00:00
|
|
|
|
wrapper = function () {
|
|
|
|
|
|
eventEmitter.off( event, wrapper );
|
|
|
|
|
|
return listener.apply( this, arguments );
|
2014-08-31 21:26:08 +00:00
|
|
|
|
};
|
2015-02-26 01:59:38 +00:00
|
|
|
|
return this.on( event, wrapper );
|
2014-08-31 21:26:08 +00:00
|
|
|
|
};
|
2014-08-20 22:35:43 +00:00
|
|
|
|
|
2014-08-31 21:26:08 +00:00
|
|
|
|
/**
|
|
|
|
|
|
* Remove a specific listener from a specific event.
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param {string} event Type of event to remove listener from
|
|
|
|
|
|
* @param {Function|string} [method] Listener to remove. Must be in the same form as was passed
|
|
|
|
|
|
* to "on". Omit to remove all listeners.
|
|
|
|
|
|
* @param {Object} [context=null] Context object function or method call
|
|
|
|
|
|
* @chainable
|
|
|
|
|
|
* @throws {Error} Listener argument is not a function or a valid method name
|
|
|
|
|
|
*/
|
|
|
|
|
|
oo.EventEmitter.prototype.off = function ( event, method, context ) {
|
|
|
|
|
|
var i, bindings;
|
2014-08-20 22:35:43 +00:00
|
|
|
|
|
2014-08-31 21:26:08 +00:00
|
|
|
|
if ( arguments.length === 1 ) {
|
|
|
|
|
|
// Remove all bindings for event
|
2015-11-11 17:02:06 +00:00
|
|
|
|
delete this.bindings[ event ];
|
2014-08-31 21:26:08 +00:00
|
|
|
|
return this;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
validateMethod( method, context );
|
2014-08-20 22:35:43 +00:00
|
|
|
|
|
2015-11-11 17:02:06 +00:00
|
|
|
|
if ( !hasOwn.call( this.bindings, event ) || !this.bindings[ event ].length ) {
|
2014-08-31 21:26:08 +00:00
|
|
|
|
// No matching bindings
|
|
|
|
|
|
return this;
|
2013-12-03 19:48:24 +00:00
|
|
|
|
}
|
2014-08-20 22:35:43 +00:00
|
|
|
|
|
2014-08-31 21:26:08 +00:00
|
|
|
|
// Default to null context
|
|
|
|
|
|
if ( arguments.length < 3 ) {
|
|
|
|
|
|
context = null;
|
|
|
|
|
|
}
|
2013-12-03 19:48:24 +00:00
|
|
|
|
|
2014-08-31 21:26:08 +00:00
|
|
|
|
// Remove matching handlers
|
2015-11-11 17:02:06 +00:00
|
|
|
|
bindings = this.bindings[ event ];
|
2014-08-31 21:26:08 +00:00
|
|
|
|
i = bindings.length;
|
|
|
|
|
|
while ( i-- ) {
|
2015-11-11 17:02:06 +00:00
|
|
|
|
if ( bindings[ i ].method === method && bindings[ i ].context === context ) {
|
2014-08-31 21:26:08 +00:00
|
|
|
|
bindings.splice( i, 1 );
|
2014-08-20 22:35:43 +00:00
|
|
|
|
}
|
2013-12-03 19:48:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2014-08-31 21:26:08 +00:00
|
|
|
|
// Cleanup if now empty
|
|
|
|
|
|
if ( bindings.length === 0 ) {
|
2015-11-11 17:02:06 +00:00
|
|
|
|
delete this.bindings[ event ];
|
2013-12-03 19:48:24 +00:00
|
|
|
|
}
|
2014-08-31 21:26:08 +00:00
|
|
|
|
return this;
|
|
|
|
|
|
};
|
2013-12-03 19:48:24 +00:00
|
|
|
|
|
2014-08-31 21:26:08 +00:00
|
|
|
|
/**
|
|
|
|
|
|
* Emit an event.
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param {string} event Type of event
|
2015-11-11 17:02:06 +00:00
|
|
|
|
* @param {...Mixed} args First in a list of variadic arguments passed to event handler (optional)
|
2015-07-23 19:18:59 +00:00
|
|
|
|
* @return {boolean} Whether the event was handled by at least one listener
|
2014-08-31 21:26:08 +00:00
|
|
|
|
*/
|
|
|
|
|
|
oo.EventEmitter.prototype.emit = function ( event ) {
|
2015-01-23 20:18:10 +00:00
|
|
|
|
var args = [],
|
|
|
|
|
|
i, len, binding, bindings, method;
|
2014-08-31 21:26:08 +00:00
|
|
|
|
|
2014-11-06 18:47:13 +00:00
|
|
|
|
if ( hasOwn.call( this.bindings, event ) ) {
|
2014-08-31 21:26:08 +00:00
|
|
|
|
// Slicing ensures that we don't get tripped up by event handlers that add/remove bindings
|
2015-11-11 17:02:06 +00:00
|
|
|
|
bindings = this.bindings[ event ].slice();
|
2015-01-23 20:18:10 +00:00
|
|
|
|
for ( i = 1, len = arguments.length; i < len; i++ ) {
|
2015-11-11 17:02:06 +00:00
|
|
|
|
args.push( arguments[ i ] );
|
2015-01-23 20:18:10 +00:00
|
|
|
|
}
|
2014-08-31 21:26:08 +00:00
|
|
|
|
for ( i = 0, len = bindings.length; i < len; i++ ) {
|
2015-11-11 17:02:06 +00:00
|
|
|
|
binding = bindings[ i ];
|
2014-08-31 21:26:08 +00:00
|
|
|
|
if ( typeof binding.method === 'string' ) {
|
|
|
|
|
|
// Lookup method by name (late binding)
|
|
|
|
|
|
method = binding.context[ binding.method ];
|
|
|
|
|
|
} else {
|
|
|
|
|
|
method = binding.method;
|
|
|
|
|
|
}
|
|
|
|
|
|
method.apply(
|
|
|
|
|
|
binding.context,
|
|
|
|
|
|
binding.args ? binding.args.concat( args ) : args
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
return false;
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Connect event handlers to an object.
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param {Object} context Object to call methods on when events occur
|
|
|
|
|
|
* @param {Object.<string,string>|Object.<string,Function>|Object.<string,Array>} methods List of
|
|
|
|
|
|
* event bindings keyed by event name containing either method names, functions or arrays containing
|
|
|
|
|
|
* method name or function followed by a list of arguments to be passed to callback before emitted
|
|
|
|
|
|
* arguments
|
|
|
|
|
|
* @chainable
|
|
|
|
|
|
*/
|
|
|
|
|
|
oo.EventEmitter.prototype.connect = function ( context, methods ) {
|
|
|
|
|
|
var method, args, event;
|
2013-12-03 19:48:24 +00:00
|
|
|
|
|
|
|
|
|
|
for ( event in methods ) {
|
2015-11-11 17:02:06 +00:00
|
|
|
|
method = methods[ event ];
|
2014-08-31 21:26:08 +00:00
|
|
|
|
// Allow providing additional args
|
|
|
|
|
|
if ( Array.isArray( method ) ) {
|
|
|
|
|
|
args = method.slice( 1 );
|
2015-11-11 17:02:06 +00:00
|
|
|
|
method = method[ 0 ];
|
2014-08-31 21:26:08 +00:00
|
|
|
|
} else {
|
|
|
|
|
|
args = [];
|
|
|
|
|
|
}
|
|
|
|
|
|
// Add binding
|
|
|
|
|
|
this.on( event, method, args, context );
|
2013-12-03 19:48:24 +00:00
|
|
|
|
}
|
2014-08-31 21:26:08 +00:00
|
|
|
|
return this;
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Disconnect event handlers from an object.
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param {Object} context Object to disconnect methods from
|
|
|
|
|
|
* @param {Object.<string,string>|Object.<string,Function>|Object.<string,Array>} [methods] List of
|
|
|
|
|
|
* event bindings keyed by event name. Values can be either method names or functions, but must be
|
|
|
|
|
|
* consistent with those used in the corresponding call to "connect".
|
|
|
|
|
|
* @chainable
|
|
|
|
|
|
*/
|
|
|
|
|
|
oo.EventEmitter.prototype.disconnect = function ( context, methods ) {
|
2015-11-11 17:02:06 +00:00
|
|
|
|
var i, event, method, bindings;
|
2014-08-31 21:26:08 +00:00
|
|
|
|
|
|
|
|
|
|
if ( methods ) {
|
|
|
|
|
|
// Remove specific connections to the context
|
|
|
|
|
|
for ( event in methods ) {
|
2015-11-11 17:02:06 +00:00
|
|
|
|
method = methods[ event ];
|
|
|
|
|
|
if ( Array.isArray( method ) ) {
|
|
|
|
|
|
method = method[ 0 ];
|
|
|
|
|
|
}
|
|
|
|
|
|
this.off( event, method, context );
|
2014-08-31 21:26:08 +00:00
|
|
|
|
}
|
|
|
|
|
|
} else {
|
|
|
|
|
|
// Remove all connections to the context
|
|
|
|
|
|
for ( event in this.bindings ) {
|
2015-11-11 17:02:06 +00:00
|
|
|
|
bindings = this.bindings[ event ];
|
2014-08-31 21:26:08 +00:00
|
|
|
|
i = bindings.length;
|
|
|
|
|
|
while ( i-- ) {
|
|
|
|
|
|
// bindings[i] may have been removed by the previous step's
|
|
|
|
|
|
// this.off so check it still exists
|
2015-11-11 17:02:06 +00:00
|
|
|
|
if ( bindings[ i ] && bindings[ i ].context === context ) {
|
|
|
|
|
|
this.off( event, bindings[ i ].method, context );
|
2014-08-31 21:26:08 +00:00
|
|
|
|
}
|
2013-12-03 19:48:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2014-08-31 21:26:08 +00:00
|
|
|
|
return this;
|
|
|
|
|
|
};
|
2014-07-23 20:53:29 +00:00
|
|
|
|
|
2014-08-31 21:26:08 +00:00
|
|
|
|
}() );
|
2014-08-20 22:35:43 +00:00
|
|
|
|
|
2015-11-11 17:02:06 +00:00
|
|
|
|
( function () {
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Contain and manage a list of OO.EventEmitter items.
|
|
|
|
|
|
*
|
|
|
|
|
|
* Aggregates and manages their events collectively.
|
|
|
|
|
|
*
|
|
|
|
|
|
* This mixin must be used in a class that also mixes in OO.EventEmitter.
|
|
|
|
|
|
*
|
|
|
|
|
|
* @abstract
|
|
|
|
|
|
* @class OO.EmitterList
|
|
|
|
|
|
* @constructor
|
|
|
|
|
|
*/
|
|
|
|
|
|
oo.EmitterList = function OoEmitterList() {
|
|
|
|
|
|
this.items = [];
|
|
|
|
|
|
this.aggregateItemEvents = {};
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/* Events */
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Item has been added
|
|
|
|
|
|
*
|
|
|
|
|
|
* @event add
|
|
|
|
|
|
* @param {OO.EventEmitter} item Added item
|
|
|
|
|
|
* @param {number} index Index items were added at
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Item has been moved to a new index
|
|
|
|
|
|
*
|
|
|
|
|
|
* @event move
|
|
|
|
|
|
* @param {OO.EventEmitter} item Moved item
|
|
|
|
|
|
* @param {number} index Index item was moved to
|
|
|
|
|
|
* @param {number} oldIndex The original index the item was in
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Item has been removed
|
|
|
|
|
|
*
|
|
|
|
|
|
* @event remove
|
|
|
|
|
|
* @param {OO.EventEmitter} item Removed item
|
|
|
|
|
|
* @param {number} index Index the item was removed from
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* @event clear The list has been cleared of items
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
/* Methods */
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Normalize requested index to fit into the bounds of the given array.
|
|
|
|
|
|
*
|
|
|
|
|
|
* @private
|
|
|
|
|
|
* @static
|
|
|
|
|
|
* @param {Array} arr Given array
|
|
|
|
|
|
* @param {number|undefined} index Requested index
|
|
|
|
|
|
* @return {number} Normalized index
|
|
|
|
|
|
*/
|
|
|
|
|
|
function normalizeArrayIndex( arr, index ) {
|
|
|
|
|
|
return ( index === undefined || index < 0 || index >= arr.length ) ?
|
|
|
|
|
|
arr.length :
|
|
|
|
|
|
index;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Get all items.
|
|
|
|
|
|
*
|
|
|
|
|
|
* @return {OO.EventEmitter[]} Items in the list
|
|
|
|
|
|
*/
|
|
|
|
|
|
oo.EmitterList.prototype.getItems = function () {
|
|
|
|
|
|
return this.items.slice( 0 );
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Get the index of a specific item.
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param {OO.EventEmitter} item Requested item
|
|
|
|
|
|
* @return {number} Index of the item
|
|
|
|
|
|
*/
|
|
|
|
|
|
oo.EmitterList.prototype.getItemIndex = function ( item ) {
|
|
|
|
|
|
return this.items.indexOf( item );
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Get number of items.
|
|
|
|
|
|
*
|
|
|
|
|
|
* @return {number} Number of items in the list
|
|
|
|
|
|
*/
|
|
|
|
|
|
oo.EmitterList.prototype.getItemCount = function () {
|
|
|
|
|
|
return this.items.length;
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Check if a list contains no items.
|
|
|
|
|
|
*
|
|
|
|
|
|
* @return {boolean} Group is empty
|
|
|
|
|
|
*/
|
|
|
|
|
|
oo.EmitterList.prototype.isEmpty = function () {
|
|
|
|
|
|
return !this.items.length;
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Aggregate the events emitted by the group.
|
|
|
|
|
|
*
|
|
|
|
|
|
* When events are aggregated, the group will listen to all contained items for the event,
|
|
|
|
|
|
* and then emit the event under a new name. The new event will contain an additional leading
|
|
|
|
|
|
* parameter containing the item that emitted the original event. Other arguments emitted from
|
|
|
|
|
|
* the original event are passed through.
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param {Object.<string,string|null>} events An object keyed by the name of the event that should be
|
|
|
|
|
|
* aggregated (e.g., ‘click’) and the value of the new name to use (e.g., ‘groupClick’).
|
|
|
|
|
|
* A `null` value will remove aggregated events.
|
|
|
|
|
|
|
|
|
|
|
|
* @throws {Error} If aggregation already exists
|
|
|
|
|
|
*/
|
|
|
|
|
|
oo.EmitterList.prototype.aggregate = function ( events ) {
|
|
|
|
|
|
var i, item, add, remove, itemEvent, groupEvent;
|
|
|
|
|
|
|
|
|
|
|
|
for ( itemEvent in events ) {
|
|
|
|
|
|
groupEvent = events[ itemEvent ];
|
|
|
|
|
|
|
|
|
|
|
|
// Remove existing aggregated event
|
|
|
|
|
|
if ( Object.prototype.hasOwnProperty.call( this.aggregateItemEvents, itemEvent ) ) {
|
|
|
|
|
|
// Don't allow duplicate aggregations
|
|
|
|
|
|
if ( groupEvent ) {
|
|
|
|
|
|
throw new Error( 'Duplicate item event aggregation for ' + itemEvent );
|
|
|
|
|
|
}
|
|
|
|
|
|
// Remove event aggregation from existing items
|
|
|
|
|
|
for ( i = 0; i < this.items.length; i++ ) {
|
|
|
|
|
|
item = this.items[ i ];
|
|
|
|
|
|
if ( item.connect && item.disconnect ) {
|
|
|
|
|
|
remove = {};
|
|
|
|
|
|
remove[ itemEvent ] = [ 'emit', this.aggregateItemEvents[ itemEvent ], item ];
|
|
|
|
|
|
item.disconnect( this, remove );
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
// Prevent future items from aggregating event
|
|
|
|
|
|
delete this.aggregateItemEvents[ itemEvent ];
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Add new aggregate event
|
|
|
|
|
|
if ( groupEvent ) {
|
|
|
|
|
|
// Make future items aggregate event
|
|
|
|
|
|
this.aggregateItemEvents[ itemEvent ] = groupEvent;
|
|
|
|
|
|
// Add event aggregation to existing items
|
|
|
|
|
|
for ( i = 0; i < this.items.length; i++ ) {
|
|
|
|
|
|
item = this.items[ i ];
|
|
|
|
|
|
if ( item.connect && item.disconnect ) {
|
|
|
|
|
|
add = {};
|
|
|
|
|
|
add[ itemEvent ] = [ 'emit', groupEvent, item ];
|
|
|
|
|
|
item.connect( this, add );
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Add items to the list.
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param {OO.EventEmitter|OO.EventEmitter[]} items Item to add or
|
|
|
|
|
|
* an array of items to add
|
|
|
|
|
|
* @param {number} [index] Index to add items at. If no index is
|
|
|
|
|
|
* given, or if the index that is given is invalid, the item
|
|
|
|
|
|
* will be added at the end of the list.
|
|
|
|
|
|
* @chainable
|
|
|
|
|
|
* @fires add
|
|
|
|
|
|
* @fires move
|
|
|
|
|
|
*/
|
|
|
|
|
|
oo.EmitterList.prototype.addItems = function ( items, index ) {
|
|
|
|
|
|
var i, oldIndex;
|
|
|
|
|
|
|
|
|
|
|
|
if ( !Array.isArray( items ) ) {
|
|
|
|
|
|
items = [ items ];
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if ( items.length === 0 ) {
|
|
|
|
|
|
return this;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
index = normalizeArrayIndex( this.items, index );
|
|
|
|
|
|
for ( i = 0; i < items.length; i++ ) {
|
|
|
|
|
|
oldIndex = this.items.indexOf( items[ i ] );
|
|
|
|
|
|
if ( oldIndex !== -1 ) {
|
|
|
|
|
|
// Move item to new index
|
|
|
|
|
|
index = this.moveItem( items[ i ], index );
|
|
|
|
|
|
this.emit( 'move', items[ i ], index, oldIndex );
|
|
|
|
|
|
} else {
|
|
|
|
|
|
// insert item at index
|
|
|
|
|
|
index = this.insertItem( items[ i ], index );
|
|
|
|
|
|
this.emit( 'add', items[ i ], index );
|
|
|
|
|
|
}
|
|
|
|
|
|
index++;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return this;
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Move an item from its current position to a new index.
|
|
|
|
|
|
*
|
|
|
|
|
|
* The item is expected to exist in the list. If it doesn't,
|
|
|
|
|
|
* the method will throw an exception.
|
|
|
|
|
|
*
|
|
|
|
|
|
* @private
|
|
|
|
|
|
* @param {OO.EventEmitter} item Items to add
|
|
|
|
|
|
* @param {number} newIndex Index to move the item to
|
|
|
|
|
|
* @return {number} The index the item was moved to
|
|
|
|
|
|
* @throws {Error} If item is not in the list
|
|
|
|
|
|
*/
|
|
|
|
|
|
oo.EmitterList.prototype.moveItem = function ( item, newIndex ) {
|
|
|
|
|
|
var existingIndex = this.items.indexOf( item );
|
|
|
|
|
|
|
|
|
|
|
|
if ( existingIndex === -1 ) {
|
|
|
|
|
|
throw new Error( 'Item cannot be moved, because it is not in the list.' );
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
newIndex = normalizeArrayIndex( this.items, newIndex );
|
|
|
|
|
|
|
|
|
|
|
|
// Remove the item from the current index
|
|
|
|
|
|
this.items.splice( existingIndex, 1 );
|
|
|
|
|
|
|
|
|
|
|
|
// Adjust new index after removal
|
|
|
|
|
|
newIndex--;
|
|
|
|
|
|
|
|
|
|
|
|
// Move the item to the new index
|
|
|
|
|
|
this.items.splice( newIndex, 0, item );
|
|
|
|
|
|
|
|
|
|
|
|
return newIndex;
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Utility method to insert an item into the list, and
|
|
|
|
|
|
* connect it to aggregate events.
|
|
|
|
|
|
*
|
|
|
|
|
|
* Don't call this directly unless you know what you're doing.
|
|
|
|
|
|
* Use #addItems instead.
|
|
|
|
|
|
*
|
2017-04-05 15:40:22 +00:00
|
|
|
|
* This method can be extended in child classes to produce
|
|
|
|
|
|
* different behavior when an item is inserted. For example,
|
|
|
|
|
|
* inserted items may also be attached to the DOM or may
|
|
|
|
|
|
* interact with some other nodes in certain ways. Extending
|
|
|
|
|
|
* this method is allowed, but if overriden, the aggregation
|
|
|
|
|
|
* of events must be preserved, or behavior of emitted events
|
|
|
|
|
|
* will be broken.
|
|
|
|
|
|
*
|
|
|
|
|
|
* If you are extending this method, please make sure the
|
|
|
|
|
|
* parent method is called.
|
|
|
|
|
|
*
|
|
|
|
|
|
* @protected
|
2015-11-11 17:02:06 +00:00
|
|
|
|
* @param {OO.EventEmitter} item Items to add
|
|
|
|
|
|
* @param {number} index Index to add items at
|
|
|
|
|
|
* @return {number} The index the item was added at
|
|
|
|
|
|
*/
|
|
|
|
|
|
oo.EmitterList.prototype.insertItem = function ( item, index ) {
|
|
|
|
|
|
var events, event;
|
|
|
|
|
|
|
|
|
|
|
|
// Add the item to event aggregation
|
|
|
|
|
|
if ( item.connect && item.disconnect ) {
|
|
|
|
|
|
events = {};
|
|
|
|
|
|
for ( event in this.aggregateItemEvents ) {
|
|
|
|
|
|
events[ event ] = [ 'emit', this.aggregateItemEvents[ event ], item ];
|
|
|
|
|
|
}
|
|
|
|
|
|
item.connect( this, events );
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
index = normalizeArrayIndex( this.items, index );
|
|
|
|
|
|
|
|
|
|
|
|
// Insert into items array
|
|
|
|
|
|
this.items.splice( index, 0, item );
|
|
|
|
|
|
return index;
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Remove items.
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param {OO.EventEmitter[]} items Items to remove
|
|
|
|
|
|
* @chainable
|
|
|
|
|
|
* @fires remove
|
|
|
|
|
|
*/
|
|
|
|
|
|
oo.EmitterList.prototype.removeItems = function ( items ) {
|
|
|
|
|
|
var i, item, index;
|
|
|
|
|
|
|
|
|
|
|
|
if ( !Array.isArray( items ) ) {
|
|
|
|
|
|
items = [ items ];
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if ( items.length === 0 ) {
|
|
|
|
|
|
return this;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Remove specific items
|
|
|
|
|
|
for ( i = 0; i < items.length; i++ ) {
|
|
|
|
|
|
item = items[ i ];
|
|
|
|
|
|
index = this.items.indexOf( item );
|
|
|
|
|
|
if ( index !== -1 ) {
|
|
|
|
|
|
if ( item.connect && item.disconnect ) {
|
|
|
|
|
|
// Disconnect all listeners from the item
|
|
|
|
|
|
item.disconnect( this );
|
|
|
|
|
|
}
|
|
|
|
|
|
this.items.splice( index, 1 );
|
|
|
|
|
|
this.emit( 'remove', item, index );
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return this;
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Clear all items
|
|
|
|
|
|
*
|
|
|
|
|
|
* @chainable
|
|
|
|
|
|
* @fires clear
|
|
|
|
|
|
*/
|
|
|
|
|
|
oo.EmitterList.prototype.clearItems = function () {
|
|
|
|
|
|
var i, item,
|
|
|
|
|
|
cleared = this.items.splice( 0, this.items.length );
|
|
|
|
|
|
|
|
|
|
|
|
// Disconnect all items
|
|
|
|
|
|
for ( i = 0; i < cleared.length; i++ ) {
|
|
|
|
|
|
item = cleared[ i ];
|
|
|
|
|
|
if ( item.connect && item.disconnect ) {
|
|
|
|
|
|
item.disconnect( this );
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
this.emit( 'clear' );
|
|
|
|
|
|
|
|
|
|
|
|
return this;
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
}() );
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Manage a sorted list of OO.EmitterList objects.
|
|
|
|
|
|
*
|
|
|
|
|
|
* The sort order is based on a callback that compares two items. The return value of
|
|
|
|
|
|
* callback( a, b ) must be less than zero if a < b, greater than zero if a > b, and zero
|
|
|
|
|
|
* if a is equal to b. The callback should only return zero if the two objects are
|
|
|
|
|
|
* considered equal.
|
|
|
|
|
|
*
|
|
|
|
|
|
* When an item changes in a way that could affect their sorting behavior, it must
|
|
|
|
|
|
* emit the itemSortChange event. This will cause it to be re-sorted automatically.
|
|
|
|
|
|
*
|
|
|
|
|
|
* This mixin must be used in a class that also mixes in OO.EventEmitter.
|
|
|
|
|
|
*
|
|
|
|
|
|
* @abstract
|
|
|
|
|
|
* @class OO.SortedEmitterList
|
|
|
|
|
|
* @mixins OO.EmitterList
|
|
|
|
|
|
* @constructor
|
|
|
|
|
|
* @param {Function} sortingCallback Callback that compares two items.
|
|
|
|
|
|
*/
|
|
|
|
|
|
oo.SortedEmitterList = function OoSortedEmitterList( sortingCallback ) {
|
|
|
|
|
|
// Mixin constructors
|
|
|
|
|
|
oo.EmitterList.call( this );
|
|
|
|
|
|
|
|
|
|
|
|
this.sortingCallback = sortingCallback;
|
|
|
|
|
|
|
|
|
|
|
|
// Listen to sortChange event and make sure
|
|
|
|
|
|
// we re-sort the changed item when that happens
|
|
|
|
|
|
this.aggregate( {
|
|
|
|
|
|
sortChange: 'itemSortChange'
|
|
|
|
|
|
} );
|
|
|
|
|
|
|
|
|
|
|
|
this.connect( this, {
|
|
|
|
|
|
itemSortChange: 'onItemSortChange'
|
|
|
|
|
|
} );
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
oo.mixinClass( oo.SortedEmitterList, oo.EmitterList );
|
|
|
|
|
|
|
|
|
|
|
|
/* Events */
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* An item has changed properties that affect its sort positioning
|
|
|
|
|
|
* inside the list.
|
|
|
|
|
|
*
|
|
|
|
|
|
* @private
|
|
|
|
|
|
* @event itemSortChange
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
/* Methods */
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Handle a case where an item changed a property that relates
|
|
|
|
|
|
* to its sorted order
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param {OO.EventEmitter} item Item in the list
|
|
|
|
|
|
*/
|
|
|
|
|
|
oo.SortedEmitterList.prototype.onItemSortChange = function ( item ) {
|
|
|
|
|
|
// Remove the item
|
|
|
|
|
|
this.removeItems( item );
|
|
|
|
|
|
// Re-add the item so it is in the correct place
|
|
|
|
|
|
this.addItems( item );
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Change the sorting callback for this sorted list.
|
|
|
|
|
|
*
|
|
|
|
|
|
* The callback receives two items. The return value of callback(a, b) must be less than zero
|
|
|
|
|
|
* if a < b, greater than zero if a > b, and zero if a is equal to b.
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param {Function} sortingCallback Sorting callback
|
|
|
|
|
|
*/
|
|
|
|
|
|
oo.SortedEmitterList.prototype.setSortingCallback = function ( sortingCallback ) {
|
|
|
|
|
|
var items = this.getItems();
|
|
|
|
|
|
|
|
|
|
|
|
this.sortingCallback = sortingCallback;
|
|
|
|
|
|
|
|
|
|
|
|
// Empty the list
|
|
|
|
|
|
this.clearItems();
|
|
|
|
|
|
// Re-add the items in the new order
|
|
|
|
|
|
this.addItems( items );
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Add items to the sorted list.
|
|
|
|
|
|
*
|
|
|
|
|
|
* @chainable
|
|
|
|
|
|
* @param {OO.EventEmitter|OO.EventEmitter[]} items Item to add or
|
|
|
|
|
|
* an array of items to add
|
|
|
|
|
|
*/
|
|
|
|
|
|
oo.SortedEmitterList.prototype.addItems = function ( items ) {
|
|
|
|
|
|
var index, i, insertionIndex;
|
|
|
|
|
|
|
|
|
|
|
|
if ( !Array.isArray( items ) ) {
|
|
|
|
|
|
items = [ items ];
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if ( items.length === 0 ) {
|
|
|
|
|
|
return this;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
for ( i = 0; i < items.length; i++ ) {
|
|
|
|
|
|
// Find insertion index
|
|
|
|
|
|
insertionIndex = this.findInsertionIndex( items[ i ] );
|
|
|
|
|
|
|
|
|
|
|
|
// Check if the item exists using the sorting callback
|
|
|
|
|
|
// and remove it first if it exists
|
|
|
|
|
|
if (
|
|
|
|
|
|
// First make sure the insertion index is not at the end
|
|
|
|
|
|
// of the list (which means it does not point to any actual
|
|
|
|
|
|
// items)
|
|
|
|
|
|
insertionIndex <= this.items.length &&
|
|
|
|
|
|
// Make sure there actually is an item in this index
|
|
|
|
|
|
this.items[ insertionIndex ] &&
|
|
|
|
|
|
// The callback returns 0 if the items are equal
|
|
|
|
|
|
this.sortingCallback( this.items[ insertionIndex ], items[ i ] ) === 0
|
|
|
|
|
|
) {
|
|
|
|
|
|
// Remove the existing item
|
|
|
|
|
|
this.removeItems( this.items[ insertionIndex ] );
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Insert item at the insertion index
|
|
|
|
|
|
index = this.insertItem( items[ i ], insertionIndex );
|
2017-04-05 15:40:22 +00:00
|
|
|
|
this.emit( 'add', items[ i ], index );
|
2015-11-11 17:02:06 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return this;
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Find the index a given item should be inserted at. If the item is already
|
|
|
|
|
|
* in the list, this will return the index where the item currently is.
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param {OO.EventEmitter} item Items to insert
|
|
|
|
|
|
* @return {number} The index the item should be inserted at
|
|
|
|
|
|
*/
|
|
|
|
|
|
oo.SortedEmitterList.prototype.findInsertionIndex = function ( item ) {
|
|
|
|
|
|
var list = this;
|
|
|
|
|
|
|
|
|
|
|
|
return oo.binarySearch(
|
|
|
|
|
|
this.items,
|
|
|
|
|
|
// Fake a this.sortingCallback.bind( null, item ) call here
|
|
|
|
|
|
// otherwise this doesn't pass tests in phantomJS
|
|
|
|
|
|
function ( otherItem ) {
|
|
|
|
|
|
return list.sortingCallback( item, otherItem );
|
|
|
|
|
|
},
|
|
|
|
|
|
true
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2017-04-05 15:40:22 +00:00
|
|
|
|
/* global hasOwn */
|
2014-08-20 22:35:43 +00:00
|
|
|
|
|
2013-12-03 19:48:24 +00:00
|
|
|
|
/**
|
|
|
|
|
|
* @class OO.Registry
|
|
|
|
|
|
* @mixins OO.EventEmitter
|
|
|
|
|
|
*
|
|
|
|
|
|
* @constructor
|
|
|
|
|
|
*/
|
|
|
|
|
|
oo.Registry = function OoRegistry() {
|
|
|
|
|
|
// Mixin constructors
|
|
|
|
|
|
oo.EventEmitter.call( this );
|
|
|
|
|
|
|
|
|
|
|
|
// Properties
|
|
|
|
|
|
this.registry = {};
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/* Inheritance */
|
|
|
|
|
|
|
|
|
|
|
|
oo.mixinClass( oo.Registry, oo.EventEmitter );
|
|
|
|
|
|
|
|
|
|
|
|
/* Events */
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* @event register
|
|
|
|
|
|
* @param {string} name
|
|
|
|
|
|
* @param {Mixed} data
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
2015-04-29 01:24:59 +00:00
|
|
|
|
/**
|
|
|
|
|
|
* @event unregister
|
|
|
|
|
|
* @param {string} name
|
|
|
|
|
|
* @param {Mixed} data Data removed from registry
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
2013-12-03 19:48:24 +00:00
|
|
|
|
/* Methods */
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Associate one or more symbolic names with some data.
|
|
|
|
|
|
*
|
2015-04-29 01:24:59 +00:00
|
|
|
|
* Any existing entry with the same name will be overridden.
|
2013-12-03 19:48:24 +00:00
|
|
|
|
*
|
|
|
|
|
|
* @param {string|string[]} name Symbolic name or list of symbolic names
|
|
|
|
|
|
* @param {Mixed} data Data to associate with symbolic name
|
|
|
|
|
|
* @fires register
|
|
|
|
|
|
* @throws {Error} Name argument must be a string or array
|
|
|
|
|
|
*/
|
|
|
|
|
|
oo.Registry.prototype.register = function ( name, data ) {
|
|
|
|
|
|
var i, len;
|
2014-03-08 01:36:25 +00:00
|
|
|
|
if ( typeof name === 'string' ) {
|
2015-11-11 17:02:06 +00:00
|
|
|
|
this.registry[ name ] = data;
|
2014-03-08 01:36:25 +00:00
|
|
|
|
this.emit( 'register', name, data );
|
|
|
|
|
|
} else if ( Array.isArray( name ) ) {
|
2013-12-03 19:48:24 +00:00
|
|
|
|
for ( i = 0, len = name.length; i < len; i++ ) {
|
2015-11-11 17:02:06 +00:00
|
|
|
|
this.register( name[ i ], data );
|
2013-12-03 19:48:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
} else {
|
2014-03-08 01:36:25 +00:00
|
|
|
|
throw new Error( 'Name must be a string or array, cannot be a ' + typeof name );
|
2013-12-03 19:48:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2015-04-29 01:24:59 +00:00
|
|
|
|
* Remove one or more symbolic names from the registry
|
2013-12-03 19:48:24 +00:00
|
|
|
|
*
|
2015-04-29 01:24:59 +00:00
|
|
|
|
* @param {string|string[]} name Symbolic name or list of symbolic names
|
|
|
|
|
|
* @fires unregister
|
|
|
|
|
|
* @throws {Error} Name argument must be a string or array
|
|
|
|
|
|
*/
|
|
|
|
|
|
oo.Registry.prototype.unregister = function ( name ) {
|
|
|
|
|
|
var i, len, data;
|
|
|
|
|
|
if ( typeof name === 'string' ) {
|
|
|
|
|
|
data = this.lookup( name );
|
|
|
|
|
|
if ( data !== undefined ) {
|
2015-11-11 17:02:06 +00:00
|
|
|
|
delete this.registry[ name ];
|
2015-04-29 01:24:59 +00:00
|
|
|
|
this.emit( 'unregister', name, data );
|
|
|
|
|
|
}
|
|
|
|
|
|
} else if ( Array.isArray( name ) ) {
|
|
|
|
|
|
for ( i = 0, len = name.length; i < len; i++ ) {
|
2015-11-11 17:02:06 +00:00
|
|
|
|
this.unregister( name[ i ] );
|
2015-04-29 01:24:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
} else {
|
|
|
|
|
|
throw new Error( 'Name must be a string or array, cannot be a ' + typeof name );
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Get data for a given symbolic name.
|
2013-12-03 19:48:24 +00:00
|
|
|
|
*
|
|
|
|
|
|
* @param {string} name Symbolic name
|
2014-03-08 01:36:25 +00:00
|
|
|
|
* @return {Mixed|undefined} Data associated with symbolic name
|
2013-12-03 19:48:24 +00:00
|
|
|
|
*/
|
|
|
|
|
|
oo.Registry.prototype.lookup = function ( name ) {
|
2014-08-20 22:35:43 +00:00
|
|
|
|
if ( hasOwn.call( this.registry, name ) ) {
|
2015-11-11 17:02:06 +00:00
|
|
|
|
return this.registry[ name ];
|
2014-08-20 22:35:43 +00:00
|
|
|
|
}
|
2013-12-03 19:48:24 +00:00
|
|
|
|
};
|
2014-07-23 20:53:29 +00:00
|
|
|
|
|
2013-12-03 19:48:24 +00:00
|
|
|
|
/**
|
|
|
|
|
|
* @class OO.Factory
|
|
|
|
|
|
* @extends OO.Registry
|
|
|
|
|
|
*
|
|
|
|
|
|
* @constructor
|
|
|
|
|
|
*/
|
|
|
|
|
|
oo.Factory = function OoFactory() {
|
2015-04-29 01:24:59 +00:00
|
|
|
|
// Parent constructor
|
2017-04-05 15:40:22 +00:00
|
|
|
|
oo.Factory.super.call( this );
|
2013-12-03 19:48:24 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/* Inheritance */
|
|
|
|
|
|
|
|
|
|
|
|
oo.inheritClass( oo.Factory, oo.Registry );
|
|
|
|
|
|
|
|
|
|
|
|
/* Methods */
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Register a constructor with the factory.
|
|
|
|
|
|
*
|
|
|
|
|
|
* Classes must have a static `name` property to be registered.
|
|
|
|
|
|
*
|
|
|
|
|
|
* function MyClass() {};
|
2014-04-02 21:40:24 +00:00
|
|
|
|
* OO.initClass( MyClass );
|
2013-12-03 19:48:24 +00:00
|
|
|
|
* // Adds a static property to the class defining a symbolic name
|
2014-04-02 21:40:24 +00:00
|
|
|
|
* MyClass.static.name = 'mine';
|
2013-12-03 19:48:24 +00:00
|
|
|
|
* // Registers class with factory, available via symbolic name 'mine'
|
|
|
|
|
|
* factory.register( MyClass );
|
|
|
|
|
|
*
|
|
|
|
|
|
* @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
|
|
|
|
|
|
*/
|
|
|
|
|
|
oo.Factory.prototype.register = function ( constructor ) {
|
|
|
|
|
|
var name;
|
|
|
|
|
|
|
|
|
|
|
|
if ( typeof constructor !== 'function' ) {
|
|
|
|
|
|
throw new Error( 'constructor must be a function, cannot be a ' + typeof constructor );
|
|
|
|
|
|
}
|
|
|
|
|
|
name = constructor.static && constructor.static.name;
|
|
|
|
|
|
if ( typeof name !== 'string' || name === '' ) {
|
|
|
|
|
|
throw new Error( 'Name must be a string and must not be empty' );
|
|
|
|
|
|
}
|
2014-03-08 01:36:25 +00:00
|
|
|
|
|
2015-04-29 01:24:59 +00:00
|
|
|
|
// Parent method
|
2017-04-05 15:40:22 +00:00
|
|
|
|
oo.Factory.super.prototype.register.call( this, name, constructor );
|
2013-12-03 19:48:24 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
2015-04-29 01:24:59 +00:00
|
|
|
|
/**
|
|
|
|
|
|
* Unregister a constructor from the factory.
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param {Function} constructor Constructor to unregister
|
|
|
|
|
|
* @throws {Error} Name must be a string and must not be empty
|
|
|
|
|
|
* @throws {Error} Constructor must be a function
|
|
|
|
|
|
*/
|
|
|
|
|
|
oo.Factory.prototype.unregister = function ( constructor ) {
|
|
|
|
|
|
var name;
|
|
|
|
|
|
|
|
|
|
|
|
if ( typeof constructor !== 'function' ) {
|
|
|
|
|
|
throw new Error( 'constructor must be a function, cannot be a ' + typeof constructor );
|
|
|
|
|
|
}
|
|
|
|
|
|
name = constructor.static && constructor.static.name;
|
|
|
|
|
|
if ( typeof name !== 'string' || name === '' ) {
|
|
|
|
|
|
throw new Error( 'Name must be a string and must not be empty' );
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Parent method
|
2017-04-05 15:40:22 +00:00
|
|
|
|
oo.Factory.super.prototype.unregister.call( this, name );
|
2015-04-29 01:24:59 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
2013-12-03 19:48:24 +00:00
|
|
|
|
/**
|
|
|
|
|
|
* 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.
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param {string} name Object name
|
2015-11-11 17:02:06 +00:00
|
|
|
|
* @param {...Mixed} [args] Arguments to pass to the constructor
|
2014-03-08 01:36:25 +00:00
|
|
|
|
* @return {Object} The new object
|
2013-12-03 19:48:24 +00:00
|
|
|
|
* @throws {Error} Unknown object name
|
|
|
|
|
|
*/
|
|
|
|
|
|
oo.Factory.prototype.create = function ( name ) {
|
2015-01-23 20:18:10 +00:00
|
|
|
|
var obj, i,
|
|
|
|
|
|
args = [],
|
2014-08-20 22:35:43 +00:00
|
|
|
|
constructor = this.lookup( name );
|
2013-12-03 19:48:24 +00:00
|
|
|
|
|
2014-08-20 22:35:43 +00:00
|
|
|
|
if ( !constructor ) {
|
2013-12-03 19:48:24 +00:00
|
|
|
|
throw new Error( 'No class registered by that name: ' + name );
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Convert arguments to array and shift the first argument (name) off
|
2015-01-23 20:18:10 +00:00
|
|
|
|
for ( i = 1; i < arguments.length; i++ ) {
|
2015-11-11 17:02:06 +00:00
|
|
|
|
args.push( arguments[ i ] );
|
2015-01-23 20:18:10 +00:00
|
|
|
|
}
|
2013-12-03 19:48:24 +00:00
|
|
|
|
|
|
|
|
|
|
// 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).
|
2017-04-05 15:40:22 +00:00
|
|
|
|
obj = Object.create( constructor.prototype );
|
2013-12-03 19:48:24 +00:00
|
|
|
|
constructor.apply( obj, args );
|
|
|
|
|
|
return obj;
|
|
|
|
|
|
};
|
2014-07-23 20:53:29 +00:00
|
|
|
|
|
2017-04-05 15:40:22 +00:00
|
|
|
|
/* eslint-env node */
|
2013-12-03 19:48:24 +00:00
|
|
|
|
if ( typeof module !== 'undefined' && module.exports ) {
|
|
|
|
|
|
module.exports = oo;
|
|
|
|
|
|
} else {
|
|
|
|
|
|
global.OO = oo;
|
|
|
|
|
|
}
|
2014-07-23 20:53:29 +00:00
|
|
|
|
|
2013-12-03 19:48:24 +00:00
|
|
|
|
}( this ) );
|