text: class DP{constructor(){this._queriedParent=null,this._queriedData=null,this._newData=null,this._queriedSelector=null,this.collections={},this.models={},this.watchers=[],this._init()}_init(){window.dp=this,document.addEventListener("DOMContentLoaded",()=>{this._collectData(),this._observeCollectedData()})}_parent(a,b){for(Element.prototype.matches||(Element.prototype.matches=Element.prototype.matchesSelector||Element.prototype.mozMatchesSelector||Element.prototype.msMatchesSelector||Element.prototype.oMatchesSelector||Element.prototype.webkitMatchesSelector||function(a){for(var b=(this.document||this.ownerDocument).querySelectorAll(a),c=b.length;0<=--c&&b.item(c)!==this;);return-1<c});a&&a!==document;a=a.parentNode)if(a.matches(b))return a}_resetQueriedData(){this._queriedParent=null,this._queriedData=null,this._queriedSelector=null}_resetNewData(){this._newData=null}_template(a){const b=a.children;if(b.length){let a=null;for(let c=0;c<b.length;c++){const d=b[c];if(0==c&&(a=[d]),"TEMPLATE"===d.nodeName)return d.content.children}return a}}_collectData(){const a=document.querySelectorAll("[dp-model],[data-dp-model]");if(a.length)for(let b=0;b<a.length;b++){let c={},d=null;const e=a[b],f=e.getAttribute("dp-model"),g=e.getAttribute("dp-watcher");this._replaceDataAttribute(e,"model");const h=this._parent(e,"[dp-collection],[data-dp-collection]");if(!h)f&&(this.models[f]?c=this.models[f]:this.models[f]={});else if(this._replaceDataAttribute(h,"collection"),d=h.getAttribute("dp-collection"),d)if(!this.collections[d]){const a=this._template(h);this.collections[d]={_elements:[h],_template:a}}else this.collections[d]._elements.includes(h)||this.collections[d]._elements.push(h),this.collections[d][f]&&(c=this.collections[d][f]);const i=this._collectPropertyData(e);let j={...c,...i};j._elements||(j._elements=[]),j._elements.push(e),j._watcher||(j._watcher=g),h?this.collections[d][f]=j:this.models[f]=j}}_collectPropertyData(a){const b={},c="property",d=a.querySelectorAll(`[dp-${c}],[${`data-dp-${c}`}]`);if(d.length)for(let a=0;a<d.length;a++){const e=d[a];this._replaceDataAttribute(e,c);const f=e.getAttribute(`dp-${c}`);b[f]=e.innerText||e.value}return b}_replaceDataAttribute(a,b){const c=`data-dp-${b}`,d=a.getAttribute(c);d&&(a.setAttribute(`dp-${b}`,d),a.removeAttribute(c))}_observeCollectedData(){const a=Object.keys(this.collections);if(a.length)for(let b=0;b<a.length;b++){const c=a[b];this._observeModelData(this.collections[c]),this.collections[c]=this._observeData(this.collections[c],this._observeCollectionDataHandler())}this._observeModelData(this.models)}_observeModelData(a){const b=Object.keys(a);if(b.length)for(let c=0;c<b.length;c++){const d=b[c];"_"!==d.substring(1,0)&&(a[d]=this._observeData(a[d],this._observeModelDataHandler()))}}_observeData(a,b){return new Proxy(a,b)}_observeCollectionsDataHandler(){return{set(a){return console.log("Setting collections data",a),!0},deleteProperty(a,b){const c=a[b]._elements;if(c)for(let a=0;a<c.length;a++){const b=c[a];b.parentNode.removeChild(b)}return this._resetQueriedData(),delete a[b]}}}_observeCollectionDataHandler(){const a=this;return{set(b,c,d){const e=b._template,f=Object.keys(d);if(e.length){const g=b._elements;if(g.length)for(let b=0;b<g.length;b++){const a=g[b];for(let g=0;g<e.length;g++){const b=e[g],h=b.cloneNode(!0);h.setAttribute("dp-model",c),d._elements||(d._elements=[]),d._elements.push(h);for(let a=0;a<f.length;a++){const b=f[a],c=d[b],e=h.querySelector(`[dp-property="${b}"]`);e&&(e.innerText=c)}h.innerHTML=h.innerHTML.replace("${reference}",c),a.appendChild(h)}}}return b[c]=a._observeData(d,a._observeModelDataHandler()),!0},deleteProperty(b,c){const d=b[c]._elements;if(d)for(let a=0;a<d.length;a++){const b=d[a];b.parentNode.removeChild(b)}return a._resetQueriedData(),delete b[c]}}}_observeModelDataHandler(){const a=this;return{set(b,c,d){if("_"!==c.substring(1,0)){const e=b._elements;if(e){a.watchers[b._watcher]&&a.watchers[b._watcher][c]&&(d=a.watchers[b._watcher][c](d,a._queriedData,a._newData)),b[c]=d;for(let a=0;a<e.length;a++){const b=e[a],f=b.querySelector(`[dp-property="${c}"]`);f&&(f.innerText=d)}}}return!0},deleteProperty(){return!0}}}collection(a){return a&&this.collections[a]?(this._queriedParent=this.collections,this._queriedData=this.collections[a],this._queriedSelector=a,this):void console.warn(`We couldn't find collection: ${a}`)}model(a){if(a){const b=a.split("."),c=b.length;if(3>c)if(2===c){const a=this.collections[b[0]];if(a&&a[b[1]])return this._queriedParent=a,this._queriedData=a[b[1]],this._queriedSelector=b[1],this}else if(this.models[b[0]])return this._queriedParent=this.models,this._queriedData=this.models[b[0]],this._queriedSelector=b[1],this}console.warn(`We couldn't find model: ${a}`)}insert(a,b){a&&this._queriedData&&!this._queriedData[a]&&"object"==typeof b&&Object.keys(b).length&&(this._queriedData[a]=b)}update(a,b=null){if(this._queriedData){if("object"==typeof a){const b=Object.keys(a).length;if(b){this._newData=Object.assign(Object.assign({},this._queriedData),a);for(let c=0;c<b;c++){const b=Object.keys(a)[c];this._queriedData[b]=a[b]}this._resetNewData()}}else"string"==typeof a&&this._queriedData[a]&&(this._queriedData[a]=b);this._resetQueriedData()}}delete(){this._queriedParent&&this._queriedSelector&&delete this._queriedParent[this._queriedSelector]}request(a={}){const b=a.method?a.method.toUpperCase():"GET",c=a.url,d=a.contentType||"json",e=a.dispatcher,f=a.callback;if(c){e&&e.classList.add("is-loading");const a=new XMLHttpRequest;if(a.open(b,c),a.withCredentials=!0,a.setRequestHeader("X-Requested-With","XMLHttpRequest"),"GET"==b)"json"===d?a.setRequestHeader("Content-Type","application/json"):"html"===d?a.setRequestHeader("Content-Type","text/html"):void 0;else if("POST"==b){const b=document.querySelector("meta[name=\"csrf-token\"]");b&&a.setRequestHeader("X-CSRF-TOKEN",b.getAttribute("content"))}a.onload=()=>{if(e&&e.classList.remove("is-loading"),f){let b;"json"===d?b=JSON.parse(a.responseText):"html"===d?b=a.responseText:void 0,f(b,{dispatcher:e})}},a.send(this.data)}else console.warning("You have to set an URL to make a \""+b+"\" request.")}get(a,b={}){this.request({url:a,method:"GET",contentType:b.contentType,callback:b.callback,dispatcher:b.dispatcher})}post(a,b={}){this.request({url:a,method:"POST",contentType:b.callback,callback:b.callback,dispatcher:b.dispatcher})}}new DP;