Fixed JS error from merge and changed spacing

revert-117-master
Gregory Pike 11 years ago
parent 3dee44cac3
commit 91271179e7

@ -34,11 +34,11 @@ angularLocalStorage.provider('localStorageService', function(){
};
};
this.$get = ['$rootScope',function($rootScope){
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
if (prefix.substr(-1)!=='.') {
if (prefix.substr(-1) !== '.') {
prefix = !!prefix ? prefix + '.' : '';
}
@ -60,14 +60,19 @@ angularLocalStorage.provider('localStorageService', function(){
return true;
} catch (e) {
$rootScope.$broadcast('LocalStorageModule.notification.error',e.message);
$rootScope.$broadcast('LocalStorageModule.notification.error', e.message);
return false;
}
};
// Directly adds a value to local storage
// If local storage is not available in the browser use cookies
// Example use: localStorageService.add('library','angular');
var addToLocalStorage = function (key, value) {
// If this browser does not support local storage use cookies
if (!browserSupportsLocalStorage()) {
$rootScope.$broadcast('LocalStorageModule.notification.warning','LOCAL_STORAGE_NOT_SUPPORTED');
$rootScope.$broadcast('LocalStorageModule.notification.warning', 'LOCAL_STORAGE_NOT_SUPPORTED');
if (notify.setItem) {
$rootScope.$broadcast('LocalStorageModule.notification.setitem', {key: key, newvalue: value, storageType: 'cookie'});
}
@ -83,12 +88,12 @@ angularLocalStorage.provider('localStorageService', function(){
if (angular.isObject(value) || angular.isArray(value)) {
value = angular.toJson(value);
}
localStorage.setItem(prefix+key, value);
localStorage.setItem(prefix + key, value);
if (notify.setItem) {
$rootScope.$broadcast('LocalStorageModule.notification.setitem', {key: key, newvalue: value, storageType: 'localStorage'});
}
} catch (e) {
$rootScope.$broadcast('LocalStorageModule.notification.error',e.message);
$rootScope.$broadcast('LocalStorageModule.notification.error', e.message);
return addToCookies(key, value);
}
return true;
@ -97,19 +102,23 @@ angularLocalStorage.provider('localStorageService', function(){
// Directly get a value from local storage
// Example use: localStorageService.get('library'); // returns 'angular'
var getFromLocalStorage = function (key) {
if (!browserSupportsLocalStorage()) {
$rootScope.$broadcast('LocalStorageModule.notification.warning','LOCAL_STORAGE_NOT_SUPPORTED');
return getFromCookies(key);
}
var item = localStorage.getItem(prefix+key);
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);
}
return item;
};
@ -117,7 +126,7 @@ angularLocalStorage.provider('localStorageService', function(){
// 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');
$rootScope.$broadcast('LocalStorageModule.notification.warning', 'LOCAL_STORAGE_NOT_SUPPORTED');
if (notify.removeItem) {
$rootScope.$broadcast('LocalStorageModule.notification.removeitem', {key: key, storageType: 'cookie'});
}
@ -130,7 +139,7 @@ angularLocalStorage.provider('localStorageService', function(){
$rootScope.$broadcast('LocalStorageModule.notification.removeitem', {key: key, storageType: 'localStorage'});
}
} catch (e) {
$rootScope.$broadcast('LocalStorageModule.notification.error',e.message);
$rootScope.$broadcast('LocalStorageModule.notification.error', e.message);
return removeFromCookies(key);
}
return true;
@ -141,7 +150,7 @@ angularLocalStorage.provider('localStorageService', function(){
var getKeysForLocalStorage = function () {
if (!browserSupportsLocalStorage()) {
$rootScope.$broadcast('LocalStorageModule.notification.warning','LOCAL_STORAGE_NOT_SUPPORTED');
$rootScope.$broadcast('LocalStorageModule.notification.warning', 'LOCAL_STORAGE_NOT_SUPPORTED');
return false;
}
@ -153,7 +162,7 @@ angularLocalStorage.provider('localStorageService', function(){
try {
keys.push(key.substr(prefixLength));
} catch (e) {
$rootScope.$broadcast('LocalStorageModule.notification.error',e.Description);
$rootScope.$broadcast('LocalStorageModule.notification.error', e.Description);
return [];
}
}
@ -167,7 +176,7 @@ angularLocalStorage.provider('localStorageService', function(){
var clearAllFromLocalStorage = function () {
if (!browserSupportsLocalStorage()) {
$rootScope.$broadcast('LocalStorageModule.notification.warning','LOCAL_STORAGE_NOT_SUPPORTED');
$rootScope.$broadcast('LocalStorageModule.notification.warning', 'LOCAL_STORAGE_NOT_SUPPORTED');
return clearAllFromCookies();
}
@ -179,7 +188,7 @@ angularLocalStorage.provider('localStorageService', function(){
try {
removeFromLocalStorage(key.substr(prefixLength));
} catch (e) {
$rootScope.$broadcast('LocalStorageModule.notification.error',e.message);
$rootScope.$broadcast('LocalStorageModule.notification.error', e.message);
return clearAllFromCookies();
}
}
@ -194,7 +203,7 @@ angularLocalStorage.provider('localStorageService', function(){
("cookie" in document && (document.cookie.length > 0 ||
(document.cookie = "test").indexOf.call(document.cookie, "test") > -1));
} catch (e) {
$rootScope.$broadcast('LocalStorageModule.notification.error',e.message);
$rootScope.$broadcast('LocalStorageModule.notification.error', e.message);
return false;
}
};
@ -209,21 +218,22 @@ angularLocalStorage.provider('localStorageService', function(){
}
if (!browserSupportsCookies()) {
$rootScope.$broadcast('LocalStorageModule.notification.error','COOKIES_NOT_SUPPORTED');
$rootScope.$broadcast('LocalStorageModule.notification.error', 'COOKIES_NOT_SUPPORTED');
return false;
}
try {
var expiry = '', expiryDate = new Date();
var expiry = '',
expiryDate = new Date();
if (value === null) {
// Mark that the cookie has expired one day ago
expiryDate.setTime(expiryDate.getTime() + (-1 * 24*60*60*1000));
expiry = "; expires="+expiryDate.toGMTString();
expiryDate.setTime(expiryDate.getTime() + (-1 * 24 * 60 * 60 * 1000));
expiry = "; expires=" + expiryDate.toGMTString();
value = '';
} else if (cookie.expiry !== 0) {
expiryDate.setTime(expiryDate.getTime() + (cookie.expiry*24*60*60*1000));
expiry = "; expires="+expiryDate.toGMTString();
expiryDate.setTime(expiryDate.getTime() + (cookie.expiry * 24 * 60 * 60 * 1000));
expiry = "; expires=" + expiryDate.toGMTString();
}
if (!!key) {
document.cookie = prefix + key + "=" + encodeURIComponent(value) + expiry + "; path="+cookie.path;
@ -239,7 +249,7 @@ angularLocalStorage.provider('localStorageService', function(){
// Example use: localStorageService.cookie.get('library'); // returns 'angular'
var getFromCookies = function (key) {
if (!browserSupportsCookies()) {
$rootScope.$broadcast('LocalStorageModule.notification.error','COOKIES_NOT_SUPPORTED');
$rootScope.$broadcast('LocalStorageModule.notification.error', 'COOKIES_NOT_SUPPORTED');
return false;
}
@ -249,8 +259,8 @@ angularLocalStorage.provider('localStorageService', function(){
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));
if (thisCookie.indexOf(prefix + key + '=') === 0) {
return decodeURIComponent(thisCookie.substring(prefix.length + key.length + 1, thisCookie.length));
}
}
return null;
@ -264,12 +274,14 @@ angularLocalStorage.provider('localStorageService', function(){
var thisCookie = null, thisKey = null;
var prefixLength = prefix.length;
var cookies = document.cookie.split(';');
for(var i=0;i < cookies.length;i++) {
for(var i = 0; i < cookies.length; i++) {
thisCookie = cookies[i];
while (thisCookie.charAt(0)===' ') {
thisCookie = thisCookie.substring(1,thisCookie.length);
while (thisCookie.charAt(0) === ' ') {
thisCookie = thisCookie.substring(1, thisCookie.length);
}
key = thisCookie.substring(prefixLength,thisCookie.indexOf('='));
key = thisCookie.substring(prefixLength, thisCookie.indexOf('='));
removeFromCookies(key);
}
};
@ -291,5 +303,5 @@ angularLocalStorage.provider('localStorageService', function(){
}
};
}]
});
});
}).call(this);