Skip to content

Commit

Permalink
fix(dist): build for v0.1.1
Browse files Browse the repository at this point in the history
  • Loading branch information
a8m committed Oct 6, 2014
1 parent 6dcc4f1 commit a8d48ff
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 32 deletions.
115 changes: 85 additions & 30 deletions dist/angular-local-storage.js
Original file line number Diff line number Diff line change
@@ -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() {
Expand Down Expand Up @@ -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;
Expand All @@ -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 {
Expand Down Expand Up @@ -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) {
Expand All @@ -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);
}

Expand All @@ -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;
Expand All @@ -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'});
}
Expand Down Expand Up @@ -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();
}

Expand Down Expand Up @@ -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()) {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand All @@ -382,6 +436,7 @@ angularLocalStorage.provider('localStorageService', function() {
clearAll: clearAllFromLocalStorage,
bind: bindToScope,
deriveKey: deriveQualifiedKey,
length: lengthOfLocalStorage,
cookie: {
set: addToCookies,
add: addToCookies, //DEPRECATED
Expand Down
4 changes: 2 additions & 2 deletions dist/angular-local-storage.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit a8d48ff

Please sign in to comment.