forked from agrublev/angularLocalStorage
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathangularLocalStorage.js
More file actions
225 lines (202 loc) · 7.48 KB
/
Copy pathangularLocalStorage.js
File metadata and controls
225 lines (202 loc) · 7.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/*
* Angular.js localStorage module
* https://github.com/marshall007/angularLocalStorage
*/
(function (window, angular, undefined) {
'use strict';
var module = angular.module('angularLocalStorage', []);
module.constant('$storeConfig', {
prefix: null
});
module.factory('$store', [
'$parse', '$injector', '$window', '$log', '$storeConfig',
function ($parse, $injector, $window, $log, config) {
/**
* Global Vars
*/
var storage = (typeof $window.localStorage === 'undefined') ? undefined : $window.localStorage;
var supported = typeof storage !== 'undefined';
var $cookieStore;
if (!supported) {
try {
$cookieStore = $injector.get('$cookieStore');
} catch (e) {
$log.log('Local Storage not supported, make sure you have angular-cookies enabled.');
}
}
var watchers = {};
var privateMethods = {
/**
* Normalizes the key to include configured prefix
* @param key - the base accessor value
* @returns {string} - the normalized accessor value
*/
getKey: function (key) {
if (typeof(config.prefix) === 'string' && config.prefix.length > 0) {
return config.prefix + ':' + key;
}
return key;
},
/**
* 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 = angular.fromJson(res);
if (typeof val === 'undefined') {
val = res;
}
if (val === 'true') {
val = true;
}
if (val === 'false') {
val = false;
}
if ($window.parseFloat(val) === val && !angular.isObject(val)) {
val = $window.parseFloat(val);
}
} catch (e) {
val = res;
}
return val;
}
};
var publicMethods = {
/**
* Set - let's you set a new localStorage key pair set
* @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) {
key = privateMethods.getKey(key);
if (!supported) {
try {
$cookieStore.put(key, value);
return value;
} catch(e) {
$log.log('Local Storage not supported, make sure you have angular-cookies enabled.');
}
}
var saver = angular.toJson(value);
storage.setItem(key, saver);
return privateMethods.parseValue(saver);
},
/**
* Get - let's you get the value of any pair you've stored
* @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) {
key = privateMethods.getKey(key);
if (!supported) {
try {
return privateMethods.parseValue($.cookie(key));
} catch (e) {
return null;
}
}
var item = storage.getItem(key);
return privateMethods.parseValue(item);
},
/**
* Remove - let's you nuke a value from localStorage
* @param key - the accessor value
* @returns {boolean} - if everything went as planned
*/
remove: function (key) {
key = privateMethods.getKey(key);
if (!supported) {
try {
$cookieStore.remove(key);
return true;
} catch (e) {
return false;
}
}
storage.removeItem(key);
return true;
},
/**
* Bind - let's you directly bind a localStorage value to a $scope variable
* @param {Angular $scope} $scope - the current scope you want the variable available in
* @param {String} key - the name of the variable you are binding
* @param {Object} opts - (optional) custom options like default value or unique store name
* Here are the available options you can set:
* * defaultValue: the default value
* * storeName: add a custom store key value instead of using the scope variable name
* @returns {*} - returns whatever the stored value is
*/
bind: function ($scope, key, opts) {
var defaultOpts = {
defaultValue: '',
storeName: ''
};
// Backwards compatibility with old defaultValue string
if (angular.isString(opts)) {
opts = angular.extend({},defaultOpts,{defaultValue:opts});
} else {
// If no defined options we use defaults otherwise extend defaults
opts = (angular.isUndefined(opts)) ? defaultOpts : angular.extend(defaultOpts,opts);
}
// Set the storeName key for the localStorage entry
// use user defined in specified
var storeName = opts.storeName || key;
// If a value doesn't already exist store it as is
if (!publicMethods.get(storeName)) {
publicMethods.set(storeName, opts.defaultValue);
}
// If it does exist assign it to the $scope value
$parse(key).assign($scope, publicMethods.get(storeName));
// Unregister existing listeners just in case
if (watchers[storeName]) {
watchers[storeName]();
}
// Register a listener for changes on the $scope value
// to update the localStorage value
watchers[storeName] = $scope.$watch(key, function (val) {
if (angular.isDefined(val)) {
publicMethods.set(storeName, val);
}
}, true);
return publicMethods.get(storeName);
},
/**
* Unbind - let's you unbind a variable from localStorage, setting the local variable
* to null and optionally removing the value from localStorage
* @param $scope - the scope the variable was initially set in
* @param key - the name of the variable you are unbinding
* @param storeName - (optional) if you used a custom storeName you will have to specify it here as well
* @param remove - (optional) whether to remove the value from localStorage
*/
unbind: function($scope,key,storeName,remove) {
if (typeof (storeName) === 'boolean') {
remove = storeName;
storeName = key;
} else {
storeName = storeName || key;
}
if (watchers[storeName]) {
watchers[storeName]();
} else {
console.warn('Watcher for "' + storeName + '" was never registered.')
}
if (remove) {
publicMethods.remove(storeName);
}
$parse(key).assign($scope, null);
},
/**
* Clear All - let's you clear out ALL localStorage variables, use this carefully!
*/
clearAll: function() {
storage.clear();
}
};
return publicMethods;
}
]);
})(window, window.angular);