moved packages out of pkg

This commit is contained in:
2026-02-04 18:57:33 +11:00
parent d2b1a252ea
commit 20308fe35c
24 changed files with 41 additions and 36 deletions

View File

@@ -0,0 +1,17 @@
function copyToClipboard(elementId, buttonId) {
const element = document.getElementById(elementId);
const button = document.getElementById(buttonId);
navigator.clipboard.writeText(element.innerText)
.then(() => {
const originalText = button.innerText;
button.innerText = 'Copied!';
setTimeout(() => {
button.innerText = originalText;
}, 2000);
})
.catch(err => {
console.error('Failed to copy:', err);
button.innerText = 'Failed';
});
}

View File

@@ -0,0 +1,45 @@
function paginateData(
formID,
rootPath,
initPage,
initPerPage,
initOrder,
initOrderBy,
) {
return {
page: initPage,
perPage: initPerPage,
order: initOrder || "ASC",
orderBy: initOrderBy || "name",
goToPage(n) {
this.page = n;
this.submit();
},
handleSortChange(value) {
const [field, direction] = value.split("|");
this.orderBy = field;
this.order = direction;
this.page = 1; // Reset to first page when sorting
this.submit();
},
setPerPage(n) {
this.perPage = n;
this.page = 1; // Reset to first page when changing per page
this.submit();
},
submit() {
var url = `${rootPath}?page=${this.page}&per_page=${this.perPage}&order=${this.order}&order_by=${this.orderBy}`;
htmx.find("#pagination-page").value = this.page;
htmx.find("#pagination-per-page").value = this.perPage;
htmx.find("#sort-order").value = this.order;
htmx.find("#sort-order-by").value = this.orderBy;
htmx.find(`#${formID}`).setAttribute("hx-post", url);
htmx.process(`#${formID}`);
htmx.trigger(`#${formID}`, "submit");
},
};
}

View File

@@ -0,0 +1,15 @@
// This function prevents the 'flash of unstyled content'
// Include it at the top of <head>
(function() {
let theme = localStorage.getItem("theme") || "system";
if (theme === "system") {
theme = window.matchMedia("(prefers-color-scheme: dark)").matches
? "dark"
: "light";
}
if (theme === "dark") {
document.documentElement.classList.add("dark");
} else {
document.documentElement.classList.remove("dark");
}
})();

View File

@@ -0,0 +1,54 @@
function progressBar(toastId, duration) {
const progressBar = {
progress: 0,
paused: false,
animateToastProgress() {
const toast = document.getElementById(toastId);
if (!toast) return;
const bar = document.getElementById([toastId, "progress"].join("-"));
const startTime = performance.now();
let totalPausedTime = 0;
let pauseStartTime = null;
const animate = (currentTime) => {
const toast = document.getElementById(toastId);
if (!toast) return; // Toast was manually removed
if (this.paused) {
// Track when pause started
if (pauseStartTime === null) {
pauseStartTime = currentTime;
}
// Keep animating while paused
requestAnimationFrame(animate);
return;
} else {
// If we were paused, accumulate the paused time
if (pauseStartTime !== null) {
totalPausedTime += currentTime - pauseStartTime;
pauseStartTime = null;
}
}
// Calculate actual elapsed time (excluding paused time)
const elapsed = currentTime - startTime - totalPausedTime;
this.progress = Math.min((elapsed / duration) * 100, 100) + "%";
bar.style["width"] = this.progress;
if (elapsed >= duration) {
toast.remove()
} else {
requestAnimationFrame(animate);
}
};
requestAnimationFrame(animate);
}
};
progressBar.animateToastProgress();
return progressBar;
}