From 92323fa83272c0de4f3b0889552ad940aa328da0 Mon Sep 17 00:00:00 2001 From: Gregory Pike Date: Fri, 14 Feb 2014 02:11:12 -0500 Subject: [PATCH] Return storage type, code formatting, and uglification --- angular-local-storage.js | 28 +++++++++++++++++----------- angular-local-storage.min.js | 2 +- demo/demo-app.js | 6 ++++++ package.json | 8 ++++---- 4 files changed, 28 insertions(+), 16 deletions(-) diff --git a/angular-local-storage.js b/angular-local-storage.js index bff911c..b96e1fe 100644 --- a/angular-local-storage.js +++ b/angular-local-storage.js @@ -3,18 +3,18 @@ 'use strict'; var angularLocalStorage = angular.module('LocalStorageModule', []); -angularLocalStorage.provider('localStorageService', function(){ +angularLocalStorage.provider('localStorageService', function() { + // You should set a prefix to avoid overwriting any local storage variables from the rest of your app // e.g. localStorageServiceProvider.setPrefix('youAppName'); // With provider you can use config as this: // myApp.config(function (localStorageServiceProvider) { // localStorageServiceProvider.prefix = 'yourAppName'; // }); - this.prefix = 'ls'; // You could change web storage type localstorage or sessionStorage - this.storageType='localStorage'; + this.storageType = 'localStorage'; // Cookie options (usually in case of fallback) // expiry = Number of days before cookies expire // 0 = Does not expire @@ -31,17 +31,17 @@ angularLocalStorage.provider('localStorageService', function(){ }; // Setter for the prefix - this.setPrefix = function(prefix){ + this.setPrefix = function(prefix) { this.prefix = prefix; }; // Setter for the storageType - this.setStorageType = function(storageType){ + this.setStorageType = function(storageType) { this.storageType = storageType; }; // Setter for cookie config - this.setStorageCookie = function(exp, path){ + this.setStorageCookie = function(exp, path) { this.cookie = { expiry: exp, path: path @@ -49,20 +49,20 @@ angularLocalStorage.provider('localStorageService', function(){ }; // Setter for cookie domain - this.setStorageCookieDomain = function(domain){ + this.setStorageCookieDomain = function(domain) { this.cookie.domain = domain; }; // Setter for notification config // itemSet & itemRemove should be booleans - this.setNotify = function(itemSet, itemRemove){ + this.setNotify = function(itemSet, itemRemove) { this.notify = { setItem: itemSet, removeItem: itemRemove }; }; - this.$get = ['$rootScope', '$window', '$document', function($rootScope, $window, $document){ + this.$get = ['$rootScope', '$window', '$document', function($rootScope, $window, $document) { var prefix = this.prefix; var cookie = this.cookie; @@ -93,6 +93,7 @@ angularLocalStorage.provider('localStorageService', function(){ return true; } catch (e) { + storageType = 'cookie'; $rootScope.$broadcast('LocalStorageModule.notification.error', e.message); return false; } @@ -298,9 +299,9 @@ angularLocalStorage.provider('localStorageService', function(){ } var cookies = $document.cookie && $document.cookie.split(';') || []; - for(var i=0;i < cookies.length;i++) { + for(var i=0; i < cookies.length; i++) { var thisCookie = cookies[i]; - while (thisCookie.charAt(0)===' ') { + while (thisCookie.charAt(0) === ' ') { thisCookie = thisCookie.substring(1,thisCookie.length); } if (thisCookie.indexOf(prefix + key + '=') === 0) { @@ -330,8 +331,13 @@ angularLocalStorage.provider('localStorageService', function(){ } }; + var getStorageType = function() { + return storageType; + }; + return { isSupported: browserSupportsLocalStorage, + getStorageType: getStorageType, set: addToLocalStorage, add: addToLocalStorage, //DEPRECATED get: getFromLocalStorage, diff --git a/angular-local-storage.min.js b/angular-local-storage.min.js index 039cca1..3c0c68b 100644 --- a/angular-local-storage.min.js +++ b/angular-local-storage.min.js @@ -1 +1 @@ -(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;r0||(c.cookie="test").indexOf.call(c.cookie,"test")>-1)}catch(b){return a.$broadcast("LocalStorageModule.notification.error",b.message),!1}},p=function(b,f){if("undefined"==typeof f)return!1;if(!o())return a.$broadcast("LocalStorageModule.notification.error","COOKIES_NOT_SUPPORTED"),!1;try{var g="",h=new Date,i="";if(null===f?(h.setTime(h.getTime()+-864e5),g="; expires="+h.toGMTString(),f=""):0!==e.expiry&&(h.setTime(h.getTime()+24*e.expiry*60*60*1e3),g="; expires="+h.toGMTString()),b){var j="; path="+e.path;e.domain&&(i="; domain="+e.domain),c.cookie=d+b+"="+encodeURIComponent(f)+g+j+i}}catch(k){return a.$broadcast("LocalStorageModule.notification.error",k.message),!1}return!0},q=function(b){if(!o())return a.$broadcast("LocalStorageModule.notification.error","COOKIES_NOT_SUPPORTED"),!1;for(var e=c.cookie&&c.cookie.split(";")||[],f=0;f= 0) { + $scope.storageType = 'Session storage' + } + if (!localStorageService.isSupported) { $scope.storageType = 'Cookie'; } diff --git a/package.json b/package.json index 058aed5..b3d80a3 100644 --- a/package.json +++ b/package.json @@ -25,10 +25,10 @@ "grunt-cli": "~0.1.9" }, "devDependencies": { - "time-grunt": "~0.1.1", - "load-grunt-tasks": "~0.1.0", - "grunt-contrib-jshint": "~0.6.4", + "time-grunt": "~0.2.9", + "load-grunt-tasks": "~0.3.0", + "grunt-contrib-jshint": "~0.8.0", "grunt": "~0.4.2", - "grunt-contrib-uglify": "~0.2.7" + "grunt-contrib-uglify": "~0.3.2" } }