2017-08-25 09:18:52 -07:00
|
|
|
const Component = require('react').Component
|
|
|
|
const h = require('react-hyperscript')
|
|
|
|
const inherits = require('util').inherits
|
|
|
|
const InputNumber = require('./input-number.js')
|
2017-08-25 12:20:23 -07:00
|
|
|
const findDOMNode = require('react-dom').findDOMNode
|
2017-08-25 09:18:52 -07:00
|
|
|
|
|
|
|
module.exports = GasTooltip
|
|
|
|
|
|
|
|
inherits(GasTooltip, Component)
|
|
|
|
function GasTooltip () {
|
|
|
|
Component.call(this)
|
|
|
|
this.state = {
|
|
|
|
gasLimit: 0,
|
|
|
|
gasPrice: 0,
|
|
|
|
}
|
|
|
|
|
|
|
|
this.updateGasPrice = this.updateGasPrice.bind(this);
|
|
|
|
this.updateGasLimit = this.updateGasLimit.bind(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
GasTooltip.prototype.updateGasPrice = function (newPrice) {
|
|
|
|
const { onFeeChange } = this.props
|
|
|
|
const { gasLimit } = this.state
|
|
|
|
|
|
|
|
this.setState({ gasPrice: newPrice })
|
|
|
|
onFeeChange({ gasLimit, gasPrice: newPrice })
|
|
|
|
}
|
|
|
|
|
|
|
|
GasTooltip.prototype.updateGasLimit = function (newLimit) {
|
|
|
|
const { onFeeChange } = this.props
|
|
|
|
const { gasPrice } = this.state
|
|
|
|
|
|
|
|
this.setState({ gasLimit: newLimit })
|
|
|
|
onFeeChange({ gasLimit: newLimit, gasPrice })
|
|
|
|
}
|
|
|
|
|
|
|
|
GasTooltip.prototype.render = function () {
|
|
|
|
const { position, title, children, className, isOpen } = this.props
|
|
|
|
const { gasPrice, gasLimit } = this.state
|
2017-08-25 12:20:23 -07:00
|
|
|
|
|
|
|
this.manageListeners()
|
2017-08-25 09:18:52 -07:00
|
|
|
|
|
|
|
return isOpen
|
|
|
|
? h('div.customize-gas-tooltip-container', {}, [
|
|
|
|
h('div.customize-gas-tooltip', {}, [
|
|
|
|
h('div.gas-tooltip-header.gas-tooltip-label', {}, ['Customize Gas']),
|
|
|
|
h('div.gas-tooltip-input-label', {}, [
|
|
|
|
h('span.gas-tooltip-label', {}, ['Gas Price']),
|
|
|
|
h('i.fa.fa-info-circle')
|
|
|
|
]),
|
|
|
|
h(InputNumber, {
|
|
|
|
unitLabel: 'GWEI',
|
|
|
|
step: 0.0001,
|
|
|
|
min: 0.0000,
|
|
|
|
placeholder: '0.0000',
|
|
|
|
fixed: 4,
|
|
|
|
initValue: gasPrice,
|
|
|
|
onChange: (newPrice) => this.updateGasPrice(newPrice),
|
|
|
|
}),
|
|
|
|
h('div.gas-tooltip-input-label', {}, [
|
|
|
|
h('span.gas-tooltip-label', {}, ['Gas Limit']),
|
|
|
|
h('i.fa.fa-info-circle')
|
|
|
|
]),
|
|
|
|
h(InputNumber, {
|
|
|
|
unitLabel: 'UNITS',
|
|
|
|
step: 1,
|
|
|
|
min: 0,
|
|
|
|
placeholder: '0',
|
|
|
|
initValue: gasLimit,
|
|
|
|
onChange: (newLimit) => this.updateGasLimit(newLimit),
|
|
|
|
}),
|
|
|
|
]),
|
|
|
|
h('div.gas-tooltip-arrow', {}),
|
|
|
|
])
|
|
|
|
: null
|
|
|
|
}
|
2017-08-25 12:20:23 -07:00
|
|
|
|
|
|
|
GasTooltip.prototype.manageListeners = function () {
|
|
|
|
const isOpen = this.props.isOpen
|
|
|
|
const onClickOutside = this.props.onClickOutside
|
|
|
|
|
|
|
|
if (isOpen) {
|
|
|
|
this.outsideClickHandler = onClickOutside
|
|
|
|
} else if (!isOpen) {
|
|
|
|
this.outsideClickHandler = null
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
GasTooltip.prototype.componentDidMount = function () {
|
|
|
|
if (this && document.body) {
|
|
|
|
this.globalClickHandler = this.globalClickOccurred.bind(this)
|
|
|
|
document.body.addEventListener('click', this.globalClickHandler)
|
|
|
|
var container = findDOMNode(this)
|
|
|
|
this.container = container
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
GasTooltip.prototype.componentWillUnmount = function () {
|
|
|
|
if (this && document.body) {
|
|
|
|
document.body.removeEventListener('click', this.globalClickHandler)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
GasTooltip.prototype.globalClickOccurred = function (event) {
|
|
|
|
const target = event.target
|
|
|
|
const container = findDOMNode(this)
|
|
|
|
console.log(`target`, target);
|
|
|
|
console.log(`container`, container);
|
|
|
|
console.log(`this.container`, this.container);
|
|
|
|
console.log(`this.outsideClickHandler`, this.outsideClickHandler);
|
|
|
|
if (target !== container &&
|
|
|
|
!isDescendant(container, target) &&
|
|
|
|
this.outsideClickHandler) {
|
|
|
|
this.outsideClickHandler(event)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function isDescendant (parent, child) {
|
|
|
|
var node = child.parentNode
|
|
|
|
while (node !== null) {
|
|
|
|
if (node === parent) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
node = node.parentNode
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|