BlowfishCMS
A small content management system for OpenBSD
version 0.45.6 · ISC · 2026-12-03 · releases
62 lines · 2.8 KB · download this file
// Store's Products screen: how many of each limited product are left. The
// shop's own service counts the orders; it publishes the numbers at
// /x/store/stock.json, which this fills in beside each product.
(() => {
const spots = document.querySelectorAll('[data-store-stock]');
if (!spots.length) return;
fetch('/x/store/stock.json', { credentials: 'same-origin', cache: 'no-store' })
.then((r) => (r.ok ? r.json() : null))
.then((left) => {
if (!left) return;
spots.forEach((el) => {
const n = left[el.dataset.storeStock];
if (n !== undefined) el.textContent = n + ' of ' + el.dataset.total + ' left';
});
})
.catch(() => {});
})();
// Who makes a product: only the chosen printer's fields are shown, and
// Printful's items are offered for its field. This screen can't read
// Printful's key, so the shop's service lists them (/admin/x/store/pod/).
(() => {
const maker = document.querySelector('[data-store-pod]');
if (!maker) return;
const form = maker.form;
const parts = form.querySelectorAll('[data-pod-for]');
const shipping = form.querySelector('[name=shipping][type=checkbox]');
let items = null;
function show() {
parts.forEach((el) => { el.hidden = el.dataset.podFor !== maker.value; });
if (maker.value && shipping) shipping.checked = true;
if (maker.value === 'printful') load();
}
function load() {
if (items) return;
items = [];
const note = form.querySelector('[data-printful-note]');
const list = document.getElementById('store-printful-items');
fetch('/admin/x/store/pod/printful.json', { credentials: 'same-origin', cache: 'no-store' })
.then((r) => (r.ok ? r.json() : null))
.then((d) => {
if (!d || !d.ok) { if (note && d && d.message) note.textContent = d.message; return; }
items = d.items || [];
items.forEach((v) => list.append(new Option(v.name.startsWith(v.product) ? v.name : v.product + ': ' + v.name, String(v.id))));
if (note) note.textContent = items.length
? 'Its sync variant ID: type or pick one of your ' + items.length + ' Printful items.'
: 'Your Printful store has nothing in it yet.';
})
.catch(() => {});
}
// A Printful item picked for a new product gives it its name and price.
const pick = form.querySelector('[name=pod_printful]');
pick?.addEventListener('change', () => {
const v = (items || []).find((i) => String(i.id) === pick.value.trim());
if (!v) return;
const title = form.querySelector('[name=title]'), price = form.querySelector('[name=price]');
if (title && !title.value.trim()) title.value = v.name;
if (price && !price.value.trim() && v.currency === price.dataset.currency) price.value = v.price;
});
maker.addEventListener('change', show);
show();
})();