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/dist/angular-local-storage.min.js

7 lines
4.7 KiB
JavaScript

/**
* An Angular module that gives you access to the browsers local storage
* @version v0.0.8 - 2014-08-15
* @link https://github.com/grevory/angular-local-storage
* @author grevory <greg@gregpike.ca>
* @license MIT License, http://www.opensource.org/licenses/MIT
*/!function(a,b){"use strict";var c=b.module("LocalStorageModule",[]);c.provider("localStorageService",function(){this.prefix="ls",this.storageType="localStorage",this.cookie={expiry:30,path:"/"},this.notify={setItem:!0,removeItem:!1},this.setPrefix=function(a){this.prefix=a},this.setStorageType=function(a){this.storageType=a},this.setStorageCookie=function(a,b){this.cookie={expiry:a,path:b}},this.setStorageCookieDomain=function(a){this.cookie.domain=a},this.setNotify=function(a,b){this.notify={setItem:a,removeItem:b}},this.$get=["$rootScope","$window","$document",function(a,c,d){var e,f=this,g=f.prefix,h=f.cookie,i=f.notify,j=f.storageType;d?d[0]&&(d=d[0]):d=document,"."!==g.substr(-1)&&(g=g?g+".":"");var k=function(a){return g+a},l=function(){try{var b=j in c&&null!==c[j],d=k("__"+Math.round(1e7*Math.random()));return b&&(e=c[j],e.setItem(d,""),e.removeItem(d)),b}catch(f){return j="cookie",a.$broadcast("LocalStorageModule.notification.error",f.message),!1}}(),m=function(c,d){if(!l||"cookie"===f.storageType)return a.$broadcast("LocalStorageModule.notification.warning","LOCAL_STORAGE_NOT_SUPPORTED"),i.setItem&&a.$broadcast("LocalStorageModule.notification.setitem",{key:c,newvalue:d,storageType:"cookie"}),s(c,d);"undefined"==typeof d&&(d=null);try{(b.isObject(d)||b.isArray(d))&&(d=b.toJson(d)),e&&e.setItem(k(c),d),i.setItem&&a.$broadcast("LocalStorageModule.notification.setitem",{key:c,newvalue:d,storageType:f.storageType})}catch(g){return a.$broadcast("LocalStorageModule.notification.error",g.message),s(c,d)}return!0},n=function(c){if(!l||"cookie"===f.storageType)return a.$broadcast("LocalStorageModule.notification.warning","LOCAL_STORAGE_NOT_SUPPORTED"),t(c);var d=e?e.getItem(k(c)):null;return d&&"null"!==d?"{"===d.charAt(0)||"["===d.charAt(0)?b.fromJson(d):d:null},o=function(b){if(!l)return a.$broadcast("LocalStorageModule.notification.warning","LOCAL_STORAGE_NOT_SUPPORTED"),i.removeItem&&a.$broadcast("LocalStorageModule.notification.removeitem",{key:b,storageType:"cookie"}),u(b);try{e.removeItem(k(b)),i.removeItem&&a.$broadcast("LocalStorageModule.notification.removeitem",{key:b,storageType:f.storageType})}catch(c){return a.$broadcast("LocalStorageModule.notification.error",c.message),u(b)}return!0},p=function(){if(!l)return a.$broadcast("LocalStorageModule.notification.warning","LOCAL_STORAGE_NOT_SUPPORTED"),!1;var b=g.length,c=[];for(var d in e)if(d.substr(0,b)===g)try{c.push(d.substr(b))}catch(f){return a.$broadcast("LocalStorageModule.notification.error",f.Description),[]}return c},q=function(b){b=b||"";var c=g.slice(0,-1),d=new RegExp(c+"."+b);if(!l)return a.$broadcast("LocalStorageModule.notification.warning","LOCAL_STORAGE_NOT_SUPPORTED"),v();var f=g.length;for(var h in e)if(d.test(h))try{o(h.substr(f))}catch(i){return a.$broadcast("LocalStorageModule.notification.error",i.message),v()}return!0},r=function(){try{return navigator.cookieEnabled||"cookie"in d&&(d.cookie.length>0||(d.cookie="test").indexOf.call(d.cookie,"test")>-1)}catch(b){return a.$broadcast("LocalStorageModule.notification.error",b.message),!1}},s=function(b,c){if("undefined"==typeof c)return!1;if(!r())return a.$broadcast("LocalStorageModule.notification.error","COOKIES_NOT_SUPPORTED"),!1;try{var e="",f=new Date,g="";if(null===c?(f.setTime(f.getTime()+-864e5),e="; expires="+f.toGMTString(),c=""):0!==h.expiry&&(f.setTime(f.getTime()+24*h.expiry*60*60*1e3),e="; expires="+f.toGMTString()),b){var i="; path="+h.path;h.domain&&(g="; domain="+h.domain),d.cookie=k(b)+"="+encodeURIComponent(c)+e+i+g}}catch(j){return a.$broadcast("LocalStorageModule.notification.error",j.message),!1}return!0},t=function(b){if(!r())return a.$broadcast("LocalStorageModule.notification.error","COOKIES_NOT_SUPPORTED"),!1;for(var c=d.cookie&&d.cookie.split(";")||[],e=0;e<c.length;e++){for(var f=c[e];" "===f.charAt(0);)f=f.substring(1,f.length);if(0===f.indexOf(k(b)+"="))return decodeURIComponent(f.substring(g.length+b.length+1,f.length))}return null},u=function(a){s(a,null)},v=function(){for(var a=null,b=g.length,c=d.cookie.split(";"),e=0;e<c.length;e++){for(a=c[e];" "===a.charAt(0);)a=a.substring(1,a.length);var f=a.substring(b,a.indexOf("="));u(f)}},w=function(){return j},x=function(a,c,d){var e=n(c);null===e&&b.isDefined(d)?e=d:b.isObject(e)&&b.isObject(d)&&(e=b.extend(d,e)),a[c]=e,a.$watchCollection(c,function(a){m(c,a)})};return{isSupported:l,getStorageType:w,set:m,add:m,get:n,keys:p,remove:o,clearAll:q,bind:x,deriveKey:k,cookie:{set:s,add:s,get:t,remove:u,clearAll:v}}}]})}(window,window.angular);