-
-
Notifications
You must be signed in to change notification settings - Fork 81
/
table.cljs
296 lines (277 loc) · 9.07 KB
/
table.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
(ns portal.ui.viewer.table
(:require ["react" :as react]
[clojure.spec.alpha :as s]
[portal.colors :as c]
[portal.ui.inspector :as ins]
[portal.ui.lazy :as l]
[portal.ui.select :as select]
[portal.ui.styled :as d]
[portal.ui.theme :as theme]
[reagent.core :as r]))
(defonce ^:private hover (react/createContext nil))
(defn- with-hover [& children]
(r/with-let [value (r/atom nil)]
(into [:r> (.-Provider hover) #js {:value value}] children)))
(defn- use-hover [] (react/useContext hover))
(defn- hover? [hover selector value] (= (selector @hover) value))
(defn- table [& children]
(let [theme (theme/use-theme)
context (ins/use-context)]
(into
[d/div
{:style
(cond->
{:display :grid
:grid-gap 1
:font-size (:font-size theme)
:background (::c/border theme)
:border [1 :solid (::c/border theme)]
:border-radius (:border-radius theme)
:grid-template-columns :min-content}
(not= (:depth context) 1)
(assoc :overflow :auto))}]
children)))
(defn- cell [row column child]
(let [background (ins/get-background)
theme (theme/use-theme)
width 2
border (ins/get-background2)
hover (use-hover)]
[d/div
{:on-mouse-over
(fn []
(reset! hover [row column]))
:style
{:background background
:grid-row (str (inc row))
:grid-column (str (inc column))}}
[d/div
{:style (cond->
{:height "100%"
:width "100%"
:box-sizing :border-box
:padding (:padding theme)}
(nil? child)
(assoc
:background
(list
'repeating-linear-gradient
"45deg"
[border 0]
[border width]
[background width]
[background (* 3 width)])))
:style/hover
{:background (when child (str (::c/border theme) "55"))}}
[select/with-position {:row row :column column} child]]]))
(defn- special [row column child span]
(let [background (ins/get-background)
theme (theme/use-theme)
hover (use-hover)]
[d/div
{:style
(merge
(cond
(= 0 row column)
{:z-index 3
:top 0
:left 0
:border-bottom [3 :solid (::c/border theme)]
:border-right [3 :solid (::c/border theme)]}
(zero? row)
{:z-index 2
:top 0
:text-align :center
:border-bottom [3 :solid (::c/border theme)]}
(zero? column)
{:z-index 1
:left 0
:text-align :right
:border-right [3 :solid (::c/border theme)]})
(cond
@(r/track hover? hover first row)
{:border-right [3 :solid (::c/boolean theme)]}
@(r/track hover? hover second column)
{:border-bottom [3 :solid (::c/boolean theme)]})
{:position :sticky
:background background
:box-sizing :border-box
:padding (:padding theme)
:grid-row (str (inc row)
(when span (str " / span " span)))
:grid-column (str (inc column))})}
[select/with-position {:row row :column column} child]]))
(defn- columns [cols]
[:<>
[special 0 0]
(map-indexed
(fn [col-index column]
^{:key (hash column)}
[ins/with-key column
[special 0 (inc col-index) [ins/inspector column]]])
cols)])
(defn- inspect-map-table [values]
(let [rows (seq (ins/try-sort (keys values)))
cols (or (get-in (meta values) [:portal.viewer/table :columns])
(seq (ins/try-sort (into #{} (mapcat keys (vals values))))))]
[table
[columns cols]
[l/lazy-seq
(map-indexed
(fn [row-index row]
[:<>
{:key (hash row)}
[ins/with-key row
[special (inc row-index) 0 [ins/inspector row]]]
[ins/toggle-bg
[ins/with-key row
(map-indexed
(fn [col-index column]
(let [coll (get values row)]
^{:key col-index}
[ins/with-collection coll
[ins/with-key column
[cell
(inc row-index)
(inc col-index)
(when (contains? coll column)
[ins/inspector (ins/get-props coll column) (get coll column)])]]]))
cols)]]])
rows)]]))
(defn- inspect-coll-table [values]
(let [rows (seq values)
cols (or (get-in (meta values) [:portal.viewer/table :columns])
(seq (ins/try-sort (into #{} (mapcat keys values)))))]
[table
[columns cols]
[l/lazy-seq
(map-indexed
(fn [row-index row]
^{:key row-index}
[:<>
[ins/with-key row-index
[special (inc row-index) 0 [ins/inspector row-index]]]
[ins/toggle-bg
[ins/with-key row-index
(map-indexed
(fn [col-index column]
^{:key col-index}
[ins/with-collection row
[ins/with-key column
[cell
(inc row-index)
(inc col-index)
(when (contains? row column)
[ins/inspector (ins/get-props row column) (get row column)])]]])
cols)]]])
rows)]]))
(defn- inspect-vector-table [values]
[table
[l/lazy-seq
(map-indexed
(fn [row-index row]
^{:key row-index}
[:<>
[ins/with-key row-index
[special (inc row-index) 0 [ins/inspector row-index]]]
[ins/toggle-bg
[ins/with-key row-index
(map-indexed
(fn [col-index value]
^{:key col-index}
[ins/with-collection row
[ins/with-key col-index
[cell
(inc row-index)
(inc col-index)
[ins/inspector value]]]])
row)]]])
values)]])
(defn- inspect-map [values]
(let [rows (seq (ins/try-sort (keys values)))]
[table
[l/lazy-seq
(map-indexed
(fn [row-index row]
[:<>
{:key row-index}
[ins/with-key row
[special (inc row-index) 0 [ins/inspector row]]]
[ins/toggle-bg
[ins/with-key row
[ins/with-collection values
[ins/with-key row-index
[ins/with-key row
[cell
(inc row-index)
1
[ins/inspector (ins/get-props values row) (get values row)]]]]]]]])
rows)]]))
(defn- inspect-multi-map-table [values]
(let [rows (seq (ins/try-sort (keys values)))
cols (or (get-in (meta values) [:portal.viewer/table :columns])
(seq (ins/try-sort (into #{} (comp (mapcat second) (mapcat keys)) values))))]
[table
[columns cols]
[l/lazy-seq
(map-indexed
(fn [row-index {:keys [row value index]}]
[:<>
{:key row-index}
(when (zero? index)
[ins/with-key row
[special (inc row-index) 0 [ins/inspector row] (count (get values row))]])
[ins/toggle-bg
[ins/with-key row
(map-indexed
(fn [col-index column]
^{:key col-index}
[ins/with-collection value
[ins/with-key index
[ins/with-key column
[cell
(inc row-index)
(inc col-index)
(when (contains? value column)
[ins/inspector (ins/get-props value column) (get value column)])]]]])
cols)]]])
(mapcat
(fn [row]
(map-indexed
(fn [index value]
{:row row :value value :index index})
(get values row)))
rows))]]))
;;; :spec
(s/def ::map map?)
(s/def ::coll-of-maps (s/coll-of map?))
(s/def ::multi-map (s/map-of any? ::coll-of-maps))
(s/def ::map-of-maps (s/map-of any? map?))
(s/def ::coll-of-vector (s/coll-of vector?))
(s/def ::table
(s/or :multi-map ::multi-map
:map-of-maps ::map-of-maps
:map ::map
:coll-of-maps ::coll-of-maps
:coll-of-vector ::coll-of-vector))
;;;
(defn- get-component [value]
(let [result (s/conform ::table value)]
(when-not (= result ::s/invalid)
(case (first result)
:multi-map inspect-multi-map-table
:map-of-maps inspect-map-table
:map inspect-map
:coll-of-vector inspect-vector-table
:coll-of-maps inspect-coll-table))))
(defn table-view? [value] (s/valid? ::table value))
(defn inspect-table [values]
(let [component (get-component values)]
[ins/with-collection values
[with-hover
[component values]]]))
(def viewer
{:predicate table-view?
:component inspect-table
:name :portal.viewer/table
:doc "View value as a table. Supports sticky headers and keyboard navigation."})