-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
785 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
/*can-construct@3.0.6#can-construct*/ | ||
define(function (require, exports, module) { | ||
'use strict'; | ||
var assign = require('can-util/js/assign'); | ||
var deepAssign = require('can-util/js/deep-assign'); | ||
var dev = require('can-util/js/dev'); | ||
var makeArray = require('can-util/js/make-array'); | ||
var types = require('can-types'); | ||
var namespace = require('can-namespace'); | ||
var initializing = 0; | ||
var Construct = function () { | ||
if (arguments.length) { | ||
return Construct.extend.apply(Construct, arguments); | ||
} | ||
}; | ||
var canGetDescriptor; | ||
try { | ||
Object.getOwnPropertyDescriptor({}); | ||
canGetDescriptor = true; | ||
} catch (e) { | ||
canGetDescriptor = false; | ||
} | ||
var getDescriptor = function (newProps, name) { | ||
var descriptor = Object.getOwnPropertyDescriptor(newProps, name); | ||
if (descriptor && (descriptor.get || descriptor.set)) { | ||
return descriptor; | ||
} | ||
return null; | ||
}, inheritGetterSetter = function (newProps, oldProps, addTo) { | ||
addTo = addTo || newProps; | ||
var descriptor; | ||
for (var name in newProps) { | ||
if (descriptor = getDescriptor(newProps, name)) { | ||
this._defineProperty(addTo, oldProps, name, descriptor); | ||
} else { | ||
Construct._overwrite(addTo, oldProps, name, newProps[name]); | ||
} | ||
} | ||
}, simpleInherit = function (newProps, oldProps, addTo) { | ||
addTo = addTo || newProps; | ||
for (var name in newProps) { | ||
Construct._overwrite(addTo, oldProps, name, newProps[name]); | ||
} | ||
}; | ||
assign(Construct, { | ||
constructorExtends: true, | ||
newInstance: function () { | ||
var inst = this.instance(), args; | ||
if (inst.setup) { | ||
Object.defineProperty(inst, '__inSetup', { | ||
configurable: true, | ||
enumerable: false, | ||
value: true, | ||
writable: true | ||
}); | ||
args = inst.setup.apply(inst, arguments); | ||
if (args instanceof Construct.ReturnValue) { | ||
return args.value; | ||
} | ||
inst.__inSetup = false; | ||
} | ||
if (inst.init) { | ||
inst.init.apply(inst, args || arguments); | ||
} | ||
return inst; | ||
}, | ||
_inherit: canGetDescriptor ? inheritGetterSetter : simpleInherit, | ||
_defineProperty: function (what, oldProps, propName, descriptor) { | ||
Object.defineProperty(what, propName, descriptor); | ||
}, | ||
_overwrite: function (what, oldProps, propName, val) { | ||
Object.defineProperty(what, propName, { | ||
value: val, | ||
configurable: true, | ||
enumerable: true, | ||
writable: true | ||
}); | ||
}, | ||
setup: function (base) { | ||
this.defaults = deepAssign(true, {}, base.defaults, this.defaults); | ||
}, | ||
instance: function () { | ||
initializing = 1; | ||
var inst = new this(); | ||
initializing = 0; | ||
return inst; | ||
}, | ||
extend: function (name, staticProperties, instanceProperties) { | ||
var shortName = name, klass = staticProperties, proto = instanceProperties; | ||
if (typeof shortName !== 'string') { | ||
proto = klass; | ||
klass = shortName; | ||
shortName = null; | ||
} | ||
if (!proto) { | ||
proto = klass; | ||
klass = null; | ||
} | ||
proto = proto || {}; | ||
var _super_class = this, _super = this.prototype, Constructor, prototype; | ||
prototype = this.instance(); | ||
Construct._inherit(proto, _super, prototype); | ||
if (shortName) { | ||
} else if (klass && klass.shortName) { | ||
shortName = klass.shortName; | ||
} else if (this.shortName) { | ||
shortName = this.shortName; | ||
} | ||
function init() { | ||
if (!initializing) { | ||
return (!this || this.constructor !== Constructor) && arguments.length && Constructor.constructorExtends ? Constructor.extend.apply(Constructor, arguments) : Constructor.newInstance.apply(Constructor, arguments); | ||
} | ||
} | ||
Constructor = typeof namedCtor === 'function' ? namedCtor(constructorName, init) : function () { | ||
return init.apply(this, arguments); | ||
}; | ||
for (var propName in _super_class) { | ||
if (_super_class.hasOwnProperty(propName)) { | ||
Constructor[propName] = _super_class[propName]; | ||
} | ||
} | ||
Construct._inherit(klass, _super_class, Constructor); | ||
assign(Constructor, { | ||
constructor: Constructor, | ||
prototype: prototype | ||
}); | ||
if (shortName !== undefined) { | ||
Constructor.shortName = shortName; | ||
} | ||
Constructor.prototype.constructor = Constructor; | ||
var t = [_super_class].concat(makeArray(arguments)), args = Constructor.setup.apply(Constructor, t); | ||
if (Constructor.init) { | ||
Constructor.init.apply(Constructor, args || t); | ||
} | ||
return Constructor; | ||
}, | ||
ReturnValue: function (value) { | ||
this.value = value; | ||
} | ||
}); | ||
Construct.prototype.setup = function () { | ||
}; | ||
Construct.prototype.init = function () { | ||
}; | ||
var oldIsConstructor = types.isConstructor; | ||
types.isConstructor = function (obj) { | ||
return obj.prototype instanceof Construct || oldIsConstructor.call(null, obj); | ||
}; | ||
module.exports = namespace.Construct = Construct; | ||
}); |
Oops, something went wrong.