MobileSafari private mode fix
This commit is contained in:
parent
84018d4bec
commit
fb14a167c0
1 changed files with 45 additions and 0 deletions
|
@ -3,6 +3,42 @@
|
|||
|
||||
var ls = angular.module('localStorage',[]);
|
||||
|
||||
function StoragePolyfil() {
|
||||
this.storage = Object.create(null);
|
||||
this.keyIndex = [];
|
||||
Object.defineProperty(this, "length", {
|
||||
enumerable: true,
|
||||
get: function() {
|
||||
return this.keyIndex.length;
|
||||
}
|
||||
});
|
||||
};
|
||||
StoragePolyfil.prototype.key = function(idx) {
|
||||
return this.keyIndex[idx];
|
||||
};
|
||||
StoragePolyfil.prototype.getItem = function(key) {
|
||||
return (key in this.storage) ? this.storage[key] : null;
|
||||
};
|
||||
StoragePolyfil.prototype.setItem = function(key, value) {
|
||||
if (!(key in this.storage)) {
|
||||
this.keyIndex.push(key);
|
||||
}
|
||||
this.storage[key] = value;
|
||||
};
|
||||
StoragePolyfil.prototype.clear = function() {
|
||||
this.storage = Object.create(null);
|
||||
this.keyIndex = [];
|
||||
};
|
||||
StoragePolyfil.prototype.removeItem = function(key) {
|
||||
if (!(key in storage)) {
|
||||
return;
|
||||
}
|
||||
var at = this.keyIndex.indexOf(key);
|
||||
this.keyIndex.splice(at, 1);
|
||||
delete this.storage[key];
|
||||
};
|
||||
|
||||
|
||||
ls.factory("$store", ["$parse", function($parse){
|
||||
/**
|
||||
* Global Vars
|
||||
|
@ -10,6 +46,15 @@ ls.factory("$store", ["$parse", function($parse){
|
|||
var storage = (typeof window.localStorage === 'undefined') ? undefined : window.localStorage,
|
||||
supported = !(typeof storage == 'undefined' || typeof window.JSON == 'undefined');
|
||||
|
||||
try {
|
||||
var storageTestKey = "eaf23ffe-6a8f-40a7-892b-4baf22d3ec75";
|
||||
storage.setItem(storageTestKey, 1);
|
||||
storage.removeItem(storageTestKey);
|
||||
} catch (e) {
|
||||
console.log('Warning: MobileSafari private mode detected. Switching to in-memory storage.');
|
||||
storage = new StoragePolyfil();
|
||||
}
|
||||
|
||||
if (!supported) {
|
||||
console.log('Warning: localStorage is not supported');
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue