Browse Source

Refactor local storage; have configurable prefix

dev/git-series/gccdum
Matt Godbolt 6 years ago
parent
commit
9d601e9256
  1. 3
      app.js
  2. 1
      etc/config/compiler-explorer.beta.properties
  3. 30
      static/loadSave.js
  4. 65
      static/local.js
  5. 29
      static/main.js

3
app.js

@ -175,7 +175,8 @@ function ClientOptionsHandler(fileSources) {
sources: sources,
raven: gccProps('ravenUrl', ''),
release: gitReleaseName,
environment: env
environment: env,
localStoragePrefix: gccProps('localStoragePrefix')
};
text = JSON.stringify(options);
};

1
etc/config/compiler-explorer.beta.properties

@ -1 +1,2 @@
languages=C++=//gcc.godbolt.org/beta:D=//d.godbolt.org/beta:Rust=//rust.godbolt.org/beta:Go=//go.godbolt.org/beta
localStoragePrefix=beta-

30
static/loadSave.js

@ -27,25 +27,17 @@ define(function (require) {
"use strict";
var $ = require('jquery');
var _ = require('underscore');
var Alert = require('alert');
var Alert = require('./alert');
var local = require('./local');
function safeGetLocalFiles() {
try {
var files = window.localStorage.getItem('files');
if (files) return JSON.parse(files);
}
catch (e) {
}
return {};
function getLocalFiles() {
return JSON.parse(local.get('files', "{}"));
}
function safeSetLocalFile(name, file) {
try {
var files = safeGetLocalFiles();
files[name] = file;
window.localStorage.setItem('files', JSON.stringify(files));
} catch (e) {
}
function setLocalFile(name, file) {
var files = getLocalFiles();
files[name] = file;
local.set('files', JSON.stringify(files));
}
function LoadSave() {
@ -77,7 +69,7 @@ define(function (require) {
LoadSave.prototype.populateLocalStorage = function () {
this.populate(
this.modal.find('.local-storage'),
_.map(safeGetLocalFiles(), _.bind(function (data, name) {
_.map(getLocalFiles(), _.bind(function (data, name) {
return {
name: name,
load: _.bind(function () {
@ -127,9 +119,9 @@ define(function (require) {
return;
}
var done = _.bind(function () {
safeSetLocalFile(name, this.editorText);
setLocalFile(name, this.editorText);
}, this);
if (safeGetLocalFiles()[name] !== undefined) {
if (getLocalFiles()[name] !== undefined) {
this.modal.modal('hide');
this.alert.ask(
"Replace current?",

65
static/local.js

@ -0,0 +1,65 @@
// Copyright (c) 2012-2017, Matt Godbolt
//
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// * Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
define(function (require) {
"use strict";
var options = require('./options');
var prefix = options.localStoragePrefix || '';
function get(key, ifNotPresent) {
var result;
try {
result = window.localStorage.getItem(prefix + key);
} catch (e) {
// Swallow up any security exceptions...
}
if (result === null) return ifNotPresent;
return result;
}
function set(key, value) {
try {
window.localStorage.setItem(prefix + key, value);
return true;
} catch (e) {
// Swallow up any security exceptions...
}
return false;
}
function remove(key) {
try {
window.localStorage.removeItem(prefix + key);
} catch (e) {
// Swallow up any security exceptions...
}
}
return {
set: set,
get: get,
remove: remove
};
});

29
static/main.js

@ -64,19 +64,14 @@ define(function (require) {
var Hub = require('hub');
var Raven = require('raven-js');
var settings = require('./settings');
var local = require('./local');
function setupSettings(eventHub) {
var currentSettings = {};
try {
currentSettings = JSON.parse(window.localStorage.getItem('settings'));
} catch (e) {
}
var currentSettings = JSON.parse(local.get('settings', '{}'));
function onChange(settings) {
currentSettings = settings;
try {
window.localStorage.setItem('settings', JSON.stringify(settings));
} catch (e) {
}
local.set('settings', JSON.stringify(settings));
eventHub.emit('settingsChange', settings);
}
@ -116,12 +111,7 @@ define(function (require) {
}
if (!config) {
var savedState = null;
try {
savedState = window.localStorage.getItem('gl');
} catch (e) {
// Some browsers in secure modes can throw exceptions here...
}
var savedState = local.get('gl', null);
config = savedState !== null ? JSON.parse(savedState) : defaultConfig;
}
} else {
@ -152,12 +142,7 @@ define(function (require) {
var config = layout.toConfig();
// Only preserve state in localStorage in non-embedded mode.
if (!options.embedded) {
var state = JSON.stringify(config);
try {
window.localStorage.setItem('gl', state);
} catch (e) {
// Some browsers in secure modes may throw
}
local.set('gl', JSON.stringify(config));
} else {
$('a.link').attr('href', '#' + url.serialiseState(config));
}
@ -193,7 +178,7 @@ define(function (require) {
});
$('#ui-reset').click(function () {
window.localStorage.removeItem('gl');
local.remove('gl');
window.location.reload();
});

Loading…
Cancel
Save