diff --git a/examples/cards-with-remote-content/lazy.sql b/examples/cards-with-remote-content/lazy.sql new file mode 100644 index 00000000..0feaa7e4 --- /dev/null +++ b/examples/cards-with-remote-content/lazy.sql @@ -0,0 +1,10 @@ +select 'lazy' as component; + +select + '/chart-example.sql?_sqlpage_embed' as embed, + 'card my-2' as class, + 'height:340px' as style; + +select '/map-example.sql' as embed; + +select '/table-example.sql?_sqlpage_embed' as embed; diff --git a/sqlpage/sqlpage.js b/sqlpage/sqlpage.js index caa2d71f..2af69507 100644 --- a/sqlpage/sqlpage.js +++ b/sqlpage/sqlpage.js @@ -3,24 +3,29 @@ const nonce = document.currentScript.nonce; -function sqlpage_card() { - for (const c of document.querySelectorAll("[data-pre-init=card]")) { - const source = c.dataset.embed; - fetch(c.dataset.embed) - .then(res => res.text()) - .then(html => { - const body = c.querySelector(".card-content"); - body.innerHTML = html; - c.removeAttribute("data-pre-init"); - const spinner = c.querySelector(".card-loading-placeholder"); - if (spinner) { - spinner.parentNode.removeChild(spinner); - } - const fragLoadedEvt = new CustomEvent("fragment-loaded", { - bubbles: true - }); - c.dispatchEvent(fragLoadedEvt); - }) +function sqlpage_embed() { + for (const c of document.querySelectorAll("[data-embed]:not([aria-busy=true])")) { + c.ariaBusy = true; + let url; + try { + url = new URL(c.dataset.embed, window.location.href) + } catch { + console.error(`'${c.dataset.embed}' is not a valid url`) + continue; + } + url.searchParams.set("_sqlpage_embed", ""); + + fetch(url) + .then(res => res.text()) + .then(html => { + c.innerHTML = html; + c.ariaBusy = false; + delete c.dataset.embed; + c.dispatchEvent(new CustomEvent("fragment-loaded", { + bubbles: true + })); + }) + .catch(err => console.error("Fetch error: ", err)); } } @@ -176,9 +181,14 @@ function sqlpage_form() { } } -function get_tabler_color(name) { - return getComputedStyle(document.documentElement).getPropertyValue('--tblr-' + name); +function create_tabler_color() { + const style = getComputedStyle(document.documentElement); + return function get_tabler_color(name) { + return style.getPropertyValue('--tblr-' + name); } +} + +const get_tabler_color = create_tabler_color(); function load_scripts() { let addjs = document.querySelectorAll("[data-sqlpage-js]"); @@ -198,6 +208,6 @@ function add_init_fn(f) { add_init_fn(sqlpage_table); add_init_fn(sqlpage_map); -add_init_fn(sqlpage_card); +add_init_fn(sqlpage_embed); add_init_fn(sqlpage_form); -add_init_fn(load_scripts); \ No newline at end of file +add_init_fn(load_scripts); diff --git a/sqlpage/templates/card.handlebars b/sqlpage/templates/card.handlebars index 2e42431c..236e80c5 100644 --- a/sqlpage/templates/card.handlebars +++ b/sqlpage/templates/card.handlebars @@ -26,8 +26,7 @@
+ "> {{#if link}} {{/if}} @@ -39,13 +38,19 @@ {{/if}}
{{#if title}}

{{title}}

{{/if}} -
+
{{~description~}} {{~#if description_md~}} {{{markdown description_md}}} {{~/if~}} + {{#if (and embed (ne embed_mode "iframe"))}} +
+ Loading... +
+ {{~/if~}}
- {{~#if embed ~}} {{~#if (eq embed_mode "iframe")}} - {{~else~}} -
- Loading... -
- {{~/if~}} {{~/if~}}
{{#if link}} @@ -67,11 +67,11 @@ {{/if}} {{#if footer}}
{{/if}} {{#if footer_md}} diff --git a/sqlpage/templates/lazy.handlebars b/sqlpage/templates/lazy.handlebars new file mode 100644 index 00000000..b853b5ea --- /dev/null +++ b/sqlpage/templates/lazy.handlebars @@ -0,0 +1,13 @@ +
+ {{#each_row}} +
+
+
+
+ Loading... +
+
+
+
+ {{/each_row}} +
\ No newline at end of file