1
Fork 0
mirror of https://github.com/Steffo99/bluelib.git synced 2024-12-22 11:34:21 +00:00
bluelib/examples/index.js

218 lines
4.9 KiB
JavaScript
Raw Normal View History

/**
* Check if the less compiler is done rendering Less stylesheets into CSS.
*
* @returns {boolean} `true` if the rendering is complete, `false` otherwise.
*/
function isLessDone() {
2023-05-22 17:08:29 +00:00
const lessSheets = document.querySelectorAll("link[rel=\"stylesheet/less\"]")
const cssSheets = document.querySelectorAll("style")
2023-05-22 17:08:29 +00:00
return lessSheets.length === cssSheets.length
}
/**
* Pause an async function until {@link isLessDone Less is done}.
*
* @returns {Promise<void>} Awaitable that waits until {@link isLessDone Less is done}.
*/
async function sleepUntilLessIsDone() {
2023-05-22 17:08:29 +00:00
while(!isLessDone()) {
await new Promise(resolve => setTimeout(resolve, 100))
console.debug("[Bluelib] Less seems to be done processing.")
2023-05-22 17:08:29 +00:00
}
}
2023-05-23 16:01:52 +00:00
/**
* List of stylesheet ids that should be enabled on page load.
*
* @type {string[]}
*/
const enabledByDefault = [
2023-05-22 17:08:29 +00:00
"less:dist-base:root",
"less:dist-classic:root",
"less:dist-glass:root",
"less:dist-layouts-center:root",
"less:dist-colors-royalblue:root",
"less:dist-fonts-fira-ghpages:root",
]
2023-05-23 16:45:22 +00:00
/**
* List of all `<style>` {@link HTMLElement}s with an id starting with `less:dist`.
*
* @type {{string: HTMLElement} | undefined}
*/
let lessStyles = undefined
2023-05-23 16:45:22 +00:00
/**
* List of all `<style>` {@link HTMLElement}s with an id starting with `less:dist-colors`.
*
* @type {{string: HTMLElement} | undefined}
*/
let lessColors = undefined
2023-05-23 16:45:22 +00:00
/**
* List of all `<style>` {@link HTMLElement}s with an id starting with `less:dist-fonts`.
*
* @type {{string: HTMLElement} | undefined}
*/
let lessFonts = undefined
2023-05-23 16:45:22 +00:00
/**
* The background element.
*
* @type {HTMLElement | undefined}
*/
let background = undefined
2023-05-22 17:08:29 +00:00
2023-05-23 16:45:22 +00:00
/**
* Find the `<style>` {@link HTMLElement}s with an id starting with `less:dist`, and compile them into a {`id`: `element`} object.
*
* @returns {{string: HTMLElement}}
*/
function findLessStyles() {
// noinspection JSValidateTypes
return [...document.styleSheets].filter(
2023-05-22 17:08:29 +00:00
(s) => s.ownerNode.id.startsWith("less:dist"),
).map(
(s) => (
{ [s.ownerNode.id]: s }
),
).reduce(
(p, c) => (
{ ...p, ...c }
),
{},
)
2023-05-23 16:45:22 +00:00
}
2023-05-22 17:08:29 +00:00
2023-05-23 16:45:22 +00:00
/**
* Find the `<style>` {@link HTMLElement}s with an id starting with `less:dist-colors`, and compile them into a {`id`: `element`} object.
*
* @returns {{string: HTMLElement}}
*/
function findLessColors() {
// noinspection JSValidateTypes
return [...document.styleSheets].filter(
2023-05-22 17:08:29 +00:00
(s) => s.ownerNode.id.startsWith("less:dist-colors"),
).map(
(s) => (
{ [s.ownerNode.id]: s }
),
).reduce(
(p, c) => (
{ ...p, ...c }
),
{},
)
2023-05-23 16:45:22 +00:00
}
2023-05-22 17:08:29 +00:00
2023-05-23 16:45:22 +00:00
/**
* Find the `<style>` {@link HTMLElement}s with an id starting with `less:dist-fonts`, and compile them into a {`id`: `element`} object.
*
* @returns {{string: HTMLElement}}
*/
function findLessFonts() {
// noinspection JSValidateTypes
return [...document.styleSheets].filter(
2023-05-22 17:08:29 +00:00
(s) => s.ownerNode.id.startsWith("less:dist-fonts"),
).map(
(s) => (
{ [s.ownerNode.id]: s }
),
).reduce(
(p, c) => (
{ ...p, ...c }
),
{},
)
2023-05-23 16:45:22 +00:00
}
/**
* Find and return the {@link HTMLElement} with the `layout-center-background` class.
*
* @returns {HTMLElement}
*/
function findBackground() {
return document.getElementsByClassName("layout-center-background")[0]
}
/**
* Allows the user to dynamically interact with bluelib via the interactive components available on the page.
*
* Set the values of {@link lessStyles}, {@link lessColors}, {@link lessFonts} and {@link background}.
*
* Sets the enabled state of the all {@link lessStyles} depending on their inclusion in the {@link enabledByDefault} array.
*
* Enables and unfades all input elements with the class `ruleset-toggle`.
*
* @returns {Promise<void>}
*/
async function enableChanges() {
await sleepUntilLessIsDone()
lessStyles = findLessStyles()
console.debug("Found Less stylesheets:", lessStyles)
lessColors = findLessColors()
console.debug("Found Less colors:", lessColors)
lessFonts = findLessFonts()
2023-05-22 17:08:29 +00:00
console.debug("Found Less fonts:", lessFonts)
2023-05-23 16:45:22 +00:00
background = findBackground()
2023-05-22 17:08:29 +00:00
console.debug("Found background:", background)
for(const [k, v] of Object.entries(lessStyles)) {
v.disabled = !enabledByDefault.includes(k)
}
for(const input of document.querySelectorAll(".ruleset-toggle")) {
input.disabled = false
input.parentElement.classList.remove("fade")
}
}
function toggleStyle(name) {
2023-05-22 17:08:29 +00:00
if(lessStyles === undefined) {
console.error("Less stylesheets are not yet available.")
return
}
const style = lessStyles[name]
if(style === undefined) {
console.error("No such Less stylesheet.")
return
}
style.disabled = !style.disabled
}
2022-12-28 17:12:40 +00:00
function selectColor(name, bgsrc) {
2023-05-22 17:08:29 +00:00
if(lessColors === undefined) {
console.error("Less stylesheets are not yet available.")
return
}
const style = lessStyles[name]
if(style === undefined) {
console.error("No such Less stylesheet.")
return
}
for(const c of Object.values(lessColors)) {
c.disabled = true
}
style.disabled = false
background.src = bgsrc
}
enableChanges()