|
|
|
@ -1,24 +1,41 @@
|
|
|
|
|
(function() {
|
|
|
|
|
/* Start angularLocalStorage */
|
|
|
|
|
|
|
|
|
|
var angularLocalStorage = angular.module('LocalStorageModule', []);
|
|
|
|
|
/* Start angularLocalStorage */
|
|
|
|
|
'use strict';
|
|
|
|
|
var angularLocalStorage = angular.module('LocalStorageModule', []);
|
|
|
|
|
|
|
|
|
|
angularLocalStorage.provider('localStorageService', function(){
|
|
|
|
|
// You should set a prefix to avoid overwriting any local storage variables from the rest of your app
|
|
|
|
|
// e.g. angularLocalStorage.constant('prefix', 'youAppName');
|
|
|
|
|
angularLocalStorage.value('prefix', 'ls');
|
|
|
|
|
this.prefix = 'ls';
|
|
|
|
|
// Cookie options (usually in case of fallback)
|
|
|
|
|
// expiry = Number of days before cookies expire // 0 = Does not expire
|
|
|
|
|
// path = The web path the cookie represents
|
|
|
|
|
angularLocalStorage.constant('cookie', { expiry:30, path: '/'});
|
|
|
|
|
angularLocalStorage.constant('notify', { setItem: true, removeItem: false} );
|
|
|
|
|
|
|
|
|
|
angularLocalStorage.service('localStorageService', [
|
|
|
|
|
'$rootScope',
|
|
|
|
|
'prefix',
|
|
|
|
|
'cookie',
|
|
|
|
|
'notify',
|
|
|
|
|
function($rootScope, prefix, cookie, notify) {
|
|
|
|
|
this.cookie = {
|
|
|
|
|
expiry: 30,
|
|
|
|
|
path: '/'
|
|
|
|
|
};
|
|
|
|
|
this.notify = {
|
|
|
|
|
setItem: true,
|
|
|
|
|
removeItem: false
|
|
|
|
|
};
|
|
|
|
|
this.setPrefix = function(prefix){
|
|
|
|
|
this.prefix = prefix;
|
|
|
|
|
};
|
|
|
|
|
this.setStorageCookie = function(exp, path){
|
|
|
|
|
this.cookie = {
|
|
|
|
|
expiry: exp,
|
|
|
|
|
path: path
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
this.setNotify = function(itemSet, itemRemove){
|
|
|
|
|
this.notify = {
|
|
|
|
|
setItem: itemSet,
|
|
|
|
|
removeItem: itemRemove
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
this.$get = ['$rootScope',function($rootScope){
|
|
|
|
|
var prefix = this.prefix;
|
|
|
|
|
// If there is a prefix set in the config lets use that with an appended period for readability
|
|
|
|
|
//var prefix = angularLocalStorage.constant;
|
|
|
|
|
if (prefix.substr(-1)!=='.') {
|
|
|
|
@ -28,7 +45,7 @@ angularLocalStorage.service('localStorageService', [
|
|
|
|
|
// Checks the browser to see if local storage is supported
|
|
|
|
|
var browserSupportsLocalStorage = function () {
|
|
|
|
|
try {
|
|
|
|
|
return ('localStorage' in window && window['localStorage'] !== null);
|
|
|
|
|
return ('localStorage' in window && window.localStorage !== null);
|
|
|
|
|
} catch (e) {
|
|
|
|
|
$rootScope.$broadcast('LocalStorageModule.notification.error',e.message);
|
|
|
|
|
return false;
|
|
|
|
@ -50,7 +67,7 @@ angularLocalStorage.service('localStorageService', [
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Let's convert undefined values to null to get the value consistent
|
|
|
|
|
if (typeof value == "undefined") {
|
|
|
|
|
if (typeof value === "undefined") {
|
|
|
|
|
value = null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -80,7 +97,7 @@ angularLocalStorage.service('localStorageService', [
|
|
|
|
|
var item = localStorage.getItem(prefix+key);
|
|
|
|
|
// angular.toJson will convert null to 'null', so a proper conversion is needed
|
|
|
|
|
// FIXME not a perfect solution, since a valid 'null' string can't be stored
|
|
|
|
|
if (!item || item === 'null') return null;
|
|
|
|
|
if (!item || item === 'null') {return null;}
|
|
|
|
|
|
|
|
|
|
if (item.charAt(0) === "{" || item.charAt(0) === "[") {
|
|
|
|
|
return angular.fromJson(item);
|
|
|
|
@ -179,7 +196,7 @@ angularLocalStorage.service('localStorageService', [
|
|
|
|
|
// Example use: localStorageService.cookie.add('library','angular');
|
|
|
|
|
var addToCookies = function (key, value) {
|
|
|
|
|
|
|
|
|
|
if (typeof value == "undefined") {
|
|
|
|
|
if (typeof value === "undefined") {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -221,7 +238,7 @@ angularLocalStorage.service('localStorageService', [
|
|
|
|
|
var cookies = document.cookie.split(';');
|
|
|
|
|
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) {
|
|
|
|
@ -241,7 +258,7 @@ angularLocalStorage.service('localStorageService', [
|
|
|
|
|
var cookies = document.cookie.split(';');
|
|
|
|
|
for(var i=0;i < cookies.length;i++) {
|
|
|
|
|
thisCookie = cookies[i];
|
|
|
|
|
while (thisCookie.charAt(0)==' ') {
|
|
|
|
|
while (thisCookie.charAt(0)===' ') {
|
|
|
|
|
thisCookie = thisCookie.substring(1,thisCookie.length);
|
|
|
|
|
}
|
|
|
|
|
key = thisCookie.substring(prefixLength,thisCookie.indexOf('='));
|
|
|
|
@ -265,6 +282,6 @@ angularLocalStorage.service('localStorageService', [
|
|
|
|
|
clearAll: clearAllFromCookies
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
}]);
|
|
|
|
|
}]
|
|
|
|
|
});
|
|
|
|
|
}).call(this);
|