Merge pull request #971 from Lucius-Q-User/master

MobileSafari private mode fix
This commit is contained in:
Lorenz Hübschle-Schneider 2017-11-04 14:29:48 +01:00 committed by GitHub
commit f47043fe17
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -3,6 +3,42 @@
var ls = angular.module('localStorage',[]); 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){ ls.factory("$store", ["$parse", function($parse){
/** /**
* Global Vars * Global Vars
@ -10,6 +46,15 @@ ls.factory("$store", ["$parse", function($parse){
var storage = (typeof window.localStorage === 'undefined') ? undefined : window.localStorage, var storage = (typeof window.localStorage === 'undefined') ? undefined : window.localStorage,
supported = !(typeof storage == 'undefined' || typeof window.JSON == 'undefined'); 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) { if (!supported) {
console.log('Warning: localStorage is not supported'); console.log('Warning: localStorage is not supported');
} }