-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreef.min.js
More file actions
2 lines (2 loc) · 7.09 KB
/
reef.min.js
File metadata and controls
2 lines (2 loc) · 7.09 KB
1
2
/*! Reef v7.0.1 | (c) 2020 Chris Ferdinandi | MIT License | http://github.com/cferdinandi/reef */
var Reef=function(){"use strict";var t,e=["checked","selected","value"],n=!1,r=function(t){return Object.prototype.toString.call(t).slice(8,-1).toLowerCase()},a=function(t){if(n)throw new Error(t)},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}if("array"===n)return t.map((function(t){return o(t,e)}));if("string"===n&&!e){var c=document.createElement("div");return c.textContent=t,c.innerHTML}return t},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},deleteProperty:function(e,n){return delete e[n],i(t),!0}}},u=function(t,e){var n=t.filter(e);return n.length<1?null:n[0]},l=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 n=this,u=function(t,e){return t.setters?t.store?null:t.data:t.data&&!t.store?new Proxy(t.data,c(e)):null}(e,n),l=e.store,s=e.setters,d=e.getters;(n.debounce=null,Object.defineProperties(this,{elem:{value:t,writable:true},template:{value:e.template},allowHTML:{value:e.allowHTML},lagoon:{value:e.lagoon},store:{value:l},attached:{value:[]}}),Object.defineProperty(n,"data",{get:function(){return s?o(u,!0):u},set:function(t){return l||s||(u=new Proxy(t,c(n)),i(n)),!0}}),s&&!l&&Object.defineProperty(n,"do",{value:function(t){if(!s[t])return a("There is no setter with this name.");var e=Array.prototype.slice.call(arguments);e[0]=u,s[t].apply(n,e),i(n)}}),d&&!l&&Object.defineProperty(n,"get",{value:function(t){return d[t]?d[t](u):a("There is no getter with this name.")}}),l&&"attach"in l&&l.attach(n),e.attachTo)&&("array"===r(e.attachTo)?option.attachTo:[e.attachTo]).forEach((function(t){"attach"in t&&t.attach(n)}))};l.Store=function(t){return t.lagoon=!0,new l(null,t)};var s=function(t,e){e.forEach((function(e){t.style[e]=""}))},d=function(t,e,n){(function(t,e){return t.reduce((function(t,n){if(n.att.length>7&&"default"===n.att.slice(0,7)){if(!e)return t;n.att=n.att.slice(7)}return t.push(n),t}),[])})(e,n).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){if(e.indexOf(":")>0){var n=e.trim().split(":");t.push({name:n[0]?n[0].trim():"",value:n[1]?n[1].trim():""})}return 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]||(t[e.att]=!0)}catch(t){}try{t.setAttribute(e.att,e.value||"")}catch(t){}}}))},f=function(t,e){return{att:t,value:e}},h=function(t,n){var r=function(t,n){return Array.prototype.reduce.call(t.attributes,(function(t,r){return(e.indexOf(r.name)<0||n&&"selected"===r.name)&&t.push(f(r.name,r.value)),t}),[])}(t,n);return function(t,n,r){e.forEach((function(e){!t[e]||r&&"option"===t.tagName.toLowerCase()&&"selected"===e||r&&"select"===t.tagName.toLowerCase()&&"value"===e||n.push(f(e,t[e]))}))}(t,r,n),r},p=function(t){var e;return e="text"===t.type?document.createTextNode(t.content):"comment"===t.type?document.createComment(t.content):t.isSVG?document.createElementNS("http://www.w3.org/2000/svg",t.type):document.createElement(t.type),d(e,t.atts,!0),t.children.length>0?t.children.forEach((function(t){e.appendChild(p(t))})):"text"!==t.type&&(e.textContent=t.content),e},m=function(t,n){var r,a=n.atts.filter((function(n){return!(e.indexOf(n.att)>-1)&&null===u(t.atts,(function(t){return n.att===t.att}))})),o=t.atts.filter((function(t){var e=u(n.atts,(function(e){return t.att===e.att}));return null===e||e.value!==t.value}));d(n.node,o),r=n.node,a.forEach((function(t){if("class"===t.att)r.className="";else if("style"===t.att)s(r,Array.prototype.slice.call(r.style));else{if(t.att in r)try{r[t.att]=""}catch(t){}try{r.removeAttribute(t.att)}catch(t){}}}))},v=function(t,e,n,r){var a=e.length-t.length;if(a>0)for(;a>0;a--)e[e.length-a].node.parentNode.removeChild(e[e.length-a].node);t.forEach((function(a,o){if(e[o])if(t[o].type===e[o].type){if(m(t[o],e[o]),!(r.filter((function(t){return 3!==a.node.nodeType&&function(t,e){return Element.prototype.matches&&t.matches(e)||Element.prototype.msMatchesSelector&&t.msMatchesSelector(e)||Element.prototype.webkitMatchesSelector&&t.webkitMatchesSelector(e)}(a.node,t)})).length>0))if(t[o].content&&t[o].content!==e[o].content&&(e[o].node.textContent=t[o].content),e[o].children.length>0&&a.children.length<1)e[o].node.innerHTML="";else{if(e[o].children.length<1&&a.children.length>0){var i=document.createDocumentFragment();return v(a.children,e[o].children,i,r),void e[o].node.appendChild(i)}a.children.length>0&&v(a.children,e[o].children,e[o].node,r)}}else e[o].node.parentNode.replaceChild(p(t[o]),e[o].node);else n.appendChild(p(t[o]))}))},y=function(t,e,n){return Array.prototype.map.call(t.childNodes,(function(t){var r={content:t.childNodes&&t.childNodes.length>0?null:t.textContent,atts:1!==t.nodeType?[]:h(t,n),type:3===t.nodeType?"text":8===t.nodeType?"comment":t.tagName.toLowerCase(),node:t};return r.isSVG=e||"svg"===r.type,r.children=y(t,r.isSVG,n),r}))},g=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&&i(t)}))};return l.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)},l.prototype.render=function(){if(this.lagoon)g(this.attached,this);else{if(!this.template)return a("No template was provided.");var e="string"===r(this.elem)?document.querySelector(this.elem):this.elem;if(!e)return a("The DOM element to render your template into was not found.");var n=o((this.store?this.store.data:this.data)||{},this.allowHTML),i="function"===r(this.template)?this.template(n):this.template;if(-1!==["string","number"].indexOf(r(i))&&e.innerHTML!==i.innerHTML){var c=y(function(e){if(t){var n=(new DOMParser).parseFromString(e,"text/html");return n.head.childNodes.length>0&&Array.prototype.slice.call(n.head.childNodes).reverse().forEach((function(t){n.body.insertBefore(t,n.body.firstChild)})),n.body}var r=document.createElement("div");return r.innerHTML=e,r}(i),!1,!0),u=y(e),s=this.attached.map((function(t){return t.elem}));return v(c,u,e,s),l.emit(e,"render",n),g(this.attached,this),e}}},l.prototype.attach=function(t){"array"===r(t)?this.attached.concat(t):this.attached.push(t)},l.prototype.detach=function(t){var e="array"===r(t);this.attached=this.attached.filter((function(n){return e?-1===t.indexOf(n):n!==t}))},l.debug=function(t){n=!!t},l.clone=o,t=function(){if(!window.DOMParser)return!1;var t=new DOMParser;try{t.parseFromString("x","text/html")}catch(t){return!1}return!0}(),l}();