-
Notifications
You must be signed in to change notification settings - Fork 7
/
redom.min.js
1 lines (1 loc) · 7.42 KB
/
redom.min.js
1
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).redom={})}(this,(function(e){"use strict";function t(e,t){var i=function(e){for(var t=e.split(/([.#])/),i="",n="",o=1;o<t.length;o+=2)switch(t[o]){case".":i+=" "+t[o+1];break;case"#":n=t[o+1]}return{className:i.trim(),tag:t[0]||"div",id:n}}(e),n=i.tag,o=i.id,r=i.className,l=t?document.createElementNS(t,n):document.createElement(n);return o&&(l.id=o),r&&(t?l.setAttribute("class",r):l.className=r),l}function i(e,t){var i=m(e),o=m(t);return t===o&&o.__redom_view&&(t=o.__redom_view),o.parentNode&&(n(t,o,i),i.removeChild(o)),t}function n(e,t,i){var n=t.__redom_lifecycle;if(o(n))t.__redom_lifecycle={};else{var r=i;for(t.__redom_mounted&&u(t,"onunmount");r;){var l=r.__redom_lifecycle||{};for(var s in n)l[s]&&(l[s]-=n[s]);o(l)&&(r.__redom_lifecycle=null),r=r.parentNode}}}function o(e){if(null==e)return!0;for(var t in e)if(e[t])return!1;return!0}var r=["onmount","onremount","onunmount"],l="undefined"!=typeof window&&"ShadowRoot"in window;function s(e,t,i,o){var s=m(e),a=m(t);t===a&&a.__redom_view&&(t=a.__redom_view),t!==a&&(a.__redom_view=t);var f=a.__redom_mounted,d=a.parentNode;if(f&&d!==s&&n(0,a,d),null!=i)if(o){var h=m(i);h.__redom_mounted&&u(h,"onunmount"),s.replaceChild(a,h)}else s.insertBefore(a,m(i));else s.appendChild(a);return function(e,t,i,n){for(var o=t.__redom_lifecycle||(t.__redom_lifecycle={}),s=i===n,a=!1,f=0,d=r;f<d.length;f+=1){var h=d[f];s||e!==t&&h in e&&(o[h]=(o[h]||0)+1),o[h]&&(a=!0)}if(!a)return void(t.__redom_lifecycle={});var v=i,c=!1;(s||v&&v.__redom_mounted)&&(u(t,s?"onremount":"onmount"),c=!0);for(;v;){var _=v.parentNode,p=v.__redom_lifecycle||(v.__redom_lifecycle={});for(var w in o)p[w]=(p[w]||0)+o[w];if(c)break;(v.nodeType===Node.DOCUMENT_NODE||l&&v instanceof ShadowRoot||_&&_.__redom_mounted)&&(u(v,s?"onremount":"onmount"),c=!0),v=_}}(t,a,s,d),t}function u(e,t){"onmount"===t||"onremount"===t?e.__redom_mounted=!0:"onunmount"===t&&(e.__redom_mounted=!1);var i=e.__redom_lifecycle;if(i){var n=e.__redom_view,o=0;for(var r in n&&n[t]&&n[t](),i)r&&o++;if(o)for(var l=e.firstChild;l;){var s=l.nextSibling;u(l,t),l=s}}}function a(e,t,i){var n=m(e);if("object"==typeof t)for(var o in t)f(n,o,t[o]);else f(n,t,i)}function f(e,t,i){e.style[t]=null==i?"":i}var d="http://www.w3.org/1999/xlink";function h(e,t,i,n){var o=m(e);if("object"==typeof t)for(var r in t)h(o,r,t[r],n);else{var l=o instanceof SVGElement,s="function"==typeof i;if("style"===t&&"object"==typeof i)a(o,i);else if(l&&s)o[t]=i;else if("dataset"===t)c(o,i);else if(l||!(t in o)&&!s||"list"===t){if(l&&"xlink"===t)return void v(o,i);n&&"class"===t&&(i=o.className+" "+i),null==i?o.removeAttribute(t):o.setAttribute(t,i)}else o[t]=i}}function v(e,t,i){if("object"==typeof t)for(var n in t)v(e,n,t[n]);else null!=i?e.setAttributeNS(d,t,i):e.removeAttributeNS(d,t,i)}function c(e,t,i){if("object"==typeof t)for(var n in t)c(e,n,t[n]);else null!=i?e.dataset[t]=i:delete e.dataset[t]}function _(e){return document.createTextNode(null!=e?e:"")}function p(e,t,i){for(var n=0,o=t;n<o.length;n+=1){var r=o[n];if(0===r||r){var l=typeof r;"function"===l?r(e):"string"===l||"number"===l?e.appendChild(_(r)):y(m(r))?s(e,r):r.length?p(e,r,i):"object"===l&&h(e,r,null,i)}}}function w(e){return"string"==typeof e?g(e):m(e)}function m(e){return e.nodeType&&e||!e.el&&e||m(e.el)}function y(e){return e&&e.nodeType}function g(e){for(var i,n=[],o=arguments.length-1;o-- >0;)n[o]=arguments[o+1];var r=typeof e;if("string"===r)i=t(e);else{if("function"!==r)throw new Error("At least one argument required");var l=e;i=new(Function.prototype.bind.apply(l,[null].concat(n)))}return p(m(i),n,!0),i}var b=g,N=g;function x(e){for(var t=[],n=arguments.length-1;n-- >0;)t[n]=arguments[n+1];for(var o=k(e,t,m(e).firstChild);o;){var r=o.nextSibling;i(e,o),o=r}}function k(e,t,i){for(var n=i,o=Array(t.length),r=0;r<t.length;r++)o[r]=t[r]&&m(t[r]);for(var l=0;l<t.length;l++){var u=t[l];if(u){var a=o[l];if(a!==n)if(y(a)){var f=n&&n.nextSibling,d=null!=u.__redom_index&&f===o[l+1];s(e,u,n,d),d&&(n=f)}else null!=u.length&&(n=k(e,u,n));else n=n.nextSibling}}return n}g.extend=function(){for(var e=[],t=arguments.length;t--;)e[t]=arguments[t];return g.bind.apply(g,[this].concat(e))};var S=function(e,t,i){this.View=e,this.initData=i,this.oldLookup={},this.lookup={},this.oldViews=[],this.views=[],null!=t&&(this.key="function"==typeof t?t:function(e){return function(t){return t[e]}}(t))};function A(e,t,i,n){return new D(e,t,i,n)}S.prototype.update=function(e,t){for(var i=this,n=i.View,o=i.key,r=i.initData,l=null!=o,s=this.lookup,u={},a=Array(e.length),f=this.views,d=0;d<e.length;d++){var h=e[d],v=void 0;if(l){var c=o(h);v=s[c]||new n(r,h,d,e),u[c]=v,v.__redom_id=c}else v=f[d]||new n(r,h,d,e);v.update&&v.update(h,d,e,t),m(v.el).__redom_view=v,a[d]=v}this.oldViews=f,this.views=a,this.oldLookup=s,this.lookup=u};var D=function(e,t,i,n){this.View=t,this.initData=n,this.views=[],this.pool=new S(t,i,n),this.el=w(e),this.keySet=null!=i};D.prototype.update=function(e,t){void 0===e&&(e=[]);var n=this.keySet,o=this.views;this.pool.update(e,t);var r=this.pool,l=r.views,s=r.lookup;if(n)for(var u=0;u<o.length;u++){var a=o[u];null==s[a.__redom_id]&&(a.__redom_index=null,i(this,a))}for(var f=0;f<l.length;f++){l[f].__redom_index=f}x(this,l),n&&(this.lookup=s),this.views=l},D.extend=function(e,t,i,n){return D.bind(D,e,t,i,n)},A.extend=D.extend;var E=function(e,t){this.el=_(""),this.visible=!1,this.view=null,this._placeholder=this.el,e instanceof Node?this._el=e:e.el instanceof Node?(this._el=e,this.view=e):this._View=e,this._initData=t};E.prototype.update=function(e,t){var n=this._placeholder,o=this.el.parentNode;if(e){if(!this.visible)if(this._el)s(o,this._el,n),i(o,n),this.el=m(this._el),this.visible=e;else{var r=new(0,this._View)(this._initData);this.el=m(r),this.view=r,s(o,r,n),i(o,n)}this.view&&this.view.update&&this.view.update(t)}else if(this.visible){if(this._el)return s(o,n,this._el),i(o,this._el),this.el=n,void(this.visible=e);s(o,n,this.view),i(o,this.view),this.el=n,this.view=null}this.visible=e};var j=function(e,t,i){this.el=w(e),this.views=t,this.Views=t,this.initData=i};j.prototype.update=function(e,t){if(e!==this.route){var i=this.views[e];this.route=e,i&&(i instanceof Node||i.el instanceof Node)?this.view=i:this.view=i&&new i(this.initData,t),x(this.el,[this.view])}this.view&&this.view.update&&this.view.update(t,e)};var V="http://www.w3.org/2000/svg";function C(e){for(var i,n=[],o=arguments.length-1;o-- >0;)n[o]=arguments[o+1];var r=typeof e;if("string"===r)i=t(e,V);else{if("function"!==r)throw new Error("At least one argument required");var l=e;i=new(Function.prototype.bind.apply(l,[null].concat(n)))}return p(m(i),n,!0),i}var T=C;C.extend=function(){for(var e=[],t=arguments.length;t--;)e[t]=arguments[t];return C.bind.apply(C,[this].concat(e))},C.ns=V,e.List=D,e.ListPool=S,e.Place=E,e.Router=j,e.el=b,e.h=N,e.html=g,e.list=A,e.listPool=function(e,t,i){return new S(e,t,i)},e.mount=s,e.place=function(e,t){return new E(e,t)},e.router=function(e,t,i){return new j(e,t,i)},e.s=T,e.setAttr=function(e,t,i){h(e,t,i)},e.setChildren=x,e.setData=c,e.setStyle=a,e.setXlink=v,e.svg=C,e.text=_,e.unmount=i,e.viewFactory=function(e,t){if(!e||"object"!=typeof e)throw new Error("views must be an object");if(!t||"string"!=typeof t)throw new Error("key must be a string");return function(i,n,o,r){var l=n[t],s=e[l];if(s)return new s(i,n,o,r);throw new Error("view "+l+" not found")}}}));