|
- /*!
- DMXzone State Management
- Version: 1.5.1
- (c) 2022 Wappler.io
- @build 2022-08-25 10:52:07
- */
- dmx.Component("query-manager",{initialData:{data:{}},attributes:{},methods:{set:function(t,e){this.setQueryParam(t,e)},remove:function(t){this.setQueryParam(t)},removeAll:function(){this.setQueryParam()}},render:function(t){this.update()},update:function(){this.set("data",this.parseQuery(window.location.search))},setQueryParam:function(t,e){var a=!1;if(null==e?null==t?(this.data.data={},a=!0):this.data.data[t]&&(delete this.data.data[t],a=!0):this.data.data[t]!=e&&(this.data.data[t]=e,a=!0),a){if(window.URLSearchParams){var o=new URL(window.location);o.search=new URLSearchParams(this.data.data),window.history.pushState(null,null,o)}else window.history.pushState(null,null,window.location.pathname+this.buildQuery(this.data.data)+window.location.hash);dmx.requestUpdate()}},buildQuery:function(t){var e=Object.keys(t);return e.length?"?"+e.reduce((function(e,a){return e&&(e+="&"),e+=encodeURIComponent(a)+"="+encodeURIComponent(t[a])}),""):""},parseQuery:function(t){return(t=t.replace(/^\?/,"")).split("&").reduce((function(t,e){var a=e.replace(/\+/g," ").split("=");return a[0]&&(t[decodeURIComponent(a[0])]=decodeURIComponent(a[1]||"")),t}),{})}}),dmx.Component("cookie-manager",{initialData:{data:{}},attributes:{},methods:{set:function(t,e,a){this.setCookie(t,e,a)},remove:function(t,e){(e=e||{}).expires="1970-01-01T00:00:00Z",this.setCookie(t,"",e)},removeAll:function(t){(t=t||{}).expires="1970-01-01T00:00:00Z",Object.keys(this.data.data).forEach((function(e){this.setCookie(e,"",t)}))}},render:function(t){this.cookie="",this.update()},update:function(){this.cookie!=document.cookie&&(this.cookie=document.cookie,this.set("data",this.cookie.split(/;\s*/).reduce((function(t,e){var a=e.indexOf("=");return t[decodeURIComponent(e.substr(0,a))]=decodeURIComponent(e.substr(a+1)),t}),{})))},setCookie:function(t,e,a){if(!t||/^(?:expires|max\-age|path|domain|secure)$/i.test(t))return!1;(a=a||{}).path=a.path||"/";var o=(t=(t=(t=encodeURIComponent(String(t))).replace(/%(23|24|26|2B|5E|60|7C)/g,decodeURIComponent)).replace(/[\(\)]/g,escape))+"="+(e=(e=encodeURIComponent(String(e))).replace(/%(23|24|26|2B|3A|3C|3E|3D|2F|3F|40|5B|5D|5E|60|7B|7D|7C)/g,decodeURIComponent));a.expires&&("number"==typeof a.expires&&(a.expires=Date.now()+864e5*a.expires),o+="; expires="+new Date(a.expires).toUTCString()),a.domain&&(o+="; domain="+a.domain),a.path&&(o+="; path="+a.path),a.secure&&(o+="; secure"),document.cookie=o,dmx.requestUpdate()}}),dmx.Component("local-manager",{initialData:{data:{}},attributes:{},methods:{set:function(t,e){var a=JSON.stringify(e);null!=a?window.localStorage.setItem("dmxState-"+t,a):window.localStorage.removeItem("dmxState-"+t),this.getData()},remove:function(t){window.localStorage.removeItem("dmxState-"+t),this.getData()},removeAll:function(){Object.keys(window.localStorage).forEach((function(t){t.startsWith("dmxState-")&&window.localStorage.removeItem(t)})),this.getData()}},render:function(t){this.getData()},getData:function(){this.set("data",Object.keys(window.localStorage).reduce((function(t,e){if(e.startsWith("dmxState-"))try{t[e.substr(9)]=JSON.parse(window.localStorage.getItem(e))}catch(t){console.warn("Error parsing JSON: "+window.localStorage.getItem(e))}return t}),{}))}}),dmx.Component("session-manager",{initialData:{data:{}},attributes:{},methods:{set:function(t,e){var a=JSON.stringify(e);null!=a?window.sessionStorage.setItem("dmxState-"+t,a):window.sessionStorage.removeItem("dmxState-"+t),this.getData()},remove:function(t){window.sessionStorage.removeItem("dmxState-"+t),this.getData()},removeAll:function(){Object.keys(window.sessionStorage).forEach((function(t){t.startsWith("dmxState-")&&window.sessionStorage.removeItem(t)})),this.getData()}},render:function(t){this.getData()},getData:function(){this.set("data",Object.keys(window.sessionStorage).reduce((function(t,e){if(e.startsWith("dmxState-"))try{t[e.substr(9)]=JSON.parse(window.sessionStorage.getItem(e))}catch(t){console.warn("Error parsing JSON: "+window.sessionStorage.getItem(e))}return t}),{}))}});
- //# sourceMappingURL=../maps/dmxStateManagement.js.map
|