sliders: prevent error when steps is not a number, add auto mode for using ranges values as steps (it did not work before)
This commit is contained in:
parent
1fd8523885
commit
98a27de0df
4 changed files with 23 additions and 14 deletions
|
|
@ -42,11 +42,11 @@ class Fader extends Slider {
|
|||
]},
|
||||
logScale: {type: 'boolean|number', value: false, help: 'Set to `true` to use logarithmic scale. Set to `-1` for exponential scale.'},
|
||||
sensitivity: {type: 'number', value: 1, help: 'Defines the fader\'s sensitivity when `snap` is `false` '},
|
||||
steps: {type: 'string|number|array', value: '', help: [
|
||||
steps: {type: 'number|array|string', value: '', help: [
|
||||
'Restricts the widget\'s value:',
|
||||
'- `empty` or `0`: use values defined in `range`',
|
||||
'- `number`: define a number of evenly spaced steps',
|
||||
'- `array`: use arbitrary values',
|
||||
'- `array` of numbers: use arbitrary values as steps',
|
||||
'- `auto`: use values defined in `range`'
|
||||
]},
|
||||
origin: {type: 'number', value: 'auto', help: 'Defines the starting point\'s value of the fader\'s gauge'},
|
||||
}
|
||||
|
|
@ -181,7 +181,7 @@ class Fader extends Slider {
|
|||
var width = this.getProp('horizontal') ? this.height : this.width,
|
||||
height = !this.getProp('horizontal') ? this.height : this.width
|
||||
|
||||
var percent = this.getProp('steps') ? this.valueToPercent(this.value) : this.percent,
|
||||
var percent = this.steps ? this.valueToPercent(this.value) : this.percent,
|
||||
d = Math.round(this.percentToCoord(percent)),
|
||||
o = Math.round(this.percentToCoord(this.valueToPercent(this.originValue))),
|
||||
m = this.getProp('horizontal') ? this.height / 2 : this.width / 2,
|
||||
|
|
|
|||
|
|
@ -44,9 +44,9 @@ module.exports = class Knob extends Slider {
|
|||
sensitivity: {type: 'number', value: 1, help: 'Defines the knob\'s sensitivity when `mode` is not `snap` '},
|
||||
steps: {type: 'string|number|array', value: '', help: [
|
||||
'Restricts the widget\'s value:',
|
||||
'- `empty` or `0`: use values defined in `range`',
|
||||
'- `number`: define a number of evenly spaced steps',
|
||||
'- `array`: use arbitrary values',
|
||||
'- `array` of numbers: use arbitrary values as steps',
|
||||
'- `auto`: use values defined in `range`'
|
||||
]},
|
||||
origin: {type: 'number', value: 'auto', help: 'Defines the starting point\'s value of the knob\'s gauge'},
|
||||
}
|
||||
|
|
@ -166,7 +166,7 @@ module.exports = class Knob extends Slider {
|
|||
|
||||
draw() {
|
||||
|
||||
var percent = this.getProp('steps') ? this.valueToPercent(this.value) : this.percent,
|
||||
var percent = this.steps ? this.valueToPercent(this.value) : this.percent,
|
||||
o = this.percentToAngle(this.valueToPercent(this.originValue)),
|
||||
d = this.percentToAngle(percent),
|
||||
min = this.percentToAngle(0),
|
||||
|
|
|
|||
|
|
@ -80,6 +80,7 @@ class Slider extends Canvas {
|
|||
this.on('dragend', this.dragendHandleProxy.bind(this), {element:this.widget, multitouch: options.multitouch})
|
||||
this.on('wheel',this.mousewheelHandleProxy.bind(this), {element: this.widget})
|
||||
|
||||
this.steps = null
|
||||
this.setSteps()
|
||||
|
||||
this.setValue(this.getSpringValue())
|
||||
|
|
@ -120,7 +121,7 @@ class Slider extends Canvas {
|
|||
var direction = e.deltaY > 0 ? -1 : 1,
|
||||
increment = e.ctrlKey ? 0.25 : 1
|
||||
|
||||
if (this.getProp('steps')) {
|
||||
if (this.steps) {
|
||||
var i = this.steps.indexOf(this.value),
|
||||
val
|
||||
if (i === -1) {
|
||||
|
|
@ -214,7 +215,7 @@ class Slider extends Canvas {
|
|||
|
||||
this.value = value
|
||||
|
||||
if (this.getProp('steps')) {
|
||||
if (this.steps) {
|
||||
|
||||
var diff = this.steps.map(x => Math.abs(x - this.value)),
|
||||
index = diff.indexOf(Math.min(...diff)),
|
||||
|
|
@ -237,10 +238,17 @@ class Slider extends Canvas {
|
|||
|
||||
if (this.getProp('steps')) {
|
||||
var steps = this.getProp('steps')
|
||||
this.steps = Array.isArray(steps) ?
|
||||
steps : typeof steps === 'number' ?
|
||||
Array(steps).fill(0).map((x, i) => i / (steps - 1) * (this.rangeValsMax - this.rangeValsMin) + this.rangeValsMin)
|
||||
: this.rangeVals
|
||||
if (steps === 'auto') {
|
||||
this.steps = this.rangeVals
|
||||
} else if (typeof steps === 'number' && steps > 0) {
|
||||
this.steps = Array(steps).fill(0).map((x, i) => i / (steps - 1) * (this.rangeValsMax - this.rangeValsMin) + this.rangeValsMin)
|
||||
} else if (Array.isArray(steps) && step.every(x=>typeof x === 'number')) {
|
||||
this.steps = steps
|
||||
} else {
|
||||
this.steps = null
|
||||
}
|
||||
} else {
|
||||
this.steps = null
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue