From 3d3b7e15682d68ab7a975e7593da4f7790f3aaec Mon Sep 17 00:00:00 2001 From: jean-emmanuel Date: Thu, 25 Jan 2018 23:38:44 +0100 Subject: [PATCH] switch: receive array properly (issue #136) --- CHANGELOG.md | 2 +- src/browser/js/app/widgets/buttons/switch.js | 15 ++++++++++++--- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 878e37ef..e345d47f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,7 +11,7 @@ - widgets - inputs: **new** `keys` widget, for keyboard bindings - property inheritance: new special property name `_value` (retrieve the widget's value, and update when it changes) - - switch: added `showValues` to display both values and labels in the buttons + - switch: added `showValues` to display both values and labels in the buttons; array values can now be selected via osc - rgb: **removed** `touchAddress` option - labels/texts: multiple white spaces are no longer merged - misc diff --git a/src/browser/js/app/widgets/buttons/switch.js b/src/browser/js/app/widgets/buttons/switch.js index 86f6f946..4dbb218a 100644 --- a/src/browser/js/app/widgets/buttons/switch.js +++ b/src/browser/js/app/widgets/buttons/switch.js @@ -49,6 +49,7 @@ module.exports = class Switch extends _widgets_base { if (this.getProp('horizontal')) this.widget.classList.add('horizontal') this.values = [] + this.stringValues = [] var isArray = Array.isArray(this.getProp('values')) @@ -56,6 +57,12 @@ module.exports = class Switch extends _widgets_base { this.values.push(this.getProp('values')[k]) + if (typeof this.getProp('values')[k] == 'object') { + this.stringValues.push(JSON.stringify(this.getProp('values')[k])) + } else { + this.stringValues.push(-1) + } + var label = isArray ? this.getProp('values')[k]: k if (this.getProp('showValues') && !isArray) label = label + ': ' + this.getProp('values')[k] @@ -67,7 +74,7 @@ module.exports = class Switch extends _widgets_base { this.value = undefined - this.on('draginit',(e)=>{ + this.on('draginit', (e)=>{ var index = 0, node = e.target @@ -86,9 +93,11 @@ module.exports = class Switch extends _widgets_base { } - setValue(v,options={}) { + setValue(v, options={}) { - var i = this.values.indexOf(v) + var i = typeof v == 'object' ? + this.stringValues.indexOf(JSON.stringify(v)) : + this.values.indexOf(v) DOM.each(this.widget, '.on', (el)=>{el.classList.remove('on')})