Commit 35e9fee7 authored by Dave Hoover's avatar Dave Hoover

Pulling all storage-related methods into storage.js

parent 7a9b1695
...@@ -8,7 +8,7 @@ var queryParams = new QueryParams(); ...@@ -8,7 +8,7 @@ var queryParams = new QueryParams();
var GistHandler = require('./app/gist-handler'); var GistHandler = require('./app/gist-handler');
var gistHandler = new GistHandler(); var gistHandler = new GistHandler();
var StorageHandler = require('./app/storage-handler'); var Storage = require('./app/storage');
var Editor = require('./app/editor'); var Editor = require('./app/editor');
var Renderer = require('./app/renderer'); var Renderer = require('./app/renderer');
var Compiler = require('./app/compiler'); var Compiler = require('./app/compiler');
...@@ -25,16 +25,13 @@ window.addEventListener('message', function (ev) { ...@@ -25,16 +25,13 @@ window.addEventListener('message', function (ev) {
}, false); }, false);
var run = function () { var run = function () {
var storage = new Storage(updateFiles);
function loadFiles (files) { function loadFiles (files) {
for (var f in files) { for (var f in files) {
var key = utils.fileKey(f); var key = utils.fileKey(f);
var content = files[f].content; var content = files[f].content;
if (key in window.localStorage && window.localStorage[key] !== content) { storage.loadFile(key, content);
var count = '';
while ((key + count) in window.localStorage) count = count - 1;
window.localStorage[key + count] = window.localStorage[key];
}
window.localStorage[key] = content;
} }
editor.setCacheFile(utils.fileKey(Object.keys(files)[0])); editor.setCacheFile(utils.fileKey(Object.keys(files)[0]));
updateFiles(); updateFiles();
...@@ -82,15 +79,14 @@ var run = function () { ...@@ -82,15 +79,14 @@ var run = function () {
}); });
}); });
// ----------------- storage -------------------- // ----------------- storage sync --------------------
var storageHandler = new StorageHandler(updateFiles); window.syncStorage = storage.sync;
window.syncStorage = storageHandler.sync; storage.sync();
storageHandler.sync();
// ----------------- editor ---------------------- // ----------------- editor ----------------------
var editor = new Editor(loadingFromGist); var editor = new Editor(loadingFromGist, storage);
// ----------------- tabbed menu ------------------- // ----------------- tabbed menu -------------------
...@@ -166,7 +162,7 @@ var run = function () { ...@@ -166,7 +162,7 @@ var run = function () {
var fileList = $('input.inputFile')[0].files; var fileList = $('input.inputFile')[0].files;
for (var i = 0; i < fileList.length; i++) { for (var i = 0; i < fileList.length; i++) {
var name = fileList[i].name; var name = fileList[i].name;
if (!window.localStorage[utils.fileKey(name)] || confirm('The file ' + name + ' already exists! Would you like to overwrite it?')) { if (!storage.exists(utils.fileKey(name)) || confirm('The file ' + name + ' already exists! Would you like to overwrite it?')) {
editor.uploadFile(fileList[i], function () { editor.uploadFile(fileList[i], function () {
updateFiles(); updateFiles();
}); });
...@@ -200,9 +196,7 @@ var run = function () { ...@@ -200,9 +196,7 @@ var run = function () {
$fileNameInputEl.off('keyup'); $fileNameInputEl.off('keyup');
if (newName !== originalName && confirm('Are you sure you want to rename: ' + originalName + ' to ' + newName + '?')) { if (newName !== originalName && confirm('Are you sure you want to rename: ' + originalName + ' to ' + newName + '?')) {
var content = window.localStorage.getItem(utils.fileKey(originalName)); storage.rename(utils.fileKey(originalName), utils.fileKey(newName));
window.localStorage[utils.fileKey(newName)] = content;
window.localStorage.removeItem(utils.fileKey(originalName));
editor.setCacheFile(utils.fileKey(newName)); editor.setCacheFile(utils.fileKey(newName));
} }
...@@ -218,7 +212,7 @@ var run = function () { ...@@ -218,7 +212,7 @@ var run = function () {
var name = $(this).parent().find('.name').text(); var name = $(this).parent().find('.name').text();
if (confirm('Are you sure you want to remove: ' + name + ' from local storage?')) { if (confirm('Are you sure you want to remove: ' + name + ' from local storage?')) {
window.localStorage.removeItem(utils.fileKey(name)); storage.remove(utils.fileKey(name));
editor.setNextFile(utils.fileKey(name)); editor.setNextFile(utils.fileKey(name));
updateFiles(); updateFiles();
} }
...@@ -342,7 +336,6 @@ var run = function () { ...@@ -342,7 +336,6 @@ var run = function () {
// ----------------- resizeable ui --------------- // ----------------- resizeable ui ---------------
var EDITOR_SIZE_CACHE_KEY = 'editor-size-cache';
var dragging = false; var dragging = false;
$('#dragbar').mousedown(function (e) { $('#dragbar').mousedown(function (e) {
e.preventDefault(); e.preventDefault();
...@@ -369,7 +362,7 @@ var run = function () { ...@@ -369,7 +362,7 @@ var run = function () {
} }
function getEditorSize () { function getEditorSize () {
window.localStorage[EDITOR_SIZE_CACHE_KEY] = $('#righthand-panel').width(); storage.setEditorSize($('#righthand-panel').width());
} }
$(document).mouseup(function (e) { $(document).mouseup(function (e) {
...@@ -379,13 +372,13 @@ var run = function () { ...@@ -379,13 +372,13 @@ var run = function () {
$(document).unbind('mousemove'); $(document).unbind('mousemove');
dragging = false; dragging = false;
setEditorSize(delta); setEditorSize(delta);
window.localStorage.setItem(EDITOR_SIZE_CACHE_KEY, delta); storage.setEditorSize(delta);
reAdjust(); reAdjust();
} }
}); });
// set cached defaults // set cached defaults
var cachedSize = window.localStorage.getItem(EDITOR_SIZE_CACHE_KEY); var cachedSize = storage.getEditorSize();
if (cachedSize) setEditorSize(cachedSize); if (cachedSize) setEditorSize(cachedSize);
else getEditorSize(); else getEditorSize();
...@@ -394,7 +387,7 @@ var run = function () { ...@@ -394,7 +387,7 @@ var run = function () {
var hidingRHP = false; var hidingRHP = false;
$('.toggleRHP').click(function () { $('.toggleRHP').click(function () {
hidingRHP = !hidingRHP; hidingRHP = !hidingRHP;
setEditorSize(hidingRHP ? 0 : window.localStorage[EDITOR_SIZE_CACHE_KEY]); setEditorSize(hidingRHP ? 0 : storage.getEditorSize());
$('.toggleRHP i').toggleClass('fa-angle-double-right', !hidingRHP); $('.toggleRHP i').toggleClass('fa-angle-double-right', !hidingRHP);
$('.toggleRHP i').toggleClass('fa-angle-double-left', hidingRHP); $('.toggleRHP i').toggleClass('fa-angle-double-left', hidingRHP);
if (!hidingRHP) compiler.compile(); if (!hidingRHP) compiler.compile();
...@@ -455,7 +448,7 @@ var run = function () { ...@@ -455,7 +448,7 @@ var run = function () {
compiler.compile(); compiler.compile();
}); });
storageHandler.sync(); storage.sync();
}; };
module.exports = { module.exports = {
......
...@@ -210,10 +210,7 @@ function Compiler (editor, renderer, queryParams, handleGithubCall, outputField, ...@@ -210,10 +210,7 @@ function Compiler (editor, renderer, queryParams, handleGithubCall, outputField,
continue; continue;
} }
if (editor.hasFile(m)) { if (editor.hasFile(m)) {
files[m] = window.localStorage[utils.fileKey(m)]; files[m] = editor.getFile(m);
reloop = true;
} else if (m.startsWith('./') && editor.hasFile(m.slice(2))) {
files[m] = window.localStorage[utils.fileKey(m.slice(2))];
reloop = true; reloop = true;
} else if (m in cachedRemoteFiles) { } else if (m in cachedRemoteFiles) {
files[m] = cachedRemoteFiles[m]; files[m] = cachedRemoteFiles[m];
......
...@@ -5,7 +5,7 @@ var utils = require('./utils'); ...@@ -5,7 +5,7 @@ var utils = require('./utils');
var ace = require('brace'); var ace = require('brace');
require('../mode-solidity.js'); require('../mode-solidity.js');
function Editor (loadingFromGist) { function Editor (loadingFromGist, storage) {
var SOL_CACHE_UNTITLED = utils.fileKey('Untitled'); var SOL_CACHE_UNTITLED = utils.fileKey('Untitled');
var SOL_CACHE_FILE = null; var SOL_CACHE_FILE = null;
...@@ -17,7 +17,7 @@ function Editor (loadingFromGist) { ...@@ -17,7 +17,7 @@ function Editor (loadingFromGist) {
this.newFile = function () { this.newFile = function () {
var untitledCount = ''; var untitledCount = '';
while (window.localStorage[SOL_CACHE_UNTITLED + untitledCount]) { while (storage.exists(SOL_CACHE_UNTITLED + untitledCount)) {
untitledCount = (untitledCount - 0) + 1; untitledCount = (untitledCount - 0) + 1;
} }
SOL_CACHE_FILE = SOL_CACHE_UNTITLED + untitledCount; SOL_CACHE_FILE = SOL_CACHE_UNTITLED + untitledCount;
...@@ -30,7 +30,7 @@ function Editor (loadingFromGist) { ...@@ -30,7 +30,7 @@ function Editor (loadingFromGist) {
SOL_CACHE_FILE = utils.fileKey(file.name); SOL_CACHE_FILE = utils.fileKey(file.name);
fileReader.onload = function (e) { fileReader.onload = function (e) {
window.localStorage[SOL_CACHE_FILE] = e.target.result; storage.set(SOL_CACHE_FILE, e.target.result);
sessions[SOL_CACHE_FILE] = null; sessions[SOL_CACHE_FILE] = null;
callback(); callback();
}; };
...@@ -38,7 +38,7 @@ function Editor (loadingFromGist) { ...@@ -38,7 +38,7 @@ function Editor (loadingFromGist) {
}; };
this.setCacheFileContent = function (content) { this.setCacheFileContent = function (content) {
window.localStorage.setItem(SOL_CACHE_FILE, content); storage.set(SOL_CACHE_FILE, content);
}; };
this.setCacheFile = function (cacheFile) { this.setCacheFile = function (cacheFile) {
...@@ -67,14 +67,18 @@ function Editor (loadingFromGist) { ...@@ -67,14 +67,18 @@ function Editor (loadingFromGist) {
return this.getFiles().indexOf(utils.fileKey(name)) !== -1; return this.getFiles().indexOf(utils.fileKey(name)) !== -1;
}; };
this.getFile = function (name) {
return storage.get(utils.fileKey(name));
};
function getFiles () { function getFiles () {
var files = []; var files = [];
for (var f in window.localStorage) { storage.keys().forEach(function (f) {
if (utils.isCachedFile(f)) { if (utils.isCachedFile(f)) {
files.push(f); files.push(f);
if (!sessions[f]) sessions[f] = newEditorSession(f); if (!sessions[f]) sessions[f] = newEditorSession(f);
} }
} });
return files; return files;
} }
this.getFiles = getFiles; this.getFiles = getFiles;
...@@ -85,7 +89,7 @@ function Editor (loadingFromGist) { ...@@ -85,7 +89,7 @@ function Editor (loadingFromGist) {
for (var f in filesArr) { for (var f in filesArr) {
files[utils.fileNameFromKey(filesArr[f])] = { files[utils.fileNameFromKey(filesArr[f])] = {
content: window.localStorage[filesArr[f]] content: storage.get(filesArr[f])
}; };
} }
return files; return files;
...@@ -137,7 +141,7 @@ function Editor (loadingFromGist) { ...@@ -137,7 +141,7 @@ function Editor (loadingFromGist) {
}; };
function newEditorSession (filekey) { function newEditorSession (filekey) {
var s = new ace.EditSession(window.localStorage[filekey], 'ace/mode/javascript'); var s = new ace.EditSession(storage.get(filekey), 'ace/mode/javascript');
s.setUndoManager(new ace.UndoManager()); s.setUndoManager(new ace.UndoManager());
s.setTabSize(4); s.setTabSize(4);
s.setUseSoftTabs(true); s.setUseSoftTabs(true);
...@@ -147,16 +151,15 @@ function Editor (loadingFromGist) { ...@@ -147,16 +151,15 @@ function Editor (loadingFromGist) {
function setupStuff (files) { function setupStuff (files) {
var untitledCount = ''; var untitledCount = '';
if (!files.length || window.localStorage['sol-cache']) { if (files.length === 0) {
if (loadingFromGist) return; if (loadingFromGist) return;
// Backwards-compatibility // Backwards-compatibility
while (window.localStorage[SOL_CACHE_UNTITLED + untitledCount]) { while (storage.exists(SOL_CACHE_UNTITLED + untitledCount)) {
untitledCount = (untitledCount - 0) + 1; untitledCount = (untitledCount - 0) + 1;
} }
SOL_CACHE_FILE = SOL_CACHE_UNTITLED + untitledCount; SOL_CACHE_FILE = SOL_CACHE_UNTITLED + untitledCount;
files.push(SOL_CACHE_FILE); files.push(SOL_CACHE_FILE);
window.localStorage[SOL_CACHE_FILE] = window.localStorage['sol-cache'] || BALLOT_EXAMPLE; storage.set(SOL_CACHE_FILE, BALLOT_EXAMPLE); // defined in assets/js/ballot.sol.js
window.localStorage.removeItem('sol-cache');
} }
SOL_CACHE_FILE = files[0]; SOL_CACHE_FILE = files[0];
......
...@@ -2,7 +2,56 @@ ...@@ -2,7 +2,56 @@
var utils = require('./utils'); var utils = require('./utils');
function StorageHandler (updateFiles) { function Storage (updateFiles) {
var EDITOR_SIZE_CACHE_KEY = 'editor-size-cache';
this.rename = function (originalName, newName) {
var content = this.get(originalName);
this.set(newName, content);
this.remove(originalName);
};
this.remove = function (name) {
window.localStorage.removeItem(name);
};
this.setEditorSize = function (size) {
this.set(EDITOR_SIZE_CACHE_KEY, size);
};
this.getEditorSize = function () {
return this.get(EDITOR_SIZE_CACHE_KEY);
};
this.getFileContent = function (key) {
return this.get(utils.fileKey(key));
};
this.exists = function (key) {
return !!this.get(key);
};
this.set = function (key, content) {
window.localStorage.setItem(key, content);
};
this.get = function (key) {
return window.localStorage.getItem(key);
};
this.keys = function () {
return Object.keys(window.localStorage);
};
this.loadFile = function (filename, content) {
if (this.exists(filename) && this.get(filename) !== content) {
var count = '';
while (this.exists(filename + count)) count = count - 1;
this.rename(filename, filename + count);
}
this.set(filename, content);
};
this.sync = function () { this.sync = function () {
if (typeof chrome === 'undefined' || !chrome || !chrome.storage || !chrome.storage.sync) { if (typeof chrome === 'undefined' || !chrome || !chrome.storage || !chrome.storage.sync) {
return; return;
...@@ -44,4 +93,4 @@ function StorageHandler (updateFiles) { ...@@ -44,4 +93,4 @@ function StorageHandler (updateFiles) {
}; };
} }
module.exports = StorageHandler; module.exports = Storage;
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment