From a8d48ff5c74540ccdc18dee32f3a16e8876f7f60 Mon Sep 17 00:00:00 2001 From: Ariel Mashraki Date: Tue, 7 Oct 2014 01:15:20 +0300 Subject: [PATCH] fix(dist): build for v0.1.1 --- dist/angular-local-storage.js | 115 ++++++++++++++++++++++-------- dist/angular-local-storage.min.js | 4 +- 2 files changed, 87 insertions(+), 32 deletions(-) diff --git a/dist/angular-local-storage.js b/dist/angular-local-storage.js index 79e4ea3..84d8722 100644 --- a/dist/angular-local-storage.js +++ b/dist/angular-local-storage.js @@ -1,12 +1,30 @@ /** * An Angular module that gives you access to the browsers local storage - * @version v0.0.8 - 2014-08-15 + * @version v0.1.1 - 2014-10-07 * @link https://github.com/grevory/angular-local-storage * @author grevory * @license MIT License, http://www.opensource.org/licenses/MIT */ (function ( window, angular, undefined ) { +/*jshint globalstrict:true*/ 'use strict'; + +var isDefined = angular.isDefined, + isUndefined = angular.isUndefined, + isNumber = angular.isNumber, + isObject = angular.isObject, + isArray = angular.isArray, + extend = angular.extend, + toJson = angular.toJson, + fromJson = angular.fromJson; + + +// Test if string is only contains numbers +// e.g '1' => true, "'1'" => true +function isStringNumber(num) { + return /^-?\d+\.?\d*$/.test(num.replace(/["']/g, '')); +} + var angularLocalStorage = angular.module('LocalStorageModule', []); angularLocalStorage.provider('localStorageService', function() { @@ -68,9 +86,7 @@ angularLocalStorage.provider('localStorageService', function() { }; }; - - - this.$get = ['$rootScope', '$window', '$document', function($rootScope, $window, $document) { + this.$get = ['$rootScope', '$window', '$document', '$parse', function($rootScope, $window, $document, $parse) { var self = this; var prefix = self.prefix; var cookie = self.cookie; @@ -91,7 +107,7 @@ angularLocalStorage.provider('localStorageService', function() { } var deriveQualifiedKey = function(key) { return prefix + key; - } + }; // Checks the browser to see if local storage is supported var browserSupportsLocalStorage = (function () { try { @@ -123,24 +139,28 @@ angularLocalStorage.provider('localStorageService', function() { // If local storage is not available in the browser use cookies // Example use: localStorageService.add('library','angular'); var addToLocalStorage = function (key, value) { + // Let's convert undefined values to null to get the value consistent + if (isUndefined(value)) { + value = null; + } else if (isObject(value) || isArray(value) || isNumber(+value || value)) { + value = toJson(value); + } // If this browser does not support local storage use cookies if (!browserSupportsLocalStorage || self.storageType === 'cookie') { - $rootScope.$broadcast('LocalStorageModule.notification.warning', 'LOCAL_STORAGE_NOT_SUPPORTED'); + if (!browserSupportsLocalStorage) { + $rootScope.$broadcast('LocalStorageModule.notification.warning', 'LOCAL_STORAGE_NOT_SUPPORTED'); + } + if (notify.setItem) { $rootScope.$broadcast('LocalStorageModule.notification.setitem', {key: key, newvalue: value, storageType: 'cookie'}); } return addToCookies(key, value); } - // Let's convert undefined values to null to get the value consistent - if (typeof value === "undefined") { - value = null; - } - try { - if (angular.isObject(value) || angular.isArray(value)) { - value = angular.toJson(value); + if (isObject(value) || isArray(value)) { + value = toJson(value); } if (webStorage) {webStorage.setItem(deriveQualifiedKey(key), value)}; if (notify.setItem) { @@ -158,7 +178,10 @@ angularLocalStorage.provider('localStorageService', function() { var getFromLocalStorage = function (key) { if (!browserSupportsLocalStorage || self.storageType === 'cookie') { - $rootScope.$broadcast('LocalStorageModule.notification.warning','LOCAL_STORAGE_NOT_SUPPORTED'); + if (!browserSupportsLocalStorage) { + $rootScope.$broadcast('LocalStorageModule.notification.warning','LOCAL_STORAGE_NOT_SUPPORTED'); + } + return getFromCookies(key); } @@ -169,8 +192,8 @@ angularLocalStorage.provider('localStorageService', function() { return null; } - if (item.charAt(0) === "{" || item.charAt(0) === "[") { - return angular.fromJson(item); + if (item.charAt(0) === "{" || item.charAt(0) === "[" || isStringNumber(item)) { + return fromJson(item); } return item; @@ -179,8 +202,11 @@ angularLocalStorage.provider('localStorageService', function() { // Remove an item from local storage // Example use: localStorageService.remove('library'); // removes the key/value pair of library='angular' var removeFromLocalStorage = function (key) { - if (!browserSupportsLocalStorage) { - $rootScope.$broadcast('LocalStorageModule.notification.warning', 'LOCAL_STORAGE_NOT_SUPPORTED'); + if (!browserSupportsLocalStorage || self.storageType === 'cookie') { + if (!browserSupportsLocalStorage) { + $rootScope.$broadcast('LocalStorageModule.notification.warning', 'LOCAL_STORAGE_NOT_SUPPORTED'); + } + if (notify.removeItem) { $rootScope.$broadcast('LocalStorageModule.notification.removeitem', {key: key, storageType: 'cookie'}); } @@ -235,8 +261,11 @@ angularLocalStorage.provider('localStorageService', function() { var tempPrefix = prefix.slice(0, -1); var testRegex = new RegExp(tempPrefix + '.' + regularExpression); - if (!browserSupportsLocalStorage) { - $rootScope.$broadcast('LocalStorageModule.notification.warning', 'LOCAL_STORAGE_NOT_SUPPORTED'); + if (!browserSupportsLocalStorage || self.storageType === 'cookie') { + if (!browserSupportsLocalStorage) { + $rootScope.$broadcast('LocalStorageModule.notification.warning', 'LOCAL_STORAGE_NOT_SUPPORTED'); + } + return clearAllFromCookies(); } @@ -273,8 +302,10 @@ angularLocalStorage.provider('localStorageService', function() { // Example use: localStorageService.cookie.add('library','angular'); var addToCookies = function (key, value) { - if (typeof value === "undefined") { + if (isUndefined(value)) { return false; + } else if(isArray(value) || isObject(value)) { + value = toJson(value); } if (!browserSupportsCookies()) { @@ -325,7 +356,13 @@ angularLocalStorage.provider('localStorageService', function() { thisCookie = thisCookie.substring(1,thisCookie.length); } if (thisCookie.indexOf(deriveQualifiedKey(key) + '=') === 0) { - return decodeURIComponent(thisCookie.substring(prefix.length + key.length + 1, thisCookie.length)); + var storedValues = decodeURIComponent(thisCookie.substring(prefix.length + key.length + 1, thisCookie.length)) + try{ + var obj = JSON.parse(storedValues); + return fromJson(obj) + }catch(e){ + return storedValues + } } } return null; @@ -355,22 +392,39 @@ angularLocalStorage.provider('localStorageService', function() { return storageType; }; - var bindToScope = function(scope, key, def) { - var value = getFromLocalStorage(key); + var bindToScope = function(scope, scopeKey, def, lsKey) { + if (!lsKey) { + lsKey = scopeKey; + } + + var value = getFromLocalStorage(lsKey); - if (value === null && angular.isDefined(def)) { + if (value === null && isDefined(def)) { value = def; - } else if (angular.isObject(value) && angular.isObject(def)) { - value = angular.extend(def, value); + } else if (isObject(value) && isObject(def)) { + value = extend(def, value); } - scope[key] = value; + $parse(scopeKey).assign(scope, value); - scope.$watchCollection(key, function(newVal) { - addToLocalStorage(key, newVal); + scope.$watchCollection(scopeKey, function(newVal) { + addToLocalStorage(lsKey, newVal); }); }; + // Return localStorageService.length + // ignore keys that not owned + var lengthOfLocalStorage = function() { + var count = 0; + var storage = $window[storageType]; + for(var i = 0; i < storage.length; i++) { + if(storage.key(i).indexOf(prefix) === 0 ) { + count++; + } + } + return count; + }; + return { isSupported: browserSupportsLocalStorage, getStorageType: getStorageType, @@ -382,6 +436,7 @@ angularLocalStorage.provider('localStorageService', function() { clearAll: clearAllFromLocalStorage, bind: bindToScope, deriveKey: deriveQualifiedKey, + length: lengthOfLocalStorage, cookie: { set: addToCookies, add: addToCookies, //DEPRECATED diff --git a/dist/angular-local-storage.min.js b/dist/angular-local-storage.min.js index 4672c1f..7f76903 100644 --- a/dist/angular-local-storage.min.js +++ b/dist/angular-local-storage.min.js @@ -1,7 +1,7 @@ /** * An Angular module that gives you access to the browsers local storage - * @version v0.0.8 - 2014-08-15 + * @version v0.1.1 - 2014-10-07 * @link https://github.com/grevory/angular-local-storage * @author grevory * @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;e0||(l.cookie="test").indexOf.call(l.cookie,"test")>-1)}catch(b){return a.$broadcast("LocalStorageModule.notification.error",b.message),!1}},B=function(b,c){if(e(c))return!1;if((h(c)||g(c))&&(c=j(c)),!A())return a.$broadcast("LocalStorageModule.notification.error","COOKIES_NOT_SUPPORTED"),!1;try{var d="",f=new Date,i="";if(null===c?(f.setTime(f.getTime()+-864e5),d="; expires="+f.toGMTString(),c=""):0!==q.expiry&&(f.setTime(f.getTime()+24*q.expiry*60*60*1e3),d="; expires="+f.toGMTString()),b){var k="; path="+q.path;q.domain&&(i="; domain="+q.domain),l.cookie=t(b)+"="+encodeURIComponent(c)+d+k+i}}catch(m){return a.$broadcast("LocalStorageModule.notification.error",m.message),!1}return!0},C=function(b){if(!A())return a.$broadcast("LocalStorageModule.notification.error","COOKIES_NOT_SUPPORTED"),!1;for(var c=l.cookie&&l.cookie.split(";")||[],d=0;d