tinypin/client/reef.min.js

2 lines
No EOL
6.7 KiB
JavaScript

/*! Reef v7.6.6 | (c) 2021 Chris Ferdinandi | MIT License | http://github.com/cferdinandi/reef */
var Reef=function(){"use strict";var t=!1,e=function(){if(!window.DOMParser)return!1;var t=new DOMParser;try{t.parseFromString("x","text/html")}catch(t){return!1}return!0}(),n=function(t){return Array.prototype.slice.call(t)},r=function(t){return Object.prototype.toString.call(t).slice(8,-1).toLowerCase()},a=function(e){if(t)throw new Error(e)},o=function(t,e){var n=r(t);if("object"===n){var a={};for(var i in t)t.hasOwnProperty(i)&&(a[i]=o(t[i],e));return a}return"array"===n?t.map((function(t){return o(t,e)})):"string"!==n||e?t:t.replace(/[^\w-_. ]/gi,(function(t){return"&#"+t.charCodeAt(0)+";"})).replace(/javascript:/gi,"")},i=function(t){t.debounce&&window.cancelAnimationFrame(t.debounce),t.debounce=window.requestAnimationFrame((function(){t.render()}))},c=function(t){return{get:function(e,n){return["object","array"].indexOf(r(e[n]))>-1?new Proxy(e[n],c(t)):e[n]},set:function(e,n,r){return e[n]===r||(e[n]=r,i(t)),!0}}},u=function(t,e){var n=t.filter(e);return n.length<1?null:n[0]},l=["checked","selected","value"],s=function(t,e){e.forEach((function(e){t.style[e]=""}))},f=function(t,e){e.forEach((function(e){if("class"===e.att)t.className=e.value;else if("style"===e.att)!function(t,e){var n=function(t){return t.split(";").reduce((function(t,e){var n=e.indexOf(":");return n&&t.push({name:e.slice(0,n).trim(),value:e.slice(n+1).trim()}),t}),[])}(e),r=Array.prototype.filter.call(t.style,(function(e){return null===u(n,(function(n){return n.name===e&&n.value===t.style[e]}))}));s(t,r),function(t,e){e.forEach((function(e){t.style[e.name]=e.value}))}(t,n)}(t,e.value);else{if(e.att in t)try{t[e.att]=e.value,t[e.att]||0===t[e.att]||(t[e.att]="value"!==e.att||e.value)}catch(t){}try{t.setAttribute(e.att,e.value)}catch(t){}}}))},d=function(t,e){e.forEach((function(e){if("class"===e.att)t.className="";else if("style"===e.att)s(t,n(t.style));else{if(e.att in t)try{t[e.att]=""}catch(t){}try{t.removeAttribute(e.att)}catch(t){}}}))},h=function(t,e){return{att:t,value:e}},p=function(t,e){if(1!==t.nodeType)return[];var n=function(t,e){return Array.prototype.reduce.call(t.attributes,(function(t,n){return!(l.indexOf(n.name)<0||e&&"selected"===n.name)||n.name.length>7&&"default"===n.name.slice(0,7)||t.push(h(n.name,n.value)),t}),[])}(t,e);return function(t,e,n){l.forEach((function(n){e.push(h(n,t.getAttribute(n)))}))}(t,n),n},m=function(t){return 3===t.nodeType?"text":8===t.nodeType?"comment":t.tagName.toLowerCase()},v=function(t){return t.childNodes&&t.childNodes.length>0?null:t.textContent},y=function(t){1===t.nodeType&&(Array.prototype.forEach.call(t.attributes,(function(e){e.name.length<8||"default"!==e.name.slice(0,7)||(f(t,[h(e.name.slice(7).toLowerCase(),e.value)]),d(t,[h(e.name,e.value)]))})),t.childNodes&&Array.prototype.forEach.call(t.childNodes,(function(t){y(t)})))},g=function(t,e,r){var a=n(e.childNodes),o=n(t.childNodes),i=a.length-o.length;if(i>0)for(;i>0;i--)a[a.length-i].parentNode.removeChild(a[a.length-i]);o.forEach((function(t,n){if(!a[n])return y(t),void e.appendChild(t.cloneNode(!0));if(m(t)===m(a[n])){if(function(t,e){var n=p(t,!0),r=p(e),a=r.filter((function(t){return null===u(n,(function(e){return t.att===e.att}))})),o=n.filter((function(t){var e=u(r,(function(e){return t.att===e.att}));return null===e||e.value!==t.value}));f(e,o),d(e,a)}(t,a[n]),!(r.filter((function(e){return 3!==t.nodeType&&function(t,e){return Element.prototype.matches&&t.matches(e)||Element.prototype.msMatchesSelector&&t.msMatchesSelector(e)||Element.prototype.webkitMatchesSelector&&t.webkitMatchesSelector(e)}(t,e)})).length>0)){var o=v(t);if(o&&o!==v(a[n])&&(a[n].textContent=o),a[n].childNodes.length>0&&t.childNodes.length<1)a[n].innerHTML="";else{if(a[n].childNodes.length<1&&t.childNodes.length>0){var i=document.createDocumentFragment();return g(t,i,r),void a[n].appendChild(i)}t.childNodes.length>0&&g(t,a[n],r)}}}else a[n].parentNode.replaceChild(t.cloneNode(!0),a[n])}))},w=function(t,e){t&&t.forEach((function(t){if(t.attached.indexOf(e)>-1)return a(e.elem+" has attached nodes that it is also attached to, creating an infinite loop.");"render"in t&&t.render()}))},b=function(t,e){if(!(t||e&&e.lagoon))return a("You did not provide an element to make into a component.");if(!e||!e.template&&!e.lagoon)return a("You did not provide a template for this component.");var u=this,l=function(t,e){return t.setters?t.store?null:t.data:t.data&&!t.store?new Proxy(t.data,c(e)):null}(e,u),s=e.store,f=e.router,d=e.setters,h=e.getters;(u.debounce=null,Object.defineProperties(u,{elem:{value:t},template:{value:e.template},allowHTML:{value:e.allowHTML},lagoon:{value:e.lagoon},store:{value:s},attached:{value:[]},router:{value:f}}),Object.defineProperty(u,"data",{get:function(){return d?o(l,!0):l},set:function(t){return s||d||(l=new Proxy(t,c(u)),i(u)),!0}}),d&&!s&&Object.defineProperty(u,"do",{value:function(t){if(!d[t])return a("There is no setter with this name.");var e=n(arguments);e[0]=l,d[t].apply(u,e),i(u)}}),h&&!s&&Object.defineProperty(u,"get",{value:function(t){return h[t]?h[t](l):a("There is no getter with this name.")}}),f&&"addComponent"in f&&f.addComponent(u),s&&"attach"in s&&s.attach(u),e.attachTo)&&("array"===r(e.attachTo)?e.attachTo:[e.attachTo]).forEach((function(t){"attach"in t&&t.attach(u)}))};return b.Store=function(t){return t.lagoon=!0,new b(null,t)},b.emit=function(t,e,n){var r;if(!t||!e)return a("You did not provide an element or event name.");r=new CustomEvent(e,{bubbles:!0,detail:n}),t.dispatchEvent(r)},b.prototype.render=function(){if(this.lagoon)w(this.attached,this);else{if(!this.template)return a("No template was provided.");var t="string"===r(this.elem)?document.querySelector(this.elem):this.elem;if(!t)return a("The DOM element to render your template into was not found.");var i=o((this.store?this.store.data:this.data)||{},this.allowHTML),c="function"===r(this.template)?this.template(i,this.router?this.router.current:t,t):this.template;if(!(["string","number"].indexOf(r(c))<0)){var u=this.attached.map((function(t){return t.elem}));return g(function(t){if(e){var r=(new DOMParser).parseFromString(t,"text/html");return r.head&&r.head.childNodes&&r.head.childNodes.length>0&&n(r.head.childNodes).reverse().forEach((function(t){r.body.insertBefore(t,r.body.firstChild)})),r.body||document.createElement("body")}var a=document.createElement("div");return a.innerHTML=t,a}(c),t,u),b.emit(t,"render",i),w(this.attached,this),t}}},b.prototype.attach=function(t){"array"===r(t)?this.attached.push.apply(this.attached,t):this.attached.push(t)},b.prototype.detach=function(t){var e="array"===r(t)?t:[t],n=this;e.forEach((function(t){var e=n.attached.indexOf(t);e<0||n.attached.splice(e,1)}))},b.debug=function(e){t=!!e},b.clone=o,b._={trueTypeOf:r,err:a},b}();