;; DOM rendering for the demo shell — only this namespace touches the
;; document. Pure output; event wiring lives in __NAME__.main.
(ns __NAME__.game.ui
(:require [__NAME__.i18n :refer (t tn)]))
(defn- $ ^js [s]
(js/document.querySelector s))
(defn show-room []
(.add (.-classList ($ "#ovJoin")) "hidden")
(.remove (.-classList ($ "#main")) "hidden")
(.focus ($ "#noteinput")))
(defn set-status [relay-up? open-count]
(.toggle (.-classList ($ "#statdot")) "on" (boolean relay-up?))
(set! (.-textContent ($ "#stattext"))
(cond
(pos? open-count) (tn "status.linked" open-count)
relay-up? (t "status.linking")
:else (t "hud.offline"))))
(defn render-roster
"rows: #js [#js {:id :nick :me} …]"
[rows]
(let [^js box ($ "#roster")]
(.replaceChildren box)
(doseq [^js r (array-seq rows)]
(let [row (js/document.createElement "div")
nick (js/document.createElement "span")]
(set! (.-className row) (str "rrow" (if (unchecked-get r "me") " me" "")))
(set! (.-textContent nick) (unchecked-get r "nick"))
(.appendChild row nick)
(when (unchecked-get r "me")
(let [you (js/document.createElement "span")]
(set! (.-className you) "you")
(set! (.-textContent you) (t "roster.you"))
(.appendChild row you)))
(.appendChild box row)))))
(defn render-notes
"notes: #js [#js {:from :nick :text :ts} …]"
[notes my-id]
(let [^js box ($ "#notes")]
(.replaceChildren box)
(if (zero? (.-length ^js notes))
(let [empty-el (js/document.createElement "div")]
(set! (.-className empty-el) "hint")
(set! (.-textContent empty-el) (t "notes.empty"))
(.appendChild box empty-el))
(do
(doseq [^js n (array-seq notes)]
(let [row (js/document.createElement "div")
who (js/document.createElement "span")
text (js/document.createElement "span")]
(set! (.-className row)
(str "note" (if (identical? (unchecked-get n "from") my-id) " mine" "")))
(set! (.-className who) "who")
(set! (.-textContent who) (unchecked-get n "nick"))
(set! (.-textContent text) (unchecked-get n "text"))
(.append row who text)
(.appendChild box row)))
(set! (.-scrollTop box) (.-scrollHeight box))))))
(defonce ^:private toast-timer (volatile! nil))
(defn toast [msg]
(let [el ($ "#toast")]
(set! (.-textContent el) msg)
(set! (.. el -style -opacity) "1")
(when-some [tt @toast-timer] (js/clearTimeout tt))
(vreset! toast-timer (js/setTimeout (fn [] (set! (.. el -style -opacity) "0")) 2600))))