dropdown: make values dynamic, drop unreleased support for sendKey and receiving key (issue #140)

This commit is contained in:
jean-emmanuel 2018-02-07 09:49:21 +01:00
parent b6449c5a8f
commit d32a8a3105
4 changed files with 59 additions and 48 deletions

View file

@ -1,7 +1,7 @@
var Widget = require('../common/widget'),
{iconify} = require('../../ui/utils')
module.exports = class Dropdown extends Widget {
class Dropdown extends Widget {
static defaults() {
@ -26,7 +26,6 @@ module.exports = class Dropdown extends Widget {
_dropdown:'dropdown',
values:{"Value 1":1,"Value 2":2},
sendKey: false,
value:'',
_osc:'osc',
@ -48,22 +47,10 @@ module.exports = class Dropdown extends Widget {
this.values = []
this.keys = []
var i = 0,
values = this.getProp('values')
if (!Array.isArray(values) && !(typeof values === 'object' && values !== null)) {
values = [values]
}
for (var k in values) {
this.values.push(values[k])
this.keys.push(k)
this.select.innerHTML += `<option value="${i}">${iconify(parseFloat(k) != k ? k : values[k])}</option>`
i++
}
this.parseValues()
this.select.addEventListener('change', ()=>{
this.setValue(this.values[this.select.selectedIndex], {sync:true, send:true, fromLocal:true})
this.setValue(this.values[this.select.selectedIndex - 1], {sync:true, send:true, fromLocal:true})
})
this.value = undefined
@ -72,25 +59,64 @@ module.exports = class Dropdown extends Widget {
}
parseValues() {
var i = 0,
values = this.getProp('values'),
html = ''
if (!Array.isArray(values) && !(typeof values === 'object' && values !== null)) {
values = values !== '' ? [values] : []
}
html += `<option value=""></option>`
this.values = []
for (var k in values) {
this.values.push(values[k])
html += `<option value="${i}">${iconify(parseFloat(k) != k ? k : values[k])}</option>`
i++
}
this.select.innerHTML = html
}
setValue(v,options={}) {
var i = this.values.indexOf(v)
if (i == -1) i = this.keys.indexOf(v)
this.value = this.values[i]
if (i == -1 && typeof v == 'number' && v >= 0 && v < this.values.length) i = v
if (!options.fromLocal) this.select.selectedIndex = i + 1
if (options.send) this.sendValue()
if (options.sync) this.changed(options)
}
onPropChanged(propName, options) {
var ret = super.onPropChanged(...arguments)
switch (propName) {
case 'values':
this.parseValues()
return
if (i != -1) {
this.value = this.getProp('sendKey') ? this.keys[i] : this.values[i]
if (!options.fromLocal) this.select.selectedIndex = i
if (options.send) this.sendValue()
if (options.sync) this.changed(options)
this.container.classList.remove('noselect')
} else {
this.value = undefined
this.select.selectedIndex = -1
if (options.sync) this.changed(options)
this.container.classList.add('noselect')
}
}}
return ret
}
}
Dropdown.dynamicProps = Dropdown.prototype.constructor.dynamicProps.concat(
'values'
)
module.exports = Dropdown

View file

@ -151,17 +151,6 @@
width:100rem;
height: 60rem;
padding:1rem;
&.noselect .select:before {
content:"...";
position: absolute;
padding-left:10rem;
left:0;
top:50%;
margin-top:-12rem;
height: 20rem;
line-height: 20rem;
pointer-events: none;
}
&.nolabel {
--color-border:var(--color-bg);
padding: 0;