Files
supermarket/internal/web/static/app.js
T
davidandClaude Opus 5.5 a89121f1ec Redesign the UI on brain's look: sidebar with search, Overview, modules, grouped Settings
- Left sidebar as in brain: search at the top (press /), Overview,
  Inventory, the modules (Content with Inbox, Sources, Scheduled and
  Published; later phases greyed out), Settings at the bottom; a top bar
  with breadcrumbs, and a menu that slides in on narrow screens.
- brain's colour tokens, type, radius and shadows, with a dark theme that
  follows the system setting.
- Settings as grouped tabs: Profile; Social accounts (was Credentials, now
  keys and sign-in together per network); Core & Hermes; AI, which now
  holds the API keys too. Old addresses redirect.
- /search matches every word across posts, fetched items, the inventory,
  products and settings pages.
- Overview: a card per module, then the setup checklist, folded once done.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-21 07:50:24 -07:00

129 lines
4.6 KiB
JavaScript

// Small behaviours for Supermarket's pages. Loaded as a file because the
// Content-Security-Policy allows no inline scripts.
(function () {
"use strict";
// Show the fields for the chosen AI provider ([data-show-for]).
function syncProvider() {
var chosen = document.querySelector('input[data-toggle="provider"]:checked');
if (!chosen) return;
document.querySelectorAll("[data-show-for]").forEach(function (el) {
el.hidden = el.getAttribute("data-show-for") !== chosen.value;
});
}
document.addEventListener("change", function (e) {
if (e.target.matches('input[data-toggle="provider"]')) syncProvider();
});
// Press / to search (unless typing somewhere already).
document.addEventListener("keydown", function (e) {
if (e.key !== "/" || e.ctrlKey || e.metaKey || e.altKey) return;
var t = e.target;
if (t.isContentEditable || /^(INPUT|TEXTAREA|SELECT)$/.test(t.tagName)) return;
var box = document.getElementById("search-box");
if (box) {
e.preventDefault();
box.focus();
box.select();
}
});
document.addEventListener("click", function (e) {
// The menu on narrow screens.
if (e.target.closest("[data-nav-toggle]")) {
document.body.classList.toggle("nav-open");
return;
}
if (e.target.closest("[data-nav-close]")) {
document.body.classList.remove("nav-open");
return;
}
// Copy a <pre>'s text: <button data-copy="id-of-pre">
var copy = e.target.closest("[data-copy]");
if (copy) {
var src = document.getElementById(copy.getAttribute("data-copy"));
if (src && navigator.clipboard) {
navigator.clipboard.writeText(src.textContent).then(function () {
var label = copy.textContent;
copy.textContent = "Copied";
setTimeout(function () { copy.textContent = label; }, 1500);
});
}
return;
}
// Fill a field from a list: <button data-fill="field-name" data-value="v">
var fill = e.target.closest("[data-fill]");
if (fill) {
var field = document.querySelector('[name="' + fill.getAttribute("data-fill") + '"]');
if (field) {
field.value = fill.getAttribute("data-value");
field.focus();
}
}
});
// Ask before submitting a form with data-confirm. A form with data-busy
// shows that text on the button that submitted it, and can't be sent twice,
// while a slow request (fetching, writing, publishing) runs. With
// data-busy-on, only that button's value counts as slow.
document.addEventListener("submit", function (e) {
var form = e.target;
var msg = form.getAttribute("data-confirm");
if (msg && !window.confirm(msg)) {
e.preventDefault();
return;
}
var busy = form.getAttribute("data-busy");
if (!busy) return;
var btn = e.submitter;
var only = form.getAttribute("data-busy-on");
if (only && (!btn || btn.value !== only)) return;
if (form.dataset.sending) {
e.preventDefault();
return;
}
form.dataset.sending = "1";
if (btn) {
// A disabled button isn't submitted, so carry its value in a hidden field.
if (btn.name) {
var keep = document.createElement("input");
keep.type = "hidden";
keep.name = btn.name;
keep.value = btn.value;
form.appendChild(keep);
}
btn.textContent = busy;
setTimeout(function () { btn.disabled = true; }, 0);
}
});
// Live length of a post: <textarea data-count="x|linkedin" data-limit
// data-reserve> followed by its <p class="count">. X counts most characters
// outside Latin, punctuation and symbols as 2, like the server does.
function weighted(s) {
var n = 0;
for (var ch of s) {
var c = ch.codePointAt(0);
if (c <= 0x10ff || (c >= 0x2000 && c <= 0x200d) || (c >= 0x2010 && c <= 0x201f) || (c >= 0x2032 && c <= 0x2037)) n += 1;
else n += 2;
}
return n;
}
function count(area) {
var out = document.getElementById(area.getAttribute("aria-describedby"));
if (!out) return;
var limit = parseInt(area.getAttribute("data-limit"), 10);
var reserve = parseInt(area.getAttribute("data-reserve") || "0", 10);
var text = area.value.trim();
var n = area.getAttribute("data-count") === "x" ? weighted(text) + reserve : Array.from(text).length;
out.textContent = n + " / " + limit + (reserve ? " including the link" : "");
out.classList.toggle("over", n > limit);
}
document.addEventListener("input", function (e) {
if (e.target.matches("textarea[data-count]")) count(e.target);
});
document.addEventListener("DOMContentLoaded", syncProvider);
})();