diff --git a/src/app.js b/src/app.js index 04158f3e32..745ba439ad 100644 --- a/src/app.js +++ b/src/app.js @@ -1,4 +1,4 @@ -/* global alert, confirm, prompt, Option, Worker, chrome */ +/* global alert, confirm, prompt, FileReader, Option, Worker, chrome */ 'use strict' var $ = require('jquery') @@ -10,6 +10,7 @@ var GistHandler = require('./app/gist-handler') var gistHandler = new GistHandler() var Storage = require('./app/storage') +var Files = require('./app/files') var Config = require('./app/config') var Editor = require('./app/editor') var Renderer = require('./app/renderer') @@ -22,6 +23,8 @@ var EventManager = require('./lib/eventManager') var StaticAnalysis = require('./app/staticanalysis/staticAnalysisView') var OffsetToLineColumnConverter = require('./lib/offsetToLineColumnConverter') +var examples = require('./app/example-contracts') + // The event listener needs to be registered as early as possible, because the // parent will send the message upon the "load" event. var filesToLoad = null @@ -40,16 +43,31 @@ var run = function () { var self = this this.event = new EventManager() var storage = new Storage() + var files = new Files(storage) var config = new Config(storage) + var currentFile + + // return all the files, except the temporary/readonly ones + function packageFiles () { + return files.list() + .filter(function (path) { if (!files.isReadOnly(path)) { return path } }) + .map(function (path) { return { content: files.get(path) } }) + } + + function createNonClashingName (path) { + var counter = '' + while (files.exists(path + counter)) { + counter = (counter | 0) + 1 + } + return path + counter + } // Add files received from remote instance (i.e. another browser-solidity) function loadFiles (files) { for (var f in files) { - storage.loadFile(f, files[f].content) + files.set(createNonClashingName(f), files[f].content) } - // Set the first file as current tab - editor.setCacheFile(Object.keys(files)[0]) - updateFiles() + switchToNextFile() } // Replace early callback with instant response @@ -87,6 +105,11 @@ var run = function () { }) }) + // insert ballot contract if there are no files available + if (!loadingFromGist && (files.list().length === 0)) { + files.set(examples.ballot.name, examples.ballot.content) + } + // ----------------- Chrome cloud storage sync -------------------- function chromeCloudSync () { @@ -103,11 +126,11 @@ var run = function () { console.log('comparing to cloud', key, resp) if (typeof resp[key] !== 'undefined' && obj[key] !== resp[key] && confirm('Overwrite "' + key + '"? Click Ok to overwrite local file with file from cloud. Cancel will push your local file to the cloud.')) { console.log('Overwriting', key) - storage.set(key, resp[key]) + files.set(key, resp[key]) updateFiles() } else { console.log('add to obj', obj, key) - obj[key] = storage.get(key) + obj[key] = files.get(key) } done++ if (done >= count) { @@ -118,9 +141,9 @@ var run = function () { }) } - for (var y in storage.keys()) { + for (var y in files.list()) { console.log('checking', y) - obj[y] = storage.get(y) + obj[y] = files.get(y) count++ check(y) } @@ -131,7 +154,7 @@ var run = function () { // ----------------- editor ---------------------- - var editor = new Editor(loadingFromGist, storage) + var editor = new Editor() // ----------------- tabbed menu ------------------- $('#options li').click(function (ev) { @@ -155,7 +178,7 @@ var run = function () { $('#gist').click(function () { if (confirm('Are you sure you want to publish all your files anonymously as a public gist on github.com?')) { - var files = editor.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=' + queryParams.get().version + '&optimize=' + queryParams.get().optimize + '&gist=' $.ajax({ @@ -182,7 +205,7 @@ var run = function () { if (target === null) { return } - var files = editor.packageFiles() + var files = packageFiles() $('