diff --git a/src/app.js b/src/app.js index 04158f3e32..0b4b70fde1 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,28 @@ 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 + + function packageFiles () { + return files.list().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 +102,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 () { @@ -131,7 +151,7 @@ var run = function () { // ----------------- editor ---------------------- - var editor = new Editor(loadingFromGist, storage) + var editor = new Editor() // ----------------- tabbed menu ------------------- $('#options li').click(function (ev) { @@ -155,7 +175,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 +202,7 @@ var run = function () { if (target === null) { return } - var files = editor.packageFiles() + var files = packageFiles() $('