input: fix some sync bugs and pimp input's code

This commit is contained in:
jean-emmanuel 2017-07-11 18:18:43 +02:00
parent 2b31f4e8d0
commit 39dacddaa7
5 changed files with 44 additions and 33 deletions

View file

@ -54,36 +54,14 @@ module.exports = class Input extends _canvas_base {
this.value = ''
this.stringValue = ''
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((e)=>{
e.stopPropagation()
i.off('change')
var v = i.val()
i.blur()
this.setValue(v, {sync:true, send:true})
})
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()
})
this.canvas.on('focus', this.focus.bind(this))
this.input = $('<input></input>')
this.input.on('change', (e)=>{
e.stopPropagation()
})
}
@ -92,6 +70,38 @@ module.exports = class Input extends _canvas_base {
}
focus() {
this.canvas.attr('tabindex','-1')
this.input
.val(this.stringValue)
.prependTo(this.widget)
.focus()
.on('blur', (e)=>{
this.blur()
})
.on('keydown', (e)=>{
if (e.keyCode==13) this.blur()
if (e.keyCode==27) this.blur(false)
})
}
blur(change=true) {
if (change) this.inputChange()
this.input.off('blur keydown')
this.canvas.attr('tabindex','0')
this.input.detach()
}
inputChange() {
this.setValue(this.input.val(), {sync:true, send:true})
}
onRemove() {
$('body').off(`change.${this.hash}`)

View file

@ -88,6 +88,10 @@ module.exports = class Formula extends _widgets_base {
$('body').on(`change.${this.hash}`,this.syncHandle.bind(this))
this.input.widget.on('change', (e)=>{
e.stopPropagation()
})
this.input.setValue('...')
}

View file

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

View file

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

View file

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