website-risg/templates/static/app.js

125 lines
3.5 KiB
JavaScript
Raw Normal View History

2024-02-19 22:08:57 +01:00
document.addEventListener("DOMContentLoaded", function () {
changeTheme();
initcolorTheme();
apply_stickies();
2024-02-21 14:39:20 +01:00
initSearch();
2024-02-17 11:27:54 +01:00
});
2024-02-21 14:39:20 +01:00
function initSearch() {
const input = document.querySelector('#filter-js');
if(input) {
filterElements(input.value);
input.addEventListener('input', () => {
filterElements(input.value);
});
}
}
function filterElements(input) {
const elements = document.querySelectorAll('[data-filterable="true"]');
Array.prototype.forEach.call(elements, (element) => {
// set both strings (input & dataset filter) to lowercase to not be case sensitive
let filterString = element.dataset.filter?.toLocaleLowerCase();
// bulk hide all elements
element.style.display = 'none';
// show if input matches
if(filterString?.includes(input.toLocaleLowerCase())) {
element.style.display = 'flex';
}
});
}
2024-02-19 18:11:02 +01:00
function changeTheme() {
2024-02-19 22:08:57 +01:00
let toggleBtn = document.querySelector("#theme-toggle-js");
2024-02-17 11:27:54 +01:00
2024-02-19 22:08:57 +01:00
if (toggleBtn) {
toggleBtn.addEventListener("click", function () {
if (toggleBtn.dataset.theme === "light") {
setTheme("dark", true);
} else {
setTheme("light", true);
}
});
}
2024-02-19 18:11:02 +01:00
}
2024-02-17 11:27:54 +01:00
2024-02-19 18:11:02 +01:00
/***
2024-02-19 22:08:57 +01:00
* init javascript
* 1) detect native color scheme or use set theme in local storage
* 2) detect view (desktop or responsive) if on mobile device with touch screen
* 3) set base font size to 112.5% -> 18px
*/
2024-02-19 18:11:02 +01:00
function initcolorTheme() {
2024-02-19 22:08:57 +01:00
colorThemeWatcher();
let theme = localStorage.getItem("theme");
if (theme == null || theme === "auto") {
if (window.matchMedia && window.matchMedia("(prefers-color-scheme: dark)").matches) {
setTheme("dark", false);
} else {
setTheme("light", false);
}
} else {
setTheme(theme);
}
2024-02-19 18:11:02 +01:00
}
/***
2024-02-19 22:08:57 +01:00
* Listener operating system native color configuration
*/
2024-02-19 18:11:02 +01:00
function colorThemeWatcher() {
2024-02-19 22:08:57 +01:00
try {
window.matchMedia("(prefers-color-scheme: dark)").addEventListener("change", (e) => {
setTheme(e.matches ? "dark" : "light");
});
} catch {
console.warn("color theme watcher not supported");
}
2024-02-19 18:11:02 +01:00
}
/**
2024-02-19 22:08:57 +01:00
* Define color scheme, colors without losing the base font size configuration
* and add data-theme to html tag
* @param theme
*/
2024-02-19 18:11:02 +01:00
function setTheme(theme, setLocalStorage = true) {
2024-02-19 22:08:57 +01:00
let toggleBtn = document.querySelector("#theme-toggle-js");
2024-02-19 18:11:02 +01:00
2024-02-19 22:08:57 +01:00
if (setLocalStorage) {
localStorage.setItem("theme", theme);
}
if (toggleBtn) toggleBtn.setAttribute("data-theme", theme);
2024-02-19 18:11:02 +01:00
2024-02-19 22:08:57 +01:00
if (document.documentElement.dataset.theme === "dark" && theme === "light") {
document.documentElement.dataset.theme = "light";
} else if (document.documentElement.dataset.theme === "light" && theme === "dark") {
document.documentElement.dataset.theme = "dark";
}
2024-02-17 11:27:54 +01:00
}
2024-02-19 22:08:57 +01:00
window.addEventListener("scroll", function () {
apply_stickies();
});
function apply_stickies() {
var _$stickies = [].slice.call(document.querySelectorAll('details[open] .sticky'))
_$stickies.forEach(function (_$sticky) {
2024-02-19 22:08:57 +01:00
if (CSS.supports && CSS.supports("position", "sticky")) {
apply_sticky_class(_$sticky);
}
2024-02-19 22:08:57 +01:00
});
}
function apply_sticky_class(_$sticky) {
2024-02-19 22:08:57 +01:00
var currentOffset = _$sticky.getBoundingClientRect().top;
var stickyOffset = parseInt(getComputedStyle(_$sticky).top.replace("px", ""));
var isStuck = currentOffset <= stickyOffset;
2024-02-19 22:08:57 +01:00
_$sticky.classList.toggle("js-is-sticky", isStuck);
}