@@ -346,26 +346,37 @@ wdg[:options][] = ["c", "d", "e"]
346
346
function togglebuttons (T:: WidgetTheme , options:: AbstractObservable ;
347
347
className = " " ,
348
348
activeclass = getclass (:button , " active" ),
349
+ multiple = false ,
349
350
index = nothing , value = automatic,
350
351
container = node (:div , className = getclass (:togglebuttons )), wrap= identity,
351
352
label = nothing , readout = false , vskip = 1 em, kwargs... )
352
353
353
354
vals2idxs = map (Vals2Idxs, options)
354
- p = initvalueindex (value, index, vals2idxs; default = first (vals2idxs[]) )
355
+ p = initvalueindex (value, index, vals2idxs; multiple = multiple )
355
356
value, index = p. first, p. second
356
357
357
358
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
+
359
370
btn = node (:span ,
360
371
node (:label , attributes = Dict (" data-bind" => " text : key" )),
361
372
attributes= Dict (" data-bind" =>
362
- " click: $updateSelected , css: {'$activeclass ' : \$ root.index() == val , '$className ' : true}" ),
373
+ " click: $updateSelected , css: {'$activeclass ' : $active , '$className ' : true}" ),
363
374
)
364
375
option_array = _js_array (options)
365
376
template = container (attributes = " data-bind" => " foreach : options_js" )(wrap (btn))
366
377
367
378
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) )
369
380
slap_design! (ui)
370
381
371
382
w = Widget {:togglebuttons} ([" options" => options, " index" => ui[" index" ], " vals2idxs" => vals2idxs];
0 commit comments