pulling some widely-used functions/data into app/utils.js

pull/1/head
Dave Hoover 9 years ago
parent b57d751da2
commit 9c4a31afe0
  1. 47
      src/app.js
  2. 15
      src/app/compiler.js
  3. 8
      src/app/editor.js
  4. 6
      src/app/storage-handler.js
  5. 19
      src/app/utils.js

@ -2,6 +2,7 @@ var $ = require('jquery');
var UniversalDApp = require('./universal-dapp.js'); var UniversalDApp = require('./universal-dapp.js');
var web3 = require('./web3-adapter.js'); var web3 = require('./web3-adapter.js');
var utils = require('./app/utils');
var queryParams = require('./app/query-params'); var queryParams = require('./app/query-params');
var gistHandler = require('./app/gist-handler'); var gistHandler = require('./app/gist-handler');
@ -36,7 +37,7 @@ var run = function() {
function loadFiles(files) { function loadFiles(files) {
for (var f in files) { for (var f in files) {
var key = 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) { if (key in window.localStorage && window.localStorage[key] != content) {
var count = ''; var count = '';
@ -46,7 +47,7 @@ var run = function() {
} }
window.localStorage[key] = content; window.localStorage[key] = content;
} }
editor.setCacheFile(fileKey(Object.keys(files)[0])); editor.setCacheFile(utils.fileKey(Object.keys(files)[0]));
updateFiles(); updateFiles();
} }
@ -75,16 +76,14 @@ var run = function() {
// ----------------- storage -------------------- // ----------------- storage --------------------
var SOL_CACHE_FILE_PREFIX = 'sol-cache-file-'; var storageHandler = new StorageHandler(updateFiles);
var storageHandler = new StorageHandler(updateFiles, SOL_CACHE_FILE_PREFIX);
window.syncStorage = storageHandler.sync; window.syncStorage = storageHandler.sync;
storageHandler.sync(); storageHandler.sync();
// ----------------- editor ---------------------- // ----------------- editor ----------------------
var editor = new Editor(loadingFromGist, SOL_CACHE_FILE_PREFIX); var editor = new Editor(loadingFromGist);
// ----------------- tabbed menu ------------------- // ----------------- tabbed menu -------------------
@ -150,7 +149,7 @@ var run = function() {
var filesArr = editor.getFiles(); var filesArr = editor.getFiles();
for (var f in filesArr) { for (var f in filesArr) {
files[fileNameFromKey(filesArr[f])] = { files[utils.fileNameFromKey(filesArr[f])] = {
content: localStorage[filesArr[f]] content: localStorage[filesArr[f]]
}; };
} }
@ -228,10 +227,10 @@ 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( fileKey(originalName) ); var content = window.localStorage.getItem( utils.fileKey(originalName) );
window.localStorage[fileKey( newName )] = content; window.localStorage[utils.fileKey( newName )] = content;
window.localStorage.removeItem( fileKey( originalName) ); window.localStorage.removeItem( utils.fileKey( originalName) );
editor.setCacheFile(fileKey( newName )); editor.setCacheFile(utils.fileKey( newName ));
} }
updateFiles(); updateFiles();
@ -246,8 +245,8 @@ 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( fileKey( name ) ); window.localStorage.removeItem( utils.fileKey( name ) );
editor.setNextFile(fileKey(name)); editor.setNextFile(utils.fileKey(name));
updateFiles(); updateFiles();
} }
return false; return false;
@ -255,13 +254,13 @@ var run = function() {
function showFileHandler(ev) { function showFileHandler(ev) {
ev.preventDefault(); ev.preventDefault();
editor.setCacheFile(fileKey( $(this).find('.name').text() )); editor.setCacheFile(utils.fileKey( $(this).find('.name').text() ));
updateFiles(); updateFiles();
return false; return false;
} }
function fileTabFromKey(key) { function fileTabFromKey(key) {
var name = fileNameFromKey(key); var name = utils.fileNameFromKey(key);
return $('#files .file').filter(function(){ return $(this).find('.name').text() == name; }); return $('#files .file').filter(function(){ return $(this).find('.name').text() == name; });
} }
@ -287,18 +286,10 @@ var run = function() {
} }
function fileTabTemplate(key) { function fileTabTemplate(key) {
var name = fileNameFromKey(key); var name = utils.fileNameFromKey(key);
return $('<li class="file"><span class="name">'+name+'</span><span class="remove"><i class="fa fa-close"></i></span></li>'); return $('<li class="file"><span class="name">'+name+'</span><span class="remove"><i class="fa fa-close"></i></span></li>');
} }
function fileKey( name ) {
return SOL_CACHE_FILE_PREFIX + name;
}
function fileNameFromKey(key) {
return key.replace( SOL_CACHE_FILE_PREFIX, '' );
}
$filesWrapper = $('.files-wrapper'); $filesWrapper = $('.files-wrapper');
$scrollerRight = $('.scroller-right'); $scrollerRight = $('.scroller-right');
$scrollerLeft = $('.scroller-left'); $scrollerLeft = $('.scroller-left');
@ -469,7 +460,7 @@ var run = function() {
var errFile = err[1]; var errFile = err[1];
var errLine = parseInt(err[2], 10) - 1; var errLine = parseInt(err[2], 10) - 1;
var errCol = err[4] ? parseInt(err[4], 10) : 0; var errCol = err[4] ? parseInt(err[4], 10) : 0;
if (errFile == '' || errFile == fileNameFromKey(editor.getCacheFile())) { if (errFile == '' || errFile == utils.fileNameFromKey(editor.getCacheFile())) {
compiler.addAnnotation({ compiler.addAnnotation({
row: errLine, row: errLine,
column: errCol, column: errCol,
@ -478,9 +469,9 @@ var run = function() {
}); });
} }
$error.click(function(ev){ $error.click(function(ev){
if (errFile != '' && errFile != fileNameFromKey(editor.getCacheFile()) && editor.getFiles().indexOf(fileKey(errFile)) !== -1) { if (errFile != '' && errFile != utils.fileNameFromKey(editor.getCacheFile()) && editor.getFiles().indexOf(utils.fileKey(errFile)) !== -1) {
// Switch to file // Switch to file
editor.setCacheFile(fileKey(errFile)); editor.setCacheFile(utils.fileKey(errFile));
updateFiles(); updateFiles();
//@TODO could show some error icon in files with errors //@TODO could show some error icon in files with errors
} }
@ -674,7 +665,7 @@ var run = function() {
return $.getJSON('https://api.github.com/repos/' + root + '/contents/' + path, cb); return $.getJSON('https://api.github.com/repos/' + root + '/contents/' + path, cb);
} }
var compiler = new Compiler(editor, renderContracts, renderError, errortype, fileNameFromKey, fileKey, handleGithubCall, $('#output'), function() { return hidingRHP; }); var compiler = new Compiler(editor, renderContracts, renderError, errortype, handleGithubCall, $('#output'), function() { return hidingRHP; });
function setVersionText(text) { function setVersionText(text) {
$('#version').text(text); $('#version').text(text);

@ -1,6 +1,7 @@
var queryParams = require('./query-params'); var queryParams = require('./query-params');
var utils = require('./utils');
function Compiler(editor, renderContracts, renderError, errortype, fileNameFromKey, fileKey, handleGithubCall, outputField, hidingRHP) { function Compiler(editor, renderContracts, renderError, errortype, handleGithubCall, outputField, hidingRHP) {
var compileJSON; var compileJSON;
var compilerAcceptsMultipleFiles; var compilerAcceptsMultipleFiles;
@ -36,7 +37,7 @@ function Compiler(editor, renderContracts, renderError, errortype, fileNameFromK
window.localStorage.setItem(editor.getCacheFile(), input); window.localStorage.setItem(editor.getCacheFile(), input);
var files = {}; var files = {};
files[fileNameFromKey(editor.getCacheFile())] = input; files[utils.fileNameFromKey(editor.getCacheFile())] = input;
gatherImports(files, missingInputs, function(input, error) { gatherImports(files, missingInputs, function(input, error) {
outputField.empty(); outputField.empty();
if (input === null) { if (input === null) {
@ -151,7 +152,7 @@ function Compiler(editor, renderContracts, renderError, errortype, fileNameFromK
importHints = importHints || []; importHints = importHints || [];
if (!compilerAcceptsMultipleFiles) if (!compilerAcceptsMultipleFiles)
{ {
cb(files[fileNameFromKey(editor.getCacheFile())]); cb(files[utils.fileNameFromKey(editor.getCacheFile())]);
return; return;
} }
var importRegex = /^\s*import\s*[\'\"]([^\'\"]+)[\'\"];/g; var importRegex = /^\s*import\s*[\'\"]([^\'\"]+)[\'\"];/g;
@ -166,11 +167,11 @@ function Compiler(editor, renderContracts, renderError, errortype, fileNameFromK
while (importHints.length > 0) { while (importHints.length > 0) {
var m = importHints.pop(); var m = importHints.pop();
if (m in files) continue; if (m in files) continue;
if (editor.getFiles().indexOf(fileKey(m)) !== -1) { if (editor.getFiles().indexOf(utils.fileKey(m)) !== -1) {
files[m] = window.localStorage[fileKey(m)]; files[m] = window.localStorage[utils.fileKey(m)];
reloop = true; reloop = true;
} else if (m.startsWith('./') && editor.getFiles().indexOf(fileKey(m.slice(2))) !== -1) { } else if (m.startsWith('./') && editor.getFiles().indexOf(utils.fileKey(m.slice(2))) !== -1) {
files[m] = window.localStorage[fileKey(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];

@ -1,7 +1,9 @@
var utils = require('./utils');
var ace = require('brace'); var ace = require('brace');
require('../mode-solidity.js'); require('../mode-solidity.js');
function Editor(loadingFromGist, SOL_CACHE_FILE_PREFIX) { function Editor(loadingFromGist) {
this.newFile = function() { this.newFile = function() {
untitledCount = ''; untitledCount = '';
@ -38,7 +40,7 @@ function Editor(loadingFromGist, SOL_CACHE_FILE_PREFIX) {
this.getFiles = function() { this.getFiles = function() {
var files = []; var files = [];
for (var f in localStorage ) { for (var f in localStorage ) {
if (f.indexOf( SOL_CACHE_FILE_PREFIX, 0 ) === 0) { if (f.indexOf( utils.getCacheFilePrefix(), 0 ) === 0) {
files.push(f); files.push(f);
if (!sessions[f]) sessions[f] = newEditorSession(f); if (!sessions[f]) sessions[f] = newEditorSession(f);
} }
@ -116,7 +118,7 @@ function Editor(loadingFromGist, SOL_CACHE_FILE_PREFIX) {
editor.resize(true); editor.resize(true);
} }
var SOL_CACHE_UNTITLED = SOL_CACHE_FILE_PREFIX + 'Untitled'; var SOL_CACHE_UNTITLED = utils.getCacheFilePrefix() + 'Untitled';
var SOL_CACHE_FILE = null; var SOL_CACHE_FILE = null;
var editor = ace.edit("input"); var editor = ace.edit("input");

@ -1,4 +1,6 @@
function StorageHandler(updateFiles, SOL_CACHE_FILE_PREFIX) { var utils = require('./utils');
function StorageHandler(updateFiles) {
this.sync = function() { this.sync = function() {
@ -30,7 +32,7 @@ function StorageHandler(updateFiles, SOL_CACHE_FILE_PREFIX) {
for (var y in window.localStorage) { for (var y in window.localStorage) {
console.log("checking", y); console.log("checking", y);
obj[y] = window.localStorage.getItem(y); obj[y] = window.localStorage.getItem(y);
if (y.indexOf(SOL_CACHE_FILE_PREFIX) !== 0) continue; if (y.indexOf(utils.getCacheFilePrefix()) !== 0) continue;
count++; count++;
check(y); check(y);
} }

@ -0,0 +1,19 @@
var SOL_CACHE_FILE_PREFIX = 'sol-cache-file-';
function getCacheFilePrefix() {
return SOL_CACHE_FILE_PREFIX;
}
function fileKey( name ) {
return getCacheFilePrefix() + name;
}
function fileNameFromKey(key) {
return key.replace( getCacheFilePrefix(), '' );
}
module.exports = {
getCacheFilePrefix: getCacheFilePrefix,
fileKey: fileKey,
fileNameFromKey: fileNameFromKey
};
Loading…
Cancel
Save