switch/menu/dropdown: unified value parsing

dropdown: remove `showValues`
This commit is contained in:
bordun 2020-03-23 14:25:56 +01:00
parent 71a67cdb64
commit e49c9ce00e
4 changed files with 40 additions and 86 deletions

View file

@ -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`<select class="no-keybinding"></select>`)
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`
<option value="${i}">
${raw(iconify(parseFloat(k) != k ? k : values[k]))}
${this.keys[i]}
</option>
`)
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

View file

@ -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`
<div class="item" style="${circular ? `transform: rotate(${ac}deg) skew(${skew}deg)` : `flex: ${weights[i]}`}">
<div style="${circular ? `transform: skew(${-skew}deg) rotate(${-90 + angle / 2}deg)` : ''}"><span style="${circular ? `transform: rotate(${-ac + 90 - angle / 2}deg)` : ''}">${raw(iconify(parseFloat(k) != k ? k : values[k]))}</span></div>
<div style="${circular ? `transform: skew(${-skew}deg) rotate(${-90 + angle / 2}deg)` : ''}"><span style="${circular ? `transform: rotate(${-ac + 90 - angle / 2}deg)` : ''}">${raw(iconify(this.keys[i]))}</span></div>
</div>
`)
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)

View file

@ -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`
<value> ${raw(iconify(label))}</value>
`)
}
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`
<value>${raw(iconify(this.keys[i]))}</value>
`)
}
}
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)
}
}