main.html 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <pstm-name>main</pstm-name>
  7. <pstm-title>ClankerSaddle</pstm-title>
  8. <script pstm-res-src="statum.js"></script>
  9. <link rel="stylesheet" pstm-res-href="app.css">
  10. <link rel="icon" type="image/png" pstm-res-href="clanker-saddle.png">
  11. <link rel="apple-touch-icon" pstm-res-href="clanker-saddle.png">
  12. <link rel="stm-guard" require="auth != null || location.pathname == '/login'" href="/login">
  13. <script>
  14. // Shared page plumbing. showToast: slide-in notifications (used in
  15. // place of alert). initQuoteComments: registers a container matcher
  16. // for the text-highlight -> comment popover — selecting text inside
  17. // a matched container shows a floating button, then a popover that
  18. // posts the comment with the quoted text via the action the page
  19. // supplies.
  20. // Shared UI engines (parameterised per page):
  21. //
  22. // showToast — every page's toast corner.
  23. //
  24. // initQuoteComments — the text-selection quote popover + dialog
  25. // posting a comment with the quoted text; pages pass a `matches`
  26. // node predicate and the action accessor.
  27. //
  28. // initPlanReviews (plan 16 R3, arch 15 — the ~180-line pattern
  29. // that lived duplicated between the task and goal pages, now
  30. // goal-only after the task-page plan review died with the poll
  31. // era): the plan/completion REVIEW remark queue. "Add remark"
  32. // appends to a per-block queue rendered at the bottom of the
  33. // review body, persists across statum re-renders (the maps live
  34. // outside the render cycle), rides the rejection NUMBERED (each
  35. // remark with its selection quote prefixed like a comment), and
  36. // clears on accept. Remark-less rejection is allowed when the
  37. // feedback textarea carries the substance (plan 16 S2). The
  38. // rejection body rides `feedback`; the SERVER wraps the per-kind
  39. // stable prefix (R4 — templates carry no verdict constants). The
  40. // page supplies `resolveBlock(id)` — how a [data-plan-review]
  41. // container's block row resolves from page state.
  42. window.initPlanReviews = function (config) {
  43. if (!window.statum || !statum.afterEvaluate) return;
  44. var resolveBlock = (config && config.resolveBlock)
  45. || function () { return null; };
  46. var remarksByBlock = {};
  47. var rejectOpenByBlock = {};
  48. var remarkPopover = null;
  49. statum.afterEvaluate(syncPlanRemarks);
  50. document.addEventListener('click', function (event) {
  51. if (!event.target.closest) return;
  52. var accept = event.target.closest('[data-plan-accept]');
  53. if (accept) {
  54. // Accept ignores/clears the queued remarks.
  55. var accepted = accept.closest('[data-plan-review]');
  56. if (accepted) {
  57. var acceptId = accepted.getAttribute('data-block-id');
  58. remarksByBlock[acceptId] = [];
  59. rejectOpenByBlock[acceptId] = false;
  60. }
  61. return;
  62. }
  63. var toggle = event.target.closest('[data-plan-toggle]');
  64. if (toggle) {
  65. hideRemarkPopover();
  66. var review = toggle.closest('[data-plan-review]');
  67. var id = review && review.getAttribute('data-block-id');
  68. if (id) rejectOpenByBlock[id] = !rejectOpenByBlock[id];
  69. syncPlanRemarks();
  70. return;
  71. }
  72. var submit = event.target.closest('[data-plan-submit]');
  73. if (submit) {
  74. submitPlanRemarks(submit.closest('[data-plan-review]'));
  75. return;
  76. }
  77. var remove = event.target.closest('[data-plan-remove]');
  78. if (remove) {
  79. var entry = remove.closest('[data-plan-remark-id]');
  80. var blockId = remove.closest('[data-plan-review]').getAttribute('data-block-id');
  81. var list = remarksByBlock[blockId] || [];
  82. var index = parseInt(entry.getAttribute('data-plan-remark-id'), 10);
  83. if (!isNaN(index) && list[index]) list.splice(index, 1);
  84. remarksByBlock[blockId] = list;
  85. syncPlanRemarks();
  86. }
  87. });
  88. document.addEventListener('mouseup', function (event) {
  89. if (!event.target.closest) return;
  90. if (remarkPopover && remarkPopover.contains(event.target)) return;
  91. var body = event.target.closest('[data-plan-body]');
  92. if (!body) {
  93. hideRemarkPopover();
  94. return;
  95. }
  96. var review = body.closest('[data-plan-review]');
  97. if (!review || review.getAttribute('data-plan-open') !== '1') return;
  98. var selection = window.getSelection();
  99. var text = selection ? String(selection.toString()).trim() : '';
  100. if (text.length === 0) {
  101. hideRemarkPopover();
  102. return;
  103. }
  104. showRemarkPopover(event.pageX, event.pageY, text, review);
  105. });
  106. document.addEventListener('mousedown', function (event) {
  107. if (remarkPopover && event.target.closest
  108. && event.target.closest('.plan-popover')) return;
  109. hideRemarkPopover();
  110. });
  111. function showRemarkPopover(x, y, quote, review) {
  112. hideRemarkPopover();
  113. remarkPopover = document.createElement('div');
  114. remarkPopover.className = 'plan-popover';
  115. remarkPopover.style.left = Math.min(x, window.innerWidth - 140) + 'px';
  116. remarkPopover.style.top = y + 'px';
  117. var quoted = document.createElement('blockquote');
  118. quoted.textContent = quote;
  119. remarkPopover.appendChild(quoted);
  120. var body = document.createElement('textarea');
  121. body.placeholder = 'Your remark on the selected plan text';
  122. remarkPopover.appendChild(body);
  123. var actions = document.createElement('div');
  124. actions.className = 'actions';
  125. var add = document.createElement('button');
  126. add.type = 'button';
  127. add.className = 'primary';
  128. add.textContent = 'Add remark';
  129. add.addEventListener('click', function () {
  130. if (body.value.trim().length === 0) return;
  131. var id = review.getAttribute('data-block-id');
  132. var list = remarksByBlock[id] || [];
  133. list.push({ quote: quote, body: body.value.trim() });
  134. remarksByBlock[id] = list;
  135. hideRemarkPopover();
  136. syncPlanRemarks();
  137. });
  138. var cancel = document.createElement('button');
  139. cancel.type = 'button';
  140. cancel.textContent = 'Cancel';
  141. cancel.addEventListener('click', hideRemarkPopover);
  142. actions.appendChild(add);
  143. actions.appendChild(cancel);
  144. remarkPopover.appendChild(actions);
  145. document.body.appendChild(remarkPopover);
  146. body.focus();
  147. }
  148. function hideRemarkPopover() {
  149. if (remarkPopover) {
  150. remarkPopover.remove();
  151. remarkPopover = null;
  152. }
  153. }
  154. function submitPlanRemarks(review) {
  155. if (!review) return;
  156. var id = review.getAttribute('data-block-id');
  157. var list = remarksByBlock[id] || [];
  158. var extra = review.querySelector('[data-plan-feedback] textarea[name="feedback"]');
  159. var extraText = extra && extra.value.trim().length > 0 ? extra.value.trim() : '';
  160. // Remark-less rejection is allowed when the feedback
  161. // textarea carries the review's substance — one of the
  162. // two must be non-empty (plan 16 S2).
  163. if (list.length === 0 && extraText.length === 0) {
  164. if (window.showToast) window.showToast('Highlight some text and add a remark, or write feedback first', 'error');
  165. return;
  166. }
  167. var block = resolveBlock(id);
  168. if (!block || !block.answer_action) return;
  169. // The rejection body keeps the historical shape: every
  170. // queued remark NUMBERED in queue order, each with its
  171. // selection quote prefixed like a comment, then the
  172. // free-text feedback.
  173. var feedback = list.map(function (item, index) {
  174. return (item.quote
  175. ? '> ' + item.quote.replace(/\n/g, '\n> ') + '\n\n'
  176. : '') + (index + 1) + '. ' + item.body;
  177. }).join('\n\n');
  178. if (extraText.length > 0) {
  179. feedback += (feedback.length > 0 ? '\n\n' : '') + extraText;
  180. }
  181. remarksByBlock[id] = [];
  182. rejectOpenByBlock[id] = false;
  183. statum.act(block.answer_action, { feedback: feedback }).catch(function () { });
  184. }
  185. function syncPlanRemarks() {
  186. document.querySelectorAll('[data-plan-review]').forEach(function (review) {
  187. var id = review.getAttribute('data-block-id');
  188. var open = rejectOpenByBlock[id] ? '1' : '0';
  189. review.setAttribute('data-plan-open', open);
  190. var feedback = review.querySelector('[data-plan-feedback]');
  191. if (feedback) {
  192. if (rejectOpenByBlock[id]) feedback.removeAttribute('hidden');
  193. else feedback.setAttribute('hidden', '');
  194. }
  195. var list = review.querySelector('[data-plan-remarks]');
  196. if (!list) return;
  197. while (list.firstChild) list.removeChild(list.firstChild);
  198. (remarksByBlock[id] || []).forEach(function (item, index) {
  199. var entry = document.createElement('li');
  200. entry.setAttribute('data-plan-remark-id', String(index));
  201. if (item.quote) {
  202. var quoted = document.createElement('blockquote');
  203. quoted.textContent = item.quote;
  204. entry.appendChild(quoted);
  205. }
  206. var body = document.createElement('div');
  207. body.textContent = item.body;
  208. entry.appendChild(body);
  209. var remove = document.createElement('button');
  210. remove.type = 'button';
  211. remove.className = 'inline';
  212. remove.setAttribute('data-plan-remove', '');
  213. remove.textContent = 'Remove';
  214. entry.appendChild(remove);
  215. list.appendChild(entry);
  216. });
  217. });
  218. }
  219. };
  220. if (window.statum) {
  221. (function () {
  222. 'use strict';
  223. window.showToast = function (message, kind) {
  224. var stack = document.querySelector('.toast-stack');
  225. if (!stack) {
  226. stack = document.createElement('div');
  227. stack.className = 'toast-stack';
  228. document.body.appendChild(stack);
  229. }
  230. var toast = document.createElement('div');
  231. toast.className = 'toast ' + (kind || '');
  232. toast.textContent = message;
  233. stack.appendChild(toast);
  234. window.setTimeout(function () {
  235. toast.classList.add('leaving');
  236. window.setTimeout(function () { toast.remove(); }, 300);
  237. }, 5000);
  238. };
  239. statum.onNotifyHandler = function (kind, message) {
  240. showToast(message || kind, kind);
  241. };
  242. var quoteHandlers = [];
  243. var quoteButton = null;
  244. var quotePopover = null;
  245. var quotedText = '';
  246. var activeHandler = null;
  247. window.initQuoteComments = function (handler) {
  248. quoteHandlers.push(handler);
  249. };
  250. document.addEventListener('mouseup', function (event) {
  251. if (quoteButton && quoteButton.contains(event.target)) return;
  252. var selection = window.getSelection();
  253. var text = selection ? String(selection.toString()).trim() : '';
  254. activeHandler = matchHandler(event.target);
  255. if (text.length === 0 || !activeHandler) {
  256. hideQuoteUi();
  257. return;
  258. }
  259. quotedText = text;
  260. showQuoteButton(event.pageX, event.pageY);
  261. });
  262. document.addEventListener('mousedown', function (event) {
  263. if (quoteButton && quoteButton.contains(event.target)) return;
  264. if (quotePopover && quotePopover.contains(event.target)) return;
  265. hideQuoteUi();
  266. });
  267. function matchHandler(node) {
  268. for (var i = 0; i < quoteHandlers.length; i++) {
  269. if (quoteHandlers[i].matches(node)) return quoteHandlers[i];
  270. }
  271. return null;
  272. }
  273. function showQuoteButton(x, y) {
  274. hideQuoteUi();
  275. quoteButton = document.createElement('button');
  276. quoteButton.type = 'button';
  277. quoteButton.className = 'quote-button';
  278. quoteButton.textContent = 'Comment on this';
  279. quoteButton.style.left = Math.max(x - 60, 8) + 'px';
  280. quoteButton.style.top = (y - 36) + 'px';
  281. quoteButton.addEventListener('click', function (event) {
  282. event.preventDefault();
  283. openQuoteDialog();
  284. });
  285. document.body.appendChild(quoteButton);
  286. }
  287. function openQuoteDialog() {
  288. if (quoteButton) { quoteButton.remove(); quoteButton = null; }
  289. var dialog = document.getElementById('quote-comment-dialog');
  290. if (!dialog) return;
  291. var quote = dialog.querySelector('#quote-text');
  292. var field = dialog.querySelector('input[name="quote"]');
  293. if (quote) quote.textContent = quotedText;
  294. if (field) field.value = quotedText;
  295. var returnField = dialog.querySelector('input[name="return_to"]');
  296. if (returnField) returnField.value = location.pathname + location.search;
  297. if (typeof dialog.showModal === 'function') {
  298. dialog.showModal();
  299. } else {
  300. dialog.setAttribute('open', '');
  301. }
  302. }
  303. function hideQuoteUi() {
  304. if (quoteButton) { quoteButton.remove(); quoteButton = null; }
  305. if (quotePopover) { quotePopover.remove(); quotePopover = null; }
  306. activeHandler = null;
  307. }
  308. })();
  309. }
  310. </script>
  311. </head>
  312. <body>
  313. <header>
  314. <h1><img class="brand-emblem" alt="" pstm-res-src="clanker-saddle-emblem.png">ClankerSaddle</h1>
  315. <nav>
  316. <a href="/">Dashboard</a>
  317. <!-- spry:nav-begin -->
  318. <a href="/projects">Projects</a>
  319. <a href="/tasks">Tasks</a>
  320. <a href="/goals" class="nav-goal"><span class="goal-icon">🎯</span> Goals</a>
  321. <a href="/saddles">Saddles</a>
  322. <a href="/rules">Rules</a>
  323. <!-- spry:nav-end -->
  324. <span stm-if="auth != null"><strong stm-text="auth.username">user</strong>
  325. <button type="button" class="inline" stm-action="auth.logout">Log out</button>
  326. <!-- spry:nav-auth-begin -->
  327. <!-- spry:nav-auth-end -->
  328. </span>
  329. <a stm-if="auth == null" href="/login">Log in</a>
  330. </nav>
  331. </header>
  332. <main>
  333. <div stm-preloader>Loading…</div>
  334. <div stm-content hidden>
  335. <pstm-content></pstm-content>
  336. </div>
  337. </main>
  338. <footer><p>ClankerSaddle — the agent works while you sleep.</p></footer>
  339. </body>
  340. </html>