index.html 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  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">
  6. <title>Statum Demo</title>
  7. <style>
  8. :root {
  9. --bg: #f6f7f9;
  10. --card: #fff;
  11. --line: #e3e6ea;
  12. --accent: #3a6ea5;
  13. --muted: #6b7280;
  14. }
  15. * {
  16. box-sizing: border-box;
  17. }
  18. body {
  19. margin: 0;
  20. font-family: system-ui, Segoe UI, Roboto, sans-serif;
  21. background: var(--bg);
  22. color: #1f2933;
  23. line-height: 1.5;
  24. }
  25. header {
  26. background: var(--accent);
  27. color: #fff;
  28. padding: 1rem 1.5rem;
  29. display: flex;
  30. align-items: baseline;
  31. gap: 1rem;
  32. flex-wrap: wrap;
  33. }
  34. header h1 {
  35. margin: 0;
  36. font-size: 1.25rem;
  37. }
  38. header .role {
  39. margin-left: auto;
  40. opacity: .9;
  41. }
  42. main {
  43. max-width: 900px;
  44. margin: 0 auto;
  45. padding: 1.5rem;
  46. }
  47. section {
  48. background: var(--card);
  49. border: 1px solid var(--line);
  50. border-radius: 10px;
  51. padding: 1rem 1.25rem;
  52. margin-bottom: 1.25rem;
  53. }
  54. h2 {
  55. margin-top: 0;
  56. font-size: 1.05rem;
  57. color: var(--muted);
  58. text-transform: uppercase;
  59. letter-spacing: .04em;
  60. }
  61. button {
  62. font: inherit;
  63. padding: .35rem .8rem;
  64. border: 1px solid var(--line);
  65. border-radius: 6px;
  66. background: #fff;
  67. cursor: pointer;
  68. }
  69. button:hover {
  70. border-color: var(--accent);
  71. }
  72. button:disabled {
  73. opacity: .55;
  74. cursor: progress;
  75. }
  76. /* stm-class targets */
  77. .high {
  78. color: #b00020;
  79. font-weight: 700;
  80. }
  81. .featured {
  82. border-color: #f0a500;
  83. box-shadow: 0 0 0 2px #f0a50033;
  84. }
  85. .out {
  86. opacity: .5;
  87. }
  88. /* stm-busy-class */
  89. .busy {
  90. outline: 2px solid var(--accent);
  91. outline-offset: 2px;
  92. }
  93. /* stm-on-error */
  94. .error {
  95. border-color: #b00020;
  96. background: #fde8ea;
  97. }
  98. .ok {
  99. background: #e6f4ea;
  100. border: 1px solid #b7deca;
  101. padding: .5rem .75rem;
  102. border-radius: 6px;
  103. }
  104. .counter {
  105. display: inline-flex;
  106. align-items: center;
  107. gap: .75rem;
  108. }
  109. .counter .value {
  110. min-width: 2.5ch;
  111. text-align: center;
  112. font-size: 1.5rem;
  113. }
  114. .grid {
  115. display: grid;
  116. grid-template-columns: repeat(auto-fill, minmax(160px, 1fr));
  117. gap: 1rem;
  118. }
  119. .card {
  120. border: 1px solid var(--line);
  121. border-radius: 8px;
  122. padding: .75rem;
  123. }
  124. .card h3 {
  125. margin: 0 0 .25rem;
  126. }
  127. .price {
  128. color: var(--muted);
  129. }
  130. .pill {
  131. display: inline-block;
  132. font-size: .75rem;
  133. padding: .1rem .5rem;
  134. border-radius: 999px;
  135. background: #eef2f7;
  136. color: var(--muted);
  137. }
  138. .pill.stock {
  139. background: #e6f4ea;
  140. color: #1e7e34;
  141. }
  142. .pill.soldout {
  143. background: #fde8ea;
  144. color: #b00020;
  145. }
  146. ul.lines {
  147. padding-left: 1.1rem;
  148. }
  149. .muted {
  150. color: var(--muted);
  151. }
  152. /* Toasts rendered by the custom onNotifyHandler/onErrorHandler overrides. */
  153. #toasts {
  154. position: fixed;
  155. right: 1rem;
  156. bottom: 1rem;
  157. display: flex;
  158. flex-direction: column;
  159. gap: .5rem;
  160. z-index: 50;
  161. max-width: 22rem;
  162. }
  163. .toast {
  164. background: var(--card);
  165. border: 1px solid var(--line);
  166. border-left: 4px solid var(--accent);
  167. border-radius: 6px;
  168. padding: .5rem .75rem;
  169. box-shadow: 0 2px 8px rgba(0, 0, 0, .12);
  170. font-size: .9rem;
  171. transition: opacity .4s ease;
  172. }
  173. .toast.success { border-left-color: #1e7e34; }
  174. .toast.error { border-left-color: #b00020; }
  175. .toast.info { border-left-color: var(--accent); }
  176. .toast.hide { opacity: 0; }
  177. </style>
  178. </head>
  179. <!--
  180. Body-level overrides (optional): stm-entrypoint / stm-slots point the library
  181. at this server's endpoints. They default to /_statum/entrypoint and
  182. /_statum/slots, so they are shown here only to demonstrate the overrides.
  183. -->
  184. <body stm-entrypoint="/_statum/entrypoint" stm-slots="/_statum/slots">
  185. <header>
  186. <h1>Statum Demo</h1>
  187. <!-- stm-text -->
  188. <span>Role: <span stm-text="user.name">Guest</span></span>
  189. <span class="role">
  190. <!-- stm-if / stm-else-if / stm-else chain -->
  191. <span stm-if="user.role === 'admin'">administrator tools enabled</span>
  192. <span stm-else-if="user.role === 'member'">member tools</span>
  193. <span stm-else>read-only guest</span>
  194. </span>
  195. <button stm-action="user.toggleAction">Toggle role</button>
  196. </header>
  197. <main>
  198. <!-- stm-preloader is shown until the entrypoint resolves; stm-content after. -->
  199. <div stm-preloader>Loading application state…</div>
  200. <!--
  201. stm-content is pre-hidden in the HTML so the un-bound template never
  202. flashes before the script runs; statum removes `hidden` once ready.
  203. -->
  204. <div stm-content hidden>
  205. <!--
  206. Intentional leftover: this pstm-* markup should have been resolved
  207. during server-side pre-rendering. The library logs a single console
  208. warning about it on load to demonstrate that detection.
  209. -->
  210. <div pstm-component="header" hidden aria-hidden="true"></div>
  211. <!-- Counter: stm-text, stm-class.x, stm-if, stm-action, stm-data-*, stm-attribute, stm-disabled -->
  212. <section>
  213. <h2>Counter <span class="muted">(stm-text · stm-class · stm-if · stm-data · stm-attribute · stm-disabled)</span></h2>
  214. <div class="counter">
  215. <!-- stm-disabled disables the element + descendants when the predicate is true -->
  216. <button stm-action="counter.decrement" stm-disabled="counter.value &lt;= 0">−</button>
  217. <!-- stm-class.high toggles when value > 5; stm-attribute.data-value sets a data-* attr -->
  218. <span class="value" stm-text="counter.value" stm-class.high="counter.value > 5" stm-attribute.data-value="counter.value">0</span>
  219. <button stm-action="counter.increment">+</button>
  220. <button stm-action="counter.reset">Reset</button>
  221. <!-- stm-data-amount sends a literal value with the request -->
  222. <button stm-action="counter.add" stm-data-amount="5">+5</button>
  223. <!-- stm-attribute.checked toggles a boolean attribute from a boolean result -->
  224. <label class="pill"><input type="checkbox" stm-attribute.checked="counter.high"> high flag</label>
  225. </div>
  226. <!-- stm-if on a derived boolean -->
  227. <p stm-if="counter.high">That is a high number!</p>
  228. </section>
  229. <!-- Products: stm-for-..-in, stm-key, stm-class, stm-if/else, stm-action, stm-busy-class -->
  230. <section>
  231. <h2>Products <span class="muted">(stm-for · stm-key · stm-class · stm-action · stm-busy-class)</span></h2>
  232. <div class="grid">
  233. <div class="card"
  234. stm-for-product-in="products.items"
  235. stm-key="product.id"
  236. stm-class.featured="product.featured"
  237. stm-class.out="!product.inStock">
  238. <h3 stm-text="product.name">Product</h3>
  239. <div class="price">$<span stm-text="product.price">0</span></div>
  240. <p>
  241. <span class="pill stock" stm-if="product.inStock">In stock</span>
  242. <span class="pill soldout" stm-else>Sold out</span>
  243. <span class="pill" stm-if="product.featured">featured</span>
  244. </p>
  245. <!--
  246. Per-item context (the product id) is baked into the action's
  247. `private` field server-side, so no stm-data expression is needed.
  248. stm-busy-class applies while the request is in flight.
  249. -->
  250. <button stm-action="product.addToCartAction"
  251. stm-busy-class="busy"
  252. stm-data-confirm="true">Add to cart</button>
  253. </div>
  254. </div>
  255. </section>
  256. <!-- Cart: stm-text, stm-if/else, nested stm-for, and the always-send slot -->
  257. <section>
  258. <h2>Cart <span class="muted">(stm-text · stm-if/else · nested stm-for)</span></h2>
  259. <p stm-if="cart.empty">Your cart is empty.</p>
  260. <div stm-else>
  261. <p><strong>Items:</strong> <span stm-text="cart.count">0</span>
  262. &nbsp; <strong>Total:</strong> $<span stm-text="cart.total">0</span></p>
  263. <ul class="lines">
  264. <!-- nested loop inside an stm-else branch -->
  265. <li stm-for-line-in="cart.items" stm-key="line.id">
  266. <span stm-text="line.name"></span> ×
  267. <span stm-text="line.qty"></span>
  268. ($<span stm-text="line.price"></span>)
  269. </li>
  270. </ul>
  271. <button stm-action="cart.checkoutAction" stm-disabled="cart.empty">Checkout</button>
  272. <button stm-action="cart.clearAction">Clear cart</button>
  273. </div>
  274. </section>
  275. <!-- Notice: stm-html, and an error directive to demonstrate stm-on-error -->
  276. <section>
  277. <h2>Notice <span class="muted">(stm-html · stm-on-error)</span></h2>
  278. <!-- stm-html renders server-provided HTML content -->
  279. <div stm-html="notice.html">No notice set.</div>
  280. <p>
  281. <button stm-action="notice.setAction">Set HTML notice</button>
  282. <!-- stm-on-error applies these classes when the action fails -->
  283. <button stm-action="notice.failAction" stm-on-error="error">Trigger error</button>
  284. </p>
  285. </section>
  286. <!--
  287. Live channel: subscribe/unsubscribe directives drive a realtime
  288. Server-Sent Events connection; broadcasting pushes a `set` to every
  289. subscribed tab and also returns a `notify` directive. stm-def-{name}-as
  290. defines a local variable for this element and its descendants.
  291. Open a second tab and broadcast to see the update arrive live.
  292. -->
  293. <section>
  294. <h2>Live channel <span class="muted">(subscribe · realtime · notify · stm-def)</span></h2>
  295. <div stm-def-stamp-as="live.at ? 'last updated ' + live.at : ''">
  296. <p>
  297. <strong stm-text="live.text">…</strong>
  298. <span class="muted" stm-text="stamp"></span>
  299. </p>
  300. <form stm-action="live.sendAction">
  301. <input type="text" name="msg" placeholder="Broadcast to every open tab" style="min-width:260px">
  302. <button type="submit">Broadcast</button>
  303. </form>
  304. <p class="muted">
  305. <button stm-action="live.muteAction">Stop live updates</button>
  306. <button stm-action="live.resumeAction">Resume live updates</button>
  307. </p>
  308. </div>
  309. </section>
  310. <section>
  311. <h2>Session <span class="muted">(stm-confirm)</span></h2>
  312. <p>
  313. <!-- stm-confirm shows a confirm() dialog before the action runs. -->
  314. <button stm-action="user.resetAllAction" stm-confirm="Reset all session state?">Reset everything</button>
  315. </p>
  316. </section>
  317. </div>
  318. </main>
  319. <!-- Container for the toast notifications rendered by the handlers below. -->
  320. <div id="toasts"></div>
  321. <!-- The library auto-initialises on DOMContentLoaded. -->
  322. <script src="/statum.js"></script>
  323. <script>
  324. // Override the default UI handlers so `notify` directives (and errors)
  325. // render as styled toasts instead of using the browser's native alert().
  326. // These run before DOMContentLoaded fires, so they are in place before the
  327. // entrypoint is processed.
  328. statum.onNotifyHandler = function (kind, message) {
  329. toast((kind ? '[' + kind + '] ' : '') + (message || ''), kind || 'info');
  330. };
  331. statum.onErrorHandler = function (err, element) {
  332. toast('Error: ' + (err && err.message ? err.message : 'something went wrong'), 'error');
  333. return true; // keep the default behaviour (stm-on-error classes + rejection)
  334. };
  335. function toast(msg, kind) {
  336. var box = document.getElementById('toasts');
  337. if (!box) return;
  338. var el = document.createElement('div');
  339. el.className = 'toast ' + kind;
  340. el.textContent = msg;
  341. box.appendChild(el);
  342. setTimeout(function () {
  343. el.classList.add('hide');
  344. setTimeout(function () { el.remove(); }, 400);
  345. }, 3500);
  346. }
  347. </script>
  348. </body>
  349. </html>