remix-project mirror
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
remix-project/src/universal-dapp-ui.js

122 lines
3.8 KiB

/* global */
'use strict'
var $ = require('jquery')
var yo = require('yo-yo')
var helper = require('./lib/helper')
var copyToClipboard = require('./app/ui/copy-to-clipboard')
var css = require('./universal-dapp-styles')
7 years ago
var MultiParamManager = require('./multiParamManager')
/*
trigger debugRequested
*/
function UniversalDAppUI (udapp, opts = {}) {
var self = this
this.udapp = udapp
self.el = yo`<div class=${css.udapp}></div>`
}
UniversalDAppUI.prototype.reset = function () {
this.el.innerHTML = ''
}
UniversalDAppUI.prototype.renderInstance = function (contract, address, contractName) {
var abi = this.udapp.getABI(contract)
return this.renderInstanceFromABI(abi, address, contractName)
}
// TODO this function was named before "appendChild".
// this will render an instance: contract name, contract address, and all the public functions
// basically this has to be called for the "atAddress" (line 393) and when a contract creation succeed
// this returns a DOM element
UniversalDAppUI.prototype.renderInstanceFromABI = function (contractABI, address, contractName) {
var self = this
address = (address.slice(0, 2) === '0x' ? '' : '0x') + address.toString('hex')
var instance = yo`<div class="instance ${css.instance}" id="instance${address}"></div>`
var context = self.udapp.context()
var shortAddress = helper.shortenAddress(address)
var title = yo`
<div class="${css.title}" onclick=${toggleClass}>
<div class="${css.titleText}"> ${contractName} at ${shortAddress} (${context}) </div>
${copyToClipboard(() => address)}
</div>`
if (self.udapp.removable_instances) {
var close = yo`<div class="${css.udappClose}" onclick=${remove}><i class="${css.closeIcon} fa fa-close" aria-hidden="true"></i></div>`
instance.appendChild(close)
}
function remove () {
var instanceContainer = document.querySelector('[class^="instanceContainer"]')
var noInstancesText = yo`<div class="${css.noInstancesText}">Currently you have no contract instances.</div>`
instanceContainer.appendChild(noInstancesText)
instance.remove()
}
function toggleClass () {
$(instance).toggleClass(`${css.hidesub}`)
}
instance.appendChild(title)
// Add the fallback function
var fallback = self.udapp.getFallbackInterface(contractABI)
if (fallback) {
instance.appendChild(this.getCallButton({
funABI: fallback,
address: address,
contractAbi: contractABI,
contractName: contractName
}))
}
$.each(contractABI, (i, funABI) => {
if (funABI.type !== 'function') {
return
}
// @todo getData cannot be used with overloaded functions
instance.appendChild(this.getCallButton({
funABI: funABI,
address: address,
contractAbi: contractABI,
contractName: contractName
}))
})
return instance
}
// TODO this is used by renderInstance when a new instance is displayed.
// this returns a DOM element.
UniversalDAppUI.prototype.getCallButton = function (args) {
var self = this
// args.funABI, args.address [fun only]
// args.contractName [constr only]
var lookupOnly = args.funABI.constant
7 years ago
var outputOverride = yo`<div class=${css.value}></div>` // show return value
7 years ago
function clickButton (valArr, inputsValues) {
self.udapp.call(true, args, inputsValues, lookupOnly, (decoded) => {
outputOverride.innerHTML = ''
outputOverride.appendChild(decoded)
})
}
var multiParamManager = new MultiParamManager(lookupOnly, args.funABI, (valArray, inputsValues, domEl) => {
clickButton(valArray, inputsValues, domEl)
7 years ago
}, self.udapp.getInputs(args.funABI))
7 years ago
var contractActionsContainer = yo`<div class="${css.contractActionsContainer}" >${multiParamManager.render()}</div>`
contractActionsContainer.appendChild(outputOverride)
7 years ago
return contractActionsContainer
}
module.exports = UniversalDAppUI