text: class DView{constructor(t,e,i){if(this.benchmarkEnabled=!0,this.debug=!1,this.conf={keepNodeNames:!0,extendArray:!0},this.benchmarkEnabled&&(this.startTime=(new Date).getTime()),this.styledNodes=[],this.styles=this.processStyles(i)||[],this.data=this.initData(t||{}),this.actions=e||{},this.ignoreTags=["i","b","a","p","div","span","button","u","pre","small","h1","h2","h3","h4","h5","h6","canvas","img","audio","video","input","select","button","label"],this.components={},this.initComponents(),this.DOMOriginal=document.body.innerHTML,this.rendered=!1,this.render(),this.rendered=!0,this.benchmarkEnabled){let t=this.benchmarkTime();this.renderTime=t,this.debug&&console.log(`Page rendered in ${this.renderTime}ms`)}}parse(t,e){void 0===t&&(t=new DViewNode(this,document.body)),e instanceof Array||(e=[]),(e=Array.from(e)).push(t.name),t.children=[];let i=t.node.childNodes;for(let n=0;n<i.length;n++){let s=i[n];if(1==s.nodeType){let i=this.components[s.tagName.toLowerCase()],n=null!=i;if(t.isList()){let s=t.data(),h=t.listNode();for(let a=0;a<s.length;a++){let s=Array.from(e);if(n){let e=i.clone();e.parent=t,e.listIndex=a,e.pathSet(s),t.children.push(e)}else t.children.push(this.parse(new DViewNode(this,this.parent?this.parent.listItem.cloneNode(!0):h.cloneNode(!0),s,t,a),Array.from(e)))}}else if(n){let n=i.clone();n.parent=t,n.pathSet(Array.from(e)),t.children.push(n)}else t.children.push(this.parse(new DViewNode(this,s,Array.from(e),t),Array.from(e)))}else t.children.push(s)}return t}dom(t){return void 0===t&&(t=this.objectModel),document.createElement("div").appendChild(t.dom())}render(){this.reset(),document.body.innerHTML="",document.body.appendChild(this.dom()),this.objectModel.initNode()}reset(){document.body.innerHTML=this.DOMOriginal,this.objectModel=this.parse()}initComponents(){let t=document.querySelectorAll("d-component");for(let e=0;e<t.length;e++){let i=t[e];if(-1==i.getAttributeNames().indexOf("name")){console.warn("Skipped component with no name");continue}let n=i.getAttribute("name");this.components[n]instanceof DViewNode?console.warn(`Component ${n} already defined, skipping`):(i.setAttribute("d-name",n),this.components["d-"+n]=this.parse(new DViewNode(this,i.cloneNode(!0)),[]),i.parentNode.removeChild(i))}}initData(t){if(t instanceof Array){for(let e=0;e<t.length;e++)"object"==typeof t[e]&&(t[e]=this.initData(t[e]));return t}let e=new DViewData(this),i=Object.keys(t);for(let n=0;n<i.length;n++){let s=i[n];if(t[s]instanceof Array){for(let e=0;e<t[s].length;e++){let i=t[s][e];i instanceof Array&&(t[s][e]=this.initData(i))}e.setVal(s,t[s],!0)}else"object"==typeof t[s]?e.setVal(s,this.initData(t[s]),!0):e.setVal(s,t[s],!0)}return e}query(t){return this.objectModel.query(t)}queryAll(t){return this.objectModel.queryAll(t)}pathClean(t){return t.filter((t=>-1==this.ignoreTags.indexOf(t)))}withinPath(t,e,i,n,s){if("boolean"!=typeof i&&(i=!1),"boolean"!=typeof s&&(s=!1),"boolean"!=typeof n&&(n=!1),i&&t.length!=e.length)return!1;let h=t.length-e.length;if(h<0)return!1;for(let i=0;i<h+1;i++){let a=!0;for(let n=0;n<e.length;n++)if(e[n]!=t[i+n]){a=!1;break}if(a&&(!s||i==h))return!0;if(n)return!1}return!1}withinPathLoose(t,e){let i=0;for(let n=0;n<e.length;n++){let s=!1;for(let h=i;h<t.length;h++)if(e[n]==t[h]){i=h,s=!0;break}if(!s)return!1}return!0}processStyles(t){if("object"!=typeof t)return[];let e=[];return Object.keys(t).forEach((i=>{let n={path:i.split("."),styles:t[i]};e.push(n)})),e}dataChanged(){this.stylesUpdate()}stylesUpdate(){this.styledNodes.forEach((t=>{t.styleUpdate()}))}benchmarkTime(){return(new Date).getTime()-this.startTime}exec(t,e){e instanceof Array||(e=[]),"function"==typeof this.actions[t]?this.actions[t].apply(this,e):console.warn(`Action ${t} not defined`)}}class DViewData{constructor(t){this.dview=t,this.data={},this.nodes=[]}setVal(t,e,i){if("boolean"!=typeof i&&(i=!1),e instanceof Array){for(let t=0;t<e.length;t++)if("object"==typeof e[t]&&!(e[t]instanceof Array)){let i=new DViewData(this.dview);Object.keys(e[t]).forEach((n=>{i.setVal(n,e[t][n],!0)})),e[t]=i}this.dview.conf.extendArray&&(e.add=i=>{"object"!=typeof i||i instanceof DViewData||(i=this.dview.initData(i));let n=e.push(i);return this.changed(t),n},e.addStart=i=>{"object"!=typeof i||i instanceof DViewData||(i=this.dview.initData(i));let n=e.unshift(i);return this.changed(t),n},e.removeEnd=()=>{let i=e.pop();return this.changed(t),i},e.removeStart=()=>{let i=e.shift();return this.changed(t),i},e.remove=i=>{let n=e.splice(i,1);return this.changed(t),n},e.alter=(i,n,s)=>{let h;return void 0!==s?("object"!=typeof s||s instanceof DViewData||(s=this.dview.initData(s)),h=e.splice(i,n,s)):h=e.splice(i,n),this.changed(t),h})}let n=t.toLowerCase();if(void 0===this.data[n]){Object.defineProperty(this,n,{get:function(){return"function"==typeof this.data[n]&&"data"!=n?this.data[n].apply(this.dview,[]):this.data[n]},set:function(t){return this.data[n]=t,this.changed(n),this.dview.dataChanged(),this}}),n!==t&&Object.defineProperty(this,t,{get:function(){return"function"==typeof this.data[n]&&"data"!=n?this.data[n].apply(this.dview,[]):this.data[n]},set:function(t){return this.data[n]=t,this.changed(n),this.dview.dataChanged(),this}}),this.nodesUsingData().filter((t=>t.name==n)).forEach((t=>{t.subscribe(this,n)}))}i?this.data[n]=e:this[n]=e}uses(t,e){t instanceof DViewNode&&this.nodes.push([t,e])}changed(t){if(this.dview.debug&&console.log("changed "+t),0==this.nodes.length)return!1;if(0==this.dview.rendered)return!1;this.nodes=this.nodes.filter((t=>0==t[0].destroyed)),this.nodes.filter((e=>e.indexOf(t)>-1||e.indexOf("*")>-1)).reduce(((t,e)=>(-1==t.indexOf(e[0])&&t.push(e[0]),t)),[]).forEach((t=>{t.cacheClear(),t.redraw()}))}clone(){let t=new DViewData(this.dview);return Object.keys(this.data).forEach((e=>{this.data[e]instanceof DViewData?t[e]=this.data[e].clone():t.setVal(e,this.data[e],!0)})),t}nodesUsingData(t){if(!this.dview.rendered)return[];t instanceof Array||(t=this.dview.objectModel.children);let e=[],i=[];for(let n=0;n<t.length;n++)"function"==typeof t[n].data&&(t[n].data()==this&&(e=e.concat([t[n]])),i=i.concat(t[n].childNodes()));return i.length>0&&(e=e.concat(this.nodesUsingData(i))),e}}class DViewNode{constructor(t,e,i,n,s){if(this.dview=t,this.parent=n||null,this.listIndex="number"==typeof s?s:null,i=i instanceof Array?Array.from(i):[],this.node=e,this.name=e.tagName.toLowerCase(),"tag"===this.name&&console.log(s),this.children=[],this.path=i,this.listItem=null,this.nodeOriginal=e.cloneNode(!0),this.classNamesOriginal=[],this.showCondition=null,this.showConditionData=null,this.showConditionKey=null,this.showConditionPath=null,this.HTMLData=!1,this.dataSubscribed=[],this.destroyed=!1,this.inDOM=!0,this.initialized=!1,this.dataCached=null,this.node.hasAttribute("d-name")){let t=this.node.getAttribute("d-name").toLowerCase();this.name=t}this.isList()&&(this.listItem=this.listNode().cloneNode(!0)),this.parent instanceof DViewNode&&this.isDataNode()&&this.subscribe(this.parent.data(),this.name),this.styles().length>0&&this.dview.styledNodes.push(this)}dom(){if(this.clickInitialized=!1,this.destroyed)return this.nodeRemove(),null;let t=document.createElement("div");if(this.dview.ignoreTags.indexOf(this.node.tagName.toLowerCase())>-1?t=document.createElement(this.node.tagName.toLowerCase()):this.dview.conf.keepNodeNames&&"body"!=this.name&&(t=document.createElement(this.name)),this.node.getAttributeNames().forEach((e=>{t.setAttribute(e,this.node.getAttribute(e))})),this.node=t,this.initNode(),this.styleUpdate(),this.parent&&(this.disabled||this.missingData()&&this.parent.missingData()||!this.show()||this.isFlatArrayItem()&&this.parent.data().length-1<this.listIndex))return this.inDOM=!1,this.nodeRemove(),null;let e=!1;if(null!=this.parentNode()&&(e=this.isFlatArrayItem()),this.hasData()&&(!e||this.isFlatArrayItemValue())&&!this.ignoredTag()){let e=this.dataType();if(["string","number"].indexOf(e)>-1)if(this.HTMLData){let e=document.createElement("div");e.innerHTML=this.data().toString();for(let i=e.childNodes.length-1;i>-1;i--)t.appendChild(e.childNodes[e.childNodes.length-i-1])}else{let e=document.createTextNode(this.data().toString());t.appendChild(e)}}for(let e=0;e<this.children.length;e++){let i=this.children[e];if(i instanceof DViewNode){let e=i.dom();null!==e&&t.appendChild(e)}else t.appendChild(i)}return this.inDOM=!0,t}initNode(){if(this.node.hasAttribute("d-model")){let t=this.node.getAttribute("d-model");this.node.addEventListener("input",(()=>{let e,i,n;if(t.indexOf(".")>-1){let e=t.split(".");n=e.pop(),i=this.root().query(e)}else i=this,n=t;e=i.data(),i.missingData()&&i.parent.data().setVal(i.name,new DViewData(this.dview)),e=i.data(),e.setVal(n,this.node.value)}))}if(!this.initialized){if(this.classNamesOriginal=this.node.className.split(" ").filter((t=>t.length>0)),this.node.hasAttribute("d-if")){let t=this.node.getAttribute("d-if").toLowerCase(),e=!1;t.indexOf("!")>-1&&(e=!0,t=/!(.+)/.exec(t)[1].trim());let i=t.split("."),n=null,s=null;if(1==i.length?(n=this.data(),s=i[0]):(s=i.pop(),n=null),null==n&&(n=this.root().query(i).data()),n instanceof DViewData&&"function"==typeof n.data[s]||"function"==typeof n[s]){let t="function"==typeof n[s]?n[s]:n.data[s];this.showCondition=e?function(){return!t.apply(this,[])}:function(){return t.apply(this,[])},this.showConditionData=null,this.showConditionKey=null,n.uses(this,"*")}else this.showConditionData=n,this.showConditionKey=s,this.showCondition=e?function(t){return!t}:function(t){return t},n instanceof DViewData&&n.uses(this,s)}this.node.hasAttribute("d-ignore")&&(this.ignored=!0)}if(!this.clickInitialized){if(this.node.hasAttribute("d-click")){let t=this.node.getAttribute("d-click");this.node.addEventListener("click",(e=>{e.stopPropagation(),this.dview.exec(t,[this.dview.objectModel,this])}))}this.clickInitialized=!0}this.node.hasAttribute("d-html")&&(this.HTMLData=!0),this.initialized=!0;for(let t=0;t<this.children.length;t++)this.children[t]instanceof DViewNode&&this.children[t].initNode()}listNode(){let t=null;for(var e=0;e<this.nodeOriginal.childNodes.length;e++)if(1==this.nodeOriginal.childNodes[e].nodeType){t=this.nodeOriginal.childNodes[e];break}return t}getListIndex(){if(null!==this.listIndex)return this.listIndex;let t=this.parent;for(;null!==t;){if(null!==t.listIndex)return t.listIndex;t=t.parent}return null}hasData(){return"undefined"!==this.dataType()}dataType(){return typeof this.data()}data(){if(null!==this.dataCached)return this.dataCached;let t=this.dataRaw();if(null===t)return null;if(t instanceof DViewData)return t;if("function"==typeof t)return this.computedDataSubscribed||(this.subscribe(this.parentNode().data(),"*"),this.computedDataSubscribed=!0),t.apply(this.dview,[]);if("object"==typeof t&&!(t instanceof Array)){let e=Object.keys(t);for(let i=0;i<e.length;i++){let n=e[i].toLowerCase();n!==e[i]&&(t[n]=t[e[i]],delete t[e[i]])}}return this.isDataNode()&&(this.dataCached=t),t}dataRaw(){if("body"==this.name&&null==this.parent)return this.dview.data;if(null==this.parent)return null;let t,e=this.parent.data();if("number"==typeof this.listIndex&&this.parent.data()instanceof Array)t=e[this.listIndex];else{if(null==e)return this.parent.data();if(void 0===e[this.name])return e;t=e[this.name]}return t}isList(){return this.data()instanceof Array}redraw(){if(this.dataCached=null,!this.dview.rendered)return!1;if(this.clickInitialized=!1,this.dview.debug&&console.log("redraw",this.name),!this.show()&&this.parent.nodeContains(this.node))try{return this.node.parentNode.removeChild(this.node),this.clickInitialized=!1,!1}catch(t){}if(!this.show())return!1;if(this.isList()&&(null===this.listItem&&(this.debug&&console.log("Trying to find list item for "+this.name),this.listItem=this.listNode().cloneNode(!0)),this.childrenUpdate()),null!=this.node.parentNode){this.dview.debug&&console.log("C0");try{this.clickInitialized=!1;let t=this.node,e=this.dom();this.parent.node.insertBefore(e,t),this.parent.node.removeChild(t),this.node=e}catch(t){this.dview.debug&&console.log(t)}}else{try{this.parent.node.removeChild(this.node)}catch(t){this.dview.debug&&console.log(t)}let t=this.parent.children.indexOf(this);if(this.clickInitialized=!1,null!=this.dom()&&1==this.show()){try{if(this.parent.node.appendChild(this.node),0==t)if(this.parent.children[1]){let t=this.parent.children[1];this.parent.node.insertBefore(this.node,t.node?t.node:t),this.dview.debug&&console.log("c1.1")}else this.parent.node.appendChild(this.node),this.dview.debug&&console.log("c1.2");else{let e=this.parent.children[t+1];e.node?(this.dview.debug&&console.log("c2.1"),e.node.parentNode.insertBefore(this.node,e.node)):(this.parent.node.insertBefore(this.node,e),this.dview.debug&&console.log("c2.2"))}}catch(t){this.dview.debug&&console.log(t)}return!1}}}childrenUpdate(){try{let t=this.data(),e=this.listItem||this.listNode();if(null==e)return!1;let i=this.childNodes(),n=i.length;if(i.some((t=>null===t.listIndex)))for(let t=0;t<n;t++)i[t].listIndex=t;if(t.length>n)for(let i=0;i<t.length-n;i++)this.children.push(this.dview.parse(new DViewNode(this.dview,e.cloneNode(!0),this.path.concat([this.name]),this,n+i),Array.from(this.path)));if(t.length<n){let e=n-t.length,i=0;for(let t=this.children.length-1;t>-1;t--)if(this.children[t]instanceof DViewNode){if(this.children.splice(t,1)[0].unsubscribe(),i++,e==i)break}}this.childNodes().forEach((t=>{t.cacheClear()}))}catch(t){this.dview.debug&&console.log(this.name,this,t)}}subscribe(t,e){t instanceof DViewData&&(t.uses(this,e),this.dataSubscribed.push([t,e]))}unsubscribe(){this.dataSubscribed.forEach((t=>{t[0].nodes=t[0].nodes.filter((t=>t[0]!==this))}))}queryOld(t,e){void 0===e&&(e=this);let i=t[0],n=null;e.name==i&&(n=e);for(let i=0;i<e.children.length;i++)if(e.children[i]instanceof DViewNode){let s=this.query(t,e.children[i]);null!==s&&(n=s)}return null!==n?t.length>1?(t.shift(),this.query(t,n)):n:null}query(t){let e=this.queryAll(t,!0);return e.length>0?e[0]:null}queryAll(t,e,i){"boolean"!=typeof e&&(e=!0),i instanceof Array||(i=[this]);let n=[],s=[];for(let e=0;e<i.length;e++)this.dview.withinPathLoose(i[e].path.concat([i[e].name]),t)&&n.push(i[e]),s=s.concat(i[e].childNodes());return s.length>0&&(n=n.concat(this.queryAll(t,e,s))),e&&(n=n.filter((e=>e.name==t[t.length-1]||e.node.tagName.toLowerCase()==t[t-1]))),n}parentNode(){let t=this;do{if(t=t.parent,null==t)return null;if(t.isDataNode()||null==t.parent)return t}while(null!==t);return null}parentFind(t){let e=this;do{if(e=e.parent,null==e)return null;if(e.name==t)return e}while(null!==e);return null}parentWithData(){let t=this;for(;;){if(t=t.parent,null==t.parent)return t;if(t.parent.data()!=t.data())return t}}childNodes(){return this.children.filter((t=>t instanceof DViewNode))}childByData(t){return this.childNodes().find((e=>e.data()===t))}isDataNode(){return null!=this.parent&&(!this.ignoredTag()&&!this.parent.isList()&&"number"!=typeof this.listIndex)}missingData(){return null!=this.parent&&(this.isDataNode()&&this.data()==this.parent.data())}show(){if(null===this.showCondition)return!0;let t=[];return null!=this.showConditionData&&(null!=this.showConditionKey?t.push(this.showConditionData[this.showConditionKey]):t.push(this.showConditionData)),t.push(this),1==this.showCondition.apply(this.dview,t)}destroy(){this.dview.debug&&console.log("destroyed "+this.name),this.destroyed=!0,this.inDOM=!1,this.nodeRemove(),this.unsubscribe(),this.redraw()}nodeContains(t){for(let e=0;e<this.node.childNodes.length;e++)if(this.node.childNodes[e]==t)return!0;return!1}setShow(t,e,i){this.showCondition=t,this.showConditionData=e,this.showConditionKey=i,this.redraw(),e.uses(this,i)}root(){if(this.dview.rendered)return this.dview.objectModel;{let t=this;do{if(null==t.parent)return t;t=t.parent}while(null!=t);return t}}isFlatArrayItem(){let t=this.parentNode();return!!t&&(t.isList()&&this.parent.data()instanceof Array&&"object"!==this.dataType())}isFlatArrayItemValue(){return this.isFlatArrayItem()&&"item"==this.name}ignoredTag(){return this.ignored||this.dview.ignoreTags.indexOf(this.name)>-1}styles(){return this.dview.styles.filter((t=>this.dview.withinPathLoose(this.path.concat([this.name]),t.path)))}getClassNames(){let t=this.styles(),e=[];return this.classNamesOriginal&&(e=e.concat(this.classNamesOriginal)),t.forEach((t=>{Object.keys(t.styles).forEach((i=>{t.styles[i].apply(this.dview,[this])&&e.push(i)}))})),e}styleUpdate(){this.node.className=this.getClassNames().join(" ")}nodeRemove(){try{this.parent.node.removeChild(this.node)}catch(t){}}cacheClear(){this.dataCached=null,this.childNodes().forEach((t=>{t.cacheClear()}))}clone(){let t=new DViewNode(this.dview,this.nodeOriginal.cloneNode(!0),Array.from(this.path),this.parent,this.listIndex);t.children=Array.from(this.children),t.showCondition=this.showCondition,t.showConditionData=this.showConditionData,t.showConditionKey=this.showConditionKey;for(let e=0;e<t.children.length;e++)t.children[e]instanceof DViewNode?(t.children[e]=t.children[e].clone(),t.children[e].parent=t):t.children[e]=t.children[e].cloneNode(!0);return t}pathSet(t){t=Array.from(t),this.path=Array.from(t),t.push(this.name),this.childNodes().forEach((e=>{e.pathSet(t)}))}}
  