forked from LeaVerou/bliss
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbliss.min.js
1 lines (1 loc) · 8.71 KB
/
bliss.min.js
1
!function(){"use strict";function t(t,n,r){for(var i in n){if(r){var s=e.type(r);if("own"===r&&!n.hasOwnProperty(i)||"array"===s&&-1===r.indexOf(i)||"regexp"===s&&!r.test(i)||"function"===s&&!r.call(n,i))continue}var o=Object.getOwnPropertyDescriptor(n,i);!o||o.writable&&o.configurable&&o.enumerable&&!o.get&&!o.set?t[i]=n[i]:(delete t[i],Object.defineProperty(t,i,o))}return t}var e=self.Bliss=t(function(t,n){return"string"===e.type(t)?(n||document).querySelector(t):t||null},self.Bliss);t(e,{extend:t,property:e.property||"_",sources:{},$:function(t,e){return t instanceof Node||t instanceof Window?[t]:Array.prototype.slice.call("string"==typeof t?(e||document).querySelectorAll(t):t||[])},type:function(t){if(null===t)return"null";if(void 0===t)return"undefined";var e=(Object.prototype.toString.call(t).match(/^\[object\s+(.*?)\]$/)[1]||"").toLowerCase();return"number"==e&&isNaN(t)?"nan":e},defined:function(){for(var t=0;t<arguments.length;t++)if(void 0!==arguments[t])return arguments[t]},create:function(t,n){if(1===arguments.length){if("string"===e.type(t))return document.createTextNode(t);n=t,t=n.tag,delete n.tag}return e.set(document.createElement(t||"div"),n)},ready:function(t){return t=t||document,new Promise(function(e,n){"loading"!==t.readyState?e():t.addEventListener("DOMContentLoaded",function(){e()})})},Class:function(t){var n=t.constructor||function(){};delete t.constructor;var r=t["abstract"];delete t["abstract"];var i=function(){if(r&&this.constructor===i)throw new Error("Abstract classes cannot be directly instantiated.");return this.constructor["super"]&&this.constructor["super"]!=i&&this.constructor["super"].apply(this,arguments),n.apply(this,arguments)};i["super"]=t["extends"]||null,delete t["extends"],i.prototype=e.extend(Object.create(i["super"]&&i["super"].prototype),{constructor:i}),e.extend(i,t["static"]),delete t["static"];for(var s in t)s in e.classProps&&(e.classProps[s].call(i,i.prototype,t[s]),delete t[s]);return e.extend(i.prototype,t),i.prototype["super"]=i["super"]?i["super"].prototype:null,i},classProps:{lazy:function(t,n,r){if(arguments.length>=3)Object.defineProperty(t,n,{get:function(){return delete this[n],this[n]=r.call(this)},configurable:!0,enumerable:!0});else if(2===arguments.length)for(var i in n)e.lazy(t,i,n[i]);return t},live:function(t,n,r){if(arguments.length>=3)Object.defineProperty(t,n,{get:function(){var t=this["_"+n],e=r.get&&r.get.call(this,t);return void 0!==e?e:t},set:function(t){var e=this["_"+n],i=r.set&&r.set.call(this,t,e);this["_"+n]=void 0!==i?i:t},configurable:r.configurable,enumerable:r.enumerable});else if(2===arguments.length)for(var i in n)e.live(t,i,n[i]);return t}},include:function(){var t=arguments[arguments.length-1],n=2===arguments.length?arguments[0]:!1,r=document.createElement("script");return n?Promise.resolve():new Promise(function(n,i){e.set(r,{async:!0,onload:function(){n(),e.remove(r)},onerror:function(){i()},src:t,inside:document.head})})},fetch:function(t,n){if(!t)throw new TypeError("URL parameter is mandatory and cannot be "+t);t=new URL(t,location),n=n||{},n.data=n.data||"",n.method=n.method||"GET",n.headers=n.headers||{};var r=new XMLHttpRequest;"string"!==e.type(n.data)&&(n.data=Object.keys(n.data).map(function(t){return t+"="+encodeURIComponent(n.data[t])}).join("&")),"GET"===n.method&&n.data&&(t.search+=n.data),document.body.setAttribute("data-loading",t),r.open(n.method,t,!n.sync);for(var i in n)if(i in r)try{r[i]=n[i]}catch(s){self.console&&console.error(s)}"GET"===n.method||n.headers["Content-type"]||n.headers["Content-Type"]||r.setRequestHeader("Content-type","application/x-www-form-urlencoded");for(var o in n.headers)r.setRequestHeader(o,n.headers[o]);return new Promise(function(t,e){r.onload=function(){document.body.removeAttribute("data-loading"),0===r.status||r.status>=200&&r.status<300||304===r.status?t(r):e(Error(r.statusText))},r.onerror=function(){document.body.removeAttribute("data-loading"),e(Error("Network Error"))},r.send("GET"===n.method?null:n.data)})}});var n=e.property;e.Element=function(t){this.subject=t,this.data={},this.bliss={}},e.Element.prototype={set:function(t){"string"===e.type(arguments[0])&&(t={},t[arguments[0]]=arguments[1]);for(var n in t)n in e.setProps?e.setProps[n].call(this,t[n]):n in this?this[n]=t[n]:(this.setAttribute||console.log(this),this.setAttribute(n,t[n]))},transition:function(t,n){return n=+n||400,new Promise(function(r,i){if("transition"in this.style){var s=e.extend({},this.style,/^transition(Duration|Property)$/);e.style(this,{transitionDuration:(n||400)+"ms",transitionProperty:Object.keys(t).join(", ")}),e.once(this,"transitionend",function(){clearTimeout(o),e.style(this,s),r(this)});var o=setTimeout(r,n+50,this);e.style(this,t)}else e.style(this,t),r(this)}.bind(this))},fire:function(t,n){var r=document.createEvent("HTMLEvents");r.initEvent(t,!0,!0),this.dispatchEvent(e.extend(r,n))}},e.setProps={style:function(t){e.extend(this.style,t)},attributes:function(t){for(var e in t)this.setAttribute(e,t[e])},properties:function(t){e.extend(this,t)},events:function(t){if(t&&t.addEventListener){var e=this;if(t[n]&&t[n].bliss){var r=t[n].bliss.listeners;for(var i in r)r[i].forEach(function(t){e.addEventListener(i,t.callback,t.capture)})}for(var s in t)0===s.indexOf("on")&&(this[s]=t[s])}else for(var o in t)o.split(/\s+/).forEach(function(e){this.addEventListener(e,t[o])},this)},once:function(t){2==arguments.length&&(t={},t[arguments[0]]=arguments[1]);for(var e in t)e.split(/\s+/).forEach(function(n){var r=this;this.addEventListener(n,function i(){return r.removeEventListener(n,i),t[e].apply(this,arguments)})},this)},delegate:function(t){3===arguments.length?(t={},t[arguments[0]]={},t[arguments[0]][arguments[1]]=arguments[2]):2===arguments.length&&(t={},t[arguments[0]]=arguments[1]);var e=this;for(var n in t)!function(t,n){e.addEventListener(t,function(t){for(var e in n)t.target.matches(e)&&n[e].call(this,t)})}(n,t[n])},contents:function(t){(t||0===t)&&(Array.isArray(t)?t:[t]).forEach(function(t){/^(string|number|object)$/.test(e.type(t))&&(t=e.create(t)),t instanceof Node&&this.appendChild(t)},this)},inside:function(t){t.appendChild(this)},before:function(t){t.parentNode.insertBefore(this,t)},after:function(t){t.parentNode.insertBefore(this,t.nextSibling)},start:function(t){t.insertBefore(this,t.firstChild)},around:function(t){t.parentNode&&e.before(this,t),(/^template$/i.test(this.nodeName)?this.content||this:this).appendChild(t)}},e.Array=function(t){this.subject=t},e.Array.prototype={all:function(t){var e=$$(arguments).slice(1);return this[t].apply(this,e)}},e.add=function(t,n,r){n=e.extend({$:!0,element:!0,array:!0},n),"string"===e.type(arguments[0])&&(t={},t[arguments[0]]=arguments[1]);for(var i in t)!function(t,i){"function"==e.type(i)&&(!n.element||t in e.Element.prototype&&r||(e.Element.prototype[t]=function(){return this.subject&&e.defined(i.apply(this.subject,arguments),this.subject)}),!n.array||t in e.Array.prototype&&r||(e.Array.prototype[t]=function(){var t=arguments;return this.subject.map(function(n){return n&&e.defined(i.apply(n,t),n)})}),n.$&&(e.sources[t]=e[t]=i,(n.array||n.element)&&(e[t]=function(){var r=[].slice.apply(arguments),i=r.shift(),s=n.array&&Array.isArray(i)?"Array":"Element";return e[s].prototype[t].apply({subject:i},r)})))}(i,t[i])},e.add(e.Array.prototype,{element:!1}),e.add(e.Element.prototype),e.add(e.setProps),e.add(e.classProps,{element:!1,array:!1});var r=document.createElement("_");e.add(e.extend({},HTMLElement.prototype,function(t){return"function"===e.type(r[t])}),null,!0)}(),function(t){"use strict";if(Bliss&&!Bliss.shy){var e=Bliss.property;if(t.add({clone:function(){var e=this.cloneNode(!0),n=t.$("*",e).concat(e);return t.$("*",this).concat(this).forEach(function(e,r,i){t.events(n[r],e)}),e}},{array:!1}),Object.defineProperty(Node.prototype,e,{get:function s(){return Object.defineProperty(Node.prototype,e,{get:void 0}),Object.defineProperty(this,e,{value:new t.Element(this)}),Object.defineProperty(Node.prototype,e,{get:s}),this[e]},configurable:!0}),Object.defineProperty(Array.prototype,e,{get:function(){return Object.defineProperty(this,e,{value:new t.Array(this)}),this[e]},configurable:!0}),self.EventTarget&&"addEventListener"in EventTarget.prototype){var n=EventTarget.prototype.addEventListener,r=EventTarget.prototype.removeEventListener,i=function(t,e,n){return!(n.callback===t&&n.capture==e)};EventTarget.prototype.addEventListener=function(t,r,s){if(this[e]&&r){var o=this[e].bliss.listeners=this[e].bliss.listeners||{};o[t]=o[t]||[],0===o[t].filter(i.bind(null,r,s)).length&&o[t].push({callback:r,capture:s})}return n.call(this,t,r,s)},EventTarget.prototype.removeEventListener=function(t,n,s){if(this[e]&&n){var o=this[e].bliss.listeners=this[e].bliss.listeners||{};o[t]=o[t]||[],o[t]=o[t].filter(i.bind(null,n,s))}return r.call(this,t,n,s)}}self.$=self.$||t,self.$$=self.$$||t.$}}(Bliss);