'use strict'
var yo = require('yo-yo')
var css = require('./universal-dapp-styles')
// var helper = require('../remix/remix-lib/src/execution/txHelper')
class MultiParamManager {
/**
*
* @param {bool} lookupOnly
* @param {Object} funABI
* @param {Function} clickMultiCallBack
* @param {string} inputs
* @param {string} title
*
*/
constructor (lookupOnly, funABI, clickCallBack, inputs, title) {
this.lookupOnly = lookupOnly
this.funABI = funABI
this.clickCallBack = clickCallBack
this.inputs = inputs
this.title = title
this.basicInputField
this.multiFields
}
switchMethodViewOn () {
this.contractActionsContainerSingle.style.display = 'none'
this.contractActionsContainerMulti.style.display = 'flex'
this.makeMultiVal()
}
switchMethodViewOff () {
this.contractActionsContainerSingle.style.display = 'flex'
this.contractActionsContainerMulti.style.display = 'none'
if (this.getMultiValsString()) this.basicInputField.value = this.getMultiValsString()
}
getMultiValsString () {
var valArray = this.multiFields.querySelectorAll('input')
var notEmpty = 0
var ret = ''
for (var k = 0; k < valArray.length; k++) {
var elA = valArray[k].value
if (elA) notEmpty++
}
if (notEmpty) {
for (var j = 0; j < valArray.length; j++) {
if (ret !== '') ret += ','
var elVal = valArray[j].value
elVal = elVal.replace(/(^|,\s+|,)(\w+|)(\s+,|,|$)/g, '$1"$2"$3') // replace non quoted string or number by quoted string
ret += elVal
}
}
return ret
}
emptyInputs () {
var valArray = this.multiFields.querySelectorAll('input')
for (var k = 0; k < valArray.length; k++) {
valArray[k].value = ''
}
this.basicInputField.value = ''
}
makeMultiVal () {
var inputString = this.basicInputField.value
if (inputString) {
inputString = inputString.replace(/(^|,\s+|,)(\w+|)(\s+,|,|$)/g, '$1"$2"$3')
var inputJSON = JSON.parse('[' + inputString + ']')
var multiInputs = this.multiFields.querySelectorAll('input')
for (var k = 0; k < multiInputs.length; k++) {
if (inputJSON[k]) {
multiInputs[k].value = JSON.stringify(inputJSON[k])
}
}
}
}
createMultiFields () {
if (this.funABI.inputs) {
return yo`
${this.funABI.inputs.map(function (inp) {
return yo`
`
})}
`
}
}
render () {
var title
if (this.title) {
title = this.title
} else if (this.funABI.name) {
title = this.funABI.name
} else {
title = '(fallback)'
}
this.basicInputField = yo``
this.basicInputField.setAttribute('placeholder', this.inputs)
this.basicInputField.setAttribute('title', this.inputs)
var onClick = (domEl) => {
this.clickCallBack(this.funABI.inputs, this.basicInputField.value)
this.emptyInputs()
}
this.contractActionsContainerSingle = yo`
${this.basicInputField} { this.switchMethodViewOn() }} title=${title} >
`
this.multiFields = this.createMultiFields()
var multiOnClick = () => {
var valsString = this.getMultiValsString()
if (valsString) {
this.clickCallBack(this.funABI.inputs, valsString)
} else {
this.clickCallBack(this.funABI.inputs, '')
}
this.emptyInputs()
}
var button = yo``
this.contractActionsContainerMulti = yo`
{ this.switchMethodViewOff() }} class="${css.multiHeader}">
${title}
${this.multiFields}
${button}
`
var contractProperty = yo`${this.contractActionsContainerSingle} ${this.contractActionsContainerMulti}
`
if (this.lookupOnly) {
contractProperty.classList.add(css.constant)
button.setAttribute('title', (title + ' - call'))
button.innerHTML = 'call'
this.contractActionsContainerSingle.querySelector(`.${css.instanceButton}`).setAttribute('title', (title + ' - call'))
} else {
button.innerHTML = 'transact'
}
if (this.funABI.inputs && this.funABI.inputs.length > 0) {
contractProperty.classList.add(css.hasArgs)
} else {
this.contractActionsContainerSingle.querySelector('i').style.visibility = 'hidden'
this.basicInputField.style.display = 'none'
}
if (this.funABI.payable === true) {
contractProperty.classList.add(css.payable)
button.setAttribute('title', (title + ' - transact (payable)'))
this.contractActionsContainerSingle.querySelector('button').setAttribute('title', (title + ' - transact (payable)'))
}
if (!this.lookupOnly && this.funABI.payable === false) {
button.setAttribute('title', (title + ' - transact (not payable)'))
this.contractActionsContainerSingle.querySelector('button').setAttribute('title', (title + ' - transact (not payable)'))
}
return contractProperty
}
}
module.exports = MultiParamManager