WebOrbiton
v1.5.0

AtheraStudio Code

261 lines · 8.6 KB
  1. (function () {
  2. "use strict";
  3. ​
  4. var A = window.Athera;
  5. var util = A.util;
  6. ​
  7. var LIMIT = 500;
  8. var HISTORY = 50;
  9. var MIN_H = 90;
  10. ​
  11. var root, body, input, grip, toggleBtn, badgeError, badgeWarn, filterBtns;
  12. var handlers = { jump: null, evaluate: null };
  13. var entries = [];
  14. var filter = "all";
  15. var open = false;
  16. var history = [];
  17. var cursor = 0;
  18. var counts = { error: 0, warn: 0 };
  19. var seq = 0;
  20. ​
  21. function matches(entry) {
  22. if (filter === "all") return true;
  23. if (filter === "error") return entry.level === "error";
  24. if (filter === "warn") return entry.level === "warn";
  25. return entry.level !== "error" && entry.level !== "warn";
  26. }
  27. ​
  28. function sameAs(a, b) {
  29. return a && b && a.level === b.level && a.text === b.text && a.source === b.source &&
  30. a.path === b.path && a.line === b.line && a.group === b.group && a.level !== "input" && a.level !== "result";
  31. }
  32. ​
  33. function updateBadges() {
  34. badgeError.hidden = !counts.error;
  35. badgeError.textContent = String(counts.error);
  36. badgeWarn.hidden = !counts.warn;
  37. badgeWarn.textContent = String(counts.warn);
  38. filterBtns.forEach(function (b) {
  39. var key = b.getAttribute("data-filter");
  40. b.classList.toggle("is-active", key === filter);
  41. var n = b.querySelector("[data-n]");
  42. if (n) n.textContent = key === "error" && counts.error ? String(counts.error) : key === "warn" && counts.warn ? String(counts.warn) : "";
  43. });
  44. }
  45. ​
  46. function buildRow(entry) {
  47. var row = util.el("div", { class: "ath-console-row is-" + entry.level });
  48. if (entry.group) row.style.paddingLeft = 10 + entry.group * 14 + "px";
  49. var icon = entry.level === "error" ? "x" : entry.level === "warn" ? "warning" : "";
  50. var lead = util.el("span", { class: "ath-console-lead" });
  51. if (icon) lead.innerHTML = A.icon(icon);
  52. row.appendChild(lead);
  53. if (entry.source === "php") row.appendChild(util.el("span", { class: "ath-console-tag" }, "PHP"));
  54. row.appendChild(util.el("pre", { class: "ath-console-text" }, entry.text));
  55. var n = util.el("span", { class: "ath-console-repeat", hidden: true }, "");
  56. row.appendChild(n);
  57. if (entry.label) {
  58. if (entry.path != null && handlers.jump) {
  59. var btn = util.el("button", { type: "button", class: "ath-console-loc", title: entry.path || entry.label }, entry.label);
  60. btn.addEventListener("click", function () { handlers.jump(entry.path, entry.line, entry.col); });
  61. row.appendChild(btn);
  62. } else {
  63. row.appendChild(util.el("span", { class: "ath-console-loc is-plain" }, entry.label));
  64. }
  65. }
  66. entry.node = row;
  67. entry.repeatNode = n;
  68. return row;
  69. }
  70. ​
  71. function nearBottom() {
  72. return body.scrollHeight - body.scrollTop - body.clientHeight < 28;
  73. }
  74. ​
  75. function placeholder() {
  76. if (body.querySelector(".ath-console-row")) return;
  77. body.innerHTML = "";
  78. body.appendChild(util.el("div", { class: "ath-console-empty" }, A.t("console.empty")));
  79. }
  80. ​
  81. function renderAll() {
  82. body.innerHTML = "";
  83. entries.forEach(function (e) { if (matches(e)) body.appendChild(buildRow(e)); else e.node = null; });
  84. placeholder();
  85. updateBadges();
  86. body.scrollTop = body.scrollHeight;
  87. }
  88. ​
  89. function add(entry) {
  90. var last = entries[entries.length - 1];
  91. if (sameAs(last, entry)) {
  92. last.n = (last.n || 1) + 1;
  93. if (last.level === "error") counts.error++;
  94. if (last.level === "warn") counts.warn++;
  95. if (last.repeatNode) {
  96. last.repeatNode.hidden = false;
  97. last.repeatNode.textContent = String(last.n);
  98. }
  99. updateBadges();
  100. return;
  101. }
  102. entry.n = 1;
  103. entry.seq = ++seq;
  104. entries.push(entry);
  105. if (entry.level === "error") counts.error++;
  106. if (entry.level === "warn") counts.warn++;
  107. if (entries.length > LIMIT) {
  108. var old = entries.shift();
  109. if (old.node && old.node.parentNode) old.node.parentNode.removeChild(old.node);
  110. }
  111. if (matches(entry)) {
  112. var stick = nearBottom();
  113. var empty = body.querySelector(".ath-console-empty");
  114. if (empty) empty.remove();
  115. body.appendChild(buildRow(entry));
  116. if (stick || entry.level === "input") body.scrollTop = body.scrollHeight;
  117. }
  118. updateBadges();
  119. }
  120. ​
  121. function setOpen(on) {
  122. open = !!on;
  123. root.classList.toggle("is-open", open);
  124. toggleBtn.setAttribute("aria-expanded", String(open));
  125. A.store.set("console", open);
  126. if (open) body.scrollTop = body.scrollHeight;
  127. }
  128. ​
  129. function initGrip() {
  130. function clamp(h) {
  131. var max = Math.max(MIN_H, Math.round(root.parentNode.clientHeight * 0.7));
  132. return Math.max(MIN_H, Math.min(max, h));
  133. }
  134. var saved = Number(A.store.get("consoleH", 0));
  135. if (saved) root.style.setProperty("--ath-console-h", clamp(saved) + "px");
  136. grip.addEventListener("pointerdown", function (e) {
  137. e.preventDefault();
  138. var startY = e.clientY;
  139. var startH = body.offsetHeight;
  140. grip.setPointerCapture(e.pointerId);
  141. root.classList.add("is-resizing");
  142. function move(ev) {
  143. root.style.setProperty("--ath-console-h", clamp(startH + startY - ev.clientY) + "px");
  144. }
  145. function up() {
  146. grip.removeEventListener("pointermove", move);
  147. grip.removeEventListener("pointerup", up);
  148. grip.removeEventListener("pointercancel", up);
  149. root.classList.remove("is-resizing");
  150. A.store.set("consoleH", body.offsetHeight);
  151. }
  152. grip.addEventListener("pointermove", move);
  153. grip.addEventListener("pointerup", up);
  154. grip.addEventListener("pointercancel", up);
  155. });
  156. }
  157. ​
  158. function initInput() {
  159. input.addEventListener("keydown", function (e) {
  160. if (e.key === "Enter") {
  161. var code = input.value.trim();
  162. if (!code) return;
  163. e.preventDefault();
  164. if (history[history.length - 1] !== code) history.push(code);
  165. if (history.length > HISTORY) history.shift();
  166. cursor = history.length;
  167. input.value = "";
  168. add({ level: "input", text: code, source: "js", group: 0 });
  169. if (!handlers.evaluate || handlers.evaluate(code) === false) {
  170. add({ level: "error", text: A.t("console.no_page"), source: "js", group: 0, kind: "eval" });
  171. }
  172. } else if (e.key === "ArrowUp") {
  173. if (!history.length) return;
  174. e.preventDefault();
  175. cursor = Math.max(0, cursor - 1);
  176. input.value = history[cursor] || "";
  177. } else if (e.key === "ArrowDown") {
  178. if (!history.length) return;
  179. e.preventDefault();
  180. cursor = Math.min(history.length, cursor + 1);
  181. input.value = history[cursor] || "";
  182. } else if (e.key === "Escape") {
  183. input.blur();
  184. }
  185. });
  186. }
  187. ​
  188. A.console = {
  189. init: function (el, h) {
  190. root = el;
  191. handlers = h || handlers;
  192. body = el.querySelector("[data-console-body]");
  193. input = el.querySelector("[data-console-input]");
  194. grip = el.querySelector("[data-console-grip]");
  195. toggleBtn = el.querySelector("[data-console-toggle]");
  196. badgeError = el.querySelector("[data-console-errors]");
  197. badgeWarn = el.querySelector("[data-console-warnings]");
  198. filterBtns = [].slice.call(el.querySelectorAll("[data-filter]"));
  199. ​
  200. toggleBtn.addEventListener("click", function () { setOpen(!open); });
  201. el.querySelector("[data-console-clear]").addEventListener("click", function () { A.console.reset(); });
  202. filterBtns.forEach(function (b) {
  203. b.addEventListener("click", function () {
  204. filter = b.getAttribute("data-filter");
  205. renderAll();
  206. });
  207. });
  208. initGrip();
  209. initInput();
  210. body.addEventListener("transitionend", function (e) {
  211. if (e.target === body && e.propertyName === "height" && open) body.scrollTop = body.scrollHeight;
  212. });
  213. setOpen(A.store.get("console", false));
  214. A.console.reset();
  215. requestAnimationFrame(function () {
  216. requestAnimationFrame(function () { root.classList.add("is-armed"); });
  217. });
  218. },
  219. ​
  220. reset: function () {
  221. entries = [];
  222. counts.error = 0;
  223. counts.warn = 0;
  224. body.innerHTML = "";
  225. placeholder();
  226. updateBadges();
  227. },
  228. ​
  229. add: add,
  230. ​
  231. entries: function () {
  232. return entries.map(function (e) {
  233. 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 };
  234. });
  235. },
  236. ​
  237. counts: function () {
  238. return { error: counts.error, warn: counts.warn };
  239. },
  240. ​
  241. addMany: function (list) {
  242. (list || []).forEach(add);
  243. },
  244. ​
  245. setVisible: function (on) {
  246. root.hidden = !on;
  247. },
  248. ​
  249. setOpen: setOpen,
  250. ​
  251. toggle: function () {
  252. setOpen(!open);
  253. if (open) input.focus();
  254. },
  255. ​
  256. isOpen: function () {
  257. return open;
  258. }
  259. };
  260. })();
  261. ​