Commit 871f31c6 authored by Dave Hoover's avatar Dave Hoover

Initial refactoring effort: extract methods into modules

parent 1bf2dae9
...@@ -4,6 +4,10 @@ var web3 = require('./web3-adapter.js'); ...@@ -4,6 +4,10 @@ var web3 = require('./web3-adapter.js');
var ace = require('brace'); var ace = require('brace');
require('./mode-solidity.js'); require('./mode-solidity.js');
var queryParams = require('./app/query-params');
var gistHandler = require('./app/gist-handler');
var StorageHandler = require('./app/storage-handler');
// The event listener needs to be registered as early as possible, because the // The event listener needs to be registered as early as possible, because the
// parent will send the message upon the "load" event. // parent will send the message upon the "load" event.
var filesToLoad = null; var filesToLoad = null;
...@@ -20,55 +24,15 @@ var run = function() { ...@@ -20,55 +24,15 @@ var run = function() {
// ------------------ query params (hash) ---------------- // ------------------ query params (hash) ----------------
function getQueryParams() {
var qs = window.location.hash.substr(1);
if (window.location.search.length > 0) {
// use legacy query params instead of hash
window.location.hash = window.location.search.substr(1);
window.location.search = "";
}
var params = {};
var parts = qs.split("&");
for (var x in parts) {
var keyValue = parts[x].split("=");
if (keyValue[0] !== "") params[keyValue[0]] = keyValue[1];
}
return params;
}
function updateQueryParams(params) {
var currentParams = getQueryParams();
var keys = Object.keys(params);
for (var x in keys) {
currentParams[keys[x]] = params[keys[x]];
}
var queryString = "#";
var updatedKeys = Object.keys(currentParams);
for( var y in updatedKeys) {
queryString += updatedKeys[y] + "=" + currentParams[updatedKeys[y]] + "&";
}
window.location.hash = queryString.slice(0, -1);
}
function syncQueryParams() { function syncQueryParams() {
$('#optimize').attr( 'checked', (getQueryParams().optimize == "true") ); $('#optimize').attr( 'checked', (queryParams.get().optimize == "true") );
} }
window.onhashchange = syncQueryParams; window.onhashchange = syncQueryParams;
syncQueryParams(); syncQueryParams();
// ------------------ gist load ---------------- // ------------------ gist load ----------------
function getGistId(str) {
var idr = /[0-9A-Fa-f]{8,}/;
var match = idr.exec(str);
return match ? match[0] : null;
}
function loadFiles(files) { function loadFiles(files) {
for (var f in files) { for (var f in files) {
var key = fileKey(f); var key = fileKey(f);
...@@ -85,25 +49,12 @@ var run = function() { ...@@ -85,25 +49,12 @@ var run = function() {
updateFiles(); updateFiles();
} }
var queryParams = getQueryParams(); gistHandler.handleLoad(function(gistId) {
var loadingFromGist = false; $.ajax({
if (typeof queryParams['gist'] != undefined) {
var gistId;
if (queryParams['gist'] === '') {
var str = prompt("Enter the URL or ID of the Gist you would like to load.");
if (str !== '') {
gistId = getGistId( str );
loadingFromGist = !!gistId;
}
} else {
gistId = queryParams['gist'];
loadingFromGist = !!gistId;
}
if (loadingFromGist) $.ajax({
url: 'https://api.github.com/gists/'+gistId, url: 'https://api.github.com/gists/'+gistId,
jsonp: 'callback', jsonp: 'callback',
dataType: 'jsonp', dataType: 'jsonp',
success: function(response){ success: function(response) {
if (response.data) { if (response.data) {
if (!response.data.files) { if (!response.data.files) {
alert( "Gist load error: " + response.data.message ); alert( "Gist load error: " + response.data.message );
...@@ -113,7 +64,7 @@ var run = function() { ...@@ -113,7 +64,7 @@ var run = function() {
} }
} }
}); });
} });
loadFilesCallback = function(files) { loadFilesCallback = function(files) {
loadFiles(files); loadFiles(files);
...@@ -123,52 +74,15 @@ var run = function() { ...@@ -123,52 +74,15 @@ var run = function() {
// ----------------- storage -------------------- // ----------------- storage --------------------
function syncStorage() { var SOL_CACHE_FILE_PREFIX = 'sol-cache-file-';
if (typeof chrome === 'undefined' || !chrome || !chrome.storage || !chrome.storage.sync) return;
var obj = {};
var done = false;
var count = 0;
var dont = 0;
function check(key){
chrome.storage.sync.get( key, function(resp){
console.log("comparing to cloud", key, resp);
if (typeof resp[key] != 'undefined' && obj[key] !== resp[key] && confirm("Overwrite '" + fileNameFromKey(key) + "'? Click Ok to overwrite local file with file from cloud. Cancel will push your local file to the cloud.")) {
console.log("Overwriting", key );
localStorage.setItem( key, resp[key] );
updateFiles();
} else {
console.log( "add to obj", obj, key);
obj[key] = localStorage[key];
}
done++;
if (done >= count) chrome.storage.sync.set( obj, function(){
console.log( "updated cloud files with: ", obj, this, arguments);
});
});
}
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;
count++;
check(y);
}
}
window.syncStorage = syncStorage;
syncStorage();
var storageHandler = new StorageHandler(SOL_CACHE_FILE_PREFIX);
window.syncStorage = storageHandler.sync;
storageHandler.sync();
// ----------------- editor ---------------------- // ----------------- editor ----------------------
var SOL_CACHE_FILE_PREFIX = 'sol-cache-file-';
var SOL_CACHE_UNTITLED = SOL_CACHE_FILE_PREFIX + 'Untitled'; var SOL_CACHE_UNTITLED = SOL_CACHE_FILE_PREFIX + 'Untitled';
var SOL_CACHE_FILE = null; var SOL_CACHE_FILE = null;
...@@ -285,7 +199,7 @@ var run = function() { ...@@ -285,7 +199,7 @@ var run = function() {
if (confirm("Are you sure you want to publish all your files anonymously as a public gist on github.com?")) { if (confirm("Are you sure you want to publish all your files anonymously as a public gist on github.com?")) {
var files = packageFiles(); var files = packageFiles();
var description = "Created using browser-solidity: Realtime Ethereum Contract Compiler and Runtime. \n Load this file by pasting this gists URL or ID at https://ethereum.github.io/browser-solidity/#version=" + getQueryParams().version + "&optimize="+ getQueryParams().optimize +"&gist="; var description = "Created using browser-solidity: Realtime Ethereum Contract Compiler and Runtime. \n Load this file by pasting this gists URL or ID at https://ethereum.github.io/browser-solidity/#version=" + queryParams.get().version + "&optimize="+ queryParams.get().optimize +"&gist=";
$.ajax({ $.ajax({
url: 'https://api.github.com/gists', url: 'https://api.github.com/gists',
...@@ -516,7 +430,7 @@ var run = function() { ...@@ -516,7 +430,7 @@ var run = function() {
} }
}); });
$('#versionSelector').change(function() { $('#versionSelector').change(function() {
updateQueryParams({version: $('#versionSelector').val() }); queryParams.update({version: $('#versionSelector').val() });
loadVersion($('#versionSelector').val()); loadVersion($('#versionSelector').val());
}); });
...@@ -626,7 +540,7 @@ var run = function() { ...@@ -626,7 +540,7 @@ var run = function() {
if (input === null) { if (input === null) {
renderError(error); renderError(error);
} else { } else {
var optimize = getQueryParams().optimize; var optimize = queryParams.get().optimize;
compileJSON(input, optimize ? 1 : 0); compileJSON(input, optimize ? 1 : 0);
} }
}); });
...@@ -788,7 +702,7 @@ var run = function() { ...@@ -788,7 +702,7 @@ var run = function() {
var worker = null; var worker = null;
var loadVersion = function(version) { var loadVersion = function(version) {
$('#version').text("(loading)"); $('#version').text("(loading)");
updateQueryParams({version: version}); queryParams.update({version: version});
var isFirefox = typeof InstallTrigger !== 'undefined'; var isFirefox = typeof InstallTrigger !== 'undefined';
if (document.location.protocol != 'file:' && Worker !== undefined && isFirefox) { if (document.location.protocol != 'file:' && Worker !== undefined && isFirefox) {
// Workers cannot load js on "file:"-URLs and we get a // Workers cannot load js on "file:"-URLs and we get a
...@@ -811,7 +725,7 @@ var run = function() { ...@@ -811,7 +725,7 @@ var run = function() {
} }
}; };
loadVersion( getQueryParams().version || 'soljson-latest.js'); loadVersion( queryParams.get().version || 'soljson-latest.js');
editor.getSession().on('change', onChange); editor.getSession().on('change', onChange);
editor.on('changeSession', function(){ editor.on('changeSession', function(){
...@@ -820,7 +734,7 @@ var run = function() { ...@@ -820,7 +734,7 @@ var run = function() {
}); });
document.querySelector('#optimize').addEventListener('change', function(){ document.querySelector('#optimize').addEventListener('change', function(){
updateQueryParams({optimize: document.querySelector('#optimize').checked }); queryParams.update({optimize: document.querySelector('#optimize').checked });
compile(); compile();
}); });
...@@ -1041,8 +955,7 @@ var run = function() { ...@@ -1041,8 +955,7 @@ var run = function() {
return funABI; return funABI;
}; };
syncStorage(); storageHandler.sync();
}; };
module.exports = { module.exports = {
......
var queryParams = require('./query-params');
function handleLoad(cb) {
var params = queryParams.get();
var loadingFromGist = false;
if (typeof params['gist'] != undefined) {
var gistId;
if (params['gist'] === '') {
var str = prompt("Enter the URL or ID of the Gist you would like to load.");
if (str !== '') {
gistId = getGistId( str );
loadingFromGist = !!gistId;
}
} else {
gistId = params['gist'];
loadingFromGist = !!gistId;
}
if (loadingFromGist) cb(gistId);
}
}
function getGistId(str) {
var idr = /[0-9A-Fa-f]{8,}/;
var match = idr.exec(str);
return match ? match[0] : null;
}
module.exports = {
handleLoad: handleLoad
};
function getQueryParams() {
var qs = window.location.hash.substr(1);
if (window.location.search.length > 0) {
// use legacy query params instead of hash
window.location.hash = window.location.search.substr(1);
window.location.search = "";
}
var params = {};
var parts = qs.split("&");
for (var x in parts) {
var keyValue = parts[x].split("=");
if (keyValue[0] !== "") params[keyValue[0]] = keyValue[1];
}
return params;
}
function updateQueryParams(params) {
var currentParams = getQueryParams();
var keys = Object.keys(params);
for (var x in keys) {
currentParams[keys[x]] = params[keys[x]];
}
var queryString = "#";
var updatedKeys = Object.keys(currentParams);
for( var y in updatedKeys) {
queryString += updatedKeys[y] + "=" + currentParams[updatedKeys[y]] + "&";
}
window.location.hash = queryString.slice(0, -1);
}
module.exports = {
get: getQueryParams,
update: updateQueryParams,
};
\ No newline at end of file
function StorageHandler(SOL_CACHE_FILE_PREFIX) {
this.sync = function() {
if (typeof chrome === 'undefined' || !chrome || !chrome.storage || !chrome.storage.sync) return;
var obj = {};
var done = false;
var count = 0
var dont = 0;
function check(key){
chrome.storage.sync.get( key, function(resp){
console.log("comparing to cloud", key, resp);
if (typeof resp[key] != 'undefined' && obj[key] !== resp[key] && confirm("Overwrite '" + fileNameFromKey(key) + "'? Click Ok to overwrite local file with file from cloud. Cancel will push your local file to the cloud.")) {
console.log("Overwriting", key );
localStorage.setItem( key, resp[key] );
updateFiles();
} else {
console.log( "add to obj", obj, key);
obj[key] = localStorage[key];
}
done++;
if (done >= count) chrome.storage.sync.set( obj, function(){
console.log( "updated cloud files with: ", obj, this, arguments);
})
})
}
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;
count++;
check(y);
}
};
}
module.exports = StorageHandler;
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