Skip to content

Commit d583fb4

Browse files
committed
double sided linking of toggle
1 parent 2cc80f9 commit d583fb4

File tree

3 files changed

+83
-66
lines changed

3 files changed

+83
-66
lines changed

NetworkDynamicsInspector/src/widgets.jl

Lines changed: 65 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -560,64 +560,65 @@ function _selection_to_jsselection(options, selection)
560560
end
561561
end
562562

563-
struct ToggleSwitch
564-
value::Observable{Bool}
563+
@kwdef struct ToggleSwitch
564+
value::Observable{Bool} = Observable{Bool}()
565+
height::Int = 24
566+
width::Int = 35
567+
inset::Int = 3
568+
checked_color::String = "#2196F3"
569+
background_color::String = "#ccc"
570+
thumb_color::String = "white"
571+
transition_time::Float64 = 0.4
572+
label::String = ""
565573
end
566574

567-
function Bonito.jsrender(session::Session, slider::ToggleSwitch)
568-
# main properties
569-
height = 24
570-
width = 35
571-
inset = 3
572-
checked_color = "#2196F3"
573-
background_color = "#ccc"
574-
thumb_color = "white"
575-
transition_time = 0.4
576-
label = "rel to u0"
575+
function Bonito.jsrender(session::Session, toggle::ToggleSwitch)
576+
thumb_diameter = toggle.height - 2 * toggle.inset
577+
translate = toggle.width - thumb_diameter - 2*toggle.inset
577578

578-
thumb_diameter = height - 2 * inset
579-
translate = width - thumb_diameter - 2*inset
580-
581-
582-
switch_style = Styles(
579+
domlabel_style = Styles(
583580
"position" => "relative",
584581
"display" => "inline-block",
585-
"width" => "$(width)px",
586-
"height" => "$(height)px",
582+
"cursor" => "pointer",
587583
)
588584

589585
input_style = Styles(
590586
CSS(
591587
"opacity" => "0",
592588
"width" => "0",
593589
"height" => "0",
590+
"position" => "absolute",
594591
),
595-
CSS(":checked + .slider",
596-
"background-color" => checked_color,
592+
CSS(":checked + .switch-label-grid .slider",
593+
"background-color" => toggle.checked_color,
597594
),
598-
CSS(":focus + .slider",
599-
"box-shadow" => "0 0 1px $(checked_color)",
595+
CSS(":focus + .switch-label-grid .slider",
596+
"box-shadow" => "0 0 1px $(toggle.checked_color)",
600597
),
601-
CSS(":checked + .slider:before",
598+
CSS(":checked + .switch-label-grid .slider:before",
602599
"-webkit-transform" => "translateX($(translate)px)",
603600
"-ms-transform" => "translateX($(translate)px)",
604601
"transform" => "translateX($(translate)px)",
605602
),
606603
)
607604

605+
slider_grid_style = Styles(
606+
"position" => "relative",
607+
"display" => "inline-grid",
608+
"grid-template-columns" => "auto auto",
609+
"align-items" => "center",
610+
"grid-gap" => "10px",
611+
)
612+
608613
slider_style = Styles(
609614
CSS(
610-
"position" => "absolute",
611-
"cursor" => "pointer",
612-
"top" => "0",
613-
"left" => "0",
614-
"right" => "0",
615-
# "width" => "$(width)px",
615+
"width" => "$(toggle.width)px",
616+
"height" => "$(toggle.height)px",
616617
"bottom" => "0",
617-
"background-color" => background_color,
618-
"-webkit-transition" => "$(transition_time)s",
619-
"transition" => "$(transition_time)s",
620-
"border-radius" => "$(height/2)px",
618+
"background-color" => toggle.background_color,
619+
"-webkit-transition" => "$(toggle.transition_time)s",
620+
"transition" => "$(toggle.transition_time)s",
621+
"border-radius" => "$(toggle.height/2)px",
621622
),
622623
CSS(":hover",
623624
# "background-color" => "#2196F3",
@@ -627,31 +628,41 @@ function Bonito.jsrender(session::Session, slider::ToggleSwitch)
627628
"content" => "''",
628629
"height" => "$(thumb_diameter)px",
629630
"width" => "$(thumb_diameter)px",
630-
"left" => "$(inset)px",
631-
"bottom" => "$(inset)px",
631+
"left" => "$(toggle.inset)px",
632+
"bottom" => "$(toggle.inset)px",
632633
"background-color" => "white",
633-
"-webkit-transition" => "$(transition_time)s",
634-
"transition" => "$(transition_time)s",
634+
"-webkit-transition" => "$(toggle.transition_time)s",
635+
"transition" => "$(toggle.transition_time)s",
635636
"border-radius" => "50%",
636637
)
637638
)
638639

639-
container = DOM.div(
640-
DOM.label(
641-
DOM.input(
642-
type="checkbox",
643-
checked=slider.value[],
644-
onchange=js"""
645-
(e) => {
646-
$(slider.value).notify(e.target.checked);
647-
}
648-
""";
649-
style=input_style
650-
),
651-
DOM.span(; class="slider", style=slider_style),
652-
DOM.span(label; class="sliederlabel");
653-
class="switch", style=switch_style
654-
)
640+
jscode = js"""
641+
(input) => {
642+
input.onchange = function() {
643+
$(toggle.value).notify(this.checked);
644+
}
645+
$(toggle.value).on(val => {
646+
input.checked = val
647+
});
648+
}
649+
"""
650+
651+
inputdom = DOM.input(
652+
type="checkbox",
653+
checked=toggle.value[],
654+
style=input_style
655+
)
656+
onload(session, inputdom, jscode)
657+
658+
container = DOM.label(
659+
inputdom,
660+
DOM.div(
661+
DOM.div(; class="slider", style=slider_style),
662+
DOM.div(toggle.label; class="sliederlabel");
663+
class="switch-label-grid", style=slider_grid_style
664+
);
665+
style=domlabel_style
655666
)
656667
jsrender(session, container)
657668
end
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using NetworkDynamicsInspector, Bonito
2+
3+
tog = Observable{Bool}(true)
4+
let
5+
_app = App() do session
6+
NetworkDynamicsInspector.clear_obs!(tog)
7+
toggle = NetworkDynamicsInspector.ToggleSwitch(value=tog, label="Toggle me")
8+
on(toggle.value) do state
9+
@info "value = $state"
10+
end
11+
DOM.div(
12+
DOM.span("text before"),
13+
toggle,
14+
)
15+
end;
16+
serve_app(_app)
17+
end
18+
tog[] = !tog[]

test/Inspector_test.jl

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -124,15 +124,3 @@ let
124124
end
125125
server_ref[] = Bonito.Server(_app, "0.0.0.0", 8080)
126126
end
127-
128-
tog = Observable{Bool}(false)
129-
on(tog) do state
130-
@info "value = $state"
131-
end
132-
let
133-
_app = App() do session
134-
toggle = NetworkDynamicsInspector.ToggleSwitch(tog)
135-
toggle
136-
end;
137-
serve_app(_app)
138-
end

0 commit comments

Comments
 (0)