can
Version:
MIT-licensed, client-side, JavaScript framework that makes building rich web applications easy.
146 lines (145 loc) • 5.66 kB
JavaScript
/*!
* CanJS - 2.3.34
* http://canjs.com/
* Copyright (c) 2018 Bitovi
* Mon, 30 Apr 2018 20:56:51 GMT
* Licensed MIT
*/
/*can@2.3.34#construct/construct*/
define(['can/util/string'], function (can) {
var initializing = 0;
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 {
can.Construct._overwrite(addTo, oldProps, name, newProps[name]);
}
}
}, simpleInherit = function (newProps, oldProps, addTo) {
addTo = addTo || newProps;
for (var name in newProps) {
can.Construct._overwrite(addTo, oldProps, name, newProps[name]);
}
};
can.Construct = function () {
if (arguments.length) {
return can.Construct.extend.apply(can.Construct, arguments);
}
};
can.extend(can.Construct, {
constructorExtends: true,
newInstance: function () {
var inst = this.instance(), args;
if (inst.setup) {
inst.__inSetup = true;
args = inst.setup.apply(inst, arguments);
delete inst.__inSetup;
}
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) {
what[propName] = val;
},
setup: function (base, fullName) {
this.defaults = can.extend(true, {}, base.defaults, this.defaults);
},
instance: function () {
initializing = 1;
var inst = new this();
initializing = 0;
return inst;
},
extend: function (name, staticProperties, instanceProperties) {
var fullName = name, klass = staticProperties, proto = instanceProperties;
if (typeof fullName !== 'string') {
proto = klass;
klass = fullName;
fullName = null;
}
if (!proto) {
proto = klass;
klass = null;
}
proto = proto || {};
var _super_class = this, _super = this.prototype, Constructor, parts, current, _fullName, _shortName, propName, shortName, namespace, prototype;
prototype = this.instance();
can.Construct._inherit(proto, _super, prototype);
if (fullName) {
parts = fullName.split('.');
shortName = parts.pop();
} else if (klass && klass.shortName) {
shortName = klass.shortName;
} else if (this.shortName) {
shortName = this.shortName;
}
if (typeof constructorName === 'undefined') {
Constructor = function () {
return init.apply(this, arguments);
};
}
function init() {
if (!initializing) {
return this.constructor !== Constructor && arguments.length && Constructor.constructorExtends ? Constructor.extend.apply(Constructor, arguments) : Constructor.newInstance.apply(Constructor, arguments);
}
}
for (propName in _super_class) {
if (_super_class.hasOwnProperty(propName)) {
Constructor[propName] = _super_class[propName];
}
}
can.Construct._inherit(klass, _super_class, Constructor);
if (fullName) {
current = can.getObject(parts.join('.'), window, true);
namespace = current;
_fullName = can.underscore(fullName.replace(/\./g, '_'));
_shortName = can.underscore(shortName);
current[shortName] = Constructor;
}
can.extend(Constructor, {
constructor: Constructor,
prototype: prototype,
namespace: namespace,
_shortName: _shortName,
fullName: fullName,
_fullName: _fullName
});
if (shortName !== undefined) {
Constructor.shortName = shortName;
}
Constructor.prototype.constructor = Constructor;
var t = [_super_class].concat(can.makeArray(arguments)), args = Constructor.setup.apply(Constructor, t);
if (Constructor.init) {
Constructor.init.apply(Constructor, args || t);
}
return Constructor;
}
});
can.Construct.prototype.setup = function () {
};
can.Construct.prototype.init = function () {
};
return can.Construct;
});