inputs: add editable option: fix sync/send on input change for sliders/pads

This commit is contained in:
jean-emmanuel 2017-07-11 10:42:44 +02:00
parent c6ceb59f97
commit 311450f4ff
6 changed files with 38 additions and 28 deletions

View file

@ -15,6 +15,10 @@
- default: `empty`
- usage: a widget's `id` whose value will be linked to the input
### `editable`
- type: `boolean`
- default: `true`
- usage: set to `false` to make the input non-editable
### `horizontal`
- type: `boolean`

View file

@ -26,6 +26,7 @@ module.exports = class Input extends _canvas_base {
_plot:'input',
widgetId:'',
editable:true,
_osc:'osc',
@ -53,32 +54,36 @@ module.exports = class Input extends _canvas_base {
this.value = ''
this.stringValue = ''
this.canvas.on('focus',()=>{
this.canvas.attr('tabindex','-1')
let v = this.stringValue
let i = $('<input></input>')
.prependTo(this.widget)
.val(v)
.focus()
.change(()=>{
this.setValue(i.val(), {sync:true, send:true})
i.blur()
})
i.blur(()=>{
this.canvas.attr('tabindex','0')
i.remove()
$(document).off('.fakeInput')
})
$(document).on('fake-click.fakeInput',function(e, d){
if (d.originalEvent.target!=i[0]) {
if (this.getProp('editable')) {
this.canvas.on('focus',()=>{
this.canvas.attr('tabindex','-1')
let v = this.stringValue
let i = $('<input></input>')
.prependTo(this.widget)
.val(v)
.focus()
.change(()=>{
this.setValue(i.val(), {sync:true, send:true})
i.blur()
}
}).on('keydown.fakeInput', function(e){
if (e.keyCode==13) i.change()
if (e.keyCode==27) i.off('change').blur()
})
i.blur(()=>{
this.canvas.attr('tabindex','0')
i.remove()
$(document).off('.fakeInput')
})
$(document).on('fake-click.fakeInput',function(e, d){
if (d.originalEvent.target!=i[0]) {
i.blur()
}
}).on('keydown.fakeInput', function(e){
if (e.keyCode==13) i.change()
if (e.keyCode==27) i.off('change').blur()
})
})
})
}
if (this.getProp('widgetId').length) $('body').on(`change.${this.hash}`,this.syncHandle.bind(this))
@ -134,9 +139,9 @@ module.exports = class Input extends _canvas_base {
this.stringValue = this.getStringValue()
this.draw()
if (this.getProp('widgetId').length && !options.sync) {
if (this.getProp('widgetId').length && options.hash != this.hash) {
var widget = widgetManager.getWidgetById(this.getProp('widgetId'))
options.hash = this.hash
for (var i=widget.length-1; i>=0; i--) {
if (widget[i].setValue) {
widget[i].setValue(this.value, options)

View file

@ -56,6 +56,7 @@ module.exports = class Formula extends _widgets_base {
this.input = new Input({
props:{
...Input.defaults(),
editable:false,
precision:this.getProp('precision')
},
parent:this, parentNode:this.widget

View file

@ -123,7 +123,7 @@ module.exports = class Rgb extends _pads_base {
this.widget.append(this.input.widget)
this.input.widget.on('change', (e)=>{
e.preventDefault()
this.setValue(this.input.getValue())
this.setValue(this.input.getValue(), {sync:true, send:true})
this.showValue()
})

View file

@ -133,7 +133,7 @@ module.exports = class Xy extends _pads_base {
this.widget.append(this.input.widget)
this.input.widget.on('change', (e)=>{
e.preventDefault()
this.setValue(this.input.getValue())
this.setValue(this.input.getValue(), {sync:true, send:true})
this.showValue()
})

View file

@ -84,7 +84,7 @@ module.exports = class _sliders_base extends _canvas_base {
this.widget.append(this.input.widget)
this.input.widget.on('change', (e)=>{
e.preventDefault()
this.setValue(this.input.getValue())
this.setValue(this.input.getValue(), {sync:true, send:true})
this.showValue()
})