@@ -13,6 +13,9 @@ angularLocalStorage.provider('localStorageService', function(){
13
13
14
14
this . prefix = 'ls' ;
15
15
16
+ // You could change web storage type localstorage or sessionStorage
17
+ this . storageType = 'localStorage' ;
18
+
16
19
// Cookie options (usually in case of fallback)
17
20
// expiry = Number of days before cookies expire // 0 = Does not expire
18
21
// path = The web path the cookie represents
@@ -32,6 +35,11 @@ angularLocalStorage.provider('localStorageService', function(){
32
35
this . prefix = prefix ;
33
36
} ;
34
37
38
+ // Setter for the storageType
39
+ this . setStorageType = function ( storageType ) {
40
+ this . storageType = storageType ;
41
+ } ;
42
+
35
43
// Setter for cookie config
36
44
this . setStorageCookie = function ( exp , path ) {
37
45
this . cookie = {
@@ -59,6 +67,8 @@ angularLocalStorage.provider('localStorageService', function(){
59
67
var prefix = this . prefix ;
60
68
var cookie = this . cookie ;
61
69
var notify = this . notify ;
70
+ var storageType = this . storageType ;
71
+ var webStorage = $window [ storageType ] ;
62
72
63
73
// If there is a prefix set in the config lets use that with an appended period for readability
64
74
if ( prefix . substr ( - 1 ) !== '.' ) {
@@ -68,7 +78,7 @@ angularLocalStorage.provider('localStorageService', function(){
68
78
// Checks the browser to see if local storage is supported
69
79
var browserSupportsLocalStorage = ( function ( ) {
70
80
try {
71
- var supported = ( 'localStorage' in $window && $window [ 'localStorage' ] !== null ) ;
81
+ var supported = ( storageType in $window && $window [ storageType ] !== null ) ;
72
82
73
83
// When Safari (OS X or iOS) is in private browsing mode, it appears as though localStorage
74
84
// is available, but trying to call .setItem throws an exception.
@@ -77,8 +87,8 @@ angularLocalStorage.provider('localStorageService', function(){
77
87
// that exceeded the quota."
78
88
var key = prefix + '__' + Math . round ( Math . random ( ) * 1e7 ) ;
79
89
if ( supported ) {
80
- localStorage . setItem ( key , '' ) ;
81
- localStorage . removeItem ( key ) ;
90
+ webStorage . setItem ( key , '' ) ;
91
+ webStorage . removeItem ( key ) ;
82
92
}
83
93
84
94
return true ;
@@ -111,9 +121,9 @@ angularLocalStorage.provider('localStorageService', function(){
111
121
if ( angular . isObject ( value ) || angular . isArray ( value ) ) {
112
122
value = angular . toJson ( value ) ;
113
123
}
114
- localStorage . setItem ( prefix + key , value ) ;
124
+ webStorage . setItem ( prefix + key , value ) ;
115
125
if ( notify . setItem ) {
116
- $rootScope . $broadcast ( 'LocalStorageModule.notification.setitem' , { key : key , newvalue : value , storageType : 'localStorage' } ) ;
126
+ $rootScope . $broadcast ( 'LocalStorageModule.notification.setitem' , { key : key , newvalue : value , storageType : this . storageType } ) ;
117
127
}
118
128
} catch ( e ) {
119
129
$rootScope . $broadcast ( 'LocalStorageModule.notification.error' , e . message ) ;
@@ -131,7 +141,7 @@ angularLocalStorage.provider('localStorageService', function(){
131
141
return getFromCookies ( key ) ;
132
142
}
133
143
134
- var item = localStorage . getItem ( prefix + key ) ;
144
+ var item = webStorage . getItem ( prefix + key ) ;
135
145
// angular.toJson will convert null to 'null', so a proper conversion is needed
136
146
// FIXME not a perfect solution, since a valid 'null' string can't be stored
137
147
if ( ! item || item === 'null' ) {
@@ -157,9 +167,9 @@ angularLocalStorage.provider('localStorageService', function(){
157
167
}
158
168
159
169
try {
160
- localStorage . removeItem ( prefix + key ) ;
170
+ webStorage . removeItem ( prefix + key ) ;
161
171
if ( notify . removeItem ) {
162
- $rootScope . $broadcast ( 'LocalStorageModule.notification.removeitem' , { key : key , storageType : 'localStorage' } ) ;
172
+ $rootScope . $broadcast ( 'LocalStorageModule.notification.removeitem' , { key : key , storageType : this . storageType } ) ;
163
173
}
164
174
} catch ( e ) {
165
175
$rootScope . $broadcast ( 'LocalStorageModule.notification.error' , e . message ) ;
@@ -179,7 +189,7 @@ angularLocalStorage.provider('localStorageService', function(){
179
189
180
190
var prefixLength = prefix . length ;
181
191
var keys = [ ] ;
182
- for ( var key in localStorage ) {
192
+ for ( var key in webStorage ) {
183
193
// Only return keys that are for this app
184
194
if ( key . substr ( 0 , prefixLength ) === prefix ) {
185
195
try {
@@ -211,7 +221,7 @@ angularLocalStorage.provider('localStorageService', function(){
211
221
212
222
var prefixLength = prefix . length ;
213
223
214
- for ( var key in localStorage ) {
224
+ for ( var key in webStorage ) {
215
225
// Only remove items that are for this app and match the regular expression
216
226
if ( testRegex . test ( key ) ) {
217
227
try {
0 commit comments