magix-components
Version:
1 lines • 1.78 kB
JavaScript
define("mx-storage/index",["magix"],function(t,e,a){var d,i,_,n=t("magix"),r={},o=!1,c={};i="localStorage";var s=function(){var t=(+new Date).toString(),e=JSON.stringify(c);r.__du=e,r.__dv=t,"__dw"==i&&(_.setAttribute("__dv",t),_.setAttribute("__du",e),_.save("__dx"))},l=n.mix({set:function(t,e){c[t]=e,s()},get:function(t){return c[t]},del:function(t){delete c[t],s()}},n.Event);if(i in window)try{localStorage.setItem(i,n.guid()),o=!0,localStorage.removeItem(i)}catch(t){}if(o)r=localStorage,d=r.__dv;else if((i="globalStorage")in window)try{var u=window[i];r="localhost"==location.hostname?u.__dy:u[location.hostname],d=r.__dv}catch(t){}else if((_=document.createElement("link")).addBehavior){_.style.behavior="url(#default#userData)",document.getElementsByTagName("head")[0].appendChild(_);try{_.load("__dx")}catch(t){_.setAttribute("__dx","{}"),_.save("__dx"),_.load("__dx")}var v="{}";try{v=_.getAttribute("__dx"),d=_.getAttribute("__dv")}catch(t){}i="__dw",r.__du=v}var g=function(){var t;clearTimeout(g.timer),g.timer=setTimeout(function(){if("__dw"==i){_.load("__dx");try{t=_.getAttribute("__dv")}catch(t){}}else t=r.__dv;if(t&&t!=d){d=t;var e=void 0,a="{}";if("__dw"==i){_.load("__dx");try{a=_.getAttribute("__dx")}catch(t){}r.__du=a}e=JSON.parse(r.__du);for(var o={},s=0,u=n.keys(c).concat(n.keys(e));s<u.length;s++){var v=u[s];if(1!==o[v]){o[v]=1;var g=JSON.stringify(c[v]);JSON.stringify(e[v])!=g&&l.fire("change",{key:v,from:c[v],to:e[v]})}}c=e}},25)};if("addEventListener"in window&&window.addEventListener("pageshow",function(t){t.persisted&&g()},!1),"__dw"==i?setInterval(g,1e3):"addEventListener"in window?window.addEventListener("storage",g,!1):document.attachEvent("onstorage",g),r.__du)try{c=JSON.parse(r.__du)}catch(t){r.__du="{}"}else r.__du="{}";a.exports=l});