buttons: modularize remaining widgets

This commit is contained in:
jean-emmanuel 2016-12-05 19:59:25 +01:00
parent 15d869a5f6
commit 74741eed4e
5 changed files with 216 additions and 198 deletions

View file

@ -5,8 +5,8 @@ var widgets = {
// buttons
toggle: require('./widgets/buttons/toggle'),
switch: require('./widgets/switch'),
push: require('./widgets/push'),
push: require('./widgets/buttons/push'),
switch: require('./widgets/buttons/switch'),
// pads
xy: require('./widgets/pads/xy'),

View file

@ -0,0 +1,117 @@
var _widgets_base = require('../common/_widgets_base'),
$document = $(document)
module.exports.options = {
type:'push',
id:'auto',
linkId:'',
separator1:'style',
label:'auto',
left:'auto',
top:'auto',
width:'auto',
height:'auto',
color:'auto',
css:'',
separator2:'osc',
on:1,
off:0,
norelease:false,
precision:2,
address:'auto',
preArgs:[],
target:[]
}
var Push = module.exports.Push = function(widgetData,container) {
_widgets_base.apply(this, arguments)
this.widget = $(`
<div class="light">
</div>\
`)
this.state = 0
this.active = 0
this.lastChanged = 'state'
this.widget.on('drag',function(e){e.stopPropagation()})
this.widget.on('draginit.push',()=>{
this.widget.off('draginit.push')
this.fakeclick()
})
this.widget.getValue = ()=>{
return this[this.lastChanged] ?
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.widget.setValue = this.setValue.bind(this)
}
Push.prototype = Object.create(_widgets_base.prototype)
Push.prototype.constructor = Push
Push.prototype.fakeclick = function(){
if (!this.active) this.setValuePrivate(this.widgetData.on,{send:true,sync:true})
$document.on('dragend.push',()=>{
this.setValuePrivate(this.widgetData.off,{send:true,sync:true})
$document.off('dragend.push')
this.widget.on('draginit.push',()=>{
this.widget.off('draginit.push')
this.fakeclick()
})
})
}
Push.prototype.setValuePrivate = function(v,options={}) {
if (v===this.widgetData.on || (this.widgetData.on != null && v.value === this.widgetData.on.value && v.value !== undefined)) {
this.widget.addClass('active')
this.active = 1
if (options.send) this.sendValue(v)
this.lastChanged = 'active'
if (options.sync) this.widget.trigger({type:'sync',id:this.widgetData.id,widget:this.widget, linkId:this.widgetData.linkId, options:options})
} else if (v===this.widgetData.off || (this.widgetData.off != null && v.value === this.widgetData.off.value && v.value !== undefined)) {
this.widget.removeClass('active')
this.active = 0
if (options.send) this.sendValue(v, this.widgetData.norelease)
this.lastChanged = 'active'
if (options.sync) this.widget.trigger({type:'sync',id:this.widgetData.id,widget:this.widget, linkId:this.widgetData.linkId, options:options})
}
}
Push.prototype.setValue = function(v,options={}) {
if (!options.fromExternal) {
if (options.send || options.sync) this.setValuePrivate(v,options)
return
}
if (v===this.widgetData.on || (this.widgetData.on != null && v.value === this.widgetData.on.value && v.value !== undefined)) {
this.widget.addClass('on')
this.state = 1
if (options.send) this.sendValue(v)
this.lastChanged = 'state'
if (options.sync) this.widget.trigger({type:'sync',id:this.widgetData.id,widget:this.widget, linkId:this.widgetData.linkId,options:options})
} else if (v===this.widgetData.off || (this.widgetData.off != null && v.value === this.widgetData.off.value && v.value !== undefined)) {
this.widget.removeClass('on')
this.state = 0
if (options.send) this.sendValue(v)
this.lastChanged = 'state'
if (options.sync) this.widget.trigger({type:'sync',id:this.widgetData.id,widget:this.widget, linkId:this.widgetData.linkId,options:options})
}
}
module.exports.create = function(widgetData, container) {
var push = new Push(widgetData, container)
return push.widget
}

View file

@ -0,0 +1,97 @@
var _widgets_base = require('../common/_widgets_base'),
$document = $(document)
module.exports.options = {
type:'switch',
id:'auto',
linkId:'',
separator1:'style',
label:'auto',
left:'auto',
top:'auto',
width:'auto',
height:'auto',
horizontal:false,
color:'auto',
css:'',
separator2:'osc',
values:{"Value 1":1,"Value 2":2},
value:'',
precision:2,
address:'auto',
preArgs:[],
target:[]
}
var Switch = module.exports.Switch = function(widgetData,container) {
_widgets_base.apply(this, arguments)
this.widget = $(`
<div class="switch">
</div>
`)
if (this.widgetData.horizontal) {
this.widget.addClass('horizontal')
}
this.values = []
for (k in this.widgetData.values) {
this.values.push(this.widgetData.values[k])
$('<div class="value">'+k+'</div>').data({value:this.widgetData.values[k]}).appendTo(this.widget)
}
this.value = undefined
this.widget.find('.value').on('draginit',(e,dd)=>{
var index = 0,
node = e.target
while ( (node = node.previousSibling) ) {
if (node.nodeType != 3 || !/^\s*$/.test(node.data)) {
index++;
}
}
var value = this.values[index]
if (value!=this.value || this.value===undefined) this.setValue(value,{sync:true,send:true})
})
this.widget.getValue = ()=>{
return this.value
}
this.widget.setValue = this.setValue.bind(this)
}
Switch.prototype = Object.create(_widgets_base.prototype)
Switch.prototype.constructor = Switch
Switch.prototype.setValue = function(v,options={}) {
var i = this.values.indexOf(v)
if (i!=-1) {
this.value = this.values[i]
this.widget.find('.on').removeClass('on')
this.widget.find('.value').eq(i).addClass('on')
if (options.send) this.sendValue(this.value)
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 _switch = new Switch(widgetData, container)
return _switch.widget
}

View file

@ -1,115 +0,0 @@
var sendOsc = require('./utils').sendOsc
module.exports.options = {
type:'push',
id:'auto',
linkId:'',
separator1:'style',
label:'auto',
left:'auto',
top:'auto',
width:'auto',
height:'auto',
color:'auto',
css:'',
separator2:'osc',
on:1,
off:0,
norelease:false,
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.active = 0
widget.lastChanged = 'state'
widget.on('drag',function(e){e.stopPropagation()})
widget.on('draginit.push',function(){
widget.off('draginit.push')
widget.fakeclick()
})
widget.fakeclick = function(){
if (!widget.active) widget.setValuePrivate(widgetData.on,{send:true,sync:true})
$document.on('dragend.push',function(){
widget.setValuePrivate(widgetData.off,{send:true,sync:true})
$document.off('dragend.push')
widget.on('draginit.push',function(){
widget.off('draginit.push')
widget.fakeclick()
})
})
}
widget.getValue = function() {
return widget[widget.lastChanged] ?
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.setValuePrivate = function(v,options={}) {
if (v===widgetData.on || (widgetData.on != null && v.value === widgetData.on.value && v.value !== undefined)) {
widget.addClass('active')
widget.active = 1
if (options.send) widget.sendValue(v)
widget.lastChanged = 'active'
if (options.sync) widget.trigger({type:'sync',id:widgetData.id,widget:widget, linkId:widgetData.linkId, options:options})
} else if (v===widgetData.off || (widgetData.off != null && v.value === widgetData.off.value && v.value !== undefined)) {
widget.removeClass('active')
widget.active = 0
if (options.send) widget.sendValue(v, widgetData.norelease)
widget.lastChanged = 'active'
if (options.sync) widget.trigger({type:'sync',id:widgetData.id,widget:widget, linkId:widgetData.linkId, options:options})
}
}
widget.setValue = function(v,options={}) {
if (!options.fromExternal) {
if (options.send || options.sync) widget.setValuePrivate(v,options)
return
}
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(v)
widget.lastChanged = 'state'
if (options.sync) widget.trigger({type:'sync',id:widgetData.id,widget:widget, linkId:widgetData.linkId,options:options})
} 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(v)
widget.lastChanged = 'state'
if (options.sync) widget.trigger({type:'sync',id:widgetData.id,widget:widget, linkId:widgetData.linkId,options:options})
}
}
widget.sendValue = function(v, norelease) {
var args = widgetData.preArgs.concat(v)
sendOsc({
target:widgetData.target,
address:widgetData.address,
precision:widgetData.precision,
args:args,
syncOnly:norelease
})
}
return widget
}

View file

@ -1,81 +0,0 @@
var sendOsc = require('./utils').sendOsc
module.exports.options = {
type:'switch',
id:'auto',
linkId:'',
separator1:'style',
label:'auto',
left:'auto',
top:'auto',
width:'auto',
height:'auto',
horizontal:false,
color:'auto',
css:'',
separator2:'osc',
values:{"Value 1":1,"Value 2":2},
value:'',
precision:2,
address:'auto',
preArgs:[],
target:[]
}
module.exports.create = function(widgetData,container) {
var widget = $(`
<div class="switch">
</div>
`),
$document = $(document)
if (widgetData.horizontal) {
widget.addClass('horizontal')
}
widget.values = []
for (k in widgetData.values) {
widget.values.push(widgetData.values[k])
$('<div class="value">'+k+'</div>').data({value:widgetData.values[k]}).appendTo(widget)
}
widget.value = undefined
widget.find('.value').on('draginit',function(e,dd){
var data = $(this).data()
if (data.value!=widget.value || widget.value===undefined) widget.setValue(data.value,{sync:true,send:true})
})
widget.getValue = function() {
return widget.value
}
widget.setValue = function(v,options={}) {
var i = widget.values.indexOf(v)
if (i!=-1) {
widget.value = widget.values[i]
widget.find('.on').removeClass('on')
widget.find('.value').eq(i).addClass('on')
if (options.send) widget.sendValue(widget.value)
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
}