From 35e9fee7186c8b67c2df81200a6f55a0d9a19c8c Mon Sep 17 00:00:00 2001 From: Dave Hoover Date: Fri, 24 Jun 2016 11:44:46 -0500 Subject: [PATCH] Pulling all storage-related methods into storage.js --- src/app.js | 39 +++++++--------- src/app/compiler.js | 5 +- src/app/editor.js | 27 ++++++----- src/app/{storage-handler.js => storage.js} | 53 +++++++++++++++++++++- 4 files changed, 83 insertions(+), 41 deletions(-) rename src/app/{storage-handler.js => storage.js} (51%) diff --git a/src/app.js b/src/app.js index d8aaeba334..1336256a29 100644 --- a/src/app.js +++ b/src/app.js @@ -8,7 +8,7 @@ var queryParams = new QueryParams(); var GistHandler = require('./app/gist-handler'); var gistHandler = new GistHandler(); -var StorageHandler = require('./app/storage-handler'); +var Storage = require('./app/storage'); var Editor = require('./app/editor'); var Renderer = require('./app/renderer'); var Compiler = require('./app/compiler'); @@ -25,16 +25,13 @@ window.addEventListener('message', function (ev) { }, false); var run = function () { + var storage = new Storage(updateFiles); + function loadFiles (files) { for (var f in files) { var key = utils.fileKey(f); var content = files[f].content; - if (key in window.localStorage && window.localStorage[key] !== content) { - var count = ''; - while ((key + count) in window.localStorage) count = count - 1; - window.localStorage[key + count] = window.localStorage[key]; - } - window.localStorage[key] = content; + storage.loadFile(key, content); } editor.setCacheFile(utils.fileKey(Object.keys(files)[0])); updateFiles(); @@ -82,15 +79,14 @@ var run = function () { }); }); - // ----------------- storage -------------------- + // ----------------- storage sync -------------------- - var storageHandler = new StorageHandler(updateFiles); - window.syncStorage = storageHandler.sync; - storageHandler.sync(); + window.syncStorage = storage.sync; + storage.sync(); // ----------------- editor ---------------------- - var editor = new Editor(loadingFromGist); + var editor = new Editor(loadingFromGist, storage); // ----------------- tabbed menu ------------------- @@ -166,7 +162,7 @@ var run = function () { var fileList = $('input.inputFile')[0].files; for (var i = 0; i < fileList.length; i++) { 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 () { updateFiles(); }); @@ -200,9 +196,7 @@ var run = function () { $fileNameInputEl.off('keyup'); if (newName !== originalName && confirm('Are you sure you want to rename: ' + originalName + ' to ' + newName + '?')) { - var content = window.localStorage.getItem(utils.fileKey(originalName)); - window.localStorage[utils.fileKey(newName)] = content; - window.localStorage.removeItem(utils.fileKey(originalName)); + storage.rename(utils.fileKey(originalName), utils.fileKey(newName)); editor.setCacheFile(utils.fileKey(newName)); } @@ -218,7 +212,7 @@ var run = function () { var name = $(this).parent().find('.name').text(); 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)); updateFiles(); } @@ -342,7 +336,6 @@ var run = function () { // ----------------- resizeable ui --------------- - var EDITOR_SIZE_CACHE_KEY = 'editor-size-cache'; var dragging = false; $('#dragbar').mousedown(function (e) { e.preventDefault(); @@ -369,7 +362,7 @@ var run = function () { } function getEditorSize () { - window.localStorage[EDITOR_SIZE_CACHE_KEY] = $('#righthand-panel').width(); + storage.setEditorSize($('#righthand-panel').width()); } $(document).mouseup(function (e) { @@ -379,13 +372,13 @@ var run = function () { $(document).unbind('mousemove'); dragging = false; setEditorSize(delta); - window.localStorage.setItem(EDITOR_SIZE_CACHE_KEY, delta); + storage.setEditorSize(delta); reAdjust(); } }); // set cached defaults - var cachedSize = window.localStorage.getItem(EDITOR_SIZE_CACHE_KEY); + var cachedSize = storage.getEditorSize(); if (cachedSize) setEditorSize(cachedSize); else getEditorSize(); @@ -394,7 +387,7 @@ var run = function () { var hidingRHP = false; $('.toggleRHP').click(function () { 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-left', hidingRHP); if (!hidingRHP) compiler.compile(); @@ -455,7 +448,7 @@ var run = function () { compiler.compile(); }); - storageHandler.sync(); + storage.sync(); }; module.exports = { diff --git a/src/app/compiler.js b/src/app/compiler.js index 0121136a4d..966e0fb717 100644 --- a/src/app/compiler.js +++ b/src/app/compiler.js @@ -210,10 +210,7 @@ function Compiler (editor, renderer, queryParams, handleGithubCall, outputField, continue; } if (editor.hasFile(m)) { - files[m] = window.localStorage[utils.fileKey(m)]; - reloop = true; - } else if (m.startsWith('./') && editor.hasFile(m.slice(2))) { - files[m] = window.localStorage[utils.fileKey(m.slice(2))]; + files[m] = editor.getFile(m); reloop = true; } else if (m in cachedRemoteFiles) { files[m] = cachedRemoteFiles[m]; diff --git a/src/app/editor.js b/src/app/editor.js index c5892ff018..bb82550211 100644 --- a/src/app/editor.js +++ b/src/app/editor.js @@ -5,7 +5,7 @@ var utils = require('./utils'); var ace = require('brace'); require('../mode-solidity.js'); -function Editor (loadingFromGist) { +function Editor (loadingFromGist, storage) { var SOL_CACHE_UNTITLED = utils.fileKey('Untitled'); var SOL_CACHE_FILE = null; @@ -17,7 +17,7 @@ function Editor (loadingFromGist) { this.newFile = function () { var untitledCount = ''; - while (window.localStorage[SOL_CACHE_UNTITLED + untitledCount]) { + while (storage.exists(SOL_CACHE_UNTITLED + untitledCount)) { untitledCount = (untitledCount - 0) + 1; } SOL_CACHE_FILE = SOL_CACHE_UNTITLED + untitledCount; @@ -30,7 +30,7 @@ function Editor (loadingFromGist) { SOL_CACHE_FILE = utils.fileKey(file.name); 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; callback(); }; @@ -38,7 +38,7 @@ function Editor (loadingFromGist) { }; this.setCacheFileContent = function (content) { - window.localStorage.setItem(SOL_CACHE_FILE, content); + storage.set(SOL_CACHE_FILE, content); }; this.setCacheFile = function (cacheFile) { @@ -67,14 +67,18 @@ function Editor (loadingFromGist) { return this.getFiles().indexOf(utils.fileKey(name)) !== -1; }; + this.getFile = function (name) { + return storage.get(utils.fileKey(name)); + }; + function getFiles () { var files = []; - for (var f in window.localStorage) { + storage.keys().forEach(function (f) { if (utils.isCachedFile(f)) { files.push(f); if (!sessions[f]) sessions[f] = newEditorSession(f); } - } + }); return files; } this.getFiles = getFiles; @@ -85,7 +89,7 @@ function Editor (loadingFromGist) { for (var f in filesArr) { files[utils.fileNameFromKey(filesArr[f])] = { - content: window.localStorage[filesArr[f]] + content: storage.get(filesArr[f]) }; } return files; @@ -137,7 +141,7 @@ function Editor (loadingFromGist) { }; 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.setTabSize(4); s.setUseSoftTabs(true); @@ -147,16 +151,15 @@ function Editor (loadingFromGist) { function setupStuff (files) { var untitledCount = ''; - if (!files.length || window.localStorage['sol-cache']) { + if (files.length === 0) { if (loadingFromGist) return; // Backwards-compatibility - while (window.localStorage[SOL_CACHE_UNTITLED + untitledCount]) { + while (storage.exists(SOL_CACHE_UNTITLED + untitledCount)) { untitledCount = (untitledCount - 0) + 1; } SOL_CACHE_FILE = SOL_CACHE_UNTITLED + untitledCount; files.push(SOL_CACHE_FILE); - window.localStorage[SOL_CACHE_FILE] = window.localStorage['sol-cache'] || BALLOT_EXAMPLE; - window.localStorage.removeItem('sol-cache'); + storage.set(SOL_CACHE_FILE, BALLOT_EXAMPLE); // defined in assets/js/ballot.sol.js } SOL_CACHE_FILE = files[0]; diff --git a/src/app/storage-handler.js b/src/app/storage.js similarity index 51% rename from src/app/storage-handler.js rename to src/app/storage.js index 9d566d67bf..f73a9901d9 100644 --- a/src/app/storage-handler.js +++ b/src/app/storage.js @@ -2,7 +2,56 @@ 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 () { if (typeof chrome === 'undefined' || !chrome || !chrome.storage || !chrome.storage.sync) { return; @@ -44,4 +93,4 @@ function StorageHandler (updateFiles) { }; } -module.exports = StorageHandler; +module.exports = Storage;