Merge pull request #1396 from ethereum/refactor8

Clean app.js (8)
pull/1/head
yann300 6 years ago committed by GitHub
commit 5f0878386e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 187
      src/app.js
  2. 6
      src/app/execution/txLogger.js
  3. 1
      src/app/files/fileManager.js
  4. 2
      src/app/tabs/compile-tab.js
  5. 2
      src/app/tabs/settings-tab.js

@ -117,54 +117,60 @@ var css = csjs`
class App { class App {
constructor (api = {}, events = {}, opts = {}) { constructor (api = {}, events = {}, opts = {}) {
var self = this var self = this
self._api = {} self._components = {}
registry.put({api: self, name: 'app'}) registry.put({api: self, name: 'app'})
var fileStorage = new Storage('sol:') var fileStorage = new Storage('sol:')
registry.put({api: fileStorage, name: 'fileStorage'}) registry.put({api: fileStorage, name: 'fileStorage'})
var configStorage = new Storage('config:') var configStorage = new Storage('config:')
registry.put({api: configStorage, name: 'configStorage'}) registry.put({api: configStorage, name: 'configStorage'})
self._api.config = new Config(fileStorage) self._components.config = new Config(fileStorage)
registry.put({api: self._api.config, name: 'config'}) registry.put({api: self._components.config, name: 'config'})
executionContext.init(self._api.config) executionContext.init(self._components.config)
executionContext.listenOnLastBlock() executionContext.listenOnLastBlock()
self._api.filesProviders = {}
self._api.filesProviders['browser'] = new Browserfiles(fileStorage) self._components.compilerImport = new CompilerImport()
self._api.filesProviders['config'] = new BrowserfilesTree('config', configStorage) registry.put({api: self._components.compilerImport, name: 'compilerimport'})
self._api.filesProviders['config'].init() self._components.gistHandler = new GistHandler()
registry.put({api: self._api.filesProviders['browser'], name: 'fileproviders/browser'})
registry.put({api: self._api.filesProviders['config'], name: 'fileproviders/config'}) self._components.filesProviders = {}
self._components.filesProviders['browser'] = new Browserfiles(fileStorage)
self._components.filesProviders['config'] = new BrowserfilesTree('config', configStorage)
self._components.filesProviders['config'].init()
registry.put({api: self._components.filesProviders['browser'], name: 'fileproviders/browser'})
registry.put({api: self._components.filesProviders['config'], name: 'fileproviders/config'})
var remixd = new Remixd() var remixd = new Remixd()
registry.put({api: remixd, name: 'remixd/config'}) registry.put({api: remixd, name: 'remixd'})
remixd.event.register('system', (message) => { remixd.event.register('system', (message) => {
if (message.error) toolTip(message.error) if (message.error) toolTip(message.error)
}) })
self._api.filesProviders['localhost'] = new SharedFolder(remixd)
self._api.filesProviders['swarm'] = new BasicReadOnlyExplorer('swarm') self._components.filesProviders['localhost'] = new SharedFolder(remixd)
self._api.filesProviders['github'] = new BasicReadOnlyExplorer('github') self._components.filesProviders['swarm'] = new BasicReadOnlyExplorer('swarm')
self._api.filesProviders['gist'] = new NotPersistedExplorer('gist') self._components.filesProviders['github'] = new BasicReadOnlyExplorer('github')
self._api.filesProviders['ipfs'] = new BasicReadOnlyExplorer('ipfs') self._components.filesProviders['gist'] = new NotPersistedExplorer('gist')
registry.put({api: self._api.filesProviders['localhost'], name: 'fileproviders/localhost'}) self._components.filesProviders['ipfs'] = new BasicReadOnlyExplorer('ipfs')
registry.put({api: self._api.filesProviders['swarm'], name: 'fileproviders/swarm'}) registry.put({api: self._components.filesProviders['localhost'], name: 'fileproviders/localhost'})
registry.put({api: self._api.filesProviders['github'], name: 'fileproviders/github'}) registry.put({api: self._components.filesProviders['swarm'], name: 'fileproviders/swarm'})
registry.put({api: self._api.filesProviders['gist'], name: 'fileproviders/gist'}) registry.put({api: self._components.filesProviders['github'], name: 'fileproviders/github'})
registry.put({api: self._api.filesProviders['ipfs'], name: 'fileproviders/ipfs'}) registry.put({api: self._components.filesProviders['gist'], name: 'fileproviders/gist'})
registry.put({api: self._api.filesProviders, name: 'fileproviders'}) registry.put({api: self._components.filesProviders['ipfs'], name: 'fileproviders/ipfs'})
registry.put({api: self._components.filesProviders, name: 'fileproviders'})
self._view = {} self._view = {}
self._components = {}
self._components.compilerImport = new CompilerImport()
registry.put({api: self._components.compilerImport, name: 'compilerimport'})
self._components.gistHandler = new GistHandler()
self.data = { self.data = {
_layout: { _layout: {
right: { right: {
offset: self._api.config.get('right-offset') || 400, offset: self._components.config.get('right-offset') || 400,
show: true show: true
}, // @TODO: adapt sizes proportionally to browser window size }, // @TODO: adapt sizes proportionally to browser window size
left: { left: {
offset: self._api.config.get('left-offset') || 200, offset: self._components.config.get('left-offset') || 200,
show: true show: true
} }
} }
@ -179,7 +185,7 @@ class App {
if (layout.show) delta = layout.offset if (layout.show) delta = layout.offset
else delta = 0 else delta = 0
} else { } else {
self._api.config.set(`${direction}-offset`, delta) self._components.config.set(`${direction}-offset`, delta)
layout.offset = delta layout.offset = delta
} }
} }
@ -232,7 +238,7 @@ class App {
self._components.fileManager.saveCurrentFile() self._components.fileManager.saveCurrentFile()
self._components.editor.clearAnnotations() self._components.editor.clearAnnotations()
var currentFile = self._api.config.get('currentFile') var currentFile = self._components.config.get('currentFile')
if (currentFile) { if (currentFile) {
if (/.(.sol)$/.exec(currentFile)) { if (/.(.sol)$/.exec(currentFile)) {
// only compile *.sol file. // only compile *.sol file.
@ -259,9 +265,9 @@ class App {
self.event.trigger('debuggingRequested', []) self.event.trigger('debuggingRequested', [])
self._view.transactionDebugger.debug(txHash) self._view.transactionDebugger.debug(txHash)
} }
loadFromGist (gistId) { loadFromGist (params) {
const self = this const self = this
return self._components.gistHandler.handleLoad(gistId, function (gistId) { return self._components.gistHandler.handleLoad(params, function (gistId) {
request.get({ request.get({
url: `https://api.github.com/gists/${gistId}`, url: `https://api.github.com/gists/${gistId}`,
json: true json: true
@ -271,7 +277,7 @@ class App {
return return
} }
self.loadFiles(data.files, 'gist', (errorLoadingFile) => { self.loadFiles(data.files, 'gist', (errorLoadingFile) => {
if (!errorLoadingFile) self._api.filesProviders['gist'].id = gistId if (!errorLoadingFile) self._components.filesProviders['gist'].id = gistId
}) })
}) })
}) })
@ -281,14 +287,14 @@ class App {
if (!fileProvider) fileProvider = 'browser' if (!fileProvider) fileProvider = 'browser'
async.each(Object.keys(filesSet), (file, callback) => { async.each(Object.keys(filesSet), (file, callback) => {
helper.createNonClashingName(file, self._api.filesProviders[fileProvider], helper.createNonClashingName(file, self._components.filesProviders[fileProvider],
(error, name) => { (error, name) => {
if (error) { if (error) {
modalDialogCustom.alert('Unexpected error loading the file ' + error) modalDialogCustom.alert('Unexpected error loading the file ' + error)
} else if (helper.checkSpecialChars(name)) { } else if (helper.checkSpecialChars(name)) {
modalDialogCustom.alert('Special characters are not allowed') modalDialogCustom.alert('Special characters are not allowed')
} else { } else {
self._api.filesProviders[fileProvider].set(name, filesSet[file].content) self._components.filesProviders[fileProvider].set(name, filesSet[file].content)
} }
callback() callback()
}) })
@ -305,8 +311,8 @@ class App {
}, },
(error, content, cleanUrl, type, url) => { (error, content, cleanUrl, type, url) => {
if (!error) { if (!error) {
if (self._api.filesProviders[type]) { if (self._components.filesProviders[type]) {
self._api.filesProviders[type].addReadOnly(cleanUrl, content, url) self._components.filesProviders[type].addReadOnly(cleanUrl, content, url)
} }
cb(null, content) cb(null, content)
} else { } else {
@ -476,12 +482,9 @@ Please make a backup of your contracts and start using http://remix.ethereum.org
this module basically listen on user input (from terminal && editor) this module basically listen on user input (from terminal && editor)
and interpret them as commands and interpret them as commands
*/ */
var cmdInterpreter = new CommandInterpreter() var cmdInterpreter = new CommandInterpreter() // @TODO: put into editorpanel
registry.put({api: cmdInterpreter, name: 'cmdinterpreter'}) registry.put({api: cmdInterpreter, name: 'cmdinterpreter'})
var config = self._api.config
var filesProviders = self._api.filesProviders
// ----------------- file manager ---------------------------- // ----------------- file manager ----------------------------
self._components.fileManager = new FileManager() self._components.fileManager = new FileManager()
@ -503,8 +506,6 @@ Please make a backup of your contracts and start using http://remix.ethereum.org
this._view.centerpanel.appendChild(this._components.editorpanel.render()) this._view.centerpanel.appendChild(this._components.editorpanel.render())
var queryParams = new QueryParams()
// 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
@ -528,47 +529,11 @@ Please make a backup of your contracts and start using http://remix.ethereum.org
self.loadFiles(filesToLoad) self.loadFiles(filesToLoad)
} }
var loadingFromGist = self.loadFromGist(queryParams.get())
// insert ballot contract if there are no files available
if (!loadingFromGist) {
filesProviders['browser'].resolveDirectory('browser', (error, filesList) => {
if (error) console.error(error)
if (Object.keys(filesList).length === 0) {
if (!filesProviders['browser'].set(examples.ballot.name, examples.ballot.content)) {
modalDialogCustom.alert('Failed to store example contract in browser. Remix will not work properly. Please ensure Remix has access to LocalStorage. Safari in Private mode is known not to work.')
} else {
filesProviders['browser'].set(examples.ballot_test.name, examples.ballot_test.content)
}
}
})
}
window.syncStorage = chromeCloudStorageSync
chromeCloudStorageSync()
// ---------------- FilePanel -------------------- // ---------------- FilePanel --------------------
var filePanel = new FilePanel() var filePanel = new FilePanel()
self._view.leftpanel.appendChild(filePanel.render())
// TODO this should happen inside file-panel.js
var filepanelContainer = document.querySelector('#filepanel')
filepanelContainer.appendChild(filePanel.render())
filePanel.event.register('resize', delta => self._adjustLayout('left', delta)) filePanel.event.register('resize', delta => self._adjustLayout('left', delta))
var previouslyOpenedFile = config.get('currentFile')
if (previouslyOpenedFile) {
filesProviders['browser'].get(previouslyOpenedFile, (error, content) => {
if (!error && content) {
fileManager.switchFile(previouslyOpenedFile)
} else {
fileManager.switchFile()
}
})
} else {
fileManager.switchFile()
}
// ----------------- Renderer ----------------- // ----------------- Renderer -----------------
var renderer = new Renderer() var renderer = new Renderer()
registry.put({api: renderer, name: 'renderer'}) registry.put({api: renderer, name: 'renderer'})
@ -586,35 +551,19 @@ Please make a backup of your contracts and start using http://remix.ethereum.org
var node = document.getElementById('staticanalysisView') var node = document.getElementById('staticanalysisView')
node.insertBefore(staticanalysis.render(), node.childNodes[0]) node.insertBefore(staticanalysis.render(), node.childNodes[0])
// ----------------- editor resize ---------------
function onResize () {
editor.resize(document.querySelector('#editorWrap').checked)
}
onResize()
self._view.el.addEventListener('change', onResize)
document.querySelector('#editorWrap').addEventListener('change', onResize)
// ----------------- Debugger ----------------- // ----------------- Debugger -----------------
var sourceHighlighter = new SourceHighlighter() self._view.transactionDebugger = new Debugger('#debugger', new SourceHighlighter())
self._view.transactionDebugger = new Debugger('#debugger', sourceHighlighter)
self._view.transactionDebugger.addProvider('vm', executionContext.vm()) self._view.transactionDebugger.addProvider('vm', executionContext.vm())
self._view.transactionDebugger.addProvider('injected', executionContext.internalWeb3()) self._view.transactionDebugger.addProvider('injected', executionContext.internalWeb3())
self._view.transactionDebugger.addProvider('web3', executionContext.internalWeb3()) self._view.transactionDebugger.addProvider('web3', executionContext.internalWeb3())
self._view.transactionDebugger.switchProvider(executionContext.getProvider()) self._view.transactionDebugger.switchProvider(executionContext.getProvider())
var txLogger = new TxLogger() var txLogger = new TxLogger() // eslint-disable-line
txLogger.event.register('debugRequested', (hash) => {
self.startdebugging(hash)
})
var previousInput = '' var previousInput = ''
var saveTimeout = null var saveTimeout = null
function editorOnChange () { function editorOnChange () {
var currentFile = config.get('currentFile') var currentFile = self._components.config.get('currentFile')
if (!currentFile) { if (!currentFile) {
return return
} }
@ -638,18 +587,23 @@ Please make a backup of your contracts and start using http://remix.ethereum.org
}, 5000) }, 5000)
} }
// auto save the file when content changed
editor.event.register('contentChanged', editorOnChange) editor.event.register('contentChanged', editorOnChange)
// in order to save the file when switching // save the file when switching
editor.event.register('sessionSwitched', editorOnChange) editor.event.register('sessionSwitched', editorOnChange)
executionContext.event.register('contextChanged', this, function (context) { executionContext.event.register('contextChanged', this, function (context) {
self.runCompiler() self.runCompiler()
}) })
// rerun the compiler when the environement changed
executionContext.event.register('web3EndpointChanged', this, function (context) { executionContext.event.register('web3EndpointChanged', this, function (context) {
self.runCompiler() self.runCompiler()
}) })
var queryParams = new QueryParams()
// check init query parameters from the URL once the compiler is loaded
compiler.event.register('compilerLoaded', this, function (version) { compiler.event.register('compilerLoaded', this, function (version) {
previousInput = '' previousInput = ''
self.runCompiler() self.runCompiler()
@ -677,4 +631,37 @@ Please make a backup of your contracts and start using http://remix.ethereum.org
self.startdebugging(queryParams.get().debugtx) self.startdebugging(queryParams.get().debugtx)
} }
}) })
// chrome app
window.syncStorage = chromeCloudStorageSync
chromeCloudStorageSync()
var loadingFromGist = self.loadFromGist(queryParams.get())
if (!loadingFromGist) {
// insert ballot contract if there are no files to show
self._components.filesProviders['browser'].resolveDirectory('browser', (error, filesList) => {
if (error) console.error(error)
if (Object.keys(filesList).length === 0) {
if (!self._components.filesProviders['browser'].set(examples.ballot.name, examples.ballot.content)) {
modalDialogCustom.alert('Failed to store example contract in browser. Remix will not work properly. Please ensure Remix has access to LocalStorage. Safari in Private mode is known not to work.')
} else {
self._components.filesProviders['browser'].set(examples.ballot_test.name, examples.ballot_test.content)
}
}
})
}
// Open last opened file
var previouslyOpenedFile = self._components.config.get('currentFile')
if (previouslyOpenedFile) {
self._components.filesProviders['browser'].get(previouslyOpenedFile, (error, content) => {
if (!error && content) {
fileManager.switchFile(previouslyOpenedFile)
} else {
fileManager.switchFile()
}
})
} else {
fileManager.switchFile()
}
} }

@ -120,7 +120,6 @@ var css = csjs`
}` }`
/** /**
* This just export a function that register to `newTransaction` and forward them to the logger. * This just export a function that register to `newTransaction` and forward them to the logger.
* Emit debugRequested
* *
*/ */
class TxLogger { class TxLogger {
@ -141,7 +140,8 @@ class TxLogger {
editorPanel: this._components.registry.get('editorpanel').api, editorPanel: this._components.registry.get('editorpanel').api,
txListener: this._components.registry.get('txlistener').api, txListener: this._components.registry.get('txlistener').api,
eventsDecoder: this._components.registry.get('eventsdecoder').api, eventsDecoder: this._components.registry.get('eventsdecoder').api,
compiler: this._components.registry.get('compiler').api compiler: this._components.registry.get('compiler').api,
app: this._components.registry.get('app').api
} }
this.logKnownTX = this._deps.editorPanel.registerCommand('knownTransaction', (args, cmds, append) => { this.logKnownTX = this._deps.editorPanel.registerCommand('knownTransaction', (args, cmds, append) => {
@ -205,7 +205,7 @@ function debug (e, data, self) {
if (data.tx.isCall && data.tx.envMode !== 'vm') { if (data.tx.isCall && data.tx.envMode !== 'vm') {
modalDialog.alert('Cannot debug this call. Debugging calls is only possible in JavaScript VM mode.') modalDialog.alert('Cannot debug this call. Debugging calls is only possible in JavaScript VM mode.')
} else { } else {
self.event.trigger('debugRequested', [data.tx.hash]) self._deps.app.startdebugging(data.tx.hash)
} }
} }

@ -85,6 +85,7 @@ class FileManager {
self._deps.editor.discardCurrentSession() self._deps.editor.discardCurrentSession()
delete this.tabbedFiles[path] delete this.tabbedFiles[path]
this.refreshTabs() this.refreshTabs()
this.switchFile()
} }
// Display files that have already been selected // Display files that have already been selected

@ -127,7 +127,7 @@ module.exports = class CompileTab {
var error = false var error = false
if (data['error']) { if (data['error']) {
error = true error = true
self._deps.renderer.error(data['error'].formattedMessage, self._view.errorContainer, {type: data['error'].severity}) self._deps.renderer.error(data['error'].formattedMessage, self._view.errorContainer, {type: data['error'].severity || 'error'})
} }
if (data.errors && data.errors.length) { if (data.errors && data.errors.length) {
error = true error = true

@ -111,7 +111,7 @@ module.exports = class SettingsTab {
<span class="${css.checkboxText}">Always use Ethereum VM at Load</span> <span class="${css.checkboxText}">Always use Ethereum VM at Load</span>
</div> </div>
<div class="${css.crow}"> <div class="${css.crow}">
<div><input id="editorWrap" type="checkbox"></div> <div><input id="editorWrap" type="checkbox" onchange=${function () { self._deps.editor.resize(this.checked) }}></div>
<span class="${css.checkboxText}">Text Wrap</span> <span class="${css.checkboxText}">Text Wrap</span>
</div> </div>
<div class="${css.crow}"> <div class="${css.crow}">

Loading…
Cancel
Save