diff options
Diffstat (limited to 'client/lib/ui.js')
| -rw-r--r-- | client/lib/ui.js | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/client/lib/ui.js b/client/lib/ui.js new file mode 100644 index 0000000..f344f0e --- /dev/null +++ b/client/lib/ui.js @@ -0,0 +1,46 @@ +/* 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) + }) +} |
