You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.
angular-local-storage/angular-local-storage.min.js

1 line
4.2 KiB
JavaScript

(function(){var a=angular.module("LocalStorageModule",[]);a.provider("localStorageService",function(){this.prefix="ls";this.cookie={expiry:30,path:"/"};this.notify={setItem:true,removeItem:false};this.setPrefix=function(b){this.prefix=b};this.setStorageCookie=function(c,b){this.cookie={expiry:c,path:b}};this.setNotify=function(b,c){this.notify={setItem:b,removeItem:c}};this.$get=["$rootScope",function(m){var h=this.prefix;var c=this.cookie;var p=this.notify;if(h.substr(-1)!=="."){h=!!h?h+".":""}var e=(function(){try{var q=("localStorage" in window&&window.localStorage!==null);var r=h+"__"+Math.round(Math.random()*10000000);if(q){localStorage.setItem(r,"");localStorage.removeItem(r)}return true}catch(s){m.$broadcast("LocalStorageModule.notification.error",s.message);return false}}());var i=function(q,r){if(!e){m.$broadcast("LocalStorageModule.notification.warning","LOCAL_STORAGE_NOT_SUPPORTED");if(p.setItem){m.$broadcast("LocalStorageModule.notification.setitem",{key:q,newvalue:r,storageType:"cookie"})}return j(q,r)}if(typeof r==="undefined"){r=null}try{if(angular.isObject(r)||angular.isArray(r)){r=angular.toJson(r)}localStorage.setItem(h+q,r);if(p.setItem){m.$broadcast("LocalStorageModule.notification.setitem",{key:q,newvalue:r,storageType:"localStorage"})}}catch(s){m.$broadcast("LocalStorageModule.notification.error",s.message);return j(q,r)}return true};var l=function(q){if(!e){m.$broadcast("LocalStorageModule.notification.warning","LOCAL_STORAGE_NOT_SUPPORTED");return f(q)}var r=localStorage.getItem(h+q);if(!r||r==="null"){return null}if(r.charAt(0)==="{"||r.charAt(0)==="["){return angular.fromJson(r)}return r};var g=function(q){if(!e){m.$broadcast("LocalStorageModule.notification.warning","LOCAL_STORAGE_NOT_SUPPORTED");if(p.removeItem){m.$broadcast("LocalStorageModule.notification.removeitem",{key:q,storageType:"cookie"})}return n(q)}try{localStorage.removeItem(h+q);if(p.removeItem){m.$broadcast("LocalStorageModule.notification.removeitem",{key:q,storageType:"localStorage"})}}catch(r){m.$broadcast("LocalStorageModule.notification.error",r.message);return n(q)}return true};var k=function(){if(!e){m.$broadcast("LocalStorageModule.notification.warning","LOCAL_STORAGE_NOT_SUPPORTED");return false}var q=h.length;var s=[];for(var r in localStorage){if(r.substr(0,q)===h){try{s.push(r.substr(q))}catch(t){m.$broadcast("LocalStorageModule.notification.error",t.Description);return[]}}}return s};var b=function(q){var q=q||"";var v=h.slice(0,-1)+".";var t=RegExp(v+q);if(!e){m.$broadcast("LocalStorageModule.notification.warning","LOCAL_STORAGE_NOT_SUPPORTED");return d()}var r=h.length;for(var s in localStorage){if(t.test(s)){try{g(s.substr(r))}catch(u){m.$broadcast("LocalStorageModule.notification.error",u.message);return d()}}}return true};var o=function(){try{return navigator.cookieEnabled||("cookie" in document&&(document.cookie.length>0||(document.cookie="test").indexOf.call(document.cookie,"test")>-1))}catch(q){m.$broadcast("LocalStorageModule.notification.error",q.message);return false}};var j=function(s,t){if(typeof t==="undefined"){return false}if(!o()){m.$broadcast("LocalStorageModule.notification.error","COOKIES_NOT_SUPPORTED");return false}try{var r="",q=new Date();if(t===null){q.setTime(q.getTime()+(-1*24*60*60*1000));r="; expires="+q.toGMTString();t=""}else{if(c.expiry!==0){q.setTime(q.getTime()+(c.expiry*24*60*60*1000));r="; expires="+q.toGMTString()}}if(!!s){document.cookie=h+s+"="+encodeURIComponent(t)+r+"; path="+c.path}}catch(u){m.$broadcast("LocalStorageModule.notification.error",u.message);return false}return true};var f=function(s){if(!o()){m.$broadcast("LocalStorageModule.notification.error","COOKIES_NOT_SUPPORTED");return false}var t=document.cookie.split(";");for(var r=0;r<t.length;r++){var q=t[r];while(q.charAt(0)===" "){q=q.substring(1,q.length)}if(q.indexOf(h+s+"=")===0){return decodeURIComponent(q.substring(h.length+s.length+1,q.length))}}return null};var n=function(q){j(q,null)};var d=function(){var r=null,u=null;var q=h.length;var v=document.cookie.split(";");for(var t=0;t<v.length;t++){r=v[t];while(r.charAt(0)===" "){r=r.substring(1,r.length)}var s=r.substring(q,r.indexOf("="));n(s)}};return{isSupported:e,set:i,add:i,get:l,keys:k,remove:g,clearAll:b,cookie:{set:j,add:j,get:f,remove:n,clearAll:d}}}]})}).call(this);