Skip to content

Commit

Permalink
feat: demonstrate initializer
Browse files Browse the repository at this point in the history
  • Loading branch information
ctron committed Apr 18, 2024
1 parent 8d13900 commit 515bae8
Show file tree
Hide file tree
Showing 2 changed files with 130 additions and 15 deletions.
113 changes: 113 additions & 0 deletions assets/initializer.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
export default function initializer() {
return {
onStart: () => {
console.log("Loading...");
console.time("trunk-initializer");

document.body.insertAdjacentHTML("afterbegin", `
<div class="pf-v5-l-bullseye">
<div class="pf-v5-l-bullseye__item" style="width: 50%;">
<div id="initializer" class="pf-v5-c-progress">
<div
class="pf-v5-c-progress__description"
id="initializerState"
>Loading</div>
<div id="initializerStatus" class="pf-v5-c-progress__status" aria-hidden="true">
<span id="initializerLabel" class="pf-v5-c-progress__measure">0%</span>
</div>
<div
class="pf-v5-c-progress__bar"
id="initializerBar"
role="progressbar"
aria-valuemin="0"
aria-valuemax="100"
aria-valuenow="0"
aria-labelledby="progress-simple-example-description"
>
<div id="initializerValue" class="pf-v5-c-progress__indicator" style="width:0;"></div>
</div>
</div>
<div id="initializerHelper" class="pf-v5-c-progress__helper-text pf-m-hidden">
<div class="pf-v5-c-helper-text">
<div class="pf-v5-c-helper-text__item">
<span id="initializerHelperText" class="pf-v5-c-helper-text__item-text"></span>
</div>
</div>
</div>
</div>
</div>
`);

},
onProgress: ({current, total}) => {
if (!total) {
console.log("Loading...", current, "bytes");
} else {
const value = Math.round((current / total) * 100)
console.log("Loading...", value, "%")
setProgress(value);
}
},
onComplete: () => {
console.log("Loading... done!");
console.timeEnd("trunk-initializer");
},
onSuccess: (wasm) => {
console.log("Loading... successful!");
setProgress(100);
setState("Complete");
},
onFailure: (error) => {
console.error("Loading... failed!", error);
setState("Failed", error);
}
}
};

function setProgress(value) {
const label = document.getElementById("initializerLabel");
if (label) {
label.innerText = `${value}%`;
}
const bar = document.getElementById("initializerBar");
if (bar) {
bar.ariaValueNow = value;
}
const width = document.getElementById("initializerValue");
if (width) {
width.style.width = `${value}%`;
}
}

function setState(value, helperText) {
const state = document.getElementById("initializerState");
if (state) {
state.innerText = value;
}
if (value === "Failed") {
const component = document.getElementById("initializer");
if (component) {
component.classList.add("pf-m-danger");
}
const status = document.getElementById("initializerStatus");
status.innerHTML = `
<span class="pf-v5-c-progress__status-icon">
<i class="fas fa-fw fa-times-circle" aria-hidden="true"></i>
</span>
`;
}

const helper = document.getElementById("initializerHelper");
if (helper) {
if (helperText === undefined) {
helper.classList.add("pf-m-hidden");
} else {
helper.classList.remove("pf-m-hidden");
const helperContent = document.getElementById("initializerHelperText");
if (helperContent) {
helperContent.innerText = helperText;
}
}
}

}
32 changes: 17 additions & 15 deletions index.html
Original file line number Diff line number Diff line change
@@ -1,25 +1,27 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>PatternFly Yew Quickstart</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>PatternFly Yew Quickstart</title>

<link data-trunk rel="icon" href="/assets/images/Favicon-Light.png">
<link data-trunk rel="icon" type="image/png" sizes="16x16" href="/assets/images/Favicon-Light-16x16.png">
<link data-trunk rel="icon" type="image/png" sizes="32x32" href="/assets/images/Favicon-Light-32x32.png">
<link data-trunk rel="icon" type="image/png" sizes="48x48" href="/assets/images/Favicon-Light-48x48.png">
<link data-trunk rel="icon" href="/assets/images/Favicon-Light.png">
<link data-trunk rel="icon" type="image/png" sizes="16x16" href="/assets/images/Favicon-Light-16x16.png">
<link data-trunk rel="icon" type="image/png" sizes="32x32" href="/assets/images/Favicon-Light-32x32.png">
<link data-trunk rel="icon" type="image/png" sizes="48x48" href="/assets/images/Favicon-Light-48x48.png">

<link data-trunk rel="scss" href="assets/style.scss">
<!-- This path needs to be here, otherwise icons won't render correctly -->
<link data-trunk rel="copy-dir" href="node_modules/@patternfly/patternfly/assets" data-target-path="assets/">
<link data-trunk rel="copy-dir" href="assets/images">
<link data-trunk rel="scss" href="assets/style.scss">
<!-- This path needs to be here, otherwise icons won't render correctly -->
<link data-trunk rel="copy-dir" href="node_modules/@patternfly/patternfly/assets" data-target-path="assets/">
<link data-trunk rel="copy-dir" href="assets/images">

<!-- include when using the icons-far or icons-fab feature -->
<!-- also requires includes in the assets/styles.scss file -->
<link data-trunk rel="copy-dir" href="node_modules/@fortawesome/fontawesome-free/webfonts">
<!-- include when using the icons-far or icons-fab feature -->
<!-- also requires includes in the assets/styles.scss file -->
<link data-trunk rel="copy-dir" href="node_modules/@fortawesome/fontawesome-free/webfonts">

<base data-trunk-public-url/>
<link data-trunk rel="rust" href="Cargo.toml" data-wasm-opt="z" data-initializer="assets/initializer.mjs"/>

<base data-trunk-public-url/>
</head>

<body>
Expand Down

0 comments on commit 515bae8

Please sign in to comment.