-
Notifications
You must be signed in to change notification settings - Fork 0
/
keylime.min.js
1 lines (1 loc) · 9.05 KB
/
keylime.min.js
1
!function(t,e){"function"==typeof define&&define.amd?define([],e):"object"==typeof exports?module.exports=e():t.keylime=e()}(this,function(){"use strict";function keylime(t){return"string"==typeof t&&(t=createNamedConstructor(t)),convertConstructorToKeylime(t)}function KeylimeConstructor(t){this.descriptor=new KeylimeDescriptor(t),this.prototype=this.prototype||{},Object.defineProperty(this.prototype,"keylime",{value:this.descriptor})}function KeylimeDescriptor(t){this.attributes=null!==t&&"object"==typeof t?t:{},this.initializers=null}function createNamedConstructor(name,runKeylime){return assert("string"==typeof name,"A 'name' string is required to create a named constructor."),eval(["function ",name,"(attrs) {","if (!this || !(this instanceof ",name,")) {","throw new Error(\"Cannot call constructor without the 'new' keyword!\");","}",runKeylime===!1?"":"if (this.keylime) { this.keylime.init(this, attrs); }","}; ",name].join(""))}function convertConstructorToKeylime(t,e){return assert("function"==typeof t,"A function parameter is required to convert to a KeylimeConstructor"),t.__proto__=KeylimeConstructor.prototype,t.descriptor=new KeylimeDescriptor(e),Object.defineProperty(t.prototype,"keylime",{value:t.descriptor}),t}function setAttributesUsingMapAndValues(t,e,r){var i,n;assert("object"==typeof t,"A 'target' object is required to set attribute values.");for(var o in e)if(e.hasOwnProperty(o)){if(i=e[o],r&&void 0!==r[i.name])n=r[i.name];else switch(i.copyMode){case"shallow":n=clone(i.defaultValue);break;case"deep":n=clone(i.defaultValue,!0);break;default:n=i.defaultValue}if("function"==typeof n&&(n=n()),i.handlers)if("function"==typeof i.handlers)n=i.handlers(n,t,i);else if(isArray(i.handlers))for(var s=0;s<i.handlers.length;s++)n=i.handlers[s](n,t,i);t[i.name]=n}return t}function setAttributesForInstance(t,e,r){var i=null;return r instanceof KeylimeConstructor?i=r.getAttrs():void 0!==t.keylime&&(i=t.keylime.attributes),assert(null!==i&&"object"==typeof i,"You must supply an instance of a Keylime constructor, or the constructor itself to set attributes."),setAttributesUsingMapAndValues(t,i,e)}function extend(){var t=[].slice.call(arguments);t[0]="object"==typeof t[0]?t[0]:{};for(var e=1;e<t.length;e++)if("object"==typeof t[e])for(var r in t[e])t[e].hasOwnProperty(r)&&(t[0][r]=t[e][r]);return t[0]}function clone(t,e){var r,i,n="[object Boolean]",o="[object Number]",s="[object String]",a="[object Object]",u="[object Array]",l="[object Date]",c="[object RegExp]";if("object"!=typeof t)return t;switch(r=Object.prototype.toString.call(t)){case c:i=new t.constructor(t.source,/\w*$/.exec(t)),i.lastIndex=t.lastIndex;break;case n:case l:i=new t.constructor(+t);break;case o:case s:i=new t.constructor(t);break;case u:i=new t.constructor;for(var p=0;p<t.length;p++)i.push(e?clone(t[p],e):t[p]);break;case a:i=new t.constructor;for(var y in t)t.hasOwnProperty(y)&&(i[y]=e?clone(t[y],e):t[y]);break;default:i=t}return i}function registerExtension(t,e){return assert("string"==typeof t,"You must supply a 'name' parameter to register a new Keylime extension."),assert("function"==typeof e,"You must supply a 'function' parameter to register a new Keylime extension."),assert(void 0===KeylimeConstructor.prototype[t],"Cannot register an extension using the name '"+t+"', it already exists!"),KeylimeConstructor.prototype[t]=function(){var t=[].slice.call(arguments);return t.unshift(this),e.apply(e,t),this},!0}function unregisterExtension(t){return assert("string"==typeof t,"You must supply a 'name' parameter to un-register a Keylime extension."),KeylimeConstructor.prototype[t]?(KeylimeConstructor.prototype[t]=void 0,!0):!1}function KeylimeError(t){this.message=t}function assert(t,e){if(!t)throw new KeylimeError(e)}function isArray(t){return t&&"[object Array]"===Object.prototype.toString.call(t)?!0:!1}return KeylimeConstructor.prototype.__proto__=Function.prototype,KeylimeConstructor.prototype.attr=function(t,e,r){return this.descriptor.setAttr(t,e,extend({copyMode:"deep",handlers:null},r)),this},KeylimeConstructor.prototype.getAttrs=function(){return this.descriptor.attributes},KeylimeConstructor.prototype.method=function(t,e){return assert("string"==typeof t,"You must supply a 'name' to add a method to a constructor."),assert("function"==typeof e,"You must supply a 'function' to add a method to a constructor."),this.prototype[t]=e,this},KeylimeConstructor.prototype.include=function(t){var e=[].slice.call(arguments);return assert("function"==typeof t,"You must supply a 'function' to add include a mixin on a constructor."),e[0]=this,t.apply(t,e),this},KeylimeConstructor.prototype.create=function(){var t={};return t.__proto__=this.prototype,this.apply(t,arguments),t},KeylimeConstructor.prototype.on=function(t,e){var r;switch(3===arguments.length&&(r=arguments[1],e=arguments[2]),assert("string"==typeof t,"A 'name' is required to add an event handler."),assert("function"==typeof e,"A function is required to add an event handler."),t.toLowerCase()){case"init":this.descriptor.addInitHandler(e);break;case"attr":this.descriptor.addAttrHandler(r,e)}return this},KeylimeConstructor.prototype.off=function(t,e){var r,i;switch(3===arguments.length&&(r=arguments[1],e=arguments[2]),assert("string"==typeof t,"An event name is required to remove a handler."),assert("function"==typeof e,"An function is required to remove a handler."),t.toLowerCase()){case"init":i=this.descriptor.removeInitHandler(e);break;case"attr":i=this.descriptor.removeAttrHandler(r,e)}return i},KeylimeConstructor.prototype.offAny=function(t,e){var r;switch(assert("string"==typeof t,"An event name is required to remove all handlers."),t.toLowerCase()){case"init":this.descriptor.removeAllInitHandlers();break;case"attr":this.descriptor.removeAllAttrHandlers(e)}return r},KeylimeDescriptor.prototype.init=function(t,e){if(setAttributesUsingMapAndValues(t,this.attributes,e),this.initializers&&"function"==typeof this.initializers)this.initializers(t);else if(isArray(this.initializers))for(var r=0;r<this.initializers.length;r++)this.initializers[r](t);return t},KeylimeDescriptor.prototype.setAttr=function(t,e,r){return assert("string"==typeof t,"A 'name' string is required to create an attribute."),this.attributes[t]={name:t,defaultValue:e},extend(this.attributes[t],r),this},KeylimeDescriptor.prototype.getAttr=function(t){return assert("string"==typeof t,"A 'name' string is required to get an attribute."),this.attributes[t]},KeylimeDescriptor.prototype.getDefaultValueFor=function(t){var e;return assert("string"==typeof t,"A 'name' string is required to get an attribute defaultValue."),this.attributes[t]&&(e=this.attributes[t].defaultValue),e},KeylimeDescriptor.prototype.addInitHandler=function(t){return assert("function"==typeof t,"A function is required to set an init handler!"),this.initializers?"function"==typeof this.initializers?(this.initializers=[this.initializers],this.initializers.push(t)):isArray(this.initializers)&&this.initializers.push(t):this.initializers=t,this},KeylimeDescriptor.prototype.removeInitHandler=function(t){var e;if(assert("function"==typeof t,"A function reference is required to remove an init handler."),this.initializers===t)return this.initializers=null,!0;if(isArray(this.initializers)){e=[];for(var r=0;r<this.initializers.length;r++)this.initializers[r]!==t&&e.push(this.initializers[r]);return this.initializers=e.length<=0?null:e,!0}return!1},KeylimeDescriptor.prototype.removeAllInitHandlers=function(){var t=this.initializers;return this.initializers=null,t},KeylimeDescriptor.prototype.addAttrHandler=function(t,e){var r;return assert("string"==typeof t,"A 'name' is required to set a handler on an attribute."),assert("function"==typeof e,"A function is required to set a handler on an attribute."),assert(this.attributes[t],"No attribute found for '"+t+"'. Cannot set handler."),r=this.attributes[t],r.handlers?"function"==typeof r.handlers?(r.handlers=[r.handlers],r.handlers.push(e)):isArray(r.handlers)&&r.handlers.push(e):r.handlers=e,this},KeylimeDescriptor.prototype.removeAllAttrHandlers=function(t){var e;return this.attributes[t]&&(e=this.attributes[t].handlers,this.attributes[t].handlers=null),e},KeylimeDescriptor.prototype.removeAttrHandler=function(t,e){var r,i;if(assert("string"==typeof t,"A 'name' is required to remove a handler from an attribute."),assert("function"==typeof e,"A function reference is required to remove a handler from an attribute."),r=this.attributes[t],r&&r.handlers){if(r.handlers===e)return r.handlers=null,!0;if(isArray(r.handlers)){i=[];for(var n=0;n<r.handlers.length;n++)r.handlers[n]!==e&&i.push(r.handlers[n]);return r.handlers=i.length<=0?null:i,!0}}return!1},KeylimeError.prototype.__proto__=Error.prototype,keylime.version="1.0.0",keylime.registerExtension=registerExtension,keylime.unregisterExtension=unregisterExtension,keylime.util={clone:clone,extend:extend},keylime.core={createNamedConstructor:createNamedConstructor,convertConstructorToKeylime:convertConstructorToKeylime,setAttributesUsingMapAndValues:setAttributesUsingMapAndValues,setAttributesForInstance:setAttributesForInstance},keylime.prototypes={KeylimeConstructor:KeylimeConstructor,KeylimeDescriptor:KeylimeDescriptor,KeylimeError:KeylimeError},keylime});