UNPKG

redom

Version:

Tiny turboboosted JavaScript library for creating user interfaces.

1 lines 10.7 kB
!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){(null==t||t>e.length)&&(t=e.length);for(var n=0,i=Array(t);n<t;n++)i[n]=e[n];return i}function n(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function i(e,t,n){if(l())return Reflect.construct.apply(null,arguments);var i=[null];return i.push.apply(i,t),new(e.bind.apply(e,i))}function r(e,t,n){return t&&function(e,t){for(var n=0;n<t.length;n++){var i=t[n];i.enumerable=i.enumerable||!1,i.configurable=!0,"value"in i&&(i.writable=!0),Object.defineProperty(e,a(i.key),i)}}(e.prototype,t),Object.defineProperty(e,"prototype",{writable:!1}),e}function o(e,n){var i="undefined"!=typeof Symbol&&e[Symbol.iterator]||e["@@iterator"];if(!i){if(Array.isArray(e)||(i=function(e,n){if(e){if("string"==typeof e)return t(e,n);var i={}.toString.call(e).slice(8,-1);return"Object"===i&&e.constructor&&(i=e.constructor.name),"Map"===i||"Set"===i?Array.from(e):"Arguments"===i||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(i)?t(e,n):void 0}}(e))||n){i&&(e=i);var r=0,o=function(){};return{s:o,n:function(){return r>=e.length?{done:!0}:{done:!1,value:e[r++]}},e:function(e){throw e},f:o}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var l,a=!0,u=!1;return{s:function(){i=i.call(e)},n:function(){var e=i.next();return a=e.done,e},e:function(e){u=!0,l=e},f:function(){try{a||null==i.return||i.return()}finally{if(u)throw l}}}}function l(){try{var e=!Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],(function(){})))}catch(e){}return(l=function(){return!!e})()}function a(e){var t=function(e,t){if("object"!=typeof e||!e)return e;var n=e[Symbol.toPrimitive];if(void 0!==n){var i=n.call(e,t);if("object"!=typeof i)return i;throw new TypeError("@@toPrimitive must return a primitive value.")}return String(e)}(e,"string");return"symbol"==typeof t?t:t+""}function u(e){return u="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e},u(e)}function s(e,t){var n=function(e){for(var t=e.split(/([.#])/),n="",i="",r=1;r<t.length;r+=2)switch(t[r]){case".":n+=" ".concat(t[r+1]);break;case"#":i=t[r+1]}return{className:n.trim(),tag:t[0]||"div",id:i}}(e),i=n.tag,r=n.id,o=n.className,l=t?document.createElementNS(t,i):document.createElement(i);return r&&(l.id=r),o&&(t?l.setAttribute("class",o):l.className=o),l}function f(e){for(var t,n=u(e),r=arguments.length,o=new Array(r>1?r-1:0),l=1;l<r;l++)o[l-1]=arguments[l];if("string"===n)t=s(e);else{if("function"!==n)throw new Error("At least one argument required");t=i(e,o)}return j(V(t),o,!0),t}var c=f,v=f;function d(e,t){var n=t,i=V(e),r=V(n);return n===r&&r.__redom_view&&(n=r.__redom_view),r.parentNode&&(h(n,r,i),i.removeChild(r)),n}function h(e,t,n){var i=t.__redom_lifecycle;if(m(i))t.__redom_lifecycle={};else{var r=n;for(t.__redom_mounted&&p(t,"onunmount");r;){var o=r.__redom_lifecycle||{};for(var l in i)o[l]&&(o[l]-=i[l]);m(o)&&(r.__redom_lifecycle=null),r=r.parentNode}}}function m(e){if(null==e)return!0;for(var t in e)if(e[t])return!1;return!0}f.extend=function(){for(var e=arguments.length,t=new Array(e),n=0;n<e;n++)t[n]=arguments[n];return f.bind.apply(f,[this].concat(t))};var _=["onmount","onremount","onunmount"],y="undefined"!=typeof window&&"ShadowRoot"in window;function w(e,t,n,i){var r=t,l=V(e),a=V(r);r===a&&a.__redom_view&&(r=a.__redom_view),r!==a&&(a.__redom_view=r);var u=a.__redom_mounted,s=a.parentNode;if(u&&s!==l&&h(0,a,s),null!=n)if(i){var f=V(n);f.__redom_mounted&&p(f,"onunmount"),l.replaceChild(a,f)}else l.insertBefore(a,V(n));else l.appendChild(a);return function(e,t,n,i){var r;t.__redom_lifecycle||(t.__redom_lifecycle={});var l,a=t.__redom_lifecycle,u=n===i,s=!1,f=o(_);try{for(f.s();!(l=f.n()).done;){var c=l.value;u||e!==t&&c in e&&(a[c]=(a[c]||0)+1),a[c]&&(s=!0)}}catch(e){f.e(e)}finally{f.f()}if(!s)return void(t.__redom_lifecycle={});var v=n,d=!1;(u||null!==(r=v)&&void 0!==r&&r.__redom_mounted)&&(p(t,u?"onremount":"onmount"),d=!0);for(;v;){var h=v.parentNode;v.__redom_lifecycle||(v.__redom_lifecycle={});var m=v.__redom_lifecycle;for(var w in a)m[w]=(m[w]||0)+a[w];if(d)break;(v.nodeType===Node.DOCUMENT_NODE||y&&v instanceof ShadowRoot||null!=h&&h.__redom_mounted)&&(p(v,u?"onremount":"onmount"),d=!0),v=h}}(r,a,l,s),r}function p(e,t){var n;"onmount"===t||"onremount"===t?e.__redom_mounted=!0:"onunmount"===t&&(e.__redom_mounted=!1);var i=e.__redom_lifecycle;if(i){var r=e.__redom_view,o=0;for(var l in null==r||null===(n=r[t])||void 0===n||n.call(r),i)l&&o++;if(o)for(var a=e.firstChild;a;){var u=a.nextSibling;p(a,t),a=u}}}function b(e,t,n){var i=V(e);if("object"===u(t))for(var r in t)g(i,r,t[r]);else g(i,t,n)}function g(e,t,n){e.style[t]=null==n?"":n}var N="http://www.w3.org/1999/xlink";function S(e,t,n,i){var r=V(e);if("object"===u(t))for(var o in t)S(r,o,t[o],i);else{var l=r instanceof SVGElement,a="function"==typeof n;if("style"===t&&"object"===u(n))b(r,n);else if(l&&a)r[t]=n;else if("dataset"===t)A(r,n);else if(l||!(t in r)&&!a||"list"===t){if(l&&"xlink"===t)return void k(r,n);if(i&&"class"===t)return void function(e,t){null==t?e.removeAttribute("class"):e.classList?e.classList.add(t):"object"===u(e.className)&&e.className&&e.className.baseVal?e.className.baseVal="".concat(e.className.baseVal," ").concat(t).trim():e.className="".concat(e.className," ").concat(t).trim()}(r,n);null==n?r.removeAttribute(t):r.setAttribute(t,n)}else r[t]=n}}function k(e,t,n){if("object"===u(t))for(var i in t)k(e,i,t[i]);else null!=n?e.setAttributeNS(N,t,n):e.removeAttributeNS(N,t,n)}function A(e,t,n){if("object"===u(t))for(var i in t)A(e,i,t[i]);else null!=n?e.dataset[t]=n:delete e.dataset[t]}function x(e){return document.createTextNode(null!=e?e:"")}function j(e,t,n){var i,r=o(t);try{for(r.s();!(i=r.n()).done;){var l=i.value;if(0===l||l){var a=u(l);"function"===a?l(e):"string"===a||"number"===a?e.appendChild(x(l)):C(V(l))?w(e,l):l.length?j(e,l,n):"object"===a&&S(e,l,null,n)}}}catch(e){r.e(e)}finally{r.f()}}function E(e){return"string"==typeof e?f(e):V(e)}function V(e){return e.nodeType&&e||!e.el&&e||V(e.el)}function C(e){return null==e?void 0:e.nodeType}function D(e){for(var t=V(e),n=arguments.length,i=new Array(n>1?n-1:0),r=1;r<n;r++)i[r-1]=arguments[r];for(var o=T(e,i,t.firstChild);o;){var l=o.nextSibling;d(e,o),o=l}}function T(e,t,n){for(var i=n,r=Array(t.length),o=0;o<t.length;o++)r[o]=t[o]&&V(t[o]);for(var l=0;l<t.length;l++){var a=t[l];if(a){var u=r[l];if(u!==i)if(C(u)){var s,f=null===(s=i)||void 0===s?void 0:s.nextSibling,c=null!=a.__redom_index&&f===r[l+1];w(e,a,i,c),c&&(i=f)}else null!=a.length&&(i=T(e,a,i));else i=i.nextSibling}}return i}var P=function(){return r((function e(t,i,r){n(this,e),this.View=t,this.initData=r,this.oldLookup={},this.lookup={},this.oldViews=[],this.views=[],null!=i&&(this.key="function"==typeof i?i:function(e){return function(t){return t[e]}}(i))}),[{key:"update",value:function(e,t){for(var n=this.View,i=this.key,r=this.initData,o=null!=i,l=this.lookup,a={},u=Array(e.length),s=this.views,f=0;f<e.length;f++){var c,v,d=e[f],h=void 0;if(o){var m=i(d);h=l[m]||new n(r,d,f,e),a[m]=h,h.__redom_id=m}else h=s[f]||new n(r,d,f,e);null===(c=(v=h).update)||void 0===c||c.call(v,d,f,e,t),V(h.el).__redom_view=h,u[f]=h}this.oldViews=s,this.views=u,this.oldLookup=l,this.lookup=a}}])}();function L(e,t,n,i){return new O(e,t,n,i)}var O=function(){return r((function e(t,i,r,o){n(this,e),this.View=i,this.initData=o,this.views=[],this.pool=new P(i,r,o),this.el=E(t),this.keySet=null!=r}),[{key:"update",value:function(e,t){var n=this.keySet,i=this.views;this.pool.update(e||[],t);var r=this.pool,o=r.views,l=r.lookup;if(n)for(var a=0;a<i.length;a++){var u=i[a];null==l[u.__redom_id]&&(u.__redom_index=null,d(this,u))}for(var s=0;s<o.length;s++){o[s].__redom_index=s}D(this,o),n&&(this.lookup=l),this.views=o}}])}();O.extend=function(e,t,n,i){return O.bind(O,e,t,n,i)},L.extend=O.extend;var R=function(){return r((function e(t,i){n(this,e),this.el=x(""),this.visible=!1,this.view=null,this._placeholder=this.el,t instanceof Node?this._el=t:t.el instanceof Node?(this._el=t,this.view=t):this._View=t,this._initData=i}),[{key:"update",value:function(e,t){var n=this._placeholder,i=this.el.parentNode;if(e){var r,o;if(!this.visible)if(this._el)w(i,this._el,n),d(i,n),this.el=V(this._el),this.visible=e;else{var l=new(0,this._View)(this._initData);this.el=V(l),this.view=l,w(i,l,n),d(i,n)}null===(r=this.view)||void 0===r||null===(o=r.update)||void 0===o||o.call(r,t)}else if(this.visible){if(this._el)return w(i,n,this._el),d(i,this._el),this.el=n,void(this.visible=e);w(i,n,this.view),d(i,this.view),this.el=n,this.view=null}this.visible=e}}])}();var B=function(){return r((function e(t,i,r){n(this,e),this.el=E(t),this.views=i,this.Views=i,this.initData=r}),[{key:"update",value:function(e,t){var n,i;if(e!==this.route){var r=this.views[e];this.route=e,r&&(r instanceof Node||r.el instanceof Node)?this.view=r:this.view=r&&new r(this.initData,t),D(this.el,[this.view])}null===(n=this.view)||void 0===n||null===(i=n.update)||void 0===i||i.call(n,t,e)}}])}(),I="http://www.w3.org/2000/svg";function q(e){for(var t,n=u(e),r=arguments.length,o=new Array(r>1?r-1:0),l=1;l<r;l++)o[l-1]=arguments[l];if("string"===n)t=s(e,I);else{if("function"!==n)throw new Error("At least one argument required");t=i(e,o)}return j(V(t),o,!0),t}var M=q;q.extend=function(){for(var e=arguments.length,t=new Array(e),n=0;n<e;n++)t[n]=arguments[n];return q.bind.apply(q,[this].concat(t))},q.ns=I,e.List=O,e.ListPool=P,e.Place=R,e.Router=B,e.dispatch=function(e,t){var n=arguments.length>2&&void 0!==arguments[2]?arguments[2]:"redom",i=V(e),r=new CustomEvent(n,{bubbles:!0,detail:t});i.dispatchEvent(r)},e.el=c,e.h=v,e.html=f,e.list=L,e.listPool=function(e,t,n){return new P(e,t,n)},e.mount=w,e.place=function(e,t){return new R(e,t)},e.ref=function(e,t,n){return e[t]=n,n},e.router=function(e,t,n){return new B(e,t,n)},e.s=M,e.setAttr=function(e,t,n){S(e,t,n)},e.setChildren=D,e.setData=A,e.setStyle=b,e.setXlink=k,e.svg=q,e.text=x,e.unmount=d,e.viewFactory=function(e,t){if(!e||"object"!==u(e))throw new Error("views must be an object");if(!t||"string"!=typeof t)throw new Error("key must be a string");return function(n,i,r,o){var l=i[t],a=e[l];if(a)return new a(n,i,r,o);throw new Error("view ".concat(l," not found"))}}}));