Merge "Use IDatabase type hints in /maintenance"
[lhc/web/wiklou.git] / resources / lib / oojs / oojs.jquery.js
1 /*!
2 * OOjs v2.0.0 optimised for jQuery
3 * https://www.mediawiki.org/wiki/OOjs
4 *
5 * Copyright 2011-2017 OOjs Team and other contributors.
6 * Released under the MIT license
7 * https://oojs.mit-license.org
8 *
9 * Date: 2017-04-05T02:18:04Z
10 */
11 ( function ( global ) {
12
13 'use strict';
14
15 /* exported toString */
16 var
17 /**
18 * Namespace for all classes, static methods and static properties.
19 * @class OO
20 * @singleton
21 */
22 oo = {},
23 // Optimisation: Local reference to Object.prototype.hasOwnProperty
24 hasOwn = oo.hasOwnProperty,
25 toString = oo.toString;
26
27 /* Class Methods */
28
29 /**
30 * Utility to initialize a class for OO inheritance.
31 *
32 * Currently this just initializes an empty static object.
33 *
34 * @param {Function} fn
35 */
36 oo.initClass = function ( fn ) {
37 fn.static = fn.static || {};
38 };
39
40 /**
41 * Inherit from prototype to another using Object#create.
42 *
43 * Beware: This redefines the prototype, call before setting your prototypes.
44 *
45 * Beware: This redefines the prototype, can only be called once on a function.
46 * If called multiple times on the same function, the previous prototype is lost.
47 * This is how prototypal inheritance works, it can only be one straight chain
48 * (just like classical inheritance in PHP for example). If you need to work with
49 * multiple constructors consider storing an instance of the other constructor in a
50 * property instead, or perhaps use a mixin (see OO.mixinClass).
51 *
52 * function Thing() {}
53 * Thing.prototype.exists = function () {};
54 *
55 * function Person() {
56 * Person.super.apply( this, arguments );
57 * }
58 * OO.inheritClass( Person, Thing );
59 * Person.static.defaultEyeCount = 2;
60 * Person.prototype.walk = function () {};
61 *
62 * function Jumper() {
63 * Jumper.super.apply( this, arguments );
64 * }
65 * OO.inheritClass( Jumper, Person );
66 * Jumper.prototype.jump = function () {};
67 *
68 * Jumper.static.defaultEyeCount === 2;
69 * var x = new Jumper();
70 * x.jump();
71 * x.walk();
72 * x instanceof Thing && x instanceof Person && x instanceof Jumper;
73 *
74 * @param {Function} targetFn
75 * @param {Function} originFn
76 * @throws {Error} If target already inherits from origin
77 */
78 oo.inheritClass = function ( targetFn, originFn ) {
79 var targetConstructor;
80
81 if ( !originFn ) {
82 throw new Error( 'inheritClass: Origin is not a function (actually ' + originFn + ')' );
83 }
84 if ( targetFn.prototype instanceof originFn ) {
85 throw new Error( 'inheritClass: Target already inherits from origin' );
86 }
87
88 targetConstructor = targetFn.prototype.constructor;
89
90 // Using ['super'] instead of .super because 'super' is not supported
91 // by IE 8 and below (bug 63303).
92 // Provide .parent as alias for code supporting older browsers which
93 // allows people to comply with their style guide.
94 // eslint-disable-next-line dot-notation
95 targetFn[ 'super' ] = targetFn.parent = originFn;
96
97 targetFn.prototype = Object.create( originFn.prototype, {
98 // Restore constructor property of targetFn
99 constructor: {
100 value: targetConstructor,
101 enumerable: false,
102 writable: true,
103 configurable: true
104 }
105 } );
106
107 // Extend static properties - always initialize both sides
108 oo.initClass( originFn );
109 targetFn.static = Object.create( originFn.static );
110 };
111
112 /**
113 * Copy over *own* prototype properties of a mixin.
114 *
115 * The 'constructor' (whether implicit or explicit) is not copied over.
116 *
117 * This does not create inheritance to the origin. If you need inheritance,
118 * use OO.inheritClass instead.
119 *
120 * Beware: This can redefine a prototype property, call before setting your prototypes.
121 *
122 * Beware: Don't call before OO.inheritClass.
123 *
124 * function Foo() {}
125 * function Context() {}
126 *
127 * // Avoid repeating this code
128 * function ContextLazyLoad() {}
129 * ContextLazyLoad.prototype.getContext = function () {
130 * if ( !this.context ) {
131 * this.context = new Context();
132 * }
133 * return this.context;
134 * };
135 *
136 * function FooBar() {}
137 * OO.inheritClass( FooBar, Foo );
138 * OO.mixinClass( FooBar, ContextLazyLoad );
139 *
140 * @param {Function} targetFn
141 * @param {Function} originFn
142 */
143 oo.mixinClass = function ( targetFn, originFn ) {
144 var key;
145
146 if ( !originFn ) {
147 throw new Error( 'mixinClass: Origin is not a function (actually ' + originFn + ')' );
148 }
149
150 // Copy prototype properties
151 for ( key in originFn.prototype ) {
152 if ( key !== 'constructor' && hasOwn.call( originFn.prototype, key ) ) {
153 targetFn.prototype[ key ] = originFn.prototype[ key ];
154 }
155 }
156
157 // Copy static properties - always initialize both sides
158 oo.initClass( targetFn );
159 if ( originFn.static ) {
160 for ( key in originFn.static ) {
161 if ( hasOwn.call( originFn.static, key ) ) {
162 targetFn.static[ key ] = originFn.static[ key ];
163 }
164 }
165 } else {
166 oo.initClass( originFn );
167 }
168 };
169
170 /**
171 * Test whether one class is a subclass of another, without instantiating it.
172 *
173 * Every class is considered a subclass of Object and of itself.
174 *
175 * @param {Function} testFn The class to be tested
176 * @param {Function} baseFn The base class
177 * @return {boolean} Whether testFn is a subclass of baseFn (or equal to it)
178 */
179 oo.isSubclass = function ( testFn, baseFn ) {
180 return testFn === baseFn || testFn.prototype instanceof baseFn;
181 };
182
183 /* Object Methods */
184
185 /**
186 * Get a deeply nested property of an object using variadic arguments, protecting against
187 * undefined property errors.
188 *
189 * `quux = OO.getProp( obj, 'foo', 'bar', 'baz' );` is equivalent to `quux = obj.foo.bar.baz;`
190 * except that the former protects against JS errors if one of the intermediate properties
191 * is undefined. Instead of throwing an error, this function will return undefined in
192 * that case.
193 *
194 * @param {Object} obj
195 * @param {...Mixed} [keys]
196 * @return {Object|undefined} obj[arguments[1]][arguments[2]].... or undefined
197 */
198 oo.getProp = function ( obj ) {
199 var i,
200 retval = obj;
201 for ( i = 1; i < arguments.length; i++ ) {
202 if ( retval === undefined || retval === null ) {
203 // Trying to access a property of undefined or null causes an error
204 return undefined;
205 }
206 retval = retval[ arguments[ i ] ];
207 }
208 return retval;
209 };
210
211 /**
212 * Set a deeply nested property of an object using variadic arguments, protecting against
213 * undefined property errors.
214 *
215 * `oo.setProp( obj, 'foo', 'bar', 'baz' );` is equivalent to `obj.foo.bar = baz;` except that
216 * the former protects against JS errors if one of the intermediate properties is
217 * undefined. Instead of throwing an error, undefined intermediate properties will be
218 * initialized to an empty object. If an intermediate property is not an object, or if obj itself
219 * is not an object, this function will silently abort.
220 *
221 * @param {Object} obj
222 * @param {...Mixed} [keys]
223 * @param {Mixed} [value]
224 */
225 oo.setProp = function ( obj ) {
226 var i,
227 prop = obj;
228 if ( Object( obj ) !== obj || arguments.length < 2 ) {
229 return;
230 }
231 for ( i = 1; i < arguments.length - 2; i++ ) {
232 if ( prop[ arguments[ i ] ] === undefined ) {
233 prop[ arguments[ i ] ] = {};
234 }
235 if ( Object( prop[ arguments[ i ] ] ) !== prop[ arguments[ i ] ] ) {
236 return;
237 }
238 prop = prop[ arguments[ i ] ];
239 }
240 prop[ arguments[ arguments.length - 2 ] ] = arguments[ arguments.length - 1 ];
241 };
242
243 /**
244 * Delete a deeply nested property of an object using variadic arguments, protecting against
245 * undefined property errors, and deleting resulting empty objects.
246 *
247 * @param {Object} obj
248 * @param {...Mixed} [keys]
249 */
250 oo.deleteProp = function ( obj ) {
251 var i,
252 prop = obj,
253 props = [ prop ];
254 if ( Object( obj ) !== obj || arguments.length < 2 ) {
255 return;
256 }
257 for ( i = 1; i < arguments.length - 1; i++ ) {
258 if ( prop[ arguments[ i ] ] === undefined || Object( prop[ arguments[ i ] ] ) !== prop[ arguments[ i ] ] ) {
259 return;
260 }
261 prop = prop[ arguments[ i ] ];
262 props.push( prop );
263 }
264 delete prop[ arguments[ i ] ];
265 // Walk back through props removing any plain empty objects
266 while ( ( prop = props.pop() ) && oo.isPlainObject( prop ) && !Object.keys( prop ).length ) {
267 delete props[ props.length - 1 ][ arguments[ props.length ] ];
268 }
269 };
270
271 /**
272 * Create a new object that is an instance of the same
273 * constructor as the input, inherits from the same object
274 * and contains the same own properties.
275 *
276 * This makes a shallow non-recursive copy of own properties.
277 * To create a recursive copy of plain objects, use #copy.
278 *
279 * var foo = new Person( mom, dad );
280 * foo.setAge( 21 );
281 * var foo2 = OO.cloneObject( foo );
282 * foo.setAge( 22 );
283 *
284 * // Then
285 * foo2 !== foo; // true
286 * foo2 instanceof Person; // true
287 * foo2.getAge(); // 21
288 * foo.getAge(); // 22
289 *
290 * @param {Object} origin
291 * @return {Object} Clone of origin
292 */
293 oo.cloneObject = function ( origin ) {
294 var key, r;
295
296 r = Object.create( origin.constructor.prototype );
297
298 for ( key in origin ) {
299 if ( hasOwn.call( origin, key ) ) {
300 r[ key ] = origin[ key ];
301 }
302 }
303
304 return r;
305 };
306
307 /**
308 * Get an array of all property values in an object.
309 *
310 * @param {Object} obj Object to get values from
311 * @return {Array} List of object values
312 */
313 oo.getObjectValues = function ( obj ) {
314 var key, values;
315
316 if ( obj !== Object( obj ) ) {
317 throw new TypeError( 'Called on non-object' );
318 }
319
320 values = [];
321 for ( key in obj ) {
322 if ( hasOwn.call( obj, key ) ) {
323 values[ values.length ] = obj[ key ];
324 }
325 }
326
327 return values;
328 };
329
330 /**
331 * Use binary search to locate an element in a sorted array.
332 *
333 * searchFunc is given an element from the array. `searchFunc(elem)` must return a number
334 * above 0 if the element we're searching for is to the right of (has a higher index than) elem,
335 * below 0 if it is to the left of elem, or zero if it's equal to elem.
336 *
337 * To search for a specific value with a comparator function (a `function cmp(a,b)` that returns
338 * above 0 if `a > b`, below 0 if `a < b`, and 0 if `a == b`), you can use
339 * `searchFunc = cmp.bind( null, value )`.
340 *
341 * @param {Array} arr Array to search in
342 * @param {Function} searchFunc Search function
343 * @param {boolean} [forInsertion] If not found, return index where val could be inserted
344 * @return {number|null} Index where val was found, or null if not found
345 */
346 oo.binarySearch = function ( arr, searchFunc, forInsertion ) {
347 var mid, cmpResult,
348 left = 0,
349 right = arr.length;
350 while ( left < right ) {
351 // Equivalent to Math.floor( ( left + right ) / 2 ) but much faster
352 // eslint-disable-next-line no-bitwise
353 mid = ( left + right ) >> 1;
354 cmpResult = searchFunc( arr[ mid ] );
355 if ( cmpResult < 0 ) {
356 right = mid;
357 } else if ( cmpResult > 0 ) {
358 left = mid + 1;
359 } else {
360 return mid;
361 }
362 }
363 return forInsertion ? right : null;
364 };
365
366 /**
367 * Recursively compare properties between two objects.
368 *
369 * A false result may be caused by property inequality or by properties in one object missing from
370 * the other. An asymmetrical test may also be performed, which checks only that properties in the
371 * first object are present in the second object, but not the inverse.
372 *
373 * If either a or b is null or undefined it will be treated as an empty object.
374 *
375 * @param {Object|undefined|null} a First object to compare
376 * @param {Object|undefined|null} b Second object to compare
377 * @param {boolean} [asymmetrical] Whether to check only that a's values are equal to b's
378 * (i.e. a is a subset of b)
379 * @return {boolean} If the objects contain the same values as each other
380 */
381 oo.compare = function ( a, b, asymmetrical ) {
382 var aValue, bValue, aType, bType, k;
383
384 if ( a === b ) {
385 return true;
386 }
387
388 a = a || {};
389 b = b || {};
390
391 if ( typeof a.nodeType === 'number' && typeof a.isEqualNode === 'function' ) {
392 return a.isEqualNode( b );
393 }
394
395 for ( k in a ) {
396 if ( !hasOwn.call( a, k ) || a[ k ] === undefined || a[ k ] === b[ k ] ) {
397 // Support es3-shim: Without the hasOwn filter, comparing [] to {} will be false in ES3
398 // because the shimmed "forEach" is enumerable and shows up in Array but not Object.
399 // Also ignore undefined values, because there is no conceptual difference between
400 // a key that is absent and a key that is present but whose value is undefined.
401 continue;
402 }
403
404 aValue = a[ k ];
405 bValue = b[ k ];
406 aType = typeof aValue;
407 bType = typeof bValue;
408 if ( aType !== bType ||
409 (
410 ( aType === 'string' || aType === 'number' || aType === 'boolean' ) &&
411 aValue !== bValue
412 ) ||
413 ( aValue === Object( aValue ) && !oo.compare( aValue, bValue, true ) ) ) {
414 return false;
415 }
416 }
417 // If the check is not asymmetrical, recursing with the arguments swapped will verify our result
418 return asymmetrical ? true : oo.compare( b, a, true );
419 };
420
421 /**
422 * Create a plain deep copy of any kind of object.
423 *
424 * Copies are deep, and will either be an object or an array depending on `source`.
425 *
426 * @param {Object} source Object to copy
427 * @param {Function} [leafCallback] Applied to leaf values after they are cloned but before they are added to the clone
428 * @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.
429 * @return {Object} Copy of source object
430 */
431 oo.copy = function ( source, leafCallback, nodeCallback ) {
432 var key, destination;
433
434 if ( nodeCallback ) {
435 // Extensibility: check before attempting to clone source.
436 destination = nodeCallback( source );
437 if ( destination !== undefined ) {
438 return destination;
439 }
440 }
441
442 if ( Array.isArray( source ) ) {
443 // Array (fall through)
444 destination = new Array( source.length );
445 } else if ( source && typeof source.clone === 'function' ) {
446 // Duck type object with custom clone method
447 return leafCallback ? leafCallback( source.clone() ) : source.clone();
448 } else if ( source && typeof source.cloneNode === 'function' ) {
449 // DOM Node
450 return leafCallback ?
451 leafCallback( source.cloneNode( true ) ) :
452 source.cloneNode( true );
453 } else if ( oo.isPlainObject( source ) ) {
454 // Plain objects (fall through)
455 destination = {};
456 } else {
457 // Non-plain objects (incl. functions) and primitive values
458 return leafCallback ? leafCallback( source ) : source;
459 }
460
461 // source is an array or a plain object
462 for ( key in source ) {
463 destination[ key ] = oo.copy( source[ key ], leafCallback, nodeCallback );
464 }
465
466 // This is an internal node, so we don't apply the leafCallback.
467 return destination;
468 };
469
470 /**
471 * Generate a hash of an object based on its name and data.
472 *
473 * Performance optimization: <http://jsperf.com/ve-gethash-201208#/toJson_fnReplacerIfAoForElse>
474 *
475 * To avoid two objects with the same values generating different hashes, we utilize the replacer
476 * argument of JSON.stringify and sort the object by key as it's being serialized. This may or may
477 * not be the fastest way to do this; we should investigate this further.
478 *
479 * Objects and arrays are hashed recursively. When hashing an object that has a .getHash()
480 * function, we call that function and use its return value rather than hashing the object
481 * ourselves. This allows classes to define custom hashing.
482 *
483 * @param {Object} val Object to generate hash for
484 * @return {string} Hash of object
485 */
486 oo.getHash = function ( val ) {
487 return JSON.stringify( val, oo.getHash.keySortReplacer );
488 };
489
490 /**
491 * Sort objects by key (helper function for OO.getHash).
492 *
493 * This is a callback passed into JSON.stringify.
494 *
495 * @method getHash_keySortReplacer
496 * @param {string} key Property name of value being replaced
497 * @param {Mixed} val Property value to replace
498 * @return {Mixed} Replacement value
499 */
500 oo.getHash.keySortReplacer = function ( key, val ) {
501 var normalized, keys, i, len;
502 if ( val && typeof val.getHashObject === 'function' ) {
503 // This object has its own custom hash function, use it
504 val = val.getHashObject();
505 }
506 if ( !Array.isArray( val ) && Object( val ) === val ) {
507 // Only normalize objects when the key-order is ambiguous
508 // (e.g. any object not an array).
509 normalized = {};
510 keys = Object.keys( val ).sort();
511 i = 0;
512 len = keys.length;
513 for ( ; i < len; i += 1 ) {
514 normalized[ keys[ i ] ] = val[ keys[ i ] ];
515 }
516 return normalized;
517 } else {
518 // Primitive values and arrays get stable hashes
519 // by default. Lets those be stringified as-is.
520 return val;
521 }
522 };
523
524 /**
525 * Get the unique values of an array, removing duplicates
526 *
527 * @param {Array} arr Array
528 * @return {Array} Unique values in array
529 */
530 oo.unique = function ( arr ) {
531 return arr.reduce( function ( result, current ) {
532 if ( result.indexOf( current ) === -1 ) {
533 result.push( current );
534 }
535 return result;
536 }, [] );
537 };
538
539 /**
540 * Compute the union (duplicate-free merge) of a set of arrays.
541 *
542 * Arrays values must be convertable to object keys (strings).
543 *
544 * By building an object (with the values for keys) in parallel with
545 * the array, a new item's existence in the union can be computed faster.
546 *
547 * @param {...Array} arrays Arrays to union
548 * @return {Array} Union of the arrays
549 */
550 oo.simpleArrayUnion = function () {
551 var i, ilen, arr, j, jlen,
552 obj = {},
553 result = [];
554
555 for ( i = 0, ilen = arguments.length; i < ilen; i++ ) {
556 arr = arguments[ i ];
557 for ( j = 0, jlen = arr.length; j < jlen; j++ ) {
558 if ( !obj[ arr[ j ] ] ) {
559 obj[ arr[ j ] ] = true;
560 result.push( arr[ j ] );
561 }
562 }
563 }
564
565 return result;
566 };
567
568 /**
569 * Combine arrays (intersection or difference).
570 *
571 * An intersection checks the item exists in 'b' while difference checks it doesn't.
572 *
573 * Arrays values must be convertable to object keys (strings).
574 *
575 * By building an object (with the values for keys) of 'b' we can
576 * compute the result faster.
577 *
578 * @private
579 * @param {Array} a First array
580 * @param {Array} b Second array
581 * @param {boolean} includeB Whether to items in 'b'
582 * @return {Array} Combination (intersection or difference) of arrays
583 */
584 function simpleArrayCombine( a, b, includeB ) {
585 var i, ilen, isInB,
586 bObj = {},
587 result = [];
588
589 for ( i = 0, ilen = b.length; i < ilen; i++ ) {
590 bObj[ b[ i ] ] = true;
591 }
592
593 for ( i = 0, ilen = a.length; i < ilen; i++ ) {
594 isInB = !!bObj[ a[ i ] ];
595 if ( isInB === includeB ) {
596 result.push( a[ i ] );
597 }
598 }
599
600 return result;
601 }
602
603 /**
604 * Compute the intersection of two arrays (items in both arrays).
605 *
606 * Arrays values must be convertable to object keys (strings).
607 *
608 * @param {Array} a First array
609 * @param {Array} b Second array
610 * @return {Array} Intersection of arrays
611 */
612 oo.simpleArrayIntersection = function ( a, b ) {
613 return simpleArrayCombine( a, b, true );
614 };
615
616 /**
617 * Compute the difference of two arrays (items in 'a' but not 'b').
618 *
619 * Arrays values must be convertable to object keys (strings).
620 *
621 * @param {Array} a First array
622 * @param {Array} b Second array
623 * @return {Array} Intersection of arrays
624 */
625 oo.simpleArrayDifference = function ( a, b ) {
626 return simpleArrayCombine( a, b, false );
627 };
628
629 /* global $ */
630
631 oo.isPlainObject = $.isPlainObject;
632
633 /* global hasOwn */
634
635 ( function () {
636
637 /**
638 * @class OO.EventEmitter
639 *
640 * @constructor
641 */
642 oo.EventEmitter = function OoEventEmitter() {
643 // Properties
644
645 /**
646 * Storage of bound event handlers by event name.
647 *
648 * @property
649 */
650 this.bindings = {};
651 };
652
653 oo.initClass( oo.EventEmitter );
654
655 /* Private helper functions */
656
657 /**
658 * Validate a function or method call in a context
659 *
660 * For a method name, check that it names a function in the context object
661 *
662 * @private
663 * @param {Function|string} method Function or method name
664 * @param {Mixed} context The context of the call
665 * @throws {Error} A method name is given but there is no context
666 * @throws {Error} In the context object, no property exists with the given name
667 * @throws {Error} In the context object, the named property is not a function
668 */
669 function validateMethod( method, context ) {
670 // Validate method and context
671 if ( typeof method === 'string' ) {
672 // Validate method
673 if ( context === undefined || context === null ) {
674 throw new Error( 'Method name "' + method + '" has no context.' );
675 }
676 if ( typeof context[ method ] !== 'function' ) {
677 // Technically the property could be replaced by a function before
678 // call time. But this probably signals a typo.
679 throw new Error( 'Property "' + method + '" is not a function' );
680 }
681 } else if ( typeof method !== 'function' ) {
682 throw new Error( 'Invalid callback. Function or method name expected.' );
683 }
684 }
685
686 /* Methods */
687
688 /**
689 * Add a listener to events of a specific event.
690 *
691 * The listener can be a function or the string name of a method; if the latter, then the
692 * name lookup happens at the time the listener is called.
693 *
694 * @param {string} event Type of event to listen to
695 * @param {Function|string} method Function or method name to call when event occurs
696 * @param {Array} [args] Arguments to pass to listener, will be prepended to emitted arguments
697 * @param {Object} [context=null] Context object for function or method call
698 * @throws {Error} Listener argument is not a function or a valid method name
699 * @chainable
700 */
701 oo.EventEmitter.prototype.on = function ( event, method, args, context ) {
702 var bindings;
703
704 validateMethod( method, context );
705
706 if ( hasOwn.call( this.bindings, event ) ) {
707 bindings = this.bindings[ event ];
708 } else {
709 // Auto-initialize bindings list
710 bindings = this.bindings[ event ] = [];
711 }
712 // Add binding
713 bindings.push( {
714 method: method,
715 args: args,
716 context: ( arguments.length < 4 ) ? null : context
717 } );
718 return this;
719 };
720
721 /**
722 * Add a one-time listener to a specific event.
723 *
724 * @param {string} event Type of event to listen to
725 * @param {Function} listener Listener to call when event occurs
726 * @chainable
727 */
728 oo.EventEmitter.prototype.once = function ( event, listener ) {
729 var eventEmitter = this,
730 wrapper = function () {
731 eventEmitter.off( event, wrapper );
732 return listener.apply( this, arguments );
733 };
734 return this.on( event, wrapper );
735 };
736
737 /**
738 * Remove a specific listener from a specific event.
739 *
740 * @param {string} event Type of event to remove listener from
741 * @param {Function|string} [method] Listener to remove. Must be in the same form as was passed
742 * to "on". Omit to remove all listeners.
743 * @param {Object} [context=null] Context object function or method call
744 * @chainable
745 * @throws {Error} Listener argument is not a function or a valid method name
746 */
747 oo.EventEmitter.prototype.off = function ( event, method, context ) {
748 var i, bindings;
749
750 if ( arguments.length === 1 ) {
751 // Remove all bindings for event
752 delete this.bindings[ event ];
753 return this;
754 }
755
756 validateMethod( method, context );
757
758 if ( !hasOwn.call( this.bindings, event ) || !this.bindings[ event ].length ) {
759 // No matching bindings
760 return this;
761 }
762
763 // Default to null context
764 if ( arguments.length < 3 ) {
765 context = null;
766 }
767
768 // Remove matching handlers
769 bindings = this.bindings[ event ];
770 i = bindings.length;
771 while ( i-- ) {
772 if ( bindings[ i ].method === method && bindings[ i ].context === context ) {
773 bindings.splice( i, 1 );
774 }
775 }
776
777 // Cleanup if now empty
778 if ( bindings.length === 0 ) {
779 delete this.bindings[ event ];
780 }
781 return this;
782 };
783
784 /**
785 * Emit an event.
786 *
787 * @param {string} event Type of event
788 * @param {...Mixed} args First in a list of variadic arguments passed to event handler (optional)
789 * @return {boolean} Whether the event was handled by at least one listener
790 */
791 oo.EventEmitter.prototype.emit = function ( event ) {
792 var args = [],
793 i, len, binding, bindings, method;
794
795 if ( hasOwn.call( this.bindings, event ) ) {
796 // Slicing ensures that we don't get tripped up by event handlers that add/remove bindings
797 bindings = this.bindings[ event ].slice();
798 for ( i = 1, len = arguments.length; i < len; i++ ) {
799 args.push( arguments[ i ] );
800 }
801 for ( i = 0, len = bindings.length; i < len; i++ ) {
802 binding = bindings[ i ];
803 if ( typeof binding.method === 'string' ) {
804 // Lookup method by name (late binding)
805 method = binding.context[ binding.method ];
806 } else {
807 method = binding.method;
808 }
809 method.apply(
810 binding.context,
811 binding.args ? binding.args.concat( args ) : args
812 );
813 }
814 return true;
815 }
816 return false;
817 };
818
819 /**
820 * Connect event handlers to an object.
821 *
822 * @param {Object} context Object to call methods on when events occur
823 * @param {Object.<string,string>|Object.<string,Function>|Object.<string,Array>} methods List of
824 * event bindings keyed by event name containing either method names, functions or arrays containing
825 * method name or function followed by a list of arguments to be passed to callback before emitted
826 * arguments
827 * @chainable
828 */
829 oo.EventEmitter.prototype.connect = function ( context, methods ) {
830 var method, args, event;
831
832 for ( event in methods ) {
833 method = methods[ event ];
834 // Allow providing additional args
835 if ( Array.isArray( method ) ) {
836 args = method.slice( 1 );
837 method = method[ 0 ];
838 } else {
839 args = [];
840 }
841 // Add binding
842 this.on( event, method, args, context );
843 }
844 return this;
845 };
846
847 /**
848 * Disconnect event handlers from an object.
849 *
850 * @param {Object} context Object to disconnect methods from
851 * @param {Object.<string,string>|Object.<string,Function>|Object.<string,Array>} [methods] List of
852 * event bindings keyed by event name. Values can be either method names or functions, but must be
853 * consistent with those used in the corresponding call to "connect".
854 * @chainable
855 */
856 oo.EventEmitter.prototype.disconnect = function ( context, methods ) {
857 var i, event, method, bindings;
858
859 if ( methods ) {
860 // Remove specific connections to the context
861 for ( event in methods ) {
862 method = methods[ event ];
863 if ( Array.isArray( method ) ) {
864 method = method[ 0 ];
865 }
866 this.off( event, method, context );
867 }
868 } else {
869 // Remove all connections to the context
870 for ( event in this.bindings ) {
871 bindings = this.bindings[ event ];
872 i = bindings.length;
873 while ( i-- ) {
874 // bindings[i] may have been removed by the previous step's
875 // this.off so check it still exists
876 if ( bindings[ i ] && bindings[ i ].context === context ) {
877 this.off( event, bindings[ i ].method, context );
878 }
879 }
880 }
881 }
882
883 return this;
884 };
885
886 }() );
887
888 ( function () {
889
890 /**
891 * Contain and manage a list of OO.EventEmitter items.
892 *
893 * Aggregates and manages their events collectively.
894 *
895 * This mixin must be used in a class that also mixes in OO.EventEmitter.
896 *
897 * @abstract
898 * @class OO.EmitterList
899 * @constructor
900 */
901 oo.EmitterList = function OoEmitterList() {
902 this.items = [];
903 this.aggregateItemEvents = {};
904 };
905
906 /* Events */
907
908 /**
909 * Item has been added
910 *
911 * @event add
912 * @param {OO.EventEmitter} item Added item
913 * @param {number} index Index items were added at
914 */
915
916 /**
917 * Item has been moved to a new index
918 *
919 * @event move
920 * @param {OO.EventEmitter} item Moved item
921 * @param {number} index Index item was moved to
922 * @param {number} oldIndex The original index the item was in
923 */
924
925 /**
926 * Item has been removed
927 *
928 * @event remove
929 * @param {OO.EventEmitter} item Removed item
930 * @param {number} index Index the item was removed from
931 */
932
933 /**
934 * @event clear The list has been cleared of items
935 */
936
937 /* Methods */
938
939 /**
940 * Normalize requested index to fit into the bounds of the given array.
941 *
942 * @private
943 * @static
944 * @param {Array} arr Given array
945 * @param {number|undefined} index Requested index
946 * @return {number} Normalized index
947 */
948 function normalizeArrayIndex( arr, index ) {
949 return ( index === undefined || index < 0 || index >= arr.length ) ?
950 arr.length :
951 index;
952 }
953
954 /**
955 * Get all items.
956 *
957 * @return {OO.EventEmitter[]} Items in the list
958 */
959 oo.EmitterList.prototype.getItems = function () {
960 return this.items.slice( 0 );
961 };
962
963 /**
964 * Get the index of a specific item.
965 *
966 * @param {OO.EventEmitter} item Requested item
967 * @return {number} Index of the item
968 */
969 oo.EmitterList.prototype.getItemIndex = function ( item ) {
970 return this.items.indexOf( item );
971 };
972
973 /**
974 * Get number of items.
975 *
976 * @return {number} Number of items in the list
977 */
978 oo.EmitterList.prototype.getItemCount = function () {
979 return this.items.length;
980 };
981
982 /**
983 * Check if a list contains no items.
984 *
985 * @return {boolean} Group is empty
986 */
987 oo.EmitterList.prototype.isEmpty = function () {
988 return !this.items.length;
989 };
990
991 /**
992 * Aggregate the events emitted by the group.
993 *
994 * When events are aggregated, the group will listen to all contained items for the event,
995 * and then emit the event under a new name. The new event will contain an additional leading
996 * parameter containing the item that emitted the original event. Other arguments emitted from
997 * the original event are passed through.
998 *
999 * @param {Object.<string,string|null>} events An object keyed by the name of the event that should be
1000 * aggregated (e.g., ‘click’) and the value of the new name to use (e.g., ‘groupClick’).
1001 * A `null` value will remove aggregated events.
1002
1003 * @throws {Error} If aggregation already exists
1004 */
1005 oo.EmitterList.prototype.aggregate = function ( events ) {
1006 var i, item, add, remove, itemEvent, groupEvent;
1007
1008 for ( itemEvent in events ) {
1009 groupEvent = events[ itemEvent ];
1010
1011 // Remove existing aggregated event
1012 if ( Object.prototype.hasOwnProperty.call( this.aggregateItemEvents, itemEvent ) ) {
1013 // Don't allow duplicate aggregations
1014 if ( groupEvent ) {
1015 throw new Error( 'Duplicate item event aggregation for ' + itemEvent );
1016 }
1017 // Remove event aggregation from existing items
1018 for ( i = 0; i < this.items.length; i++ ) {
1019 item = this.items[ i ];
1020 if ( item.connect && item.disconnect ) {
1021 remove = {};
1022 remove[ itemEvent ] = [ 'emit', this.aggregateItemEvents[ itemEvent ], item ];
1023 item.disconnect( this, remove );
1024 }
1025 }
1026 // Prevent future items from aggregating event
1027 delete this.aggregateItemEvents[ itemEvent ];
1028 }
1029
1030 // Add new aggregate event
1031 if ( groupEvent ) {
1032 // Make future items aggregate event
1033 this.aggregateItemEvents[ itemEvent ] = groupEvent;
1034 // Add event aggregation to existing items
1035 for ( i = 0; i < this.items.length; i++ ) {
1036 item = this.items[ i ];
1037 if ( item.connect && item.disconnect ) {
1038 add = {};
1039 add[ itemEvent ] = [ 'emit', groupEvent, item ];
1040 item.connect( this, add );
1041 }
1042 }
1043 }
1044 }
1045 };
1046
1047 /**
1048 * Add items to the list.
1049 *
1050 * @param {OO.EventEmitter|OO.EventEmitter[]} items Item to add or
1051 * an array of items to add
1052 * @param {number} [index] Index to add items at. If no index is
1053 * given, or if the index that is given is invalid, the item
1054 * will be added at the end of the list.
1055 * @chainable
1056 * @fires add
1057 * @fires move
1058 */
1059 oo.EmitterList.prototype.addItems = function ( items, index ) {
1060 var i, oldIndex;
1061
1062 if ( !Array.isArray( items ) ) {
1063 items = [ items ];
1064 }
1065
1066 if ( items.length === 0 ) {
1067 return this;
1068 }
1069
1070 index = normalizeArrayIndex( this.items, index );
1071 for ( i = 0; i < items.length; i++ ) {
1072 oldIndex = this.items.indexOf( items[ i ] );
1073 if ( oldIndex !== -1 ) {
1074 // Move item to new index
1075 index = this.moveItem( items[ i ], index );
1076 this.emit( 'move', items[ i ], index, oldIndex );
1077 } else {
1078 // insert item at index
1079 index = this.insertItem( items[ i ], index );
1080 this.emit( 'add', items[ i ], index );
1081 }
1082 index++;
1083 }
1084
1085 return this;
1086 };
1087
1088 /**
1089 * Move an item from its current position to a new index.
1090 *
1091 * The item is expected to exist in the list. If it doesn't,
1092 * the method will throw an exception.
1093 *
1094 * @private
1095 * @param {OO.EventEmitter} item Items to add
1096 * @param {number} newIndex Index to move the item to
1097 * @return {number} The index the item was moved to
1098 * @throws {Error} If item is not in the list
1099 */
1100 oo.EmitterList.prototype.moveItem = function ( item, newIndex ) {
1101 var existingIndex = this.items.indexOf( item );
1102
1103 if ( existingIndex === -1 ) {
1104 throw new Error( 'Item cannot be moved, because it is not in the list.' );
1105 }
1106
1107 newIndex = normalizeArrayIndex( this.items, newIndex );
1108
1109 // Remove the item from the current index
1110 this.items.splice( existingIndex, 1 );
1111
1112 // Adjust new index after removal
1113 newIndex--;
1114
1115 // Move the item to the new index
1116 this.items.splice( newIndex, 0, item );
1117
1118 return newIndex;
1119 };
1120
1121 /**
1122 * Utility method to insert an item into the list, and
1123 * connect it to aggregate events.
1124 *
1125 * Don't call this directly unless you know what you're doing.
1126 * Use #addItems instead.
1127 *
1128 * This method can be extended in child classes to produce
1129 * different behavior when an item is inserted. For example,
1130 * inserted items may also be attached to the DOM or may
1131 * interact with some other nodes in certain ways. Extending
1132 * this method is allowed, but if overriden, the aggregation
1133 * of events must be preserved, or behavior of emitted events
1134 * will be broken.
1135 *
1136 * If you are extending this method, please make sure the
1137 * parent method is called.
1138 *
1139 * @protected
1140 * @param {OO.EventEmitter} item Items to add
1141 * @param {number} index Index to add items at
1142 * @return {number} The index the item was added at
1143 */
1144 oo.EmitterList.prototype.insertItem = function ( item, index ) {
1145 var events, event;
1146
1147 // Add the item to event aggregation
1148 if ( item.connect && item.disconnect ) {
1149 events = {};
1150 for ( event in this.aggregateItemEvents ) {
1151 events[ event ] = [ 'emit', this.aggregateItemEvents[ event ], item ];
1152 }
1153 item.connect( this, events );
1154 }
1155
1156 index = normalizeArrayIndex( this.items, index );
1157
1158 // Insert into items array
1159 this.items.splice( index, 0, item );
1160 return index;
1161 };
1162
1163 /**
1164 * Remove items.
1165 *
1166 * @param {OO.EventEmitter[]} items Items to remove
1167 * @chainable
1168 * @fires remove
1169 */
1170 oo.EmitterList.prototype.removeItems = function ( items ) {
1171 var i, item, index;
1172
1173 if ( !Array.isArray( items ) ) {
1174 items = [ items ];
1175 }
1176
1177 if ( items.length === 0 ) {
1178 return this;
1179 }
1180
1181 // Remove specific items
1182 for ( i = 0; i < items.length; i++ ) {
1183 item = items[ i ];
1184 index = this.items.indexOf( item );
1185 if ( index !== -1 ) {
1186 if ( item.connect && item.disconnect ) {
1187 // Disconnect all listeners from the item
1188 item.disconnect( this );
1189 }
1190 this.items.splice( index, 1 );
1191 this.emit( 'remove', item, index );
1192 }
1193 }
1194
1195 return this;
1196 };
1197
1198 /**
1199 * Clear all items
1200 *
1201 * @chainable
1202 * @fires clear
1203 */
1204 oo.EmitterList.prototype.clearItems = function () {
1205 var i, item,
1206 cleared = this.items.splice( 0, this.items.length );
1207
1208 // Disconnect all items
1209 for ( i = 0; i < cleared.length; i++ ) {
1210 item = cleared[ i ];
1211 if ( item.connect && item.disconnect ) {
1212 item.disconnect( this );
1213 }
1214 }
1215
1216 this.emit( 'clear' );
1217
1218 return this;
1219 };
1220
1221 }() );
1222
1223 /**
1224 * Manage a sorted list of OO.EmitterList objects.
1225 *
1226 * The sort order is based on a callback that compares two items. The return value of
1227 * callback( a, b ) must be less than zero if a < b, greater than zero if a > b, and zero
1228 * if a is equal to b. The callback should only return zero if the two objects are
1229 * considered equal.
1230 *
1231 * When an item changes in a way that could affect their sorting behavior, it must
1232 * emit the itemSortChange event. This will cause it to be re-sorted automatically.
1233 *
1234 * This mixin must be used in a class that also mixes in OO.EventEmitter.
1235 *
1236 * @abstract
1237 * @class OO.SortedEmitterList
1238 * @mixins OO.EmitterList
1239 * @constructor
1240 * @param {Function} sortingCallback Callback that compares two items.
1241 */
1242 oo.SortedEmitterList = function OoSortedEmitterList( sortingCallback ) {
1243 // Mixin constructors
1244 oo.EmitterList.call( this );
1245
1246 this.sortingCallback = sortingCallback;
1247
1248 // Listen to sortChange event and make sure
1249 // we re-sort the changed item when that happens
1250 this.aggregate( {
1251 sortChange: 'itemSortChange'
1252 } );
1253
1254 this.connect( this, {
1255 itemSortChange: 'onItemSortChange'
1256 } );
1257 };
1258
1259 oo.mixinClass( oo.SortedEmitterList, oo.EmitterList );
1260
1261 /* Events */
1262
1263 /**
1264 * An item has changed properties that affect its sort positioning
1265 * inside the list.
1266 *
1267 * @private
1268 * @event itemSortChange
1269 */
1270
1271 /* Methods */
1272
1273 /**
1274 * Handle a case where an item changed a property that relates
1275 * to its sorted order
1276 *
1277 * @param {OO.EventEmitter} item Item in the list
1278 */
1279 oo.SortedEmitterList.prototype.onItemSortChange = function ( item ) {
1280 // Remove the item
1281 this.removeItems( item );
1282 // Re-add the item so it is in the correct place
1283 this.addItems( item );
1284 };
1285
1286 /**
1287 * Change the sorting callback for this sorted list.
1288 *
1289 * The callback receives two items. The return value of callback(a, b) must be less than zero
1290 * if a < b, greater than zero if a > b, and zero if a is equal to b.
1291 *
1292 * @param {Function} sortingCallback Sorting callback
1293 */
1294 oo.SortedEmitterList.prototype.setSortingCallback = function ( sortingCallback ) {
1295 var items = this.getItems();
1296
1297 this.sortingCallback = sortingCallback;
1298
1299 // Empty the list
1300 this.clearItems();
1301 // Re-add the items in the new order
1302 this.addItems( items );
1303 };
1304
1305 /**
1306 * Add items to the sorted list.
1307 *
1308 * @chainable
1309 * @param {OO.EventEmitter|OO.EventEmitter[]} items Item to add or
1310 * an array of items to add
1311 */
1312 oo.SortedEmitterList.prototype.addItems = function ( items ) {
1313 var index, i, insertionIndex;
1314
1315 if ( !Array.isArray( items ) ) {
1316 items = [ items ];
1317 }
1318
1319 if ( items.length === 0 ) {
1320 return this;
1321 }
1322
1323 for ( i = 0; i < items.length; i++ ) {
1324 // Find insertion index
1325 insertionIndex = this.findInsertionIndex( items[ i ] );
1326
1327 // Check if the item exists using the sorting callback
1328 // and remove it first if it exists
1329 if (
1330 // First make sure the insertion index is not at the end
1331 // of the list (which means it does not point to any actual
1332 // items)
1333 insertionIndex <= this.items.length &&
1334 // Make sure there actually is an item in this index
1335 this.items[ insertionIndex ] &&
1336 // The callback returns 0 if the items are equal
1337 this.sortingCallback( this.items[ insertionIndex ], items[ i ] ) === 0
1338 ) {
1339 // Remove the existing item
1340 this.removeItems( this.items[ insertionIndex ] );
1341 }
1342
1343 // Insert item at the insertion index
1344 index = this.insertItem( items[ i ], insertionIndex );
1345 this.emit( 'add', items[ i ], index );
1346 }
1347
1348 return this;
1349 };
1350
1351 /**
1352 * Find the index a given item should be inserted at. If the item is already
1353 * in the list, this will return the index where the item currently is.
1354 *
1355 * @param {OO.EventEmitter} item Items to insert
1356 * @return {number} The index the item should be inserted at
1357 */
1358 oo.SortedEmitterList.prototype.findInsertionIndex = function ( item ) {
1359 var list = this;
1360
1361 return oo.binarySearch(
1362 this.items,
1363 // Fake a this.sortingCallback.bind( null, item ) call here
1364 // otherwise this doesn't pass tests in phantomJS
1365 function ( otherItem ) {
1366 return list.sortingCallback( item, otherItem );
1367 },
1368 true
1369 );
1370
1371 };
1372
1373 /* global hasOwn */
1374
1375 /**
1376 * @class OO.Registry
1377 * @mixins OO.EventEmitter
1378 *
1379 * @constructor
1380 */
1381 oo.Registry = function OoRegistry() {
1382 // Mixin constructors
1383 oo.EventEmitter.call( this );
1384
1385 // Properties
1386 this.registry = {};
1387 };
1388
1389 /* Inheritance */
1390
1391 oo.mixinClass( oo.Registry, oo.EventEmitter );
1392
1393 /* Events */
1394
1395 /**
1396 * @event register
1397 * @param {string} name
1398 * @param {Mixed} data
1399 */
1400
1401 /**
1402 * @event unregister
1403 * @param {string} name
1404 * @param {Mixed} data Data removed from registry
1405 */
1406
1407 /* Methods */
1408
1409 /**
1410 * Associate one or more symbolic names with some data.
1411 *
1412 * Any existing entry with the same name will be overridden.
1413 *
1414 * @param {string|string[]} name Symbolic name or list of symbolic names
1415 * @param {Mixed} data Data to associate with symbolic name
1416 * @fires register
1417 * @throws {Error} Name argument must be a string or array
1418 */
1419 oo.Registry.prototype.register = function ( name, data ) {
1420 var i, len;
1421 if ( typeof name === 'string' ) {
1422 this.registry[ name ] = data;
1423 this.emit( 'register', name, data );
1424 } else if ( Array.isArray( name ) ) {
1425 for ( i = 0, len = name.length; i < len; i++ ) {
1426 this.register( name[ i ], data );
1427 }
1428 } else {
1429 throw new Error( 'Name must be a string or array, cannot be a ' + typeof name );
1430 }
1431 };
1432
1433 /**
1434 * Remove one or more symbolic names from the registry
1435 *
1436 * @param {string|string[]} name Symbolic name or list of symbolic names
1437 * @fires unregister
1438 * @throws {Error} Name argument must be a string or array
1439 */
1440 oo.Registry.prototype.unregister = function ( name ) {
1441 var i, len, data;
1442 if ( typeof name === 'string' ) {
1443 data = this.lookup( name );
1444 if ( data !== undefined ) {
1445 delete this.registry[ name ];
1446 this.emit( 'unregister', name, data );
1447 }
1448 } else if ( Array.isArray( name ) ) {
1449 for ( i = 0, len = name.length; i < len; i++ ) {
1450 this.unregister( name[ i ] );
1451 }
1452 } else {
1453 throw new Error( 'Name must be a string or array, cannot be a ' + typeof name );
1454 }
1455 };
1456
1457 /**
1458 * Get data for a given symbolic name.
1459 *
1460 * @param {string} name Symbolic name
1461 * @return {Mixed|undefined} Data associated with symbolic name
1462 */
1463 oo.Registry.prototype.lookup = function ( name ) {
1464 if ( hasOwn.call( this.registry, name ) ) {
1465 return this.registry[ name ];
1466 }
1467 };
1468
1469 /**
1470 * @class OO.Factory
1471 * @extends OO.Registry
1472 *
1473 * @constructor
1474 */
1475 oo.Factory = function OoFactory() {
1476 // Parent constructor
1477 oo.Factory.super.call( this );
1478 };
1479
1480 /* Inheritance */
1481
1482 oo.inheritClass( oo.Factory, oo.Registry );
1483
1484 /* Methods */
1485
1486 /**
1487 * Register a constructor with the factory.
1488 *
1489 * Classes must have a static `name` property to be registered.
1490 *
1491 * function MyClass() {};
1492 * OO.initClass( MyClass );
1493 * // Adds a static property to the class defining a symbolic name
1494 * MyClass.static.name = 'mine';
1495 * // Registers class with factory, available via symbolic name 'mine'
1496 * factory.register( MyClass );
1497 *
1498 * @param {Function} constructor Constructor to use when creating object
1499 * @throws {Error} Name must be a string and must not be empty
1500 * @throws {Error} Constructor must be a function
1501 */
1502 oo.Factory.prototype.register = function ( constructor ) {
1503 var name;
1504
1505 if ( typeof constructor !== 'function' ) {
1506 throw new Error( 'constructor must be a function, cannot be a ' + typeof constructor );
1507 }
1508 name = constructor.static && constructor.static.name;
1509 if ( typeof name !== 'string' || name === '' ) {
1510 throw new Error( 'Name must be a string and must not be empty' );
1511 }
1512
1513 // Parent method
1514 oo.Factory.super.prototype.register.call( this, name, constructor );
1515 };
1516
1517 /**
1518 * Unregister a constructor from the factory.
1519 *
1520 * @param {Function} constructor Constructor to unregister
1521 * @throws {Error} Name must be a string and must not be empty
1522 * @throws {Error} Constructor must be a function
1523 */
1524 oo.Factory.prototype.unregister = function ( constructor ) {
1525 var name;
1526
1527 if ( typeof constructor !== 'function' ) {
1528 throw new Error( 'constructor must be a function, cannot be a ' + typeof constructor );
1529 }
1530 name = constructor.static && constructor.static.name;
1531 if ( typeof name !== 'string' || name === '' ) {
1532 throw new Error( 'Name must be a string and must not be empty' );
1533 }
1534
1535 // Parent method
1536 oo.Factory.super.prototype.unregister.call( this, name );
1537 };
1538
1539 /**
1540 * Create an object based on a name.
1541 *
1542 * Name is used to look up the constructor to use, while all additional arguments are passed to the
1543 * constructor directly, so leaving one out will pass an undefined to the constructor.
1544 *
1545 * @param {string} name Object name
1546 * @param {...Mixed} [args] Arguments to pass to the constructor
1547 * @return {Object} The new object
1548 * @throws {Error} Unknown object name
1549 */
1550 oo.Factory.prototype.create = function ( name ) {
1551 var obj, i,
1552 args = [],
1553 constructor = this.lookup( name );
1554
1555 if ( !constructor ) {
1556 throw new Error( 'No class registered by that name: ' + name );
1557 }
1558
1559 // Convert arguments to array and shift the first argument (name) off
1560 for ( i = 1; i < arguments.length; i++ ) {
1561 args.push( arguments[ i ] );
1562 }
1563
1564 // We can't use the "new" operator with .apply directly because apply needs a
1565 // context. So instead just do what "new" does: create an object that inherits from
1566 // the constructor's prototype (which also makes it an "instanceof" the constructor),
1567 // then invoke the constructor with the object as context, and return it (ignoring
1568 // the constructor's return value).
1569 obj = Object.create( constructor.prototype );
1570 constructor.apply( obj, args );
1571 return obj;
1572 };
1573
1574 /* eslint-env node */
1575 if ( typeof module !== 'undefined' && module.exports ) {
1576 module.exports = oo;
1577 } else {
1578 global.OO = oo;
1579 }
1580
1581 }( this ) );