-
Notifications
You must be signed in to change notification settings - Fork 1
/
keet-min.js
12 lines (11 loc) · 13.8 KB
/
keet-min.js
1
2
3
4
5
6
7
8
9
10
11
12
/**
* Keetjs v4.0.0 Alpha release: https://github.com/keetjs/keet
* Minimalist view layer for the web
*
* <<<<<<<<<<<<<<<<<<<<<<<<<<<<<< Keetjs >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
*
* Copyright 2018, Shahrul Nizam Selamat
* Released under the MIT License.
*/
var keet=function(){"use strict";function t(t){if(t.match(/([^?]*)\?([^:]*):([^;]*)|(\s*=\s*)[^;]*/g)){var e=t.split("?"),i=e[0],n=e[1].split(":")[0],o=e[1].split(":")[1];return!!this&&(this[i]?{value:b(n),state:i}:{value:b(o),state:i})}return!1}function e(t,e){"function"==typeof e&&e(t)}function i(t,e,i,n){t.nodeValue=t.nodeValue.replace(RegExp(i,"g"),n)}function n(n,o,r,l,a,d){for(var u=n.match(k),s=u.length,c=void 0,f=void 0,h=void 0;s;)if(s--,c=u[s].replace(k,"$1"),f=t.call(r,c),h=y(c)){if(e(c,l),d)return n.replace(u,r[h[0]][h[1]]);i(o,n,"{{"+c+"}}",r[h[0]][h[1]])}else if(f){if(e(f.state,l),d)return n.replace(u,f.value);i(o,n,"{{"+c+"}}",f.value)}else if(void 0!==r[c]){if(e(c,l),d)return n.replace(u,r[c]);i(o,n,"{{"+c+"}}",r[c])}}function o(t,e){for(var i=void 0;t;)i=t,t=t.nextSibling,i&&i.nodeType===w?i.isEqualNode(e)?(i.remove(),e=e.nextSibling):o(i.firstChild,e):i.isEqualNode(e)&&(i.remove(),e=e.nextSibling)}function r(t,e,i,n,r){var l=void 0,a=void 0,d=void 0,u=document.createDocumentFragment();if("initial"!==r||R.hasOwnProperty(i)){if(t.nextSibling.isEqualNode(R[i].frag.firstChild))return;d=R[i].frag.cloneNode(!0),this[i]&&t.parentNode.insertBefore(d,t.nextSibling)}else for(a=t;a;)l=a,a=a.nextSibling,l.nodeType!==w&&l.nodeValue.match(q)?(a=null,R[i]=R[i]||{},o(this.__pristineFragment__.firstChild,u.firstChild),Object.keys(R).map(function(t){return t!==i&&o(R[t].frag.firstChild,u.firstChild)}),R[i].frag=u,d=R[i].frag.cloneNode(!0),n(this,null,null,null,d,"initial"),this[i]&&l.parentNode.insertBefore(d,l)):l.nodeType!==L&&u.appendChild(l)}function l(t,e){J=U(t,e),z=W.createContextualFragment(J),z.firstChild.setAttribute("kdata-id",e["kdata-id"])}function a(t,e,i){var n=t.getElementById(e);n?n.childNodes[1].remove():i||Object.keys(R).map(function(t){return a(R[t].frag,e,!0)})}function d(t,e,i){var n=void 0,o=void 0,r=void 0,d=void 0,u=void 0,s=void 0,c=void 0,f=void 0,h=void 0,p=void 0,v=void 0,g=void 0,m=void 0,b=void 0,y=void 0,k=void 0;if(H[e]=H[e]||{},m=this[e]&&this[e].enableFiltering?"listFilter":"list",H[e][m]||(H[e][m]=t.nextSibling.cloneNode(!0)),p=H[e][m],H[e].str||(H[e].str=t.nextSibling.cloneNode(!0).outerHTML,t.nextSibling.remove(),a(this.__pristineFragment__,t.parentNode.id)),v=H[e].str,void 0!==this[e]&&this[e].hasOwnProperty(m))if(d=t.parentNode,W&&!d.hasAttribute("data-ignore")&&d.setAttribute("data-ignore",""),n=this[e][m],g=H[e].oldModel||[],W){u=$(n,g),s=$(g,n);var C=function(){for(var t=arguments.length,i=Array(t),r=0;r<t;r++)i[r]=arguments[r];if(c=i.pop(),f=g.length===n.length,f&&2!==c.childNodes.length)for(o=0;o<u.length;)h=c.querySelector(u[o]["kdata-id"]===s[o]["kdata-id"]?'[kdata-id="'+u[o]["kdata-id"]+'"]':'[kdata-id="'+s[o]["kdata-id"]+'"]'),h&&(l(v,u[o]),c.replaceChild(z,h)),o++;else if(u.length>0&&0===s.length)for(o=0;o<u.length;)l(v,u[o]),u[o]["kdata-id"]===n[n.length-1]["kdata-id"]?y=c.lastChild:(b=n.map(function(t){return t["kdata-id"]}).indexOf(u[o]["kdata-id"]),y=c.childNodes[b].nextSibling),c.insertBefore(z,y),o++;else if(0===u.length&&s.length>0)for(o=0;o<s.length;)h=c.querySelector('[kdata-id="'+s[o]["kdata-id"]+'"]'),h&&c.removeChild(h),o++;else if(u.length>0&&s.length>0){for(o=0;o<s.length;)if(h=c.querySelector('[kdata-id="'+s[o]["kdata-id"]+'"]'),h&&c.removeChild(h),o++,o===s.length)for(k=0;k<u.length;)l(v,u[k]),c.insertBefore(z,c.lastChild),k++}else for(o=0;o<n.length;)l(v,n[o]),c.insertBefore(z,c.lastChild),o++;H[e].oldModel=JSON.parse(JSON.stringify(n))};d.hasAttribute("id")&&(c=T(d.id),c?C.call(this,null,null,c):_({el:d.id},e,C.bind(this),function(){H[e].oldModel=[]}))}else for(o=0;o<n.length;)r=p.cloneNode(!0),i(this,null,r,n[o],null,"update"),r.setAttribute("kdata-id",n[o]["kdata-id"]),d.insertBefore(r,d.lastChild),o++}function u(t,e){var i=t.replace("component:",""),n=this[i];void 0!==n?K[n.ID]?T(this[i].el)?(e.parentNode.replaceChild(K[n.ID].cloneNode(!0),e),n.callBatchPoolUpdate()):(n.base=n.__pristineFragment__.cloneNode(!0),n.render(!0),e.parentNode.replaceChild(n.base,e)):(n.render(!0),K[n.ID]=n.base.cloneNode(!0),e.parentNode.replaceChild(n.base,e)):O(!1,"Component "+i+" does not exist.")}function s(t,i,n,o,l,a){var s=void 0,c=void 0,f=void 0;t.match(Z)&&(c=t.replace(Z,"$1").trim(),c.match(G)&&"initial"!==a?(f=c.replace("model:",""),d.call(this,i,f,l)):c.match(Q)?(s=c.replace("?",""),void 0!==n[s]&&(e(s,o),r.call(this,i,o,s,l,a))):c.match(X)&&"initial"!==a&&u.call(this,c,i))}function c(t,e){for(var i=void 0;e;){if(i=e,e=e.parentNode,i.nodeType===Y&&i.hasAttribute("kdata-id"))return{id:i.getAttribute("kdata-id"),node:i};i.isEqualNode(t)&&(e=null)}}function f(t,e){delete e.isModel;var i=Object.keys(e)[0],n=e[i];void 0!==this[n]&&"function"==typeof this[n]&&t.addEventListener(i,this[n].bind(this),!!e.useCapture)}function h(t,e,i,n){if(n.stopPropagation(),n.target!==n.currentTarget){var o=c(i,n.target);this[e](t.list[et(o.id,t)],n.target,o.node,n)}}function p(t,e){delete e.isModel;var i=Object.keys(e)[0],n=e[i];if(void 0!==this[n]&&"function"==typeof this[n]){var o=t.firstChild.nodeValue.replace(tt,"$1").trim();o=o.replace("model:","");t.addEventListener(i,h.bind(this,this[o],n,t),!!e.useCapture)}}function v(t,e,i,o,r,l){function a(t,e){return d(t)&&d(e)||t.isEqualNode(e)}function d(t){return null!=t.getAttribute("data-ignore")}function u(t,e){"INPUT"===t.nodeName&&t.checked!==e.checked&&(t.checked=e.checked)}function c(t,e){for(var i=e.attributes,n={},o=0;o<i.length;)n[i[o].name]=i[o].value,o++;for(var r=t.attributes,l={},a=0;a<r.length;)l[r[a].name]=r[a].value,a++;for(var d in n)t.attributes[d]&&t.attributes[d].name===d&&t.attributes[d].value!==n[d]?t.setAttribute(d,n[d]):t.hasAttribute(d)||t.setAttribute(d,n[d]);for(var u in l)e.attributes[u]&&t.attributes[u]||"evt-data"!==u&&t.removeAttribute(u)}function h(t,e){if(t.nodeType===e.nodeType)if(t.nodeType===it){if(u(t,e),a(t,e))return;m(t.firstChild,e.firstChild),t.nodeName===e.nodeName?c(t,e):t.parentNode.replaceChild(e,t)}else t.nodeValue!==e.nodeValue&&(t.nodeValue=e.nodeValue);else t.parentNode.replaceChild(e,t)}function g(t,e){return t.length-e-1}function m(t,e){for(var i=0,n=[];e;)i++,M=e,e=e.nextSibling,n.push(M);for(var o=void 0,r=t&&t.parentNode;t;)if(i--,D=t,t=t.nextSibling,o=g(n,i),D&&n[o]?h(D,n[o]):D&&!n[o]&&r.removeChild(D),null===t)for(;i>0;)i--,o=g(n,i),r.appendChild(n[o])}var b=void 0,y=void 0,k=void 0;o?k=i:r?k=r.firstChild:(y=t.base,k=y.firstChild);var C=o||t,N=function(t){var e=t.attributes,i=0,o=void 0,r=void 0,l=void 0;for(i=e.length;i--;)o=e[i],l=o.localName,r=o.nodeValue,ot.test(l)?(t.removeAttribute(l),l=n(l,t,C,null,null,!0),t.setAttribute(l,r)):ot.test(r)&&(r=n(r,t,C,null,null,!0),"checked"===l?(t.checked=""!==r,t.removeAttribute(l)):""===r?t.setAttribute(l,""):t.setAttribute(l,r))},x=function(t){for(var e=t.attributes,i=0,n=void 0,o=void 0,r=void 0,l=void 0,a=void 0,d=[],u=void 0,s=void 0;i<e.length;)n=e[i],o=n.localName,r=n.nodeValue,/^k-/.test(o)&&(l=o.replace(/^k-/,""),a=r.match(/[a-zA-Z]+(?![^(]*\))/)[0],s=r.match(/\(([^{}]+)\)/),s=s?s[1]:"",u={},u[l]=a,s&&(u[s]=!0),u.isModel=!1,d.push(u),t.hasChildNodes()&&t.firstChild.nodeType!==it&&t.firstChild.nodeValue.match(rt)&&(u.isModel=!0)),i++;if(d.length&&!t.hasAttribute("evt-data")){var c=S();t.setAttribute("evt-data",c),lt[c]=d}return d},A=void 0,_=function e(i,n){for(;i;)b=i,i=i.nextSibling,b.nodeType===it&&(b.hasAttributes()&&!T(b.id)&&(A=x.call(t,b),A.length&&A.map(function(e){return e.isModel?p.call(t,b,e):f.call(t,b,e)})),e(b.firstChild,n))},O=function i(o,r){for(;o;)b=o,o=o.nextSibling,b.nodeType===it?("update"===r&&b.hasAttributes()&&N(b),i(b.firstChild,r)):b.nodeValue.match(ot)&&(b.nodeType===nt?s.call(t,b.nodeValue,b,C,e,v,r):"update"===r&&n.call(t,b.nodeValue,b,C,e,v))},M=void 0,D=void 0;if("initial"===l||"update"===l)O(k,l);else if("event"===l)_(k,l);else if("diff"===l){var I=T(t.el);I&&!t.IS_STUB?m(I.firstChild,k):I&&m(I,k)}}function g(t){v(this,vt,null,null,null,"initial"),v(this,vt,null,null,null,"update"),v(this,vt,null,null,null,"event");var e=t||T(this.el);e?(e.nodeType===mt?e.setAttribute("data-ignore",""):(O(1===this.base.childNodes.length,"Sub-component should only has a single rootNode."),!this.base.firstChild.hasAttribute("data-ignore")&&this.base.firstChild.setAttribute("data-ignore","")),ft.call(this),t||e.appendChild(this.base),this.componentDidMount&&"function"==typeof this.componentDidMount&&this.componentDidMount()):O(!1,'No element with id: "'+this.el+'" exist.')}function m(t){var e=void 0,i=void 0,n=document.createDocumentFragment();if("string"==typeof t)for(e=t.trim().replace(/\s+/g," "),i=document.createElement("div"),i.innerHTML=e;i.firstChild;)n.appendChild(i.firstChild);else"object"===(void 0===t?"undefined":C(t))&&t.nodeType?t.nodeType===kt?n.appendChild(t):t.nodeType===bt?n=t:t.nodeType===yt?n.appendChild(t):O(!1,"Unable to parse instance, unknown type."):O(!1,"Parameter is not a string or a html element.");return this.__pristineFragment__=n.cloneNode(!0),this.base=n,pt(),this}var b=function(t){return"''"===t||'""'===t||"null"===t?"":t},y=function(t){var e=t.match(/\.*\./g);return e&&e.length>0?t.split("."):void 0},k=/{{([^{}]+)}}/g,C="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t},N=function(){function t(t,e){for(var i=0;i<e.length;i++){var n=e[i];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(t,n.key,n)}}return function(e,i,n){return i&&t(e.prototype,i),n&&t(e,n),e}}(),x=Object.assign||function(t){for(var e=1;e<arguments.length;e++){var i=arguments[e];for(var n in i)Object.prototype.hasOwnProperty.call(i,n)&&(t[n]=i[n])}return t},A=function(){var t=function(){return(1*Math.random()*1e17).toString(36)};return"KDATA-"+t()+"-"+t()},S=function(){return(1*Math.random()*1e17).toString(36)},T=function(t){return document.getElementById(t)},_=function(t,e,i,n){function o(){l=T(t.el),l&&(clearInterval(d),a=!0,i(t,e,l))}function r(){clearInterval(d),!a&&n&&"function"==typeof n&&n()}var l=T(t.el),a=!1,d=void 0;return l?l:(d=setInterval(o,0),void setTimeout(r,5))},O=function(t,e){if(!t)throw Error("(keet) "+e)},M=function(t,e){return t["kdata-id"]!==e["kdata-id"]},D=function(){for(var t=arguments.length,e=Array(t),i=0;i<t;i++)e[i]=arguments[i];this.exec&&"function"==typeof this.exec&&this.exec.apply(null,e)},I=(function(){function t(t){this.enableFiltering=t||null,this.exec=null,this.model=[],Object.defineProperty(this,"list",{enumerable:!1,configurable:!0,get:function(){return this.model},set:function(t){this.model=t,D.call(this,this.model,this.listFilter)}}),Object.defineProperty(this,"listFilter",{enumerable:!1,configurable:!0,get:function(){var t=this;return this.prop?this.model.filter(function(e){return e[t.prop]===t.value}):this.model}})}return t.prototype.subscribe=function(t){this.exec=t},t.prototype.add=function(t){this.list=this.list.concat(x({},t,{"kdata-id":S()}))},t.prototype.update=function(t){this.list=this.list.map(function(e){return M(e,t)?e:t})},t.prototype.filter=function(t,e){this.prop=t,this.value=e,this.list=this.list.map(function(t){return t})},t.prototype.destroy=function(t){this.list=this.list.filter(function(e){return M(e,t)})},t}(),RegExp(/(\schecked=")(.*?)(?=")/g)),V="",E=void 0,F=void 0,P=void 0,j=void 0,B=void 0,U=function(e,i){var n=e.match(/{{([^{}]+)}}/g);for(V=e,P=0,j=n.length;P<j;P++)E=n[P].replace(/{{([^{}]+)}}/g,"$1"),F=t.call(i,E),V=F?V.replace("{{"+E+"}}",F.value):V.replace("{{"+E+"}}",i[E]),B=V.match(I),B&&(V=17===B[0].length?V.replace(' checked="checked"'," checked"):V.replace(' checked=""',""));return V},q=/\{\{\/([^{}]+)\}\}/g,w=1,L=8,R={},$=function(t,e){return t.filter(function(t){return!e.some(function(e){var i=!0;for(var n in e)t[n]!==e[n]&&(i=!1);return i})})},W=void 0;"function"==typeof document.createRange&&(W=document.createRange());var H={},J=void 0,z=void 0,K={},Z=/{{([^{}]+)}}/g,G=/^model:/g,Q=/^\?/g,X=/^component:([^{}]+)/g,Y=1,tt=/{{([^{}]+)}}/g,et=function(t,e){return e.list.map(function(t){return t["kdata-id"]}).indexOf(t)},it=1,nt=8,ot=/{{([^{}]+)}}/g,rt=/\{\{model:([^{}]+)\}\}/g,lt=[],at=1,dt=function(){gt.call(this),this.componentDidUpdate&&"function"==typeof this.componentDidUpdate&&this.componentDidUpdate()},ut={},st=function(t,e){var i=this;ut[this.ID]=ut[this.ID]||null,clearTimeout(ut[this.ID]),ut[this.ID]=setTimeout(function(){return t.call(i)},e)},ct=function t(e){var i=this,n=void 0,o=void 0;if(e<ht.length){if(n=ht[e],o=this[n],void 0===o&&(o=y(n)),o&&Array.isArray(o)){var r=this[o[0]][o[1]];Object.defineProperty(this[o[0]],o[1],{enumerable:!1,configurable:!0,get:function(){return r},set:function(t){r=t,st.call(i,dt,at)}})}else Object.defineProperty(this,n,{enumerable:!1,configurable:!0,get:function(){return o},set:function(t){o=t,st.call(i,dt,at)}});e++,t.call(this,e)}},ft=function(){ct.call(this,0)},ht=[],pt=function(){ht=[]},vt=function(t){ht.indexOf(t)===-1&&(ht=ht.concat(t))},gt=function(){this.base=this.__pristineFragment__.cloneNode(!0),v(this,vt,null,null,null,"initial"),v(this,vt,null,null,null,"update"),v(this,null,null,null,null,"event"),v(this,null,null,null,null,"diff")},mt=1,bt=11,yt=3,kt=1;return function(){function t(){this.ID=t.indentity}return t.prototype.mount=function(t){return m.call(this,t)},t.prototype.link=function(t){return t||O(t,"No id is given as parameter."),this.el=t,this.render(),this},t.prototype.render=function(t){this.componentWillMount&&"function"==typeof this.componentWillMount&&this.componentWillMount(),t&&(this.IS_STUB=!0),g.call(this,t)},t.prototype.callBatchPoolUpdate=function(){st.call(this,dt,1)},t.prototype.subscribe=function(t){this.exec=t},t.prototype.inform=function(t){this.exec&&"function"==typeof this.exec&&this.exec(t)},N(t,null,[{key:"indentity",get:function(){return A()}}]),t}()}();
//# sourceMappingURL=keet-min.js.map