Simplify storage checking/access

git-svn-id: http://svg-edit.googlecode.com/svn/trunk@2711 eee81c28-f429-11dd-99c0-75d572ba1ddd
This commit is contained in:
Brett Zamir
2014-02-19 04:36:49 +00:00
parent 9129d652e4
commit 48f6dd42ca
2 changed files with 25 additions and 26 deletions

View File

@@ -43,7 +43,8 @@ svgEditor.addExtension('storage', function() {
// or adding of new storage takes place regardless of settings, set // or adding of new storage takes place regardless of settings, set
// the "noStorageOnLoad" config setting to true in config.js. // the "noStorageOnLoad" config setting to true in config.js.
noStorageOnLoad = svgEditor.curConfig.noStorageOnLoad, noStorageOnLoad = svgEditor.curConfig.noStorageOnLoad,
forceStorage = svgEditor.curConfig.forceStorage; forceStorage = svgEditor.curConfig.forceStorage,
storage = svgEditor.storage;
function replaceStoragePrompt (val) { function replaceStoragePrompt (val) {
val = val ? 'storagePrompt=' + val : ''; val = val ? 'storagePrompt=' + val : '';
@@ -57,13 +58,13 @@ svgEditor.addExtension('storage', function() {
} }
} }
function setSVGContentStorage (val) { function setSVGContentStorage (val) {
if ('localStorage' in window) { if (storage) {
var name = 'svgedit-' + svgEditor.curConfig.canvasName; var name = 'svgedit-' + svgEditor.curConfig.canvasName;
if (!val) { if (!val) {
window.localStorage.removeItem(name); storage.removeItem(name);
} }
else { else {
window.localStorage.setItem(name, val); storage.setItem(name, val);
} }
} }
} }
@@ -78,12 +79,12 @@ svgEditor.addExtension('storage', function() {
function emptyStorage() { function emptyStorage() {
setSVGContentStorage(''); setSVGContentStorage('');
var name, hasStorage = 'localStorage' in window; var name;
for (name in svgEditor.curPrefs) { for (name in svgEditor.curPrefs) {
if (svgEditor.curPrefs.hasOwnProperty(name)) { if (svgEditor.curPrefs.hasOwnProperty(name)) {
name = 'svg-edit-' + name; name = 'svg-edit-' + name;
if (hasStorage) { if (storage) {
window.localStorage.removeItem(name); storage.removeItem(name);
} }
expireCookie(name); expireCookie(name);
} }
@@ -115,10 +116,10 @@ svgEditor.addExtension('storage', function() {
// svgEditor.showSaveWarning = false; // svgEditor.showSaveWarning = false;
var curPrefs = svgEditor.curPrefs; var curPrefs = svgEditor.curPrefs;
for (key in curPrefs) { for (key in curPrefs) {
if (curPrefs.hasOwnProperty(key)) { // It's our own config, so we don't need to iterate up the prototype chain if (curPrefs.hasOwnProperty(key)) { // It's our own config, so we don't need to iterate up the prototype chain
var storage = svgEditor.storage, var val = curPrefs[key],
val = curPrefs[key],
store = (val != undefined); store = (val != undefined);
key = 'svg-edit-' + key; key = 'svg-edit-' + key;
if (!store) { if (!store) {
@@ -195,7 +196,7 @@ svgEditor.addExtension('storage', function() {
)) { )) {
var options = []; var options = [];
if ('localStorage' in window) { if (storage) {
options.unshift( options.unshift(
{value: 'prefsAndContent', text: uiStrings.confirmSetStorage.storagePrefsAndContent}, {value: 'prefsAndContent', text: uiStrings.confirmSetStorage.storagePrefsAndContent},
{value: 'prefsOnly', text: uiStrings.confirmSetStorage.storagePrefsOnly}, {value: 'prefsOnly', text: uiStrings.confirmSetStorage.storagePrefsOnly},

View File

@@ -237,34 +237,23 @@ TO-DOS
} }
// LOAD CONTENT // LOAD CONTENT
if ('localStorage' in window && // Cookies do not have enough available memory to hold large documents if (editor.storage && // Cookies do not have enough available memory to hold large documents
(curConfig.forceStorage || (!curConfig.noStorageOnLoad && document.cookie.match(/(?:^|;\s*)store=prefsAndContent/))) (curConfig.forceStorage || (!curConfig.noStorageOnLoad && document.cookie.match(/(?:^|;\s*)store=prefsAndContent/)))
) { ) {
var name = 'svgedit-' + curConfig.canvasName; var name = 'svgedit-' + curConfig.canvasName;
var cached = window.localStorage.getItem(name); var cached = editor.storage.getItem(name);
if (cached) { if (cached) {
editor.loadFromString(cached); editor.loadFromString(cached);
} }
} }
// LOAD PREFS // LOAD PREFS
var key, storage = false; var key;
// var host = location.hostname,
// onWeb = host && host.indexOf('.') >= 0;
// Some FF versions throw security errors here
try {
if (window.localStorage) { // && onWeb removed so Webkit works locally
storage = localStorage;
}
} catch(err) {}
editor.storage = storage;
for (key in defaultPrefs) { for (key in defaultPrefs) {
if (defaultPrefs.hasOwnProperty(key)) { // It's our own config, so we don't need to iterate up the prototype chain if (defaultPrefs.hasOwnProperty(key)) { // It's our own config, so we don't need to iterate up the prototype chain
var storeKey = 'svg-edit-' + key; var storeKey = 'svg-edit-' + key;
if (storage) { if (editor.storage) {
var val = storage.getItem(storeKey); var val = editor.storage.getItem(storeKey);
if (val) { if (val) {
defaultPrefs[key] = String(val); // Convert to string for FF (.value fails in Webkit) defaultPrefs[key] = String(val); // Convert to string for FF (.value fails in Webkit)
} }
@@ -409,6 +398,15 @@ TO-DOS
}; };
editor.init = function () { editor.init = function () {
// var host = location.hostname,
// onWeb = host && host.indexOf('.') >= 0;
// Some FF versions throw security errors here when directly accessing
try {
if ('localStorage' in window) { // && onWeb removed so Webkit works locally
editor.storage = localStorage;
}
} catch(err) {}
// Todo: Avoid var-defined functions and group functions together, etc. where possible // Todo: Avoid var-defined functions and group functions together, etc. where possible
var good_langs = []; var good_langs = [];
$('#lang_select option').each(function() { $('#lang_select option').each(function() {