-
Notifications
You must be signed in to change notification settings - Fork 138
feat: add lazy component #615
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,24 +3,30 @@ | |
|
||
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]")) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe this should be done in parallel rather than sequentially ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am not sure if I understand you. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is the query better ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes it's better ! |
||
if (c.ariaBusy === "true") continue; | ||
c.ariaBusy = true; | ||
let url; | ||
try { | ||
url = new URL(c.dataset.embed, window.location.href) | ||
} catch { | ||
console.erreur(`'${c.dataset.embed}' is not a valid url`) | ||
mtt-artis marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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 +182,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 +209,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); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<div class="{{default class "my-2"}}"{{~#if style}} style="{{style}}" {{/if~}}> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In all other components, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i know and i hesitate, this div is a light wrapper and i think user should be able to reset it easily if needed. same for should we just give no default and handle it like the style param ? |
||
{{#each_row}} | ||
<div data-embed="{{embed}}"> | ||
<div class="{{default class "card my-2"}}" {{~#if style}} style="{{style}}" {{/if~}}> | ||
<div class="h-full w-full p-2 d-flex align-items-center justify-content-center"> | ||
<div class="spinner-border" role="status" style="width: 3rem; height: 3rem;"> | ||
<span class="visually-hidden">Loading...</span> | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
{{/each_row}} | ||
</div> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should we keep
as embed
oras lazy
here ?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think
as embed
is good