Skip to content

Commit 8c4a74c

Browse files
committed
js in dom rather than evaljs/onload
1 parent fbc7f71 commit 8c4a74c

File tree

4 files changed

+46
-45
lines changed

4 files changed

+46
-45
lines changed

NetworkDynamicsInspector/src/NetworkDynamicsInspector.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ function get_webapp(app)
6767

6868
clear_obs!(app)
6969

70-
resize_gp = js"""
70+
resize_with_gp = js"""
7171
const graphplotCard = document.querySelector(".graphplot-card");
7272
7373
// Function to update the width dynamically
@@ -100,7 +100,6 @@ function get_webapp(app)
100100
// Initial update
101101
updateResizeWithGpWidth();
102102
"""
103-
Bonito.evaljs(session, resize_gp)
104103

105104
DOM.div(
106105
APP_CSS,
@@ -110,6 +109,7 @@ function get_webapp(app)
110109
gpstate_control_card(app, :vertex),
111110
gpstate_control_card(app, :edge),
112111
element_info_card(app, session),
112+
resize_with_gp;
113113
class="graphplot-col"
114114
),
115115
timeseries_col(app, session),
@@ -139,6 +139,7 @@ function inspect(sol; restart=false, reset=false, display=CURRENT_DISPLAY[])
139139
CURRENT_DISPLAY[] = display
140140

141141
appstate = if reset || isnothing(APPSTATE[])
142+
isnothing(APPSTATE[]) || clear_obs!(APPSTATE[])
142143
AppState(sol)
143144
else
144145
APPSTATE[].sol[] = sol
@@ -242,7 +243,6 @@ function element_info_card(app, session)
242243
$(eltext).on(replaceDivContentWithHTML);
243244
replaceDivContentWithHTML($(eltext).value);
244245
"""
245-
Bonito.evaljs(session, js)
246246

247247
help = HoverHelp(html"""
248248
Show details on last clicked element.
@@ -251,7 +251,7 @@ function element_info_card(app, session)
251251
""")
252252

253253
Card(
254-
[DOM.div(;id="element-info-box"), help];
254+
[DOM.div(;id="element-info-box"), help, js];
255255
class="bonito-card element-info-card resize-with-gp"
256256
)
257257
end

NetworkDynamicsInspector/src/graphplot.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ function graphplot_card(app, session)
130130
####
131131
tooltip_text = Observable{String}("")
132132

133-
js = js"""
133+
gp_tooltip_js = js"""
134134
const gpcard = document.querySelector(".graphplot-card");
135135
136136
// Create tooltip element
@@ -177,7 +177,6 @@ function graphplot_card(app, session)
177177
}
178178
});
179179
"""
180-
Bonito.evaljs(session, js)
181180

182181
nhh = NodeHoverHandler() do hstate, idx, event, axis
183182
tooltip_text[] = hstate ? sidx_to_str(VIndex(idx), app) : ""
@@ -220,7 +219,8 @@ function graphplot_card(app, session)
220219
<li><strong>Ctrl + Click</strong> resets axis after zoom</li>
221220
</ul>
222221
""")
223-
Card([WithConfig(fig; resize_to=:parent), help]; class="bonito-card graphplot-card")
222+
Card([WithConfig(fig; resize_to=:parent), help, gp_tooltip_js];
223+
class="bonito-card graphplot-card")
224224
end
225225
function _gracefully_extract_states!(vec, sol, t, idxs, rel)
226226
isvalid(s) = SII.is_variable(sol, s) || SII.is_parameter(sol, s) || SII.is_observed(sol, s)

NetworkDynamicsInspector/src/timeseries.jl

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,7 @@
11
function timeseries_col(app, session)
2-
col = DOM.div(
3-
timeslider_card(app),
4-
timeseries_cards(app, session),
5-
add_timeseries_button(app),
6-
class="timeseries-col"
7-
)
8-
92
# add js to update the active ts plot
103
onload_js = js"""
11-
(tscol) => {
4+
const tscol = document.querySelector(".timeseries-col");
125
tscol.addEventListener("click", function(event) {
136
// Find the closest .timeseries-card ancestor of the clicked element
147
const clickedCard = event.target.closest('.timeseries-card');
@@ -36,11 +29,15 @@ function timeseries_col(app, session)
3629
const triggerWindowResize_throttled = Bonito.throttle_function(triggerWindowResize, 100);
3730
const resizeObserver = new ResizeObserver(triggerWindowResize_throttled);
3831
resizeObserver.observe(tscol);
39-
}
4032
"""
41-
Bonito.onload(session, col, onload_js)
4233

43-
return col
34+
DOM.div(
35+
timeslider_card(app),
36+
timeseries_cards(app, session),
37+
add_timeseries_button(app),
38+
onload_js;
39+
class="timeseries-col"
40+
)
4441
end
4542

4643
function timeseries_cards(app, session)
@@ -224,7 +221,7 @@ function timeseries_card(app, key, session)
224221
comp_ms_id = Bonito.JSString(comp_sel.id)
225222
state_ms_id = Bonito.JSString(state_sel.id)
226223

227-
js = js"""
224+
legendlike_style_js = js"""
228225
function colorListItems(items) {
229226
let styleTag = document.getElementById("dynamic-style-$(comp_ms_id)");
230227
@@ -294,7 +291,6 @@ function timeseries_card(app, key, session)
294291
linestyleListItems(c);
295292
});
296293
"""
297-
Bonito.evaljs(session, js)
298294

299295
fig, ax = with_theme(apptheme()) do
300296
fig = Figure(size=(100, 400))
@@ -444,6 +440,7 @@ function timeseries_card(app, key, session)
444440
card = Card(
445441
[DOM.div(
446442
comp_state_sel_dom,
443+
legendlike_style_js,
447444
DOM.div(WithConfig(fig; resize_to=:parent); class="timeseries-axis-container"),
448445
closebutton(app, key);
449446
class="timeseries-card-container"

NetworkDynamicsInspector/src/widgets.jl

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -117,11 +117,12 @@ function Bonito.jsrender(session::Session, slider::ContinuousSlider)
117117
thumb_r = DOM.div(; style=thumb_style)
118118
track = DOM.div(; style=track_style)
119119
track_active = DOM.div(; style=track_active_style)
120-
container = DOM.div(track, track_active, thumb_l, thumb_r; style=container_style)
120+
121+
containerid = gendomid("slider")
121122

122123
# JavaScript for interactivity
123124
jscode = js"""
124-
(container)=> {
125+
const container = document.getElementById($(containerid));
125126
const thumb_r = $(thumb_r);
126127
const thumb_l = $(thumb_l);
127128
const track_active = $(track_active);
@@ -140,6 +141,8 @@ function Bonito.jsrender(session::Session, slider::ContinuousSlider)
140141
function thumb_event(e, thumb) {
141142
let new_pos = e.clientX - container.getBoundingClientRect().left;
142143
const width = track.offsetWidth;
144+
let llim;
145+
let rlim;
143146
if (thumb==='r') {
144147
llim = (e.shiftKey) ? (Math.ceil(width/2)+1) : thumbpos_l+1;
145148
rlim = width;
@@ -299,9 +302,9 @@ function Bonito.jsrender(session::Session, slider::ContinuousSlider)
299302
track_active.style.width = (thumbpos_r-thumbpos_l) + 'px'; // Update the active track
300303
});
301304
observer.observe(container);
302-
}
303305
"""
304-
Bonito.onload(session, container, jscode)
306+
container = DOM.div(track, track_active, thumb_l, thumb_r, jscode;
307+
style=container_style, id=containerid)
305308

306309
return Bonito.jsrender(session, container)
307310
end
@@ -399,13 +402,6 @@ function Bonito.jsrender(session::Session, tomselect::TomSelect{T}) where {T}
399402
id=tomselect.id,
400403
)
401404

402-
# node fence see https://github.com/electron/electron/issues/254
403-
container = DOM.div(
404-
TOMSELECT_ESS,
405-
TOMSELECT_CSS,
406-
tom_dom
407-
)
408-
409405
js_init = js"""
410406
($TOMSELECT_ESS).then((ts) => {
411407
const tom_dom = document.getElementById($(tomselect.id));
@@ -487,6 +483,11 @@ function Bonito.jsrender(session::Session, tomselect::TomSelect{T}) where {T}
487483
});
488484
"""
489485
Bonito.evaljs(session, js_init)
486+
container = DOM.div(
487+
TOMSELECT_ESS,
488+
TOMSELECT_CSS,
489+
tom_dom,
490+
)
490491

491492
return Bonito.jsrender(session, container)
492493
end
@@ -617,23 +618,25 @@ function Bonito.jsrender(session::Session, toggle::ToggleSwitch)
617618
"position" => "relative",
618619
)
619620

620-
jscode = js"""
621-
(input) => {
622-
input.onchange = function() {
623-
$(toggle.value).notify(this.checked);
624-
}
625-
$(toggle.value).on(val => {
626-
input.checked = val
627-
});
628-
}
629-
"""
630-
621+
id = gendomid("toggle")
631622
inputdom = DOM.input(
632623
type="checkbox",
633624
checked=toggle.value[],
634-
style=input_style
625+
style=input_style,
626+
id=id,
635627
)
636-
Bonito.onload(session, inputdom, jscode)
628+
629+
jscode = js"""
630+
const input = document.getElementById($(id));
631+
input.onchange = function() {
632+
$(toggle.value).notify(input.checked);
633+
}
634+
635+
$(toggle.value).on(val => {
636+
console.log("callback toogle switch", val, input)
637+
input.checked = val
638+
});
639+
"""
637640

638641
container = DOM.label(
639642
inputdom,
@@ -645,7 +648,8 @@ function Bonito.jsrender(session::Session, toggle::ToggleSwitch)
645648
),
646649
DOM.div(toggle.label; class="sliederlabel");
647650
class="switch-label-grid", style=slider_grid_style
648-
);
651+
),
652+
jscode;
649653
style=domlabel_style
650654
)
651655
Bonito.jsrender(session, container)

0 commit comments

Comments
 (0)