v1.5.0
AtheraStudio Code
- (function () {
- "use strict";
-
- var A = window.Athera;
- var util = A.util;
-
- var LIMIT = 500;
- var HISTORY = 50;
- var MIN_H = 90;
-
- var root, body, input, grip, toggleBtn, badgeError, badgeWarn, filterBtns;
- var handlers = { jump: null, evaluate: null };
- var entries = [];
- var filter = "all";
- var open = false;
- var history = [];
- var cursor = 0;
- var counts = { error: 0, warn: 0 };
- var seq = 0;
-
- function matches(entry) {
- if (filter === "all") return true;
- if (filter === "error") return entry.level === "error";
- if (filter === "warn") return entry.level === "warn";
- return entry.level !== "error" && entry.level !== "warn";
- }
-
- function sameAs(a, b) {
- return a && b && a.level === b.level && a.text === b.text && a.source === b.source &&
- a.path === b.path && a.line === b.line && a.group === b.group && a.level !== "input" && a.level !== "result";
- }
-
- function updateBadges() {
- badgeError.hidden = !counts.error;
- badgeError.textContent = String(counts.error);
- badgeWarn.hidden = !counts.warn;
- badgeWarn.textContent = String(counts.warn);
- filterBtns.forEach(function (b) {
- var key = b.getAttribute("data-filter");
- b.classList.toggle("is-active", key === filter);
- var n = b.querySelector("[data-n]");
- if (n) n.textContent = key === "error" && counts.error ? String(counts.error) : key === "warn" && counts.warn ? String(counts.warn) : "";
- });
- }
-
- function buildRow(entry) {
- var row = util.el("div", { class: "ath-console-row is-" + entry.level });
- if (entry.group) row.style.paddingLeft = 10 + entry.group * 14 + "px";
- var icon = entry.level === "error" ? "x" : entry.level === "warn" ? "warning" : "";
- var lead = util.el("span", { class: "ath-console-lead" });
- if (icon) lead.innerHTML = A.icon(icon);
- row.appendChild(lead);
- if (entry.source === "php") row.appendChild(util.el("span", { class: "ath-console-tag" }, "PHP"));
- row.appendChild(util.el("pre", { class: "ath-console-text" }, entry.text));
- var n = util.el("span", { class: "ath-console-repeat", hidden: true }, "");
- row.appendChild(n);
- if (entry.label) {
- if (entry.path != null && handlers.jump) {
- var btn = util.el("button", { type: "button", class: "ath-console-loc", title: entry.path || entry.label }, entry.label);
- btn.addEventListener("click", function () { handlers.jump(entry.path, entry.line, entry.col); });
- row.appendChild(btn);
- } else {
- row.appendChild(util.el("span", { class: "ath-console-loc is-plain" }, entry.label));
- }
- }
- entry.node = row;
- entry.repeatNode = n;
- return row;
- }
-
- function nearBottom() {
- return body.scrollHeight - body.scrollTop - body.clientHeight < 28;
- }
-
- function placeholder() {
- if (body.querySelector(".ath-console-row")) return;
- body.innerHTML = "";
- body.appendChild(util.el("div", { class: "ath-console-empty" }, A.t("console.empty")));
- }
-
- function renderAll() {
- body.innerHTML = "";
- entries.forEach(function (e) { if (matches(e)) body.appendChild(buildRow(e)); else e.node = null; });
- placeholder();
- updateBadges();
- body.scrollTop = body.scrollHeight;
- }
-
- function add(entry) {
- var last = entries[entries.length - 1];
- if (sameAs(last, entry)) {
- last.n = (last.n || 1) + 1;
- if (last.level === "error") counts.error++;
- if (last.level === "warn") counts.warn++;
- if (last.repeatNode) {
- last.repeatNode.hidden = false;
- last.repeatNode.textContent = String(last.n);
- }
- updateBadges();
- return;
- }
- entry.n = 1;
- entry.seq = ++seq;
- entries.push(entry);
- if (entry.level === "error") counts.error++;
- if (entry.level === "warn") counts.warn++;
- if (entries.length > LIMIT) {
- var old = entries.shift();
- if (old.node && old.node.parentNode) old.node.parentNode.removeChild(old.node);
- }
- if (matches(entry)) {
- var stick = nearBottom();
- var empty = body.querySelector(".ath-console-empty");
- if (empty) empty.remove();
- body.appendChild(buildRow(entry));
- if (stick || entry.level === "input") body.scrollTop = body.scrollHeight;
- }
- updateBadges();
- }
-
- function setOpen(on) {
- open = !!on;
- root.classList.toggle("is-open", open);
- toggleBtn.setAttribute("aria-expanded", String(open));
- A.store.set("console", open);
- if (open) body.scrollTop = body.scrollHeight;
- }
-
- function initGrip() {
- function clamp(h) {
- var max = Math.max(MIN_H, Math.round(root.parentNode.clientHeight * 0.7));
- return Math.max(MIN_H, Math.min(max, h));
- }
- var saved = Number(A.store.get("consoleH", 0));
- if (saved) root.style.setProperty("--ath-console-h", clamp(saved) + "px");
- grip.addEventListener("pointerdown", function (e) {
- e.preventDefault();
- var startY = e.clientY;
- var startH = body.offsetHeight;
- grip.setPointerCapture(e.pointerId);
- root.classList.add("is-resizing");
- function move(ev) {
- root.style.setProperty("--ath-console-h", clamp(startH + startY - ev.clientY) + "px");
- }
- function up() {
- grip.removeEventListener("pointermove", move);
- grip.removeEventListener("pointerup", up);
- grip.removeEventListener("pointercancel", up);
- root.classList.remove("is-resizing");
- A.store.set("consoleH", body.offsetHeight);
- }
- grip.addEventListener("pointermove", move);
- grip.addEventListener("pointerup", up);
- grip.addEventListener("pointercancel", up);
- });
- }
-
- function initInput() {
- input.addEventListener("keydown", function (e) {
- if (e.key === "Enter") {
- var code = input.value.trim();
- if (!code) return;
- e.preventDefault();
- if (history[history.length - 1] !== code) history.push(code);
- if (history.length > HISTORY) history.shift();
- cursor = history.length;
- input.value = "";
- add({ level: "input", text: code, source: "js", group: 0 });
- if (!handlers.evaluate || handlers.evaluate(code) === false) {
- add({ level: "error", text: A.t("console.no_page"), source: "js", group: 0, kind: "eval" });
- }
- } else if (e.key === "ArrowUp") {
- if (!history.length) return;
- e.preventDefault();
- cursor = Math.max(0, cursor - 1);
- input.value = history[cursor] || "";
- } else if (e.key === "ArrowDown") {
- if (!history.length) return;
- e.preventDefault();
- cursor = Math.min(history.length, cursor + 1);
- input.value = history[cursor] || "";
- } else if (e.key === "Escape") {
- input.blur();
- }
- });
- }
-
- A.console = {
- init: function (el, h) {
- root = el;
- handlers = h || handlers;
- body = el.querySelector("[data-console-body]");
- input = el.querySelector("[data-console-input]");
- grip = el.querySelector("[data-console-grip]");
- toggleBtn = el.querySelector("[data-console-toggle]");
- badgeError = el.querySelector("[data-console-errors]");
- badgeWarn = el.querySelector("[data-console-warnings]");
- filterBtns = [].slice.call(el.querySelectorAll("[data-filter]"));
-
- toggleBtn.addEventListener("click", function () { setOpen(!open); });
- el.querySelector("[data-console-clear]").addEventListener("click", function () { A.console.reset(); });
- filterBtns.forEach(function (b) {
- b.addEventListener("click", function () {
- filter = b.getAttribute("data-filter");
- renderAll();
- });
- });
- initGrip();
- initInput();
- body.addEventListener("transitionend", function (e) {
- if (e.target === body && e.propertyName === "height" && open) body.scrollTop = body.scrollHeight;
- });
- setOpen(A.store.get("console", false));
- A.console.reset();
- requestAnimationFrame(function () {
- requestAnimationFrame(function () { root.classList.add("is-armed"); });
- });
- },
-
- reset: function () {
- entries = [];
- counts.error = 0;
- counts.warn = 0;
- body.innerHTML = "";
- placeholder();
- updateBadges();
- },
-
- add: add,
-
- entries: function () {
- return entries.map(function (e) {
- return { seq: e.seq, level: e.level, text: e.text, source: e.source, label: e.label || "", path: e.path, line: e.line, n: e.n || 1 };
- });
- },
-
- counts: function () {
- return { error: counts.error, warn: counts.warn };
- },
-
- addMany: function (list) {
- (list || []).forEach(add);
- },
-
- setVisible: function (on) {
- root.hidden = !on;
- },
-
- setOpen: setOpen,
-
- toggle: function () {
- setOpen(!open);
- if (open) input.focus();
- },
-
- isOpen: function () {
- return open;
- }
- };
- })();
-