|
|
|
@ -1,19 +1,20 @@
|
|
|
|
|
// Configure Angular Local Storage
|
|
|
|
|
var settings = {
|
|
|
|
|
|
|
|
|
|
// You should set a prefix to avoid overwriting any local storage variables from the rest of your app
|
|
|
|
|
// prefix: 'youAppNameHere'
|
|
|
|
|
appPrefix: 'test'
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/* Start angularLocalStorage */
|
|
|
|
|
|
|
|
|
|
var angularLocalStorage = angular.module('LocalStorageModule', [])
|
|
|
|
|
var angularLocalStorage = angular.module('LocalStorageModule', []);
|
|
|
|
|
|
|
|
|
|
// Set the prefix based on the settings object above
|
|
|
|
|
angularLocalStorage.constant('prefix', settings.appPrefix || '');
|
|
|
|
|
// 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.constant('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.service('localStorageService', ['prefix', function(prefix) {
|
|
|
|
|
angularLocalStorage.service('localStorageService', [
|
|
|
|
|
'$rootScope',
|
|
|
|
|
'prefix',
|
|
|
|
|
'cookie',
|
|
|
|
|
function($rootScope, prefix, cookie) {
|
|
|
|
|
|
|
|
|
|
// If there is a prefix set in the config lets use that with an appended period for readability
|
|
|
|
|
//var prefix = angularLocalStorage.constant;
|
|
|
|
@ -26,6 +27,7 @@ angularLocalStorage.service('localStorageService', ['prefix', function(prefix) {
|
|
|
|
|
try {
|
|
|
|
|
return ('localStorage' in window && window['localStorage'] !== null);
|
|
|
|
|
} catch (e) {
|
|
|
|
|
$rootScope.$broadcast('LocalStorageModule.notification.error',e.Description);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
@ -37,7 +39,7 @@ angularLocalStorage.service('localStorageService', ['prefix', function(prefix) {
|
|
|
|
|
|
|
|
|
|
// If this browser does not support local storage use cookies
|
|
|
|
|
if (!browserSupportsLocalStorage()) {
|
|
|
|
|
console.log('Cannot add to local storage. Get from cookies'); // todo
|
|
|
|
|
$rootScope.$broadcast('LocalStorageModule.notification.warning','LOCAL_STORAGE_NOT_SUPPORTED');
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -47,7 +49,7 @@ angularLocalStorage.service('localStorageService', ['prefix', function(prefix) {
|
|
|
|
|
try {
|
|
|
|
|
localStorage.setItem(prefix+key, value);
|
|
|
|
|
} catch (e) {
|
|
|
|
|
console.error(e.Description);
|
|
|
|
|
$rootScope.$broadcast('LocalStorageModule.notification.error',e.Description);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
@ -57,28 +59,27 @@ angularLocalStorage.service('localStorageService', ['prefix', function(prefix) {
|
|
|
|
|
// Example use: localStorageService.get('library'); // returns 'angular'
|
|
|
|
|
var getFromLocalStorage = function (key) {
|
|
|
|
|
if (!browserSupportsLocalStorage()) {
|
|
|
|
|
console.log('Cannot get from local storage. Use cookies'); // todo
|
|
|
|
|
$rootScope.$broadcast('LocalStorageModule.notification.warning','LOCAL_STORAGE_NOT_SUPPORTED');
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var item = localStorage.getItem(prefix+key);
|
|
|
|
|
if (!item) return null;
|
|
|
|
|
return item;
|
|
|
|
|
//or localStorage[key];
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// 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()) {
|
|
|
|
|
console.log('Cannot remove item from local storage. Remove from cookies'); // todo
|
|
|
|
|
$rootScope.$broadcast('LocalStorageModule.notification.warning','LOCAL_STORAGE_NOT_SUPPORTED');
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
localStorage.removeItem(prefix+key);
|
|
|
|
|
} catch (e) {
|
|
|
|
|
console.error(e.Description);
|
|
|
|
|
$rootScope.$broadcast('LocalStorageModule.notification.error',e.Description);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
@ -90,7 +91,7 @@ angularLocalStorage.service('localStorageService', ['prefix', function(prefix) {
|
|
|
|
|
var clearAllFromLocalStorage = function () {
|
|
|
|
|
|
|
|
|
|
if (!browserSupportsLocalStorage()) {
|
|
|
|
|
console.log('Cannot remove all items from local storage. Remove all app cookies'); // todo
|
|
|
|
|
$rootScope.$broadcast('LocalStorageModule.notification.warning','LOCAL_STORAGE_NOT_SUPPORTED');
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -102,7 +103,7 @@ angularLocalStorage.service('localStorageService', ['prefix', function(prefix) {
|
|
|
|
|
try {
|
|
|
|
|
removeFromLocalStorage(key.substr(prefixLength));
|
|
|
|
|
} catch (e) {
|
|
|
|
|
console.error(e.Description);
|
|
|
|
|
$rootScope.$broadcast('LocalStorageModule.notification.error',e.Description);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -110,12 +111,100 @@ angularLocalStorage.service('localStorageService', ['prefix', function(prefix) {
|
|
|
|
|
return true;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Checks the browser to see if cookies are supported
|
|
|
|
|
var browserSupportsCookies = function() {
|
|
|
|
|
try {
|
|
|
|
|
return navigator.cookieEnabled ||
|
|
|
|
|
("cookie" in document && (document.cookie.length > 0 ||
|
|
|
|
|
(document.cookie = "test").indexOf.call(document.cookie, "test") > -1));
|
|
|
|
|
} catch (e) {
|
|
|
|
|
$rootScope.$broadcast('LocalStorageModule.notification.error',e.Description);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Directly adds a value to cookies
|
|
|
|
|
// Typically used as a fallback is local storage is not available in the browser
|
|
|
|
|
// Example use: localStorageService.cookie.add('library','angular');
|
|
|
|
|
var addToCookies = function (key, value) {
|
|
|
|
|
|
|
|
|
|
if (typeof value == "undefined") return false;
|
|
|
|
|
|
|
|
|
|
if (!browserSupportsCookies()) {
|
|
|
|
|
$rootScope.$broadcast('LocalStorageModule.notification.error','COOKIES_NOT_SUPPORTED');
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
var expiry = '', expiryDate = new Date();
|
|
|
|
|
if (value === null) {
|
|
|
|
|
cookie.expiry = -1;
|
|
|
|
|
value = '';
|
|
|
|
|
}
|
|
|
|
|
if (cookie.expiry !== 0) {
|
|
|
|
|
expiryDate.setTime(expiryDate.getTime() + (cookie.expiry*24*60*60*1000));
|
|
|
|
|
expiry = "; expires="+expiryDate.toGMTString();
|
|
|
|
|
}
|
|
|
|
|
document.cookie = prefix + key + "=" + encodeURIComponent(value) + expiry + "; path="+cookie.path;
|
|
|
|
|
} catch (e) {
|
|
|
|
|
$rootScope.$broadcast('LocalStorageModule.notification.error',e.Description);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Directly get a value from a cookie
|
|
|
|
|
// Example use: localStorageService.cookie.get('library'); // returns 'angular'
|
|
|
|
|
var getFromCookies = function (key) {
|
|
|
|
|
if (!browserSupportsCookies()) {
|
|
|
|
|
$rootScope.$broadcast('LocalStorageModule.notification.error','COOKIES_NOT_SUPPORTED');
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var cookies = document.cookie.split(';');
|
|
|
|
|
for(var i=0;i < cookies.length;i++) {
|
|
|
|
|
var thisCookie = cookies[i];
|
|
|
|
|
while (thisCookie.charAt(0)==' ') {
|
|
|
|
|
thisCookie = thisCookie.substring(1,thisCookie.length);
|
|
|
|
|
}
|
|
|
|
|
if (thisCookie.indexOf(prefix+key+'=') == 0) {
|
|
|
|
|
return decodeURIComponent(thisCookie.substring(prefix.length+key.length+1,thisCookie.length));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var removeFromCookies = function (key) {
|
|
|
|
|
addToCookies(key,null);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var clearAllFromCookies = function () {
|
|
|
|
|
var thisCookie = null, thisKey = null;
|
|
|
|
|
var prefixLength = prefix.length;
|
|
|
|
|
var cookies = document.cookie.split(';');
|
|
|
|
|
for(var i=0;i < cookies.length;i++) {
|
|
|
|
|
thisCookie = cookies[i];
|
|
|
|
|
while (thisCookie.charAt(0)==' ') {
|
|
|
|
|
thisCookie = thisCookie.substring(1,thisCookie.length);
|
|
|
|
|
}
|
|
|
|
|
key = thisCookie.substring(prefixLength,thisCookie.indexOf('='));
|
|
|
|
|
removeFromCookies(key);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
isSupported: browserSupportsLocalStorage,
|
|
|
|
|
add: addToLocalStorage,
|
|
|
|
|
get: getFromLocalStorage,
|
|
|
|
|
remove: removeFromLocalStorage,
|
|
|
|
|
clearAll: clearAllFromLocalStorage
|
|
|
|
|
clearAll: clearAllFromLocalStorage,
|
|
|
|
|
cookie: {
|
|
|
|
|
add: addToCookies,
|
|
|
|
|
get: getFromCookies,
|
|
|
|
|
remove: removeFromCookies,
|
|
|
|
|
clearAll: clearAllFromCookies
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
}]);
|
|
|
|
|