-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathWirup.min.js
1 lines (1 loc) · 4.51 KB
/
Wirup.min.js
1
"use strict";var wirup=function(){};wirup.prototype=function(){var e,t=function(e){return document.getElementById(e)},r=(e,t,r,n)=>new Promise((a,o)=>{let i=new XMLHttpRequest;i.open(e,t,!0),i.setRequestHeader("Content-Type",r),i.setRequestHeader("cache-control","no-cache"),i.setRequestHeader("expires","0"),i.setRequestHeader("expires","Tue, 01 Jan 1980 1:00:00 GMT"),i.setRequestHeader("pragma","no-cache"),i.onreadystatechange=()=>{if(4===i.readyState){if(200===i.status)a(n(i.responseText));else{let e=i.statusText||"Your ajax request threw an error.";o(e)}}},i.send()}),n=window.location.pathname,a=()=>r("GET","views/views.json","application/json; charset=UTF-8",o),o=t=>{e=JSON.parse(t).views},i=t=>new Promise((r,a)=>{let o,i=n||"/";if(!e||0===e.length){a("Views are undefined or empty, cannot load template.");return}for(let c=0;c<e.length;c++)if(e[c].url===i){o=c;break}void 0===o&&(console.warn(`No view found for the URL: ${i}. Redirecting to default view.`),o=0),s(o,t).then(()=>r()).catch(e=>{console.error(`Error filling view for URL ${i}: ${e}`),a(`Failed to fill view for ${i}`)})}).catch(e=>{console.error(`Error in _getTemplate: ${e}`),console.error("View may not have been defined.")}),s=function(r,n){let a="views/"+e[r].viewFile;return new Promise((e,r)=>{fetch(a+"?").then(e=>{if(e.ok)return e.text();r(`Failed to load HTML view: ${a}`)}).then(a=>{let o=t(n);o?(o.innerHTML=a,e()):r(`Target element with ID "${n}" not found`)}).catch(e=>{r(e)})})},c=()=>{window.onpopstate=e=>{n=window.location.pathname,v()}},l={},d={},u={},p=()=>new Promise(function(e,r){let n=t("contentBody").querySelectorAll("[datasource]");[].forEach.call(n,e=>{e.innerHTML=f(e.tagName.toLowerCase(),e.getAttribute("datasource"))}),g("Switched View","CBody","NA"),e()}),h=e=>{t("contentBody").querySelectorAll('[datasource="'+e+'"]').forEach(t=>{t.innerHTML=f(t.tagName.toLowerCase(),e),g("Updated Data.",t.tagName.toLowerCase(),"No Comment.")})},f=(e,t)=>{let r=[],n=l[t],a=typeof n;switch(a){case"object":Array.isArray(n)?r=n.map(t=>d[e](t)):r.push(d[e](n));break;case"string":r.push(d[e](n));break;default:throw Error(`Unsupported data type: ${a}`)}return Array.isArray(r)?r.join(""):r},m=(e,t)=>new Promise((r,n)=>{let a=t.map(t=>new Promise((r,n)=>{let a=document.createElement("script");a.type="text/javascript",a.src=`${e}/${t}`,document.getElementsByTagName("head")[0].appendChild(a),a.onload=()=>r(),a.onerror=()=>n(`Failed to load script: ${t}`)}));Promise.all(a).then(r).catch(e=>{console.error(`Error loading scripts from folder: ${e}`),n(e)})}).catch(e=>{console.error(`Error in _loadScriptsFromFolder: ${e}`)}),w="",g=(e,t,r)=>{let n=new Date().getTime(),a={name:e,element:t,comment:r,profile:w,timestamp:n},o=localStorage.getItem("wuActions");localStorage.setItem("wuActions",o+","+JSON.stringify(a))},y=e=>window[e](),$=e=>new Promise(function(t,r){let n=["components.js"];e?.components&&Array.isArray(e.components)&&e.components.length>0&&(n=e.components),m("components",n).then(()=>{a().then(()=>{i("contentBody").then(()=>{p().then(()=>{c(),void 0!==e.callbackName&&y(e.callbackName)})})})})}),v=e=>{$(e)};return{wx:t,wijax:r,jsonize:function(e){try{return JSON.parse(window[e])}catch(t){return window[e]}},loadScript:function(e){return new Promise((t,r)=>{let n=document.createElement("script");n.type="text/javascript",n.src=e,document.getElementsByTagName("head")[0].appendChild(n),n.onload=()=>{t()},n.onabort=()=>{r()}})},registerComponent(e,t){d[e]=t},registerProfile(e){w=e},registerAction:g,buildComponent:f,buildComponents(e,t){let r=[];return e.forEach((e,n)=>{let a=l[t[n]],o=typeof a;switch(o){case"object":Array.isArray(a)?r=r.concat(a.map(t=>d[e](t))):r.push(d[e](a));break;case"string":r.push(d[e](a));break;default:throw Error(`Unsupported data type: ${o}`)}}),Array.isArray(r)?r.join(""):r},navigateTo(e){history.pushState(null,null,e),n=e,i("contentBody").then(()=>{p()})},registerData(e,t){Object.defineProperty(l,e,{get:function(){return this["_"+e]},set:function(t){this["_"+e],this["_"+e]=t,h(e)}}),l[e]=t},addData(e,t){let r=[];(r=l[e]).push(t),l[e]=r,r=[]},findIndexByKey:function(e,t,r){if(!this.dataStore[e])throw Error(`Data source '${e}' not found in dataStore`);let n=this.dataStore[e].findIndex(e=>e[t]===r);return n},updateData(e,t,r){let n=[];(n=l[e])[t=t-1<0?0:t-1]=r,l[e]=n,n=[]},removeData(e,t){let r=[];r=l[e],t=t-1<0?0:t-1,r.splice(t,1),l[e]=r,r=[]},dataStore:l,onLoad(e){Object.defineProperty(u,"trigger",{get:function(){return this._trigger},set:function(e){this._trigger,this._trigger=e,window[e]()}}),u.trigger=e},components:d,init:v}}(),window.wu=new wirup;