Skip to content

Commit 6247a83

Browse files
committed
use tomselect and get rid of JQuery
1 parent 12ee33e commit 6247a83

File tree

3 files changed

+43
-72
lines changed

3 files changed

+43
-72
lines changed

NetworkDynamicsInspector/assets/app.css

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -13,28 +13,6 @@ body {
1313
box-sizing: border-box;
1414
}
1515

16-
.ts-dropdown .optgroup-header{
17-
font-weight: bolder;
18-
}
19-
.ts-dropdown .option{
20-
font-family: monospace !important;
21-
}
22-
.ts-dropdown .option.selected{
23-
background-color: #dddd;
24-
}
25-
26-
.select2-container .select2-selection .select2-selection__rendered,
27-
.select2-container .select2-selection--single .select2-selection__rendered,
28-
.select2-container .select2-results__option{
29-
font-family: monospace;
30-
/* font-size: 20px; */
31-
}
32-
.select2-results__group {
33-
font-family: sans-serif;
34-
/* font-size: 12px; */
35-
/* color: blue; */
36-
}
37-
3816
.maingrid {
3917
display: grid;
4018
grid-template-columns: min-content auto;
@@ -160,3 +138,28 @@ button{
160138
font-weight: normal !important;
161139
font-family: sans-serif !important;;
162140
}
141+
142+
143+
/* Style for TomSelect elements */
144+
.ts-dropdown .optgroup-header{
145+
font-weight: bolder;
146+
}
147+
.ts-dropdown .option{
148+
font-family: monospace !important;
149+
}
150+
.ts-dropdown .option.selected{
151+
background-color: #dddd;
152+
}
153+
.ts-control .item {
154+
font-family: monospace;
155+
}
156+
157+
.ts-control .item::before {
158+
display: inline-block;
159+
margin-right: 6px;
160+
}
161+
162+
.ts-control {
163+
padding: 5px !important;
164+
padding-bottom: 3px !important;
165+
}

NetworkDynamicsInspector/src/timeseries.jl

Lines changed: 12 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -40,28 +40,6 @@ function timeseries_col(app, session)
4040
"""
4141
Bonito.onload(session, col, onload_js)
4242

43-
# HACK: sometimes, the select2 lib is not available after a tsplots change, in that case, trigger again
44-
# from js we trigger recreate_dom which notifies the tsplots
45-
recreate_dom = Observable{Bool}(true)
46-
on(_->notify(app.tsplots), recreate_dom)
47-
jqdocument = Bonito.JSString(raw"$(document)")
48-
esc = Bonito.JSString(raw"$")
49-
rebuild_dom_hack = js"""
50-
(tscol) => {
51-
function dom_changed() {
52-
// wait for ready then check if defined and recreate
53-
if (typeof $(esc).fn.select2 !== 'function') {
54-
console.warn('Select2 is not defined. Calling replot...');
55-
$(recreate_dom).notify(true);
56-
}
57-
}
58-
59-
const observer = new MutationObserver(dom_changed);
60-
observer.observe(tscol, {childList: true, subtree: true});
61-
}
62-
"""
63-
Bonito.onload(session, col, rebuild_dom_hack)
64-
6543
return col
6644
end
6745

@@ -129,14 +107,14 @@ function timeseries_card(app, key, session)
129107
nothing
130108
end
131109

132-
comp_sel = MultiSelect(comp_options, tsplot.selcomp;
110+
comp_sel = TomSelect(comp_options, tsplot.selcomp;
133111
placeholder="Select components",
134112
multi=true,
135113
option_to_string=s -> sidx_to_str(s, app),
136114
T=SymbolicCompIndex,
137115
id=gendomid("compsel"))
138116
# comp_sel_dom = Grid(DOM.span("Components"), comp_sel; columns = "70px 1fr", align_items = "center")
139-
state_sel = MultiSelect(state_options, tsplot.states;
117+
state_sel = TomSelect(state_options, tsplot.states;
140118
placeholder="Select states",
141119
multi=true,
142120
T=Symbol,
@@ -230,14 +208,12 @@ function timeseries_card(app, key, session)
230208
// Generate new styles
231209
let styleContent = "";
232210
items.forEach(({ title, color }) => {
233-
styleContent += `#$(comp_ms_id) +span li[title='${title}']::after {
234-
content: 'xx';
235-
display: inline-block;
236-
padding: 0px 1px;
237-
background-color: ${color} !important;
238-
color: ${color} !important;
239-
border-left: 1px solid #aaa;
240-
cursor: default;
211+
styleContent += `#$(comp_ms_id) +div div[data-value='${title}']::before {
212+
content: '';
213+
background-color: ${color};
214+
width: 12px;
215+
height: 12px;
216+
border-radius: 50%;
241217
}\n`;
242218
});
243219
@@ -268,14 +244,11 @@ function timeseries_card(app, key, session)
268244
269245
if(items.length > 1) {
270246
items.forEach(({ title, linestyle }) => {
271-
styleContent += `#$(state_ms_id) +span li[title='${title}']::after {
247+
styleContent += `#$(state_ms_id) +div div[data-value='${title}']::before {
272248
content: '${linestyle}';
273-
display: inline-block;
274-
padding: 0px 1px;
275249
color: inherit;
276-
border-left: 1px solid #aaa;
277-
font-size: smaller;
278-
cursor: default;
250+
border-right: 1px solid #d0d0d0;
251+
padding-right: 5px;
279252
}\n`;
280253
});
281254
@@ -417,11 +390,9 @@ function timeseries_card(app, key, session)
417390
)
418391

419392
# trigger plot on document ready
420-
jqdocument = Bonito.JSString(raw"$(document)")
421393
trigger_plot = js"""
422-
$(jqdocument).ready(function(){
394+
window.addEventListener('load', function() {
423395
console.log("Document ready, trigger plot");
424-
// $(data).notify();
425396
$(replot).notify();
426397
});
427398
"""

NetworkDynamicsInspector/src/widgets.jl

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -679,19 +679,21 @@ function Bonito.jsrender(session::Session, tomselect::TomSelect{T}) where {T}
679679
items: Array.from($(tsselection).value),
680680
optgroupField: 'class',
681681
placeholder: $(tomselect.placeholder),
682+
hidePlaceholder: true,
682683
plugins: {},
683684
};
684685
// push remove button to plugins when multi
685686
if ($(tomselect.multi)) {
686687
settings.plugins.remove_button = {
687688
title: 'Remove this item'
688689
}
690+
settings.plugins.drag_drop = {}
689691
}
690692
const tomselect = new TomSelect(tom_dom, settings);
691693
692694
function selEvent(){
693695
const newsel = Array.from(tomselect.items);
694-
console.log("Change event, update tsselection", newsel);
696+
//console.log("Change event, update tsselection", newsel);
695697
$(tsselection).notify(newsel);
696698
}
697699
//tomselect.on('item_add', selEvent);
@@ -700,7 +702,7 @@ function Bonito.jsrender(session::Session, tomselect::TomSelect{T}) where {T}
700702
// tomselect.on('item_select', selEvent); // click on tag?
701703
702704
$(tsoptions).on(val => {
703-
console.log("Got new options", val);
705+
//console.log("Got new options", val);
704706
tomselect.clear(true); // false -> silent
705707
tomselect.clearOptions();
706708
tomselect.clearOptionGroups();
@@ -709,12 +711,7 @@ function Bonito.jsrender(session::Session, tomselect::TomSelect{T}) where {T}
709711
tomselect.addOptionGroup(group.value, {label: group.label});
710712
});
711713
tomselect.refreshOptions(false); // false -> dont open
712-
console.log("Finished update of options")
713-
// seting value will update the tsselection
714-
// which triggers check in julia
715-
//console.log("Setting value in option update", $(tsselection).value);
716-
//tomselect.setValue($(tsselection).value);
717-
//tomselect.refreshItems();
714+
//console.log("Finished update of options")
718715
});
719716
720717
function array_equal(a1, a2) {
@@ -728,7 +725,7 @@ function Bonito.jsrender(session::Session, tomselect::TomSelect{T}) where {T}
728725
$(tsselection).on(val => {
729726
const current_items = Array.from(tomselect.items);
730727
if (!array_equal(val, current_items)) {
731-
console.log("Update displayed value of tomselect", val, current_items);
728+
//console.log("Update displayed value of tomselect", val, current_items);
732729
tomselect.setValue(val, true); // do not notify
733730
//tomselect.refreshItems();
734731
}

0 commit comments

Comments
 (0)