auto update accounts list

pull/1/head
yann300 6 years ago
parent 34129057b3
commit 2f48c501cc
  1. 154
      src/app/tabs/run-tab.js

@ -25,7 +25,7 @@ function runTab (opts, localRegistry) {
VARIABLES VARIABLES
--------------------------- */ --------------------------- */
var self = this var self = this
var event = new EventManager() self.event = new EventManager()
self._view = {} self._view = {}
self.data = { self.data = {
count: 0, count: 0,
@ -80,7 +80,7 @@ function runTab (opts, localRegistry) {
self._view.recorderCount = yo`<span>0</span>` self._view.recorderCount = yo`<span>0</span>`
self._view.instanceContainer = yo`<div class="${css.instanceContainer}"></div>` self._view.instanceContainer = yo`<div class="${css.instanceContainer}"></div>`
self._view.clearInstanceElement = yo` self._view.clearInstanceElement = yo`
<i class="${css.clearinstance} ${css.icon} fa fa-trash" onclick=${() => clearInstanceList(self)} <i class="${css.clearinstance} ${css.icon} fa fa-trash" onclick=${() => self.event.trigger('clearInstance', [])}
title="Clear instances list and reset recorder" aria-hidden="true"> title="Clear instances list and reset recorder" aria-hidden="true">
</i>` </i>`
self._view.instanceContainerTitle = yo` self._view.instanceContainerTitle = yo`
@ -95,7 +95,7 @@ function runTab (opts, localRegistry) {
</div>` </div>`
var container = yo`<div class="${css.runTabView}" id="runTabView" ></div>` var container = yo`<div class="${css.runTabView}" id="runTabView" ></div>`
var recorderInterface = makeRecorder(localRegistry, event, self) var recorderInterface = makeRecorder(localRegistry, self.event, self)
self._view.collapsedView = yo` self._view.collapsedView = yo`
<div class=${css.recorderCollapsedView}> <div class=${css.recorderCollapsedView}>
@ -136,82 +136,43 @@ function runTab (opts, localRegistry) {
var el = yo` var el = yo`
<div> <div>
${settings(container, self)} ${settings(container, self)}
${contractDropdown(event, self)} ${contractDropdown(self.event, self)}
${recorderCard.render()} ${recorderCard.render()}
${self._view.instanceContainer} ${self._view.instanceContainer}
</div> </div>
` `
container.appendChild(el) container.appendChild(el)
/* -------------------------
HELPER FUNCTIONS
--------------------------- */
// DROPDOWN
var selectExEnv = el.querySelector('#selectExEnvOptions')
function clearInstanceList (self) {
event.trigger('clearInstance', [])
}
function setFinalContext () {
// set the final context. Cause it is possible that this is not the one we've originaly selected
selectExEnv.value = executionContext.getProvider()
fillAccountsList(el, self)
event.trigger('clearInstance', [])
}
selectExEnv.addEventListener('change', function (event) {
let context = selectExEnv.options[selectExEnv.selectedIndex].value
executionContext.executionContextChange(context, null, () => {
modalDialogCustom.confirm(null, 'Are you sure you want to connect to an ethereum node?', () => {
modalDialogCustom.prompt(null, 'Web3 Provider Endpoint', 'http://localhost:8545', (target) => {
executionContext.setProviderFromEndpoint(target, context, (alertMsg) => {
if (alertMsg) {
modalDialogCustom.alert(alertMsg)
}
setFinalContext()
})
}, setFinalContext)
}, setFinalContext)
}, (alertMsg) => {
modalDialogCustom.alert(alertMsg)
}, setFinalContext)
})
selectExEnv.value = executionContext.getProvider()
executionContext.event.register('contextChanged', (context, silent) => {
setFinalContext()
})
fillAccountsList(el, self)
setInterval(() => {
updateAccountBalances(container, self)
}, 10000)
event.register('clearInstance', () => {
var instanceContainer = self._view.instanceContainer
var instanceContainerTitle = self._view.instanceContainerTitle
instanceContainer.innerHTML = '' // clear the instances list
instanceContainer.appendChild(instanceContainerTitle)
instanceContainer.appendChild(self._view.noInstancesText)
})
return { render () { return container } } return { render () { return container } }
} }
var accountListCallId = 0
var loadedAccounts = {}
function fillAccountsList (container, self) { function fillAccountsList (container, self) {
var $txOrigin = $(container.querySelector('#txorigin')) accountListCallId++
$txOrigin.empty() (function (callid) {
var txOrigin = container.querySelector('#txorigin')
self._deps.udapp.getAccounts((err, accounts) => { self._deps.udapp.getAccounts((err, accounts) => {
$txOrigin.empty() if (accountListCallId > callid) return
accountListCallId++
if (err) { addTooltip(`Cannot get account list: ${err}`) } if (err) { addTooltip(`Cannot get account list: ${err}`) }
if (accounts && accounts[0]) { for (var loadedaddress in loadedAccounts) {
for (var a in accounts) { $txOrigin.append($('<option />').val(accounts[a]).text(accounts[a])) } if (accounts.indexOf(loadedaddress) === -1) {
$txOrigin.val(accounts[0]) txOrigin.removeChild(txOrigin.querySelector('option[value="' + loadedaddress + '"]'))
} else { delete loadedAccounts[loadedaddress]
$txOrigin.val('unknown')
} }
}
for (var i in accounts) {
var address = accounts[i]
if (!loadedAccounts[address]) {
txOrigin.appendChild(yo`<option value="${address}" >${address}</option>`)
loadedAccounts[address] = 1
}
}
txOrigin.setAttribute('value', accounts[0])
}) })
})(accountListCallId)
} }
function updateAccountBalances (container, self) { function updateAccountBalances (container, self) {
@ -514,15 +475,22 @@ function contractDropdown (events, self) {
function settings (container, self) { function settings (container, self) {
// VARIABLES // VARIABLES
var net = yo`<span class=${css.network}></span>` var net = yo`<span class=${css.network}></span>`
const updateNetwork = () => { var networkcallid = 0
const updateNetwork = (cb) => {
networkcallid++
(function (callid) {
executionContext.detectNetwork((err, { id, name } = {}) => { executionContext.detectNetwork((err, { id, name } = {}) => {
if (networkcallid > callid) return
networkcallid++
if (err) { if (err) {
console.error(err) console.error(err)
net.innerHTML = 'can\'t detect network ' net.innerHTML = 'can\'t detect network '
} else { } else {
net.innerHTML = `<i class="${css.networkItem} fa fa-plug" aria-hidden="true"></i> ${name} (${id || '-'})` net.innerHTML = `<i class="${css.networkItem} fa fa-plug" aria-hidden="true"></i> ${name} (${id || '-'})`
} }
if (cb) cb(err, {id, name})
}) })
})(networkcallid)
} }
var environmentEl = yo` var environmentEl = yo`
<div class="${css.crow}"> <div class="${css.crow}">
@ -531,7 +499,7 @@ function settings (container, self) {
</div> </div>
<div class=${css.environment}> <div class=${css.environment}>
${net} ${net}
<select id="selectExEnvOptions" onchange=${updateNetwork} class="${css.select}"> <select id="selectExEnvOptions" onchange=${() => { updateNetwork() }} class="${css.select}">
<option id="vm-mode" <option id="vm-mode"
title="Execution environment does not connect to any node, everything is local and in memory only." title="Execution environment does not connect to any node, everything is local and in memory only."
value="vm" checked name="executionContext"> JavaScript VM value="vm" checked name="executionContext"> JavaScript VM
@ -596,7 +564,57 @@ function settings (container, self) {
updateAccountBalances(container, self) updateAccountBalances(container, self)
}) })
setInterval(updateNetwork, 5000) // DROPDOWN
var selectExEnv = environmentEl.querySelector('#selectExEnvOptions')
function setFinalContext () {
// set the final context. Cause it is possible that this is not the one we've originaly selected
selectExEnv.value = executionContext.getProvider()
self.event.trigger('clearInstance', [])
updateNetwork()
fillAccountsList(el, self)
}
self.event.register('clearInstance', () => {
var instanceContainer = self._view.instanceContainer
var instanceContainerTitle = self._view.instanceContainerTitle
instanceContainer.innerHTML = '' // clear the instances list
instanceContainer.appendChild(instanceContainerTitle)
instanceContainer.appendChild(self._view.noInstancesText)
})
selectExEnv.addEventListener('change', function (event) {
let context = selectExEnv.options[selectExEnv.selectedIndex].value
executionContext.executionContextChange(context, null, () => {
modalDialogCustom.confirm(null, 'Are you sure you want to connect to an ethereum node?', () => {
modalDialogCustom.prompt(null, 'Web3 Provider Endpoint', 'http://localhost:8545', (target) => {
executionContext.setProviderFromEndpoint(target, context, (alertMsg) => {
if (alertMsg) {
modalDialogCustom.alert(alertMsg)
}
setFinalContext()
})
}, setFinalContext)
}, setFinalContext)
}, (alertMsg) => {
modalDialogCustom.alert(alertMsg)
}, setFinalContext)
})
selectExEnv.value = executionContext.getProvider()
executionContext.event.register('contextChanged', (context, silent) => {
setFinalContext()
})
setInterval(() => {
updateNetwork()
fillAccountsList(el, self)
}, 5000)
setInterval(() => {
updateAccountBalances(container, self)
}, 10000)
function newAccount () { function newAccount () {
self._deps.udapp.newAccount('', (error, address) => { self._deps.udapp.newAccount('', (error, address) => {
if (!error) { if (!error) {

Loading…
Cancel
Save