@@ -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
@@ -54,6 +57,8 @@ angularLocalStorage.provider('localStorageService', function(){
54
57
var prefix = this . prefix ;
55
58
var cookie = this . cookie ;
56
59
var notify = this . notify ;
60
+ var storageType = this . storageType ;
61
+ var webStorage = $window [ storageType ] ;
57
62
58
63
// If there is a prefix set in the config lets use that with an appended period for readability
59
64
if ( prefix . substr ( - 1 ) !== '.' ) {
@@ -63,7 +68,7 @@ angularLocalStorage.provider('localStorageService', function(){
63
68
// Checks the browser to see if local storage is supported
64
69
var browserSupportsLocalStorage = ( function ( ) {
65
70
try {
66
- var supported = ( 'localStorage' in $window && $window [ 'localStorage' ] !== null ) ;
71
+ var supported = ( storageType in $window && $window [ storageType ] !== null ) ;
67
72
68
73
// When Safari (OS X or iOS) is in private browsing mode, it appears as though localStorage
69
74
// is available, but trying to call .setItem throws an exception.
@@ -72,8 +77,8 @@ angularLocalStorage.provider('localStorageService', function(){
72
77
// that exceeded the quota."
73
78
var key = prefix + '__' + Math . round ( Math . random ( ) * 1e7 ) ;
74
79
if ( supported ) {
75
- localStorage . setItem ( key , '' ) ;
76
- localStorage . removeItem ( key ) ;
80
+ webStorage . setItem ( key , '' ) ;
81
+ webStorage . removeItem ( key ) ;
77
82
}
78
83
79
84
return true ;
@@ -106,9 +111,9 @@ angularLocalStorage.provider('localStorageService', function(){
106
111
if ( angular . isObject ( value ) || angular . isArray ( value ) ) {
107
112
value = angular . toJson ( value ) ;
108
113
}
109
- localStorage . setItem ( prefix + key , value ) ;
114
+ webStorage . setItem ( prefix + key , value ) ;
110
115
if ( notify . setItem ) {
111
- $rootScope . $broadcast ( 'LocalStorageModule.notification.setitem' , { key : key , newvalue : value , storageType : 'localStorage' } ) ;
116
+ $rootScope . $broadcast ( 'LocalStorageModule.notification.setitem' , { key : key , newvalue : value , storageType : this . storageType } ) ;
112
117
}
113
118
} catch ( e ) {
114
119
$rootScope . $broadcast ( 'LocalStorageModule.notification.error' , e . message ) ;
@@ -126,7 +131,7 @@ angularLocalStorage.provider('localStorageService', function(){
126
131
return getFromCookies ( key ) ;
127
132
}
128
133
129
- var item = localStorage . getItem ( prefix + key ) ;
134
+ var item = webStorage . getItem ( prefix + key ) ;
130
135
// angular.toJson will convert null to 'null', so a proper conversion is needed
131
136
// FIXME not a perfect solution, since a valid 'null' string can't be stored
132
137
if ( ! item || item === 'null' ) {
@@ -152,9 +157,9 @@ angularLocalStorage.provider('localStorageService', function(){
152
157
}
153
158
154
159
try {
155
- localStorage . removeItem ( prefix + key ) ;
160
+ webStorage . removeItem ( prefix + key ) ;
156
161
if ( notify . removeItem ) {
157
- $rootScope . $broadcast ( 'LocalStorageModule.notification.removeitem' , { key : key , storageType : 'localStorage' } ) ;
162
+ $rootScope . $broadcast ( 'LocalStorageModule.notification.removeitem' , { key : key , storageType : this . storageType } ) ;
158
163
}
159
164
} catch ( e ) {
160
165
$rootScope . $broadcast ( 'LocalStorageModule.notification.error' , e . message ) ;
@@ -174,7 +179,7 @@ angularLocalStorage.provider('localStorageService', function(){
174
179
175
180
var prefixLength = prefix . length ;
176
181
var keys = [ ] ;
177
- for ( var key in localStorage ) {
182
+ for ( var key in webStorage ) {
178
183
// Only return keys that are for this app
179
184
if ( key . substr ( 0 , prefixLength ) === prefix ) {
180
185
try {
@@ -206,7 +211,7 @@ angularLocalStorage.provider('localStorageService', function(){
206
211
207
212
var prefixLength = prefix . length ;
208
213
209
- for ( var key in localStorage ) {
214
+ for ( var key in webStorage ) {
210
215
// Only remove items that are for this app and match the regular expression
211
216
if ( testRegex . test ( key ) ) {
212
217
try {
0 commit comments