From e49c9ce00eaf7c077cf001d2a2334fe8f3e4f49d Mon Sep 17 00:00:00 2001 From: bordun Date: Mon, 23 Mar 2020 14:25:56 +0100 Subject: [PATCH] switch/menu/dropdown: unified value parsing dropdown: remove `showValues` --- CHANGELOG.md | 1 + src/client/widgets/basics/dropdown.js | 25 +++------- src/client/widgets/basics/menu.js | 31 ++++-------- src/client/widgets/basics/switch.js | 69 ++++++++++----------------- 4 files changed, 40 insertions(+), 86 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5e0c1e98..4b64cf30 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -65,6 +65,7 @@ - html: allow `class`, `title` and `style` attributes - dropdown: close menu when receiving a value - dropdown: removed empty 1st option + - switch: removed `showValues` (inconsistent with menu/dropdown, feature covered by `values` property) - widget removals - `push`, `toggle`: merged into `button` diff --git a/src/client/widgets/basics/dropdown.js b/src/client/widgets/basics/dropdown.js index 2bed4497..8de87597 100644 --- a/src/client/widgets/basics/dropdown.js +++ b/src/client/widgets/basics/dropdown.js @@ -1,9 +1,9 @@ -var Widget = require('../common/widget'), +var MenuBase = require('./menu-base'), {iconify} = require('../../ui/utils'), html = require('nanohtml'), raw = require('nanohtml/raw') -class Dropdown extends Widget { +class Dropdown extends MenuBase { static description() { @@ -43,15 +43,12 @@ class Dropdown extends Widget { this.select = this.widget.appendChild(html``) this.text = DOM.get(this.widget, '.text')[0] - this.values = [] - this.parseValues() this.select.addEventListener('change', ()=>{ this.setValue(this.values[this.select.selectedIndex], {sync:true, send:true, fromLocal:true}) }) - this.value = undefined this.select.selectedIndex = -1 this.container.classList.add('noselect') @@ -59,25 +56,15 @@ class Dropdown extends Widget { parseValues() { - var i = 0, - values = this.getProp('values') - - if (!Array.isArray(values) && !(typeof values === 'object' && values !== null)) { - values = values !== '' ? [values] : [] - } + super.parseValues() this.select.innerHTML = '' - - this.values = [] - - for (var k in values) { - this.values.push(values[k]) + for (var i = 0; i < this.values.length; i++) { this.select.appendChild(html` `) - i++ } } @@ -90,7 +77,7 @@ class Dropdown extends Widget { if (!options.fromLocal) this.select.selectedIndex = i - this.text.textContent = this.select.options[i].textContent + this.text.textContent = i === -1 ? '' : this.select.options[i].textContent if (document.activeElement === this.select) { // force menu close diff --git a/src/client/widgets/basics/menu.js b/src/client/widgets/basics/menu.js index 9791c96c..75ff462f 100644 --- a/src/client/widgets/basics/menu.js +++ b/src/client/widgets/basics/menu.js @@ -1,10 +1,10 @@ -var Widget = require('../common/widget'), +var MenuBase = require('./menu-base'), {iconify} = require('../../ui/utils'), doubletab = require('../mixins/double_tap'), html = require('nanohtml'), raw = require('nanohtml/raw') -class Menu extends Widget { +class Menu extends MenuBase { static description() { @@ -60,15 +60,10 @@ class Menu extends Widget { this.opened = false - this.values = [] - this.keys = [] - this.selected = -1 - this.value = undefined this.parseValues() - if (this.getProp('doubleTap')) { doubletab(this.widget, (e)=>{ @@ -186,40 +181,30 @@ class Menu extends Widget { parseValues() { - var nval = 0, - i = 0, + super.parseValues() + + var nval = this.values.length, circular = this.getProp('layout') === 'circular', - values = this.getProp('values'), weights = this.getProp('weights'), totalWeight - if (!Array.isArray(values) && !(typeof values === 'object' && values !== null)) { - values = values !== '' ? [values] : [] - } - - this.values = [] if (this.opened) this.widget.removeChild(this.menu) this.menu.innerHTML = '' - this.keys = !Array.isArray(values) ? Object.keys(values) : values - this.values = !Array.isArray(values) ? Object.values(values) : values - - nval = Array.isArray(values) ? values.length : Object.keys(values).length weights = Array.isArray(weights) ? weights.slice(0, nval) : Array(nval).fill(1) if (weights.length < nval) weights = weights.concat(Array(nval - weights.length).fill(1)) totalWeight = weights.reduce((a, b) => a + b, 0) var ac = 0 - for (let k in values) { + for (var i = 0; i < nval; i++) { let angle = Math.min(360 * weights[i] / totalWeight, 120), skew = 90 - angle this.menu.appendChild(html`
-
${raw(iconify(parseFloat(k) != k ? k : values[k]))}
+
${raw(iconify(this.keys[i]))}
`) - i++ ac+=angle } @@ -275,7 +260,7 @@ class Menu extends Widget { DOM.each(this.menu, '.on', (el)=>{el.classList.remove('on')}) if (i > -1) DOM.get(this.menu, '.item')[i].classList.add('on') - this.text.textContent = this.keys[i] + this.text.innerHTML = iconify(this.keys[i]) if (options.send) this.sendValue() if (options.sync) this.changed(options) diff --git a/src/client/widgets/basics/switch.js b/src/client/widgets/basics/switch.js index 1ced3984..70a1ad59 100644 --- a/src/client/widgets/basics/switch.js +++ b/src/client/widgets/basics/switch.js @@ -1,9 +1,9 @@ -var Widget = require('../common/widget'), +var MenuBase = require('./menu-base'), {iconify} = require('../../ui/utils'), html = require('nanohtml'), raw = require('nanohtml/raw') -class Switch extends Widget { +class Switch extends MenuBase { static description() { @@ -19,7 +19,6 @@ class Switch extends Widget { layout: {type: 'string', value: 'vertical', choices: ['vertical', 'horizontal', 'grid'], help:''}, gridTemplate: {type: 'string|number', value: '', help:'If `layout` is `grid`, can be either a number of columns of a value css grid-template definition.'}, - showValues: {type: 'boolean', value: false, help: 'If values is an object, set to `true` to display both values and labels instead of labels only'}, values: {type: 'array|object', value: {'Value 1':1,'Value 2':2}, help: [ '`Array` of possible values to switch between : `[1,2,3]`', '`Object` of `"label":value` pairs. Numeric labels must be prepended or appended with a white space (or any other non-numeric character) otherwise the order of the values won\'t be kept', @@ -47,38 +46,8 @@ class Switch extends Widget { this.widget.style.gridTemplate = template === parseInt(template) ? `none / repeat(${template}, 1fr)` : template } + this.parseValues() - this.values = [] - this.stringValues = [] - - var values = this.getProp('values') - - if (!Array.isArray(values) && !(typeof values === 'object' && values !== null)) { - values = [values] - } - - var isArray = Array.isArray(values) - - for (var k in values) { - - this.values.push(values[k]) - - if (typeof values[k] == 'object') { - this.stringValues.push(JSON.stringify(values[k])) - } else { - this.stringValues.push(0) - } - - var label = isArray ? values[k]: k - if (this.getProp('showValues') && !isArray) label = label + ': ' + (this.stringValues[this.stringValues.length - 1] || values[k]) - - this.widget.appendChild(html` - ${raw(iconify(label))} - `) - - } - - this.value = undefined var dragCallback = (e, touchFix)=>{ @@ -92,7 +61,7 @@ class Switch extends Widget { if (node === this.widget || !this.widget.contains(node)) return - while ( (node = node.previousSibling) ) { + while ((node = node.previousSibling)) { if (node.nodeType != 3) { index++ } @@ -100,7 +69,7 @@ class Switch extends Widget { var value = this.values[index] - if (value!=this.value || this.value===undefined) this.setValue(value,{sync:true,send:true}) + if (value !== this.value || this.value === undefined) this.setValue(value, {sync: true, send: true}) } @@ -116,22 +85,34 @@ class Switch extends Widget { } + parseValues() { + + super.parseValues() + + for (var i = 0; i < this.values.length; i++) { + + this.widget.appendChild(html` + ${raw(iconify(this.keys[i]))} + `) + + } + + } + setValue(v, options={}) { - var i = typeof v == 'object' ? - this.stringValues.indexOf(JSON.stringify(v)) : - this.values.indexOf(v) + var i = this.values.indexOf(v) - DOM.each(this.widget, '.on', (el)=>{el.classList.remove('on')}) - if (i!=-1) { + if (i !== -1) { + this.value = this.values[i] + + DOM.each(this.widget, '.on', (el)=>{el.classList.remove('on')}) DOM.get(this.widget, 'value')[i].classList.add('on') + if (options.send) this.sendValue(this.value) if (options.sync) this.changed(options) - } else { - this.value = undefined - if (options.sync) this.changed(options) } }