9 lines
4.2 KiB
JavaScript
9 lines
4.2 KiB
JavaScript
/*!
|
|
App Connect State Management
|
|
Version: 2.0.0
|
|
(c) 2024 Wappler.io
|
|
@build 2024-04-15 17:48:46
|
|
*/
|
|
dmx.Component("query-manager",{initialData:{data:{}},methods:{set(e,t){this._setQueryParam(e,t)},remove(e){this._setQueryParam(e)},removeAll(){this._setQueryParam()}},render:!1,init(){this._updateHandler=this._updateHandler.bind(this),window.addEventListener("popstate",this._updateHandler),window.addEventListener("pushstate",this._updateHandler),window.addEventListener("replacestate",this._updateHandler),this._updateHandler()},destroy(){window.removeEventListener("popstate",this._updateHandler),window.removeEventListener("pushstate",this._updateHandler),window.removeEventListener("replacestate",this._updateHandler)},_setQueryParam:function(e,t){let o=!1,a=dmx.clone(this.data.data);if(null==t?null==e?(a={},o=!0):a[e]&&(delete a[e],o=!0):a[e]!=t&&(a[e]=t,o=!0),o){const e=new URL(window.location);e.search=new URLSearchParams(a),window.history.pushState(null,null,e)}},_buildQuery:function(e){const t=Object.keys(e);return t.length?"?"+t.reduce((function(t,o){return t&&(t+="&"),t+=encodeURIComponent(o)+"="+encodeURIComponent(e[o])}),""):""},_parseQuery:function(){return this.search.replace(/^\?/,"").split("&").reduce((function(e,t){const o=t.replace(/\+/g," ").split("=");return o[0]&&(e[decodeURIComponent(o[0])]=decodeURIComponent(o[1]||"")),e}),{})},_updateHandler(){this.search!==window.location.search&&(this.search=window.location.search,this.set("data",this._parseQuery()))}}),dmx.Component("cookie-manager",{initialData(){return this._cookie=document.cookie,{data:this._getCookie()}},methods:{set(e,t,o={}){this._setCookie(e,t,o)},remove(e,t={}){t.expires="1970-01-01T00:00:00Z",this._setCookie(e,"",t)},removeAll(e={}){e.expires="1970-01-01T00:00:00Z",Object.keys(this.data.data).forEach((function(t){this._setCookie(t,"",e)}))}},render:!1,_getCookie(){return this._cookie.split(/;\s*/).reduce((function(e,t){var o=t.indexOf("=");return e[decodeURIComponent(t.substr(0,o))]=decodeURIComponent(t.substr(o+1)),e}),{})},_setCookie:function(e,t,o){if(!e||/^(?:expires|max\-age|path|domain|secure)$/i.test(e))return!1;(o=o||{}).path=o.path||"/";var a=(e=(e=(e=encodeURIComponent(String(e))).replace(/%(23|24|26|2B|5E|60|7C)/g,decodeURIComponent)).replace(/[\(\)]/g,escape))+"="+(t=(t=encodeURIComponent(String(t))).replace(/%(23|24|26|2B|3A|3C|3E|3D|2F|3F|40|5B|5D|5E|60|7B|7D|7C)/g,decodeURIComponent));o.expires&&("number"==typeof o.expires&&(o.expires=Date.now()+864e5*o.expires),a+="; expires="+new Date(o.expires).toUTCString()),o.domain&&(a+="; domain="+o.domain),o.path&&(a+="; path="+o.path),o.secure&&(a+="; secure"),document.cookie=a,this._cookie=document.cookie,this.set("data",this._getCookie())}}),dmx.Component("local-manager",{initialData:{data:{}},methods:{set(e,t){const o=JSON.stringify(t);null!=o?window.localStorage.setItem("dmxState-"+e,o):window.localStorage.removeItem("dmxState-"+e),this._getData()},remove(e){window.localStorage.removeItem("dmxState-"+e),this._getData()},removeAll(){Object.keys(window.localStorage).forEach((function(e){e.startsWith("dmxState-")&&window.localStorage.removeItem(e)})),this._getData()}},render:!1,init(){this._getData()},_getData(){this.set("data",Object.keys(window.localStorage).reduce((function(e,t){if(t.startsWith("dmxState-"))try{e[t.slice(9)]=JSON.parse(window.localStorage.getItem(t))}catch(e){console.warn("Error parsing JSON: "+window.localStorage.getItem(t))}return e}),{}))}}),dmx.Component("session-manager",{initialData:{data:{}},methods:{set(e,t){const o=JSON.stringify(t);null!=o?window.sessionStorage.setItem("dmxState-"+e,o):window.sessionStorage.removeItem("dmxState-"+e),this._getData()},remove(e){window.sessionStorage.removeItem("dmxState-"+e),this._getData()},removeAll(){Object.keys(window.sessionStorage).forEach((function(e){e.startsWith("dmxState-")&&window.sessionStorage.removeItem(e)})),this._getData()}},render:!1,init(e){this._getData()},_getData(){this.set("data",Object.keys(window.sessionStorage).reduce((function(e,t){if(t.startsWith("dmxState-"))try{e[t.slice(9)]=JSON.parse(window.sessionStorage.getItem(t))}catch(e){console.warn("Error parsing JSON: "+window.sessionStorage.getItem(t))}return e}),{}))}});
|
|
//# sourceMappingURL=dmxStateManagement.js.map
|