toggle: modularize code
This commit is contained in:
parent
9ae0e5000a
commit
15d869a5f6
3 changed files with 100 additions and 90 deletions
|
|
@ -4,7 +4,7 @@ var widgets = {
|
|||
knob: require('./widgets/sliders/knob'),
|
||||
|
||||
// buttons
|
||||
toggle: require('./widgets/toggle'),
|
||||
toggle: require('./widgets/buttons/toggle'),
|
||||
switch: require('./widgets/switch'),
|
||||
push: require('./widgets/push'),
|
||||
|
||||
|
|
|
|||
99
src/browser/js/app/widgets/buttons/toggle.js
Normal file
99
src/browser/js/app/widgets/buttons/toggle.js
Normal file
|
|
@ -0,0 +1,99 @@
|
|||
var _widgets_base = require('../common/_widgets_base'),
|
||||
$document = $(document)
|
||||
|
||||
module.exports.options = {
|
||||
type:'toggle',
|
||||
id:'auto',
|
||||
linkId:'',
|
||||
|
||||
separator1:'style',
|
||||
|
||||
label:'auto',
|
||||
left:'auto',
|
||||
top:'auto',
|
||||
width:'auto',
|
||||
height:'auto',
|
||||
color:'auto',
|
||||
css:'',
|
||||
|
||||
separator2:'osc',
|
||||
|
||||
on:1,
|
||||
off:0,
|
||||
value:'',
|
||||
precision:2,
|
||||
address:'auto',
|
||||
preArgs:[],
|
||||
target:[]
|
||||
}
|
||||
|
||||
var Toggle = module.exports.Toggle = function(widgetData,container) {
|
||||
|
||||
_widgets_base.apply(this, arguments)
|
||||
|
||||
this.widget = $(`
|
||||
<div class="light">
|
||||
</div>\
|
||||
`)
|
||||
|
||||
this.widget.value = this.widget.find('span')
|
||||
this.widget.state = 0
|
||||
|
||||
this.widget.on('drag',(e)=>{e.stopPropagation()})
|
||||
this.widget.on('draginit.toggle',()=>{
|
||||
this.widget.off('draginit.toggle')
|
||||
this.fakeclick()
|
||||
})
|
||||
|
||||
|
||||
this.widget.getValue = ()=>{
|
||||
return this.widget.state ?
|
||||
this.widgetData.on != null && this.widgetData.on.value !== undefined ? this.widgetData.on.value : this.widgetData.on
|
||||
:
|
||||
this.widgetData.off != null && this.widgetData.off.value !== undefined ? this.widgetData.off.value : this.widgetData.off
|
||||
}
|
||||
this.value = this.widget.getValue()
|
||||
this.widget.setValue = this.setValue.bind(this)
|
||||
|
||||
}
|
||||
|
||||
|
||||
Toggle.prototype = Object.create(_widgets_base.prototype)
|
||||
|
||||
Toggle.prototype.constructor = Toggle
|
||||
|
||||
Toggle.prototype.fakeclick = function(){
|
||||
var newVal = this.widget.state?this.widgetData.off:this.widgetData.on
|
||||
this.widget.setValue(newVal,{sync:true,send:true})
|
||||
$document.on('dragend.toggle',()=>{
|
||||
$document.off('dragend.toggle')
|
||||
this.widget.on('draginit.toggle',()=>{
|
||||
this.widget.off('draginit.toggle')
|
||||
this.fakeclick()
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
Toggle.prototype.setValue = function(v,options={}){
|
||||
if (v===this.widgetData.on || (this.widgetData.on != null && v.value === this.widgetData.on.value && v.value !== undefined)) {
|
||||
this.widget.addClass('on')
|
||||
this.widget.state = 1
|
||||
this.value = this.widgetData.on
|
||||
if (options.send) this.sendValue(this.widgetData.on)
|
||||
} else if (v===this.widgetData.off || (this.widgetData.off != null && v.value === this.widgetData.off.value && v.value !== undefined)) {
|
||||
this.widget.removeClass('on')
|
||||
this.widget.state = 0
|
||||
this.value = this.widgetData.off
|
||||
if (options.send) this.sendValue(this.widgetData.off)
|
||||
}
|
||||
|
||||
if (options.sync) this.widget.trigger({type:'sync',id:this.widgetData.id,widget:this.widget, linkId:this.widgetData.linkId, options})
|
||||
|
||||
}
|
||||
|
||||
|
||||
module.exports.create = function(widgetData, container) {
|
||||
var toggle = new Toggle(widgetData, container)
|
||||
return toggle.widget
|
||||
}
|
||||
|
|
@ -1,89 +0,0 @@
|
|||
var sendOsc = require('./utils').sendOsc
|
||||
|
||||
module.exports.options = {
|
||||
type:'toggle',
|
||||
id:'auto',
|
||||
linkId:'',
|
||||
|
||||
separator1:'style',
|
||||
|
||||
label:'auto',
|
||||
left:'auto',
|
||||
top:'auto',
|
||||
width:'auto',
|
||||
height:'auto',
|
||||
color:'auto',
|
||||
css:'',
|
||||
|
||||
separator2:'osc',
|
||||
|
||||
on:1,
|
||||
off:0,
|
||||
value:'',
|
||||
precision:2,
|
||||
address:'auto',
|
||||
preArgs:[],
|
||||
target:[]
|
||||
}
|
||||
module.exports.create = function(widgetData,container) {
|
||||
|
||||
var widget = $(`
|
||||
<div class="light">
|
||||
</div>\
|
||||
`),
|
||||
$document = $(document)
|
||||
|
||||
widget.value = widget.find('span')
|
||||
widget.state = 0
|
||||
|
||||
widget.on('drag',function(e){e.stopPropagation()})
|
||||
widget.on('draginit.toggle',function(){
|
||||
widget.off('draginit.toggle')
|
||||
widget.fakeclick()
|
||||
})
|
||||
|
||||
widget.fakeclick = function(){
|
||||
var newVal = widget.state?widgetData.off:widgetData.on
|
||||
widget.setValue(newVal,{sync:true,send:true})
|
||||
$document.on('dragend.toggle',function(){
|
||||
$document.off('dragend.toggle')
|
||||
widget.on('draginit.toggle',function(){
|
||||
widget.off('draginit.toggle')
|
||||
widget.fakeclick()
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
widget.getValue = function() {
|
||||
return widget.state ?
|
||||
widgetData.on != null && widgetData.on.value !== undefined ? widgetData.on.value : widgetData.on
|
||||
:
|
||||
widgetData.off != null && widgetData.off.value !== undefined ? widgetData.off.value : widgetData.off
|
||||
}
|
||||
widget.setValue = function(v,options={}) {
|
||||
if (v===widgetData.on || (widgetData.on != null && v.value === widgetData.on.value && v.value !== undefined)) {
|
||||
widget.addClass('on')
|
||||
widget.state = 1
|
||||
if (options.send) widget.sendValue(widgetData.on)
|
||||
} else if (v===widgetData.off || (widgetData.off != null && v.value === widgetData.off.value && v.value !== undefined)) {
|
||||
widget.removeClass('on')
|
||||
widget.state = 0
|
||||
if (options.send) widget.sendValue(widgetData.off)
|
||||
}
|
||||
|
||||
if (options.sync) widget.trigger({type:'sync',id:widgetData.id,widget:widget, linkId:widgetData.linkId, options})
|
||||
|
||||
}
|
||||
widget.sendValue = function(v) {
|
||||
var args = widgetData.preArgs.concat(v)
|
||||
|
||||
sendOsc({
|
||||
target:widgetData.target,
|
||||
address:widgetData.address,
|
||||
precision:widgetData.precision,
|
||||
args:args
|
||||
})
|
||||
}
|
||||
return widget
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue