summaryrefslogtreecommitdiff
path: root/client/lib/ui.js
blob: 413ff1cf9b7022de7ba8cb43f0768ecb371cd816 (plain)
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
import Nexus from 'nexusui'

export const nx = window.nx = {}

/* ui - update an int/float value */

export function update_value_on_change(el, id, is_int, fn) {
  const label = document.querySelector(id + ' + .val')
  const update = v => {
    label.innerHTML = is_int ? parseInt(v) : v.toFixed(2)
    fn && fn(v)
  }
  el.on('change', update)
  update(el.value)
  el.update = update
}

/* ui - update a radio button */

export function update_radio_value_on_change(el, id, values, fn) {
  let old_v = el.active
  const label = document.querySelector(id + ' + .val')
  const update = v => {
    if (v === -1) {
      v = el.active = old_v
    } else {
      old_v = v
    }
    label.innerHTML = values[v][1]
    fn && fn(v)
  }
  el.on('change', update)
  update(el.active)
  el.update = update
}

/* ui - bind/build a select dropdown */

export function build_options(el, lists, fn) {
  Object.keys(lists).forEach(key => {
    const list = lists[key]
    const option = document.createElement('option')
    option.innerHTML = list.name
    option.value = key
    el.appendChild(option)
  })
  el.addEventListener('input', function(e){
    fn(e.target.value)
  })
}