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 web3 = require('./web3-adapter.js');
var utils = require('./app/utils');
var queryParams = require('./app/query-params');
var gistHandler = require('./app/gist-handler');
@ -36,7 +37,7 @@ var run = function() {
function loadFiles(files) {
for (var f in files) {
var key = fileKey(f);
var key = utils.fileKey(f);
var content = files[f].content;
if (key in window.localStorage && window.localStorage[key] != content) {
var count = '';
@ -46,7 +47,7 @@ var run = function() {
}
window.localStorage[key] = content;
}
editor.setCacheFile(fileKey(Object.keys(files)[0]));
editor.setCacheFile(utils.fileKey(Object.keys(files)[0]));
updateFiles();
}
@ -75,16 +76,14 @@ var run = function() {
// ----------------- storage --------------------
var SOL_CACHE_FILE_PREFIX = 'sol-cache-file-';
var storageHandler = new StorageHandler(updateFiles, SOL_CACHE_FILE_PREFIX);
var storageHandler = new StorageHandler(updateFiles);
window.syncStorage = storageHandler.sync;
storageHandler.sync();
// ----------------- editor ----------------------
var editor = new Editor(loadingFromGist, SOL_CACHE_FILE_PREFIX);
var editor = new Editor(loadingFromGist);
// ----------------- tabbed menu -------------------
@ -150,7 +149,7 @@ var run = function() {
var filesArr = editor.getFiles();
for (var f in filesArr) {
files[fileNameFromKey(filesArr[f])] = {
files[utils.fileNameFromKey(filesArr[f])] = {
content: localStorage[filesArr[f]]
};
}
@ -228,10 +227,10 @@ 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( fileKey(originalName) );
window.localStorage[fileKey( newName )] = content;
window.localStorage.removeItem( fileKey( originalName) );
editor.setCacheFile(fileKey( newName ));
var content = window.localStorage.getItem( utils.fileKey(originalName) );
window.localStorage[utils.fileKey( newName )] = content;
window.localStorage.removeItem( utils.fileKey( originalName) );
editor.setCacheFile(utils.fileKey( newName ));
}
updateFiles();
@ -246,8 +245,8 @@ 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( fileKey( name ) );
editor.setNextFile(fileKey(name));
window.localStorage.removeItem( utils.fileKey( name ) );
editor.setNextFile(utils.fileKey(name));
updateFiles();
}
return false;
@ -255,13 +254,13 @@ var run = function() {
function showFileHandler(ev) {
ev.preventDefault();
editor.setCacheFile(fileKey( $(this).find('.name').text() ));
editor.setCacheFile(utils.fileKey( $(this).find('.name').text() ));
updateFiles();
return false;
}
function fileTabFromKey(key) {
var name = fileNameFromKey(key);
var name = utils.fileNameFromKey(key);
return $('#files .file').filter(function(){ return $(this).find('.name').text() == name; });
}
@ -287,18 +286,10 @@ var run = function() {
}
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>');
}
function fileKey( name ) {
return SOL_CACHE_FILE_PREFIX + name;
}
function fileNameFromKey(key) {
return key.replace( SOL_CACHE_FILE_PREFIX, '' );
}
$filesWrapper = $('.files-wrapper');
$scrollerRight = $('.scroller-right');
$scrollerLeft = $('.scroller-left');
@ -469,7 +460,7 @@ var run = function() {
var errFile = err[1];
var errLine = parseInt(err[2], 10) - 1;
var errCol = err[4] ? parseInt(err[4], 10) : 0;
if (errFile == '' || errFile == fileNameFromKey(editor.getCacheFile())) {
if (errFile == '' || errFile == utils.fileNameFromKey(editor.getCacheFile())) {
compiler.addAnnotation({
row: errLine,
column: errCol,
@ -478,9 +469,9 @@ var run = function() {
});
}
$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
editor.setCacheFile(fileKey(errFile));
editor.setCacheFile(utils.fileKey(errFile));
updateFiles();
//@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);
}
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) {
$('#version').text(text);

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

@ -1,7 +1,9 @@
var utils = require('./utils');
var ace = require('brace');
require('../mode-solidity.js');
function Editor(loadingFromGist, SOL_CACHE_FILE_PREFIX) {
function Editor(loadingFromGist) {
this.newFile = function() {
untitledCount = '';
@ -38,7 +40,7 @@ function Editor(loadingFromGist, SOL_CACHE_FILE_PREFIX) {
this.getFiles = function() {
var files = [];
for (var f in localStorage ) {
if (f.indexOf( SOL_CACHE_FILE_PREFIX, 0 ) === 0) {
if (f.indexOf( utils.getCacheFilePrefix(), 0 ) === 0) {
files.push(f);
if (!sessions[f]) sessions[f] = newEditorSession(f);
}
@ -116,7 +118,7 @@ function Editor(loadingFromGist, SOL_CACHE_FILE_PREFIX) {
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 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() {
@ -30,7 +32,7 @@ function StorageHandler(updateFiles, SOL_CACHE_FILE_PREFIX) {
for (var y in window.localStorage) {
console.log("checking", y);
obj[y] = window.localStorage.getItem(y);
if (y.indexOf(SOL_CACHE_FILE_PREFIX) !== 0) continue;
if (y.indexOf(utils.getCacheFilePrefix()) !== 0) continue;
count++;
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