Skip to content

Commit fe268f4

Browse files
committed
bundle tomselect with sqlpage
1 parent 12cc7cf commit fe268f4

File tree

9 files changed

+42
-21
lines changed

9 files changed

+42
-21
lines changed

build.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ async fn main() {
1717
spawn(download_deps("sqlpage.css")),
1818
spawn(download_deps("tabler-icons.svg")),
1919
spawn(download_deps("apexcharts.js")),
20+
spawn(download_deps("tomselect.js")),
2021
] {
2122
h.await.unwrap();
2223
}

sqlpage/apexcharts.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,4 +190,4 @@ function bubbleTooltip({ series, seriesIndex, dataPointIndex, w }) {
190190
return tooltip.outerHTML;
191191
}
192192

193-
sqlpage_chart();
193+
add_init_function(sqlpage_chart);

sqlpage/sqlpage.js

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,6 @@
11
/* !include https://cdn.jsdelivr.net/npm/@tabler/[email protected]/dist/js/tabler.min.js */
22
/* !include https://cdn.jsdelivr.net/npm/[email protected]/dist/list.min.js */
33

4-
function sqlpage_chart() {
5-
let first_chart = document.querySelector("[data-pre-init=chart]");
6-
if (first_chart) {
7-
// Add the apexcharts js to the page
8-
const apexcharts_js = document.createElement("script");
9-
apexcharts_js.src = first_chart.dataset.js;
10-
document.head.appendChild(apexcharts_js);
11-
}
12-
}
13-
144
function sqlpage_card() {
155
for (const c of document.querySelectorAll("[data-pre-init=card]")) {
166
const source = c.dataset.embed;
@@ -151,14 +141,24 @@ function get_tabler_color(name) {
151141
return getComputedStyle(document.documentElement).getPropertyValue('--tblr-' + name);
152142
}
153143

154-
function init_components() {
155-
sqlpage_table();
156-
sqlpage_chart();
157-
sqlpage_map();
158-
sqlpage_card();
159-
sqlpage_select_dropdown();
144+
function load_scripts() {
145+
let addjs = document.querySelectorAll("[data-sqlpage-js]");
146+
for (const js of new Set([...addjs].map(({dataset}) => dataset.sqlpageJs))) {
147+
const script = document.createElement("script");
148+
script.src = js;
149+
document.head.appendChild(script);
150+
}
160151
}
161152

162-
document.addEventListener('DOMContentLoaded', init_components);
153+
function add_init_function(f) {
154+
document.addEventListener('DOMContentLoaded', f);
155+
document.addEventListener('fragment-loaded', f);
156+
if (document.readyState !== "loading") f();
157+
}
163158

164-
document.addEventListener('fragment-loaded', init_components);
159+
add_init_function(function init_components() {
160+
sqlpage_table();
161+
sqlpage_map();
162+
sqlpage_card();
163+
load_scripts();
164+
});

sqlpage/templates/chart.handlebars

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
{{#if id}}id="{{id}}"{{/if}}
33
class="card my-2 {{class}}"
44
data-pre-init="chart"
5-
data-js="/{{static_path 'apexcharts.js'}}"
5+
data-sqlpage-js="/{{static_path 'apexcharts.js'}}"
66
>
77
<div class="card-body">
88
<div class="d-flex">

sqlpage/templates/form.handlebars

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,10 @@
6363
{{~#if required}} required="required" {{/if~}}
6464
{{~#if autofocus}} autofocus {{/if~}}
6565
{{~#if multiple}} multiple {{/if~}}
66-
{{~#if dropdown}} data-pre-init="select-dropdown" {{/if~}}
66+
{{~#if dropdown}}
67+
data-pre-init="select-dropdown"
68+
data-sqlpage-js="/{{static_path 'tomselect.js'}}"
69+
{{/if~}}
6770
{{~#if placeholder}} placeholder="{{placeholder}}" {{/if~}}
6871
{{~#if create_new}} data-create_new={{create_new}} {{/if~}}
6972
>

sqlpage/tomselect.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/* !include https://cdn.jsdelivr.net/npm/[email protected]/dist/js/tom-select.popular.min.js */
2+
3+
function sqlpage_select_dropdown() {
4+
for (const s of document.querySelectorAll("[data-pre-init=select-dropdown]")) {
5+
new TomSelect(s, {
6+
create: s.dataset.create_new
7+
});
8+
}
9+
}
10+
11+
add_init_function(sqlpage_select_dropdown);

src/template_helpers.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ fn static_path_helper(v: &JsonValue) -> anyhow::Result<JsonValue> {
136136
"sqlpage.js" => Ok(static_filename!("sqlpage.js").into()),
137137
"sqlpage.css" => Ok(static_filename!("sqlpage.css").into()),
138138
"apexcharts.js" => Ok(static_filename!("apexcharts.js").into()),
139+
"tomselect.js" => Ok(static_filename!("tomselect.js").into()),
139140
other => Err(anyhow::anyhow!("unknown static file: {other:?}")),
140141
}
141142
}

src/webserver/http.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -483,6 +483,7 @@ pub fn create_app(
483483
App::new()
484484
.service(static_content::js())
485485
.service(static_content::apexcharts_js())
486+
.service(static_content::tomselect_js())
486487
.service(static_content::css())
487488
.service(static_content::icons())
488489
.default_service(fn_service(main_handler))

src/webserver/static_content.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ pub fn apexcharts_js() -> Resource {
3838
static_file_endpoint!("apexcharts", "js", "application/javascript")
3939
}
4040

41+
pub fn tomselect_js() -> Resource {
42+
static_file_endpoint!("tomselect", "js", "application/javascript")
43+
}
44+
4145
pub fn css() -> Resource {
4246
static_file_endpoint!("sqlpage", "css", "text/css")
4347
}

0 commit comments

Comments
 (0)