From 6c34c719750b463c3e2b4b18ba20e7bc5f7bfdfb Mon Sep 17 00:00:00 2001 From: jean-emmanuel Date: Wed, 7 Feb 2018 09:49:21 +0100 Subject: [PATCH] dropdown: make `values` dynamic, drop unreleased support for sendKey and receiving key (issue #140) --- CHANGELOG.md | 2 +- resources/docs/widgets/buttons.md | 8 +- .../js/app/widgets/buttons/dropdown.js | 86 ++++++++++++------- src/browser/scss/widgets/_switch.scss | 11 --- 4 files changed, 59 insertions(+), 48 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a914306d..c60f637d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,7 +9,7 @@ - prevent client to load multiple sessions simultaneously - widgets - switch: wrap non-array `values` property in an array - - dropdown: wrap non-array `values` property in an array; allow setting value by its key/index; add `sendKey` option; + - dropdown: wrap non-array `values` property in an array; always add a default empty value - property inheritance: added shorthand default to `_value` when no property is specified (`@{widgetId}`); some properties can now be dynamically changed without recreating the widget entirely - property maths: added support for math expressions everywhere via `#{}` - input: add glass effect when `editable` is `false` diff --git a/resources/docs/widgets/buttons.md b/resources/docs/widgets/buttons.md index d3c89c72..59ea57a7 100644 --- a/resources/docs/widgets/buttons.md +++ b/resources/docs/widgets/buttons.md @@ -100,14 +100,10 @@ Setting the `push`'s value externally (via osc) will toggle it's inner light. } ``` -### `values` +### `values`flash_on - type: `array|object` - default: `{"Value 1":1,"Value 2":2}` - usage: - `array` of possible values to switch between : `[1,2,3]` - `object` of `"label":value` pairs - -### `sendKey` -- type: `boolean` -- default: `false` -- usage: set to `true` to make the dropdown send its selected value's key/index as value. Either way, the dropdown's value can be set using it's value or the associated key/index. + - an empty option will always be prepended to `values` (sends an osc message without any value) diff --git a/src/browser/js/app/widgets/buttons/dropdown.js b/src/browser/js/app/widgets/buttons/dropdown.js index ef4b9bcb..5f212db3 100644 --- a/src/browser/js/app/widgets/buttons/dropdown.js +++ b/src/browser/js/app/widgets/buttons/dropdown.js @@ -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 += `` - 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 += `` + + this.values = [] + + for (var k in values) { + this.values.push(values[k]) + html += `` + 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 diff --git a/src/browser/scss/widgets/_switch.scss b/src/browser/scss/widgets/_switch.scss index e7c5779f..5d7792af 100644 --- a/src/browser/scss/widgets/_switch.scss +++ b/src/browser/scss/widgets/_switch.scss @@ -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;