boxespy/static/self.js

161 lines
4.5 KiB
JavaScript
Raw Normal View History

function showThumbnail(img_link) {
const img = document.getElementById("sample-preview");
img.src = img_link;
img.style.height = "auto";
img.style.display = "block";
}
function showThumbnailEvt(evt) {
const url = evt.target.getAttribute("data-thumbnail");
showThumbnail(url);
}
function hideThumbnail() {
const img = document.getElementById("sample-preview");
img.style.display = "none";
}
function toggleId(id) {
const e = document.getElementById(id);
const h = document.getElementById("h-" + id);
if (e.style.display == null || e.style.display === "none") {
e.style.display = "block";
h.classList.add("open");
h.setAttribute("aria-expanded", "true");
} else {
e.style.display = "none";
h.classList.remove("open");
h.setAttribute("aria-expanded", "false");
}
}
function toggleEl(el) {
const id = el.getAttribute("data-id");
toggleId(id);
}
function toggleEvt(evt) {
const id = evt.target.getAttribute("data-id");
// https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/Roles/button_role#examples
if (evt instanceof MouseEvent) {
toggleId(id);
}
if (evt instanceof KeyboardEvent && (evt.key === "Enter" || evt.key === " ")) {
evt.preventDefault();
toggleId(id);
}
}
function initToggle(el, hide = false) {
// Add event handler.
el.addEventListener("click", toggleEvt);
el.addEventListener("keydown", toggleEvt);
// Hide.
if (hide) {
toggleEl(el);
}
}
function initThumbnail(el) {
// Add event handler.
el.addEventListener("mouseenter", showThumbnailEvt);
el.addEventListener("mouseleave", hideThumbnail);
}
function initPage(num_hide = null) {
const h = document.getElementsByClassName("toggle");
let i = 0;
for (let el of h) {
if (num_hide === null || i < num_hide) {
initToggle(el, true);
} else {
initToggle(el, false);
}
i++;
}
const t = document.getElementsByClassName("thumbnail");
for (let el of t) initThumbnail(el);
}
function GridfinityTrayLayout_GenerateLayout(x, y, nx, ny, countx, county) {
// x = width in mm
// y = height in mm
// nx # of gridfinity grids in X
// ny # of gridfinity grids in Y
// countx split x into this many
// county split y into this many
layout = '';
if (countx == 0)
countx = nx;
if (county == 0)
county = ny
stepx = x / countx;
stepy = y / county;
for (i = 0; i < countx; i++) {
line = ' |'.repeat(i) + ` ,> ${stepx}mm\n`;
layout += line;
}
for (i = 0; i < county; i++) {
layout += "+-".repeat(countx) + "+\n";
layout += "| ".repeat(countx) + `|${stepy}mm\n`;
}
layout += "+-".repeat(countx) + "+\n";
return layout
}
function GridfinityTrayUpdateLayout(event) {
console.log("update");
if (window.layoutUpdated == true) {
// Don't do the update if the layout has been manually touched.
if (confirm("You have manually updated the Layout. Do you wish to regenerate it?")) {
window.layoutUpdated = false;
} else {
return;
}
}
console.log("updating");
nx = document.getElementById('nx').value;
ny = document.getElementById('ny').value;
countx = document.getElementById('countx').value;
county = document.getElementById('county').value;
margin = document.getElementById('margin').value;
x = nx*42 - margin
y = ny*42 - margin
layout_id = document.getElementById('layout');
layout_id.value = GridfinityTrayLayout_GenerateLayout(x, y, nx, ny, countx, county);
}
function setUpdated() {
console.log("this was updated");
window.layoutUpdated=true;
}
function GridfinityTrayLayoutInit() {
console.log("update init");
ids = ['nx', 'ny', 'countx', 'county', 'margin'];
window.layoutUpdated=false;
for (id_string of ids) {
id = document.getElementById(id_string);
id.addEventListener('input', GridfinityTrayUpdateLayout);
}
layout_id = document.getElementById('layout');
layout_id.addEventListener('change', setUpdated);
layout_id.addEventListener('input', setUpdated);
GridfinityTrayUpdateLayout();
layout_id = document.getElementById('layout');
layout_id.rows = 20;
layout_id.cols = 24;
}
function addCallbacks() {
if (window.location.href.includes("/GridfinityTrayLayout"))
GridfinityTrayLayoutInit();
}
document.addEventListener('DOMContentLoaded', function() {
addCallbacks();
}, false);