2014-07-18 13:23:25 +02:00
|
|
|
(function() {
|
2014-07-16 20:34:50 +02:00
|
|
|
'use strict';
|
2013-10-05 16:05:16 +02:00
|
|
|
|
|
|
|
var ls = angular.module('localStorage',[]);
|
2013-12-17 21:30:22 +01:00
|
|
|
|
2014-08-11 18:37:33 +02:00
|
|
|
ls.factory("$store", ["$parse", function($parse){
|
2013-10-05 16:05:16 +02:00
|
|
|
/**
|
|
|
|
* Global Vars
|
|
|
|
*/
|
|
|
|
var storage = (typeof window.localStorage === 'undefined') ? undefined : window.localStorage,
|
|
|
|
supported = !(typeof storage == 'undefined' || typeof window.JSON == 'undefined');
|
|
|
|
|
2014-12-22 21:15:54 +01:00
|
|
|
if (!supported) {
|
|
|
|
console.log('Warning: localStorage is not supported');
|
|
|
|
}
|
|
|
|
|
2013-10-05 16:05:16 +02:00
|
|
|
var privateMethods = {
|
|
|
|
/**
|
|
|
|
* Pass any type of a string from the localStorage to be parsed so it returns a usable version (like an Object)
|
|
|
|
* @param res - a string that will be parsed for type
|
|
|
|
* @returns {*} - whatever the real type of stored value was
|
|
|
|
*/
|
|
|
|
parseValue: function(res) {
|
|
|
|
var val;
|
|
|
|
try {
|
|
|
|
val = JSON.parse(res);
|
2014-02-08 14:20:33 +01:00
|
|
|
if (val === undefined){
|
2013-10-05 16:05:16 +02:00
|
|
|
val = res;
|
|
|
|
}
|
2014-02-08 14:20:33 +01:00
|
|
|
if (val === 'true'){
|
2013-10-05 16:05:16 +02:00
|
|
|
val = true;
|
|
|
|
}
|
2014-02-08 14:20:33 +01:00
|
|
|
if (val === 'false'){
|
2013-10-05 16:05:16 +02:00
|
|
|
val = false;
|
|
|
|
}
|
2014-12-22 22:17:15 +01:00
|
|
|
if (parseFloat(val) == val && !angular.isObject(val)) {
|
2013-10-05 16:05:16 +02:00
|
|
|
val = parseFloat(val);
|
|
|
|
}
|
|
|
|
} catch(e){
|
|
|
|
val = res;
|
|
|
|
}
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
var publicMethods = {
|
|
|
|
/**
|
2014-12-22 21:15:54 +01:00
|
|
|
* Set - lets you set a new localStorage key pair set
|
2013-10-05 16:05:16 +02:00
|
|
|
* @param key - a string that will be used as the accessor for the pair
|
|
|
|
* @param value - the value of the localStorage item
|
|
|
|
* @returns {*} - will return whatever it is you've stored in the local storage
|
|
|
|
*/
|
|
|
|
set: function(key,value){
|
|
|
|
if (!supported){
|
2014-12-22 21:15:54 +01:00
|
|
|
console.log('Local Storage not supported');
|
2013-10-05 16:05:16 +02:00
|
|
|
}
|
|
|
|
var saver = JSON.stringify(value);
|
2014-12-22 21:15:54 +01:00
|
|
|
storage.setItem(key, saver);
|
2013-10-05 16:05:16 +02:00
|
|
|
return privateMethods.parseValue(saver);
|
|
|
|
},
|
|
|
|
/**
|
2014-12-22 21:15:54 +01:00
|
|
|
* Get - lets you get the value of any pair you've stored
|
2013-10-05 16:05:16 +02:00
|
|
|
* @param key - the string that you set as accessor for the pair
|
|
|
|
* @returns {*} - Object,String,Float,Boolean depending on what you stored
|
|
|
|
*/
|
|
|
|
get: function(key){
|
|
|
|
if (!supported){
|
2014-12-22 21:15:54 +01:00
|
|
|
return null;
|
2013-10-05 16:05:16 +02:00
|
|
|
}
|
|
|
|
var item = storage.getItem(key);
|
|
|
|
return privateMethods.parseValue(item);
|
|
|
|
},
|
|
|
|
/**
|
2014-12-22 21:15:54 +01:00
|
|
|
* Remove - lets you nuke a value from localStorage
|
2013-10-05 16:05:16 +02:00
|
|
|
* @param key - the accessor value
|
|
|
|
* @returns {boolean} - if everything went as planned
|
|
|
|
*/
|
|
|
|
remove: function(key) {
|
|
|
|
if (!supported){
|
2014-12-22 21:15:54 +01:00
|
|
|
return false;
|
2013-10-05 16:05:16 +02:00
|
|
|
}
|
|
|
|
storage.removeItem(key);
|
|
|
|
return true;
|
|
|
|
},
|
2014-12-22 22:17:01 +01:00
|
|
|
/**
|
|
|
|
* Enumerate all keys
|
|
|
|
*/
|
|
|
|
enumerateKeys: function() {
|
|
|
|
var keys = [];
|
|
|
|
for (var i = 0, len = storage.length; i < len; ++i) {
|
|
|
|
keys.push(storage.key(i));
|
|
|
|
}
|
|
|
|
return keys;
|
|
|
|
},
|
2013-10-05 16:05:16 +02:00
|
|
|
/**
|
2014-12-22 21:15:54 +01:00
|
|
|
* Bind - lets you directly bind a localStorage value to a $scope variable
|
|
|
|
* @param $scope - the current scope you want the variable available in
|
|
|
|
* @param key - the name of the variable you are binding
|
|
|
|
* @param def - the default value (OPTIONAL)
|
|
|
|
* @returns {*} - returns whatever the stored value is
|
|
|
|
*/
|
|
|
|
bind: function ($scope, key, def) {
|
|
|
|
if (def === undefined) {
|
|
|
|
def = '';
|
2013-10-05 16:05:16 +02:00
|
|
|
}
|
2014-12-22 21:15:54 +01:00
|
|
|
if (publicMethods.get(key) === undefined || publicMethods.get(key) === null) {
|
|
|
|
publicMethods.set(key, def);
|
|
|
|
}
|
|
|
|
$parse(key).assign($scope, publicMethods.get(key));
|
|
|
|
$scope.$watch(key, function (val) {
|
|
|
|
publicMethods.set(key, val);
|
|
|
|
}, true);
|
|
|
|
return publicMethods.get(key);
|
|
|
|
}
|
2013-10-05 16:05:16 +02:00
|
|
|
};
|
|
|
|
return publicMethods;
|
2014-08-11 18:37:33 +02:00
|
|
|
}]);
|
2014-07-18 13:23:25 +02:00
|
|
|
})();
|