Skip to content

Commit d91b4a4

Browse files
authored
support multiple option in togglebuttons (#137)
1 parent 3198551 commit d91b4a4

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

src/optioninput.jl

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -346,26 +346,37 @@ wdg[:options][] = ["c", "d", "e"]
346346
function togglebuttons(T::WidgetTheme, options::AbstractObservable;
347347
className = "",
348348
activeclass = getclass(:button, "active"),
349+
multiple = false,
349350
index = nothing, value = automatic,
350351
container = node(:div, className = getclass(:togglebuttons)), wrap=identity,
351352
label = nothing, readout = false, vskip = 1em, kwargs...)
352353

353354
vals2idxs = map(Vals2Idxs, options)
354-
p = initvalueindex(value, index, vals2idxs; default = first(vals2idxs[]))
355+
p = initvalueindex(value, index, vals2idxs; multiple = multiple)
355356
value, index = p.first, p.second
356357

357358
className = mergeclasses("interact-widget", getclass(:button), className)
358-
updateSelected = js_lambda("\$root.index(val)")
359+
360+
updateMethod = multiple ? js"""
361+
function (val) {
362+
var id = this.index.indexOf(val);
363+
id > -1 ? this.index.splice(id, 1) : this.index.push(val);
364+
}
365+
""" : js"function (val) {this.index(val)}"
366+
active = multiple ? "\$root.index().includes(val)" : "\$root.index() == val"
367+
368+
updateSelected = js_lambda("\$root.update(val)")
369+
359370
btn = node(:span,
360371
node(:label, attributes = Dict("data-bind" => "text : key")),
361372
attributes=Dict("data-bind"=>
362-
"click: $updateSelected, css: {'$activeclass' : \$root.index() == val, '$className' : true}"),
373+
"click: $updateSelected, css: {'$activeclass' : $active, '$className' : true}"),
363374
)
364375
option_array = _js_array(options)
365376
template = container(attributes = "data-bind" => "foreach : options_js")(wrap(btn))
366377

367378
label != nothing && (template = flex_row(wdglabel(label), template))
368-
ui = knockout(template, ["index" => index, "options_js" => option_array])
379+
ui = knockout(template, ["index" => index, "options_js" => option_array], methods = Dict("update" => updateMethod))
369380
slap_design!(ui)
370381

371382
w = Widget{:togglebuttons}(["options"=>options, "index" => ui["index"], "vals2idxs" => vals2idxs];

0 commit comments

Comments
 (0)