dev / templates / app-cljs-live / client / src / __NAME__ / game / ui.cljs
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
;; 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))))

static mirror of HEAD · about · clone: git clone https://git.ardegazu.ro/dev.git