fix(dist): build for v0.1.1

master 0.1.1
Ariel Mashraki 10 years ago
parent 6dcc4f1a58
commit a8d48ff5c7

@ -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 <greg@gregpike.ca>
* @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

@ -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 <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);
*/!function(a,b){"use strict";function c(a){return/^-?\d+\.?\d*$/.test(a.replace(/["']/g,""))}var d=b.isDefined,e=b.isUndefined,f=b.isNumber,g=b.isObject,h=b.isArray,i=b.extend,j=b.toJson,k=b.fromJson,l=b.module("LocalStorageModule",[]);l.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","$parse",function(a,b,l,m){var n,o=this,p=o.prefix,q=o.cookie,r=o.notify,s=o.storageType;l?l[0]&&(l=l[0]):l=document,"."!==p.substr(-1)&&(p=p?p+".":"");var t=function(a){return p+a},u=function(){try{var c=s in b&&null!==b[s],d=t("__"+Math.round(1e7*Math.random()));return c&&(n=b[s],n.setItem(d,""),n.removeItem(d)),c}catch(e){return s="cookie",a.$broadcast("LocalStorageModule.notification.error",e.message),!1}}(),v=function(b,c){if(e(c)?c=null:(g(c)||h(c)||f(+c||c))&&(c=j(c)),!u||"cookie"===o.storageType)return u||a.$broadcast("LocalStorageModule.notification.warning","LOCAL_STORAGE_NOT_SUPPORTED"),r.setItem&&a.$broadcast("LocalStorageModule.notification.setitem",{key:b,newvalue:c,storageType:"cookie"}),B(b,c);try{(g(c)||h(c))&&(c=j(c)),n&&n.setItem(t(b),c),r.setItem&&a.$broadcast("LocalStorageModule.notification.setitem",{key:b,newvalue:c,storageType:o.storageType})}catch(d){return a.$broadcast("LocalStorageModule.notification.error",d.message),B(b,c)}return!0},w=function(b){if(!u||"cookie"===o.storageType)return u||a.$broadcast("LocalStorageModule.notification.warning","LOCAL_STORAGE_NOT_SUPPORTED"),C(b);var d=n?n.getItem(t(b)):null;return d&&"null"!==d?"{"===d.charAt(0)||"["===d.charAt(0)||c(d)?k(d):d:null},x=function(b){if(!u||"cookie"===o.storageType)return u||a.$broadcast("LocalStorageModule.notification.warning","LOCAL_STORAGE_NOT_SUPPORTED"),r.removeItem&&a.$broadcast("LocalStorageModule.notification.removeitem",{key:b,storageType:"cookie"}),D(b);try{n.removeItem(t(b)),r.removeItem&&a.$broadcast("LocalStorageModule.notification.removeitem",{key:b,storageType:o.storageType})}catch(c){return a.$broadcast("LocalStorageModule.notification.error",c.message),D(b)}return!0},y=function(){if(!u)return a.$broadcast("LocalStorageModule.notification.warning","LOCAL_STORAGE_NOT_SUPPORTED"),!1;var b=p.length,c=[];for(var d in n)if(d.substr(0,b)===p)try{c.push(d.substr(b))}catch(e){return a.$broadcast("LocalStorageModule.notification.error",e.Description),[]}return c},z=function(b){b=b||"";var c=p.slice(0,-1),d=new RegExp(c+"."+b);if(!u||"cookie"===o.storageType)return u||a.$broadcast("LocalStorageModule.notification.warning","LOCAL_STORAGE_NOT_SUPPORTED"),E();var e=p.length;for(var f in n)if(d.test(f))try{x(f.substr(e))}catch(g){return a.$broadcast("LocalStorageModule.notification.error",g.message),E()}return!0},A=function(){try{return navigator.cookieEnabled||"cookie"in l&&(l.cookie.length>0||(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<c.length;d++){for(var e=c[d];" "===e.charAt(0);)e=e.substring(1,e.length);if(0===e.indexOf(t(b)+"=")){var f=decodeURIComponent(e.substring(p.length+b.length+1,e.length));try{var g=JSON.parse(f);return k(g)}catch(h){return f}}}return null},D=function(a){B(a,null)},E=function(){for(var a=null,b=p.length,c=l.cookie.split(";"),d=0;d<c.length;d++){for(a=c[d];" "===a.charAt(0);)a=a.substring(1,a.length);var e=a.substring(b,a.indexOf("="));D(e)}},F=function(){return s},G=function(a,b,c,e){e||(e=b);var f=w(e);null===f&&d(c)?f=c:g(f)&&g(c)&&(f=i(c,f)),m(b).assign(a,f),a.$watchCollection(b,function(a){v(e,a)})},H=function(){for(var a=0,c=b[s],d=0;d<c.length;d++)0===c.key(d).indexOf(p)&&a++;return a};return{isSupported:u,getStorageType:F,set:v,add:v,get:w,keys:y,remove:x,clearAll:z,bind:G,deriveKey:t,length:H,cookie:{set:B,add:B,get:C,remove:D,clearAll:E}}}]})}(window,window.angular);