forked from ninabreznik/input-array
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinput-array.js
More file actions
62 lines (60 loc) · 2.77 KB
/
input-array.js
File metadata and controls
62 lines (60 loc) · 2.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
const bel = require('bel')
const csjs = require('csjs-inject')
const inputAddress = require("input-address")
const inputInteger = require("input-integer")
const inputBoolean = require("input-boolean")
const inputString = require("input-string")
const inputByte = require("input-byte")
const validator = require('solidity-validator')
module.exports = displayArrayInput
function displayArrayInput ({ theme: { classes: css, colors }, type, cb, focus, blur }) {
const container = bel`<div class=${css.arrayContainer}></div>`
const arr = getParsedArray(type) // uint8[2][3][] returns ['', 3, 2]
next({ container, arr, cb })
return container
function next ({ container, arr, cb }) {
var len = arr.shift()
if (len === '') {
len = 1
container.appendChild(plusMinus({ container, arr, cb }))
}
for (var i = 0; i < len; i++) append({ container, arr: [...arr], cb })
}
function append ({ container, arr, cb }) {
if (arr.length) { // recursive step
const innerContainer = bel`<div class="${css.arrayContainer}"></div>`
container.appendChild(innerContainer)
next({ container: innerContainer, arr, cb, blur })
} else { // final step (stop recursion)
container.appendChild(returnInputFields({ classes: css, colors }, type, cb, focus, blur))
}
}
function plusMinus ({ container, arr, cb }) {
return bel`<div class=${css.arrayPlusMinus}>
<i class="${css.arrayMinus} fa fa-minus" onclick=${e=>removeLast(container)}></i>
<i class="${css.arrayPlus} fa fa-plus" onclick=${e=>append({ container, arr: [...arr], cb })}></i>
</div>`
}
function removeLast (node) {
if (node.children.length > 2) node.removeChild(node.lastChild)
}
}
function returnInputFields (theme, type, cb, focus, blur) {
if (type.includes("int")) return inputInteger({ theme, type, cb, focus, blur })
else if (type.includes("byte")) return inputByte({ theme, type, cb, focus, blur })
else if (type.includes("string")) return inputString({ theme, type, cb, focus, blur })
else if (type.includes("bool")) return inputBoolean({ theme, type, cb, focus, blur })
else if (type.includes("fixed")) return inputInteger({ theme, type, cb, focus, blur })
else if (type.includes("address")) return inputAddress({ theme, type, cb, focus, blur })
}
function getParsedArray (type) {
const arr = []
const i = type.search(/\[/) // find where array starts (bool[2][])
const basicType = type.split('[')[0] // split to get basic type (bool, uint8)
const suffix = type.slice(i) // slice to get the remaining part = suffix ([2][][][])
suffix.split('][').forEach((x, i)=>{
if (x.search(/\d/) != -1) { arr.push(x.charAt(x.search(/\d/))) } // if digit is present, push the digit
else { arr.push('') } // if no, push empty string
})
return arr.reverse()
}