widget synchronization and linking is now handled with a single event handler (delegation ftw!)

This commit is contained in:
jean-emmanuel 2016-03-13 15:47:19 +01:00
parent fe35a1333a
commit b1886a101c
8 changed files with 38 additions and 53 deletions

View file

@ -1,6 +1,7 @@
var strip = require('./widgets/strip'),
panel = require('./widgets/panel'),
led = require('./widgets/led'),
plot = require('./widgets/plot'),
fader = require('./widgets/fader'),
knob = require('./widgets/knob'),
xy = require('./widgets/xy'),
@ -16,6 +17,7 @@ module.exports.widgetOptions = {
strip: strip.options,
panel: panel.options,
led: led.options,
plot: plot.options,
fader: fader.options,
knob: knob.options,
xy: xy.options,
@ -32,6 +34,7 @@ module.exports.createWidget = {
strip: strip.create,
panel: panel.create,
led: led.create,
plot: plot.create,
fader: fader.create,
knob: knob.create,
xy: xy.create,
@ -49,49 +52,31 @@ module.exports.categories = {
'Buttons':['toggle','push','switch'],
'Pads':['xy','rgb'],
'Matrix':['multifader','multitoggle','multipush'],
'Plots':['led'],
'Plots':['plot','led'],
'Containers':['panel','strip']
}
module.exports.sync = function() {
// Widget that share the same id will update each other
// without sending any extra osc message
$.each(WIDGETS,function(i,widget) {
if (widget.length>1) {
var closureSync = function(x) {
return function() {
var v = widget[x].getValue()
for (k=0;k<widget.length;k++) {
if (x!=k) {
if (document.body.contains(widget[k][0].parentNode))
widget[k].setValue(v,false,false)
}
}
$('body').off('sync.global').on('sync.global',function(e,id,widget){
// Widget that share the same id will update each other
// without sending any extra osc message
if (WIDGETS[id] && WIDGETS[id].length>1) {
var v = widget.getValue()
for (i in WIDGETS[id]) {
if (WIDGETS[id]!=widget) {
WIDGETS[id][i].setValue(v,false,false)
}
}
for (j in widget) {
widget[j].off('sync.id').on('sync.id',closureSync(j))
}
}
})
// widgets that share the same linkId will update each other.
// Updated widgets will send osc messages normally
$.each(WIDGETS_LINKED,function(i,widget) {
if (widget.length>1) {
var closureSync = function(x) {
return function() {
var v = widget[x].getValue()
for (k=0;k<widget.length;k++) {
if (x!=k) {
if (document.body.contains(widget[k][0].parentNode))
widget[k].setValue(v,true,false)
}
}
// widgets that share the same linkId will update each other.
// Updated widgets will send osc messages normally
if (WIDGETS_LINKED[id] && WIDGETS_LINKED[id].length>1) {
var v = widget.getValue()
for (i in WIDGETS_LINKED[id]) {
if (WIDGETS_LINKED[id]!=widget) {
WIDGETS_LINKED[id][i].setValue(v,true,false)
}
}
for (j in widget) {
widget[j].off('sync.link').on('sync.link',closureSync(j))
}
}
})
}

View file

@ -113,7 +113,7 @@ module.exports.create = function(widgetData,container) {
widget.sendValue(v)
widget.showValue(v)
widget.trigger('sync')
widget.trigger('sync',[widgetData.id,widget])
})
var off = 0
@ -131,7 +131,7 @@ module.exports.create = function(widgetData,container) {
widget.sendValue(v)
widget.showValue(v)
widget.trigger('sync')
widget.trigger('sync',[widgetData.id,widget])
}
@ -156,7 +156,7 @@ module.exports.create = function(widgetData,container) {
widget.sendValue(v)
widget.showValue(v)
widget.trigger('sync')
widget.trigger('sync',[widgetData.id,widget])
})
@ -192,7 +192,7 @@ module.exports.create = function(widgetData,container) {
widget.showValue(v)
if (sync) widget.trigger('sync')
if (sync) widget.trigger('sync',[widgetData.id,widget])
if (send) widget.sendValue(v)
}

View file

@ -88,7 +88,7 @@ module.exports.create = function(widgetData,container) {
widget.sendValue(v)
widget.showValue(v)
widget.trigger('sync')
widget.trigger('sync',[widgetData.id,widget])
})
@ -119,7 +119,7 @@ module.exports.create = function(widgetData,container) {
var v = widget.getValue(r)
widget.sendValue(v)
widget.trigger('sync')
widget.trigger('sync',[widgetData.id,widget])
widget.showValue(v)
}
@ -152,7 +152,7 @@ module.exports.create = function(widgetData,container) {
var v = widget.getValue(r)
widget.sendValue(v)
widget.trigger('sync')
widget.trigger('sync',[widgetData.id,widget])
widget.showValue(v)
})
@ -203,7 +203,7 @@ module.exports.create = function(widgetData,container) {
widget.showValue(v)
if (sync) widget.trigger('sync')
if (sync) widget.trigger('sync',[widgetData.id,widget])
if (send) widget.sendValue(v)
}
input.change(function(){

View file

@ -66,7 +66,7 @@ module.exports.create = function(widgetData,container) {
if (send) widget.sendValue(v)
}
if (sync) widget.trigger('sync')
if (sync) widget.trigger('sync',[widgetData.id,widget])
}
widget.sendValue = function(v) {

View file

@ -92,7 +92,7 @@ module.exports.create = function(widgetData,container) {
var v = widget.getValue()
widget.sendValue(v)
widget.showValue(v)
widget.trigger('sync')
widget.trigger('sync',[widgetData.id,widget])
}
@ -115,7 +115,7 @@ module.exports.create = function(widgetData,container) {
widget.sendValue(v)
widget.showValue(v)
widget.trigger('sync')
widget.trigger('sync',[widgetData.id,widget])
})
@ -138,7 +138,7 @@ module.exports.create = function(widgetData,container) {
widget.sendValue(v)
widget.showValue(v)
widget.trigger('sync')
widget.trigger('sync',[widgetData.id,widget])
}
@ -164,7 +164,7 @@ module.exports.create = function(widgetData,container) {
widget.sendValue(v)
widget.showValue(v)
widget.trigger('sync')
widget.trigger('sync',[widgetData.id,widget])
})
@ -208,7 +208,7 @@ module.exports.create = function(widgetData,container) {
if (sync) widget.trigger('sync')
if (sync) widget.trigger('sync',[widgetData.id,widget])
if (send) widget.sendValue(v)
}
widget.sendValue = function(v) {

View file

@ -65,7 +65,7 @@ module.exports.create = function(widgetData,container) {
next()
})
if (send) widget.sendValue(widget.value)
if (sync) widget.trigger('sync')
if (sync) widget.trigger('sync',[widgetData.id,widget])
}
}

View file

@ -67,7 +67,7 @@ module.exports.create = function(widgetData,container) {
if (send) widget.sendValue(widgetData.off)
}
if (sync) widget.trigger('sync')
if (sync) widget.trigger('sync',[widgetData.id,widget])
}
widget.sendValue = function(v) {

View file

@ -86,7 +86,7 @@ module.exports.create = function(widgetData,container) {
var v = widget.getValue()
widget.sendValue(v)
widget.showValue(v)
widget.trigger('sync')
widget.trigger('sync',[widgetData.id,widget])
}
@ -109,7 +109,7 @@ module.exports.create = function(widgetData,container) {
var v = widget.getValue()
widget.sendValue(v)
widget.showValue(v)
widget.trigger('sync')
widget.trigger('sync',[widgetData.id,widget])
})
@ -141,7 +141,7 @@ module.exports.create = function(widgetData,container) {
handle.width = w
widget.showValue(v)
if (sync) widget.trigger('sync')
if (sync) widget.trigger('sync',[widgetData.id,widget])
if (send) widget.sendValue(v)
}
widget.sendValue = function(v) {