switch values can be defined with label:value pairs wrapped in an object

This commit is contained in:
jean-emmanuel 2016-02-16 12:52:03 +01:00
parent 7be0c07565
commit b5291824ed

View file

@ -112,7 +112,7 @@ widgetOptions = {
separator2:'osc',
values:[0,1],
values:{"Value 1":1,"Value 2":2},
path:'auto',
target:[]
},
@ -404,8 +404,11 @@ createWidget.switch = function(widgetData) {
widget.addClass('horizontal')
}
for (v in widgetData.values) {
widget.append('<div class="value" data-value="'+widgetData.values[v]+'">'+widgetData.values[v]+'</div>')
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
@ -413,8 +416,8 @@ createWidget.switch = function(widgetData) {
widget.on('drag',function(e){e.stopPropagation()})
widget.on('draginit',function(e,dd){
var val = dd.target.getAttribute('data-value')
if (val!=widget.value || widget.value===undefined) widget.setValue(val,true,true)
var data = $(dd.target).data()
if (data.value!=widget.value || widget.value===undefined) widget.setValue(data.value,true,true)
})
@ -423,12 +426,11 @@ createWidget.switch = function(widgetData) {
return widget.value
}
widget.setValue = function(v,send,sync) {
var e = widget.find('.value[data-value="'+v+'"]'),
i = e.index()
if (e.length) {
widget.value = widgetData.values[i]
var i = widget.values.indexOf(v)
if (i!=-1) {
widget.value = widget.values[i]
widget.find('.on').removeClass('on')
e.addClass('on').ripple()
widget.find('.value').eq(i).addClass('on').ripple()
if (send) widget.sendValue(widget.value)
if (sync) widget.trigger('sync')
}