1
1
/**
2
2
* An Angular module that gives you access to the browsers local storage
3
- * @version v0.0.8 - 2014-08-15
3
+ * @version v0.1.1 - 2014-10-07
4
4
* @link https://github.com/grevory/angular-local-storage
5
5
* @author grevory <greg@gregpike.ca>
6
6
* @license MIT License, http://www.opensource.org/licenses/MIT
7
7
*/
8
8
( function ( window , angular , undefined ) {
9
+ /*jshint globalstrict:true*/
9
10
'use strict' ;
11
+
12
+ var isDefined = angular . isDefined ,
13
+ isUndefined = angular . isUndefined ,
14
+ isNumber = angular . isNumber ,
15
+ isObject = angular . isObject ,
16
+ isArray = angular . isArray ,
17
+ extend = angular . extend ,
18
+ toJson = angular . toJson ,
19
+ fromJson = angular . fromJson ;
20
+
21
+
22
+ // Test if string is only contains numbers
23
+ // e.g '1' => true, "'1'" => true
24
+ function isStringNumber ( num ) {
25
+ return / ^ - ? \d + \. ? \d * $ / . test ( num . replace ( / [ " ' ] / g, '' ) ) ;
26
+ }
27
+
10
28
var angularLocalStorage = angular . module ( 'LocalStorageModule' , [ ] ) ;
11
29
12
30
angularLocalStorage . provider ( 'localStorageService' , function ( ) {
@@ -68,9 +86,7 @@ angularLocalStorage.provider('localStorageService', function() {
68
86
} ;
69
87
} ;
70
88
71
-
72
-
73
- this . $get = [ '$rootScope' , '$window' , '$document' , function ( $rootScope , $window , $document ) {
89
+ this . $get = [ '$rootScope' , '$window' , '$document' , '$parse' , function ( $rootScope , $window , $document , $parse ) {
74
90
var self = this ;
75
91
var prefix = self . prefix ;
76
92
var cookie = self . cookie ;
@@ -91,7 +107,7 @@ angularLocalStorage.provider('localStorageService', function() {
91
107
}
92
108
var deriveQualifiedKey = function ( key ) {
93
109
return prefix + key ;
94
- }
110
+ } ;
95
111
// Checks the browser to see if local storage is supported
96
112
var browserSupportsLocalStorage = ( function ( ) {
97
113
try {
@@ -123,24 +139,28 @@ angularLocalStorage.provider('localStorageService', function() {
123
139
// If local storage is not available in the browser use cookies
124
140
// Example use: localStorageService.add('library','angular');
125
141
var addToLocalStorage = function ( key , value ) {
142
+ // Let's convert undefined values to null to get the value consistent
143
+ if ( isUndefined ( value ) ) {
144
+ value = null ;
145
+ } else if ( isObject ( value ) || isArray ( value ) || isNumber ( + value || value ) ) {
146
+ value = toJson ( value ) ;
147
+ }
126
148
127
149
// If this browser does not support local storage use cookies
128
150
if ( ! browserSupportsLocalStorage || self . storageType === 'cookie' ) {
129
- $rootScope . $broadcast ( 'LocalStorageModule.notification.warning' , 'LOCAL_STORAGE_NOT_SUPPORTED' ) ;
151
+ if ( ! browserSupportsLocalStorage ) {
152
+ $rootScope . $broadcast ( 'LocalStorageModule.notification.warning' , 'LOCAL_STORAGE_NOT_SUPPORTED' ) ;
153
+ }
154
+
130
155
if ( notify . setItem ) {
131
156
$rootScope . $broadcast ( 'LocalStorageModule.notification.setitem' , { key : key , newvalue : value , storageType : 'cookie' } ) ;
132
157
}
133
158
return addToCookies ( key , value ) ;
134
159
}
135
160
136
- // Let's convert undefined values to null to get the value consistent
137
- if ( typeof value === "undefined" ) {
138
- value = null ;
139
- }
140
-
141
161
try {
142
- if ( angular . isObject ( value ) || angular . isArray ( value ) ) {
143
- value = angular . toJson ( value ) ;
162
+ if ( isObject ( value ) || isArray ( value ) ) {
163
+ value = toJson ( value ) ;
144
164
}
145
165
if ( webStorage ) { webStorage . setItem ( deriveQualifiedKey ( key ) , value ) } ;
146
166
if ( notify . setItem ) {
@@ -158,7 +178,10 @@ angularLocalStorage.provider('localStorageService', function() {
158
178
var getFromLocalStorage = function ( key ) {
159
179
160
180
if ( ! browserSupportsLocalStorage || self . storageType === 'cookie' ) {
161
- $rootScope . $broadcast ( 'LocalStorageModule.notification.warning' , 'LOCAL_STORAGE_NOT_SUPPORTED' ) ;
181
+ if ( ! browserSupportsLocalStorage ) {
182
+ $rootScope . $broadcast ( 'LocalStorageModule.notification.warning' , 'LOCAL_STORAGE_NOT_SUPPORTED' ) ;
183
+ }
184
+
162
185
return getFromCookies ( key ) ;
163
186
}
164
187
@@ -169,8 +192,8 @@ angularLocalStorage.provider('localStorageService', function() {
169
192
return null ;
170
193
}
171
194
172
- if ( item . charAt ( 0 ) === "{" || item . charAt ( 0 ) === "[" ) {
173
- return angular . fromJson ( item ) ;
195
+ if ( item . charAt ( 0 ) === "{" || item . charAt ( 0 ) === "[" || isStringNumber ( item ) ) {
196
+ return fromJson ( item ) ;
174
197
}
175
198
176
199
return item ;
@@ -179,8 +202,11 @@ angularLocalStorage.provider('localStorageService', function() {
179
202
// Remove an item from local storage
180
203
// Example use: localStorageService.remove('library'); // removes the key/value pair of library='angular'
181
204
var removeFromLocalStorage = function ( key ) {
182
- if ( ! browserSupportsLocalStorage ) {
183
- $rootScope . $broadcast ( 'LocalStorageModule.notification.warning' , 'LOCAL_STORAGE_NOT_SUPPORTED' ) ;
205
+ if ( ! browserSupportsLocalStorage || self . storageType === 'cookie' ) {
206
+ if ( ! browserSupportsLocalStorage ) {
207
+ $rootScope . $broadcast ( 'LocalStorageModule.notification.warning' , 'LOCAL_STORAGE_NOT_SUPPORTED' ) ;
208
+ }
209
+
184
210
if ( notify . removeItem ) {
185
211
$rootScope . $broadcast ( 'LocalStorageModule.notification.removeitem' , { key : key , storageType : 'cookie' } ) ;
186
212
}
@@ -235,8 +261,11 @@ angularLocalStorage.provider('localStorageService', function() {
235
261
var tempPrefix = prefix . slice ( 0 , - 1 ) ;
236
262
var testRegex = new RegExp ( tempPrefix + '.' + regularExpression ) ;
237
263
238
- if ( ! browserSupportsLocalStorage ) {
239
- $rootScope . $broadcast ( 'LocalStorageModule.notification.warning' , 'LOCAL_STORAGE_NOT_SUPPORTED' ) ;
264
+ if ( ! browserSupportsLocalStorage || self . storageType === 'cookie' ) {
265
+ if ( ! browserSupportsLocalStorage ) {
266
+ $rootScope . $broadcast ( 'LocalStorageModule.notification.warning' , 'LOCAL_STORAGE_NOT_SUPPORTED' ) ;
267
+ }
268
+
240
269
return clearAllFromCookies ( ) ;
241
270
}
242
271
@@ -273,8 +302,10 @@ angularLocalStorage.provider('localStorageService', function() {
273
302
// Example use: localStorageService.cookie.add('library','angular');
274
303
var addToCookies = function ( key , value ) {
275
304
276
- if ( typeof value === "undefined" ) {
305
+ if ( isUndefined ( value ) ) {
277
306
return false ;
307
+ } else if ( isArray ( value ) || isObject ( value ) ) {
308
+ value = toJson ( value ) ;
278
309
}
279
310
280
311
if ( ! browserSupportsCookies ( ) ) {
@@ -325,7 +356,13 @@ angularLocalStorage.provider('localStorageService', function() {
325
356
thisCookie = thisCookie . substring ( 1 , thisCookie . length ) ;
326
357
}
327
358
if ( thisCookie . indexOf ( deriveQualifiedKey ( key ) + '=' ) === 0 ) {
328
- return decodeURIComponent ( thisCookie . substring ( prefix . length + key . length + 1 , thisCookie . length ) ) ;
359
+ var storedValues = decodeURIComponent ( thisCookie . substring ( prefix . length + key . length + 1 , thisCookie . length ) )
360
+ try {
361
+ var obj = JSON . parse ( storedValues ) ;
362
+ return fromJson ( obj )
363
+ } catch ( e ) {
364
+ return storedValues
365
+ }
329
366
}
330
367
}
331
368
return null ;
@@ -355,22 +392,39 @@ angularLocalStorage.provider('localStorageService', function() {
355
392
return storageType ;
356
393
} ;
357
394
358
- var bindToScope = function ( scope , key , def ) {
359
- var value = getFromLocalStorage ( key ) ;
395
+ var bindToScope = function ( scope , scopeKey , def , lsKey ) {
396
+ if ( ! lsKey ) {
397
+ lsKey = scopeKey ;
398
+ }
399
+
400
+ var value = getFromLocalStorage ( lsKey ) ;
360
401
361
- if ( value === null && angular . isDefined ( def ) ) {
402
+ if ( value === null && isDefined ( def ) ) {
362
403
value = def ;
363
- } else if ( angular . isObject ( value ) && angular . isObject ( def ) ) {
364
- value = angular . extend ( def , value ) ;
404
+ } else if ( isObject ( value ) && isObject ( def ) ) {
405
+ value = extend ( def , value ) ;
365
406
}
366
407
367
- scope [ key ] = value ;
408
+ $parse ( scopeKey ) . assign ( scope , value ) ;
368
409
369
- scope . $watchCollection ( key , function ( newVal ) {
370
- addToLocalStorage ( key , newVal ) ;
410
+ scope . $watchCollection ( scopeKey , function ( newVal ) {
411
+ addToLocalStorage ( lsKey , newVal ) ;
371
412
} ) ;
372
413
} ;
373
414
415
+ // Return localStorageService.length
416
+ // ignore keys that not owned
417
+ var lengthOfLocalStorage = function ( ) {
418
+ var count = 0 ;
419
+ var storage = $window [ storageType ] ;
420
+ for ( var i = 0 ; i < storage . length ; i ++ ) {
421
+ if ( storage . key ( i ) . indexOf ( prefix ) === 0 ) {
422
+ count ++ ;
423
+ }
424
+ }
425
+ return count ;
426
+ } ;
427
+
374
428
return {
375
429
isSupported : browserSupportsLocalStorage ,
376
430
getStorageType : getStorageType ,
@@ -382,6 +436,7 @@ angularLocalStorage.provider('localStorageService', function() {
382
436
clearAll : clearAllFromLocalStorage ,
383
437
bind : bindToScope ,
384
438
deriveKey : deriveQualifiedKey ,
439
+ length : lengthOfLocalStorage ,
385
440
cookie : {
386
441
set : addToCookies ,
387
442
add : addToCookies , //DEPRECATED
0 commit comments