Browse code

Add item list and styling

Joseph Weston authored on 05/12/2021 07:45:39
Showing 3 changed files
... ...
@@ -25,6 +25,7 @@
25 25
   (cond
26 26
    (= "Open Game License" cmdline) (db :license)
27 27
    (= "spells" cmdline) (vals spells)
28
+   (= "items" cmdline) (vals equipment)
28 29
    (contains? spells cmd) (spells cmd)
29 30
    (contains? equipment cmd) (equipment cmd)
30 31
    :else {:err (str "Unknown command '" cmdline "'")})))
... ...
@@ -142,13 +142,16 @@
142 142
                      :margin-right :0.5rem}]])
143 143
 
144 144
 (defclass item-list-panel []
145
-    {})
145
+    {}
146
+    title-style)
147
+
146 148
 
147 149
 (defclass error-message []
148 150
     {:background-color (colorscheme :faded-red)
149 151
      :color (colorscheme :light0-hard)}
150 152
     (content-before "Error: "))
151 153
 
154
+
152 155
 (defclass property-list []
153 156
     {:padding-left 0
154 157
      :margin-top :0.5rem
... ...
@@ -159,6 +162,13 @@
159 162
                               :margin-right :0.5rem}
160 163
      (content-after ":")])
161 164
 
165
+(defclass property-table []
166
+    {:border-spacing 0}
167
+    [:td :th {:padding 0
168
+              :padding-left :2ch}
169
+     (s/& s/first-child) {:padding-left 0}]
170
+    [:tr [(s/& (s/nth-child :2n+1)) {:background-color (colorscheme :light0-soft)}]])
171
+
162 172
 (defclass prompt-style []
163 173
   {:background-color (colorscheme :light0-soft)
164 174
    :font-family mono-font
... ...
@@ -50,7 +50,12 @@
50 50
     [& elements]
51 51
     (string/join " " (filter identity elements)))
52 52
 
53
-(defn- property-list    [& props]
53
+(defn- with-default [default x]
54
+  (if (nil? x) default x))
55
+
56
+(defn- third [coll] (nth coll 2))
57
+
58
+(defn- property-list [& props]
54 59
     (into
55 60
      [:ul {:class (styles/property-list)}]
56 61
      (->> props
... ...
@@ -61,8 +66,23 @@
61 66
                  [:span (first %)]
62 67
                  [:span (second %)])))))
63 68
 
64
-(defn- license-notice
65
-    [x]
69
+(defn- zip [& colls] (apply map vector colls))
70
+
71
+(defn- property-table [items & props]
72
+  (let [pprops (partition 3 props)
73
+        headings (map first pprops)
74
+        attrs (map second pprops)
75
+        getters (map third pprops)]
76
+    (println headings)
77
+    [:table {:class (styles/property-table)}
78
+      [:thead (for [[attr heading] (zip attrs headings)] [:th attr heading])]
79
+      [:tbody
80
+       (for [item items]
81
+         [:tr
82
+          (for [[attr f] (zip attrs getters)]
83
+            [:td attr (->> item f (with-default "-"))])])]]))
84
+    
85
+(defn- license-notice [x]
66 86
     (let [l (x :license-notice)]
67 87
       (when l
68 88
         [:section.license-notice
... ...
@@ -196,8 +216,19 @@
196 216
      (for [spell (sort-by :name spells)]
197 217
         [:li [:span (spell :name)][:span (-> spell short-spell-description parenthesize)]])]])
198 218
 
199
-(defn item-list-panel [_]
200
-    [:article {:class (styles/item-list-panel)}])
219
+(defn equipment-list-panel [items]
220
+  [:article {:class (styles/item-list-panel)}
221
+   (heading "Items")
222
+   (property-table items
223
+     :Name
224
+       {}
225
+       :name
226
+     :Cost
227
+       {:align :right}
228
+       cost
229
+     :Weight
230
+       {:align :right}
231
+       weight)])
201 232
 
202 233
 (defn error-message
203 234
     [{e :err}]
... ...
@@ -209,6 +240,7 @@
209 240
 (def is-spell? #(some-> % :url (string/starts-with? "/api/spells")))
210 241
 (def is-spell-list? #(some-> % first is-spell?))
211 242
 (def is-equipment? #(some-> % :url (string/starts-with? "/api/equipment")))
243
+(def is-equipment-list? #(some-> % first is-equipment?))
212 244
 (def is-weapon? #(and (is-equipment? %) (-> % :equipment_category :index (= "weapon"))))
213 245
 (def is-error? #(contains? % :err))
214 246
 
... ...
@@ -217,6 +249,7 @@
217 249
     [:div.output
218 250
      (cond
219 251
       (is-license? x) (license-panel x)
252
+      (is-equipment-list? x) (equipment-list-panel x)
220 253
       (is-spell-list? x) (spell-list-panel x)
221 254
       (is-spell? x) (spell-panel x)
222 255
       (is-weapon? x) (weapon-panel x)