/
report.cljc
380 lines (365 loc) · 21.9 KB
/
report.cljc
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
(ns com.fulcrologic.rad.rendering.semantic-ui.report
(:require
#?@(:cljs
[[com.fulcrologic.fulcro.dom :as dom :refer [div]]
[com.fulcrologic.semantic-ui.modules.popup.ui-popup :refer [ui-popup]]
[com.fulcrologic.semantic-ui.modules.popup.ui-popup-content :refer [ui-popup-content]]
[com.fulcrologic.semantic-ui.addons.pagination.ui-pagination :as sui-pagination]]
:clj
[[com.fulcrologic.fulcro.dom-server :as dom :refer [div]]])
[clojure.string :as str]
[com.fulcrologic.fulcro.components :as comp]
[com.fulcrologic.fulcro.dom.events :as evt]
[com.fulcrologic.fulcro.dom.html-entities :as ent]
[com.fulcrologic.fulcro-i18n.i18n :refer [tr trc]]
[com.fulcrologic.rad.attributes :as attr]
[com.fulcrologic.rad.control :as control]
[com.fulcrologic.rad.form :as form]
[com.fulcrologic.rad.options-util :refer [?!]]
[com.fulcrologic.rad.rendering.semantic-ui.form :as sui-form]
[com.fulcrologic.rad.report :as report]
[com.fulcrologic.rad.semantic-ui-options :as suo]
[com.fulcrologic.rad.report-options :as ro]))
(defn row-action-buttons [report-instance row-props]
(let [{::report/keys [row-actions]} (comp/component-options report-instance)
{::suo/keys [report-row-button-grouping report-row-button-renderer]} (suo/get-rendering-options report-instance)]
(when (seq row-actions)
(div {:className (or (?! report-row-button-grouping report-instance) "ui buttons")}
(map-indexed
(fn [idx {:keys [label reload? visible? disabled? action] :as control}]
(let [disabled? (boolean (?! disabled? report-instance row-props))
onClick (fn [evt]
(evt/stop-propagation! evt)
(when action
(action report-instance row-props)
(when reload?
(control/run! report-instance))))
control-props (merge control
{:label label
:key idx
:onClick onClick
:disabled? disabled?})
label (?! label report-instance row-props control-props)]
(when (or (nil? visible?) (?! visible? report-instance row-props))
(if report-row-button-renderer
(report-row-button-renderer report-instance row-props control-props)
(if (string? label)
(dom/button :.ui.button {:key idx
:disabled disabled?
:onClick onClick}
label)
label)))))
row-actions)))))
(defn column-alignment-class [report-instance attr]
(let [alignment (?! (get attr ro/column-alignment) report-instance attr)]
(case alignment
:left "left aligned"
:right "right aligned"
:center "center aligned"
"")))
(comp/defsc TableRowLayout [_ {:keys [report-instance props] :as rp}]
{}
(let [{::report/keys [columns link links on-select-row]} (comp/component-options report-instance)
links (or links link)
action-buttons (row-action-buttons report-instance props)
{:keys [highlighted?]
::report/keys [idx]} (comp/get-computed props)
sui-cell-class (suo/get-rendering-options report-instance suo/report-table-cell-class)]
(dom/tr {:classes [(when highlighted? "active")]
:onClick (fn [evt]
(evt/stop-propagation! evt)
(when-not (false? (suo/get-rendering-options report-instance suo/selectable-table-rows?))
(?! on-select-row report-instance props)
(report/select-row! report-instance idx)))}
(map-indexed
(fn [idx {::attr/keys [qualified-key] :as column}]
(let [alignment-class (column-alignment-class report-instance column)
column-classes (str alignment-class " " (report/column-classes report-instance column))]
(dom/td {:key (str "col-" qualified-key)
:classes [(?! sui-cell-class report-instance idx) column-classes]}
(let [{:keys [edit-form entity-id]} (report/form-link report-instance props qualified-key)
link-fn (get links qualified-key)
label (report/formatted-column-value report-instance props column)]
(cond
edit-form (dom/a {:onClick (fn [evt]
(evt/stop-propagation! evt)
(form/edit! report-instance edit-form entity-id))} label)
(fn? link-fn) (dom/a {:onClick (fn [evt]
(evt/stop-propagation! evt)
(link-fn report-instance props))} label)
:else label)))))
columns)
(when action-buttons
(dom/td {:key "actions"
:className (or
(?! sui-cell-class report-instance (count columns))
"collapsing")}
action-buttons)))))
(let [ui-table-row-layout (comp/factory TableRowLayout)]
(defn render-table-row [report-instance row-class row-props]
(ui-table-row-layout {:report-instance report-instance
:row-class row-class
:props row-props})))
(comp/defsc ListRowLayout [this {:keys [report-instance props]}]
{}
(let [{::report/keys [columns]} (comp/component-options report-instance)]
(let [header-column (first columns)
description-column (second columns)
{:keys [edit-form entity-id]} (some->> header-column (::attr/qualified-key) (report/form-link report-instance props))
header-label (some->> header-column (report/formatted-column-value report-instance props))
description-label (some->> description-column (report/formatted-column-value report-instance props))
action-buttons (row-action-buttons report-instance props)]
(div :.item
(div :.content
(when action-buttons
(div :.right.floated.content
action-buttons))
(when header-label
(if edit-form
(dom/a :.header {:onClick (fn [evt]
(evt/stop-propagation! evt)
(form/edit! report-instance edit-form entity-id))} header-label)
(div :.header header-label)))
(when description-label
(div :.description description-label)))))))
(let [ui-list-row-layout (comp/factory ListRowLayout {:keyfn ::report/idx})]
(defn render-list-row [report-instance row-class row-props]
(ui-list-row-layout {:report-instance report-instance
:row-class row-class
:props row-props})))
(comp/defsc StandardReportControls [this {:keys [report-instance] :as env}]
{:shouldComponentUpdate (fn [_ _ _] true)}
(let [controls (control/component-controls report-instance)
{:keys [::report/paginate?]} (comp/component-options report-instance)
{::suo/keys [report-action-button-grouping]} (suo/get-rendering-options report-instance)
{:keys [input-layout action-layout]} (control/standard-control-layout report-instance)
{:com.fulcrologic.rad.container/keys [controlled?]} (comp/get-computed report-instance)]
(comp/fragment
(div {:className (or
(?! (suo/get-rendering-options report-instance suo/controls-class))
"ui top attached compact segment")}
(dom/h3 :.ui.header
(or (some-> report-instance comp/component-options ::report/title (?! report-instance)) (trc "a table that shows a list of rows" "Report"))
(div {:className (or (?! report-action-button-grouping report-instance)
"ui right floated buttons")}
(keep (fn [k]
(let [control (get controls k)]
(when (and (or (not controlled?) (:local? control))
(-> (get control :visible? true)
(?! report-instance)))
(control/render-control report-instance k control))))
action-layout)))
(div :.ui.form
(map-indexed
(fn [idx row]
(div {:key idx
:className (or
(?! (suo/get-rendering-options report-instance suo/report-controls-row-class) report-instance idx)
(sui-form/n-fields-string (count row)))}
(keep #(let [control (get controls %)]
(when (or (not controlled?) (:local? control))
(control/render-control report-instance % control))) row)))
input-layout))
(when paginate?
(let [page-count (report/page-count report-instance)]
(when (> page-count 1)
(div :.ui.two.column.centered.grid
(div :.two.wide.column
(div {:style {:paddingTop "4px"}}
#?(:cljs
(sui-pagination/ui-pagination {:activePage (report/current-page report-instance)
:onPageChange (fn [_ data]
(report/goto-page! report-instance (comp/isoget data "activePage")))
:totalPages page-count
:size "tiny"}))))))))))))
(let [ui-standard-report-controls (comp/factory StandardReportControls)]
(defn render-standard-controls [report-instance]
(ui-standard-report-controls {:report-instance report-instance})))
(comp/defsc ListReportLayout [this {:keys [report-instance] :as env}]
{:shouldComponentUpdate (fn [_ _ _] true)
:initLocalState (fn [_] {:row-factory (memoize
(fn [cls]
(comp/computed-factory cls
{:keyfn (fn [props] (some-> props (comp/get-computed ::report/idx)))})))})}
(let [{::report/keys [BodyItem]} (comp/component-options report-instance)
render-report-body-item ((comp/get-state this :row-factory) BodyItem)
render-controls (report/control-renderer this)
extra-parent-query (comp/component-options report-instance ::report/query-inclusions)
query-inclusion-props (select-keys (comp/props report-instance) extra-parent-query)
rows (report/current-rows report-instance)
loading? (report/loading? report-instance)]
(div
(when render-controls
(render-controls report-instance))
(div :.ui.attached.segment
(div :.ui.loader {:classes [(when loading? "active")]})
(when (seq rows)
(div :.ui.relaxed.divided.list
(map-indexed (fn [idx row]
(render-report-body-item row (merge query-inclusion-props
{:report-instance report-instance
:row-class BodyItem
::report/idx idx}))) rows)))))))
(let [ui-list-report-layout (comp/factory ListReportLayout {:keyfn ::report/idx})]
(defn render-list-report-layout [report-instance]
(ui-list-report-layout {:report-instance report-instance})))
(defn render-standard-table [this {:keys [report-instance]}]
(let [{report-column-headings ::report/column-headings
report-column-infos ::report/column-infos
::report/keys [columns row-actions BodyItem compare-rows table-class]} (comp/component-options report-instance)
render-report-body-item ((comp/get-state this :row-factory) BodyItem)
column-headings (mapv (fn [{::report/keys [column-heading column-info]
::attr/keys [qualified-key] :as attr}]
{:column attr
:help (or
(?! (get report-column-infos qualified-key) report-instance)
(?! column-info report-instance))
:label (or
(?! (get report-column-headings qualified-key) report-instance)
(?! column-heading report-instance)
(some-> qualified-key name str/capitalize)
"")})
columns)
rows (report/current-rows report-instance)
extra-parent-query (comp/component-options report-instance ::report/query-inclusions)
query-inclusion-props (select-keys (comp/props report-instance) extra-parent-query)
props (comp/props report-instance)
sort-params (-> props :ui/parameters ::report/sort)
sortable? (if-not (boolean compare-rows)
(constantly false)
(if-let [sortable-columns (some-> sort-params :sortable-columns set)]
(fn [{::attr/keys [qualified-key]}] (contains? sortable-columns qualified-key))
(constantly true)))
ascending? (and sortable? (:ascending? sort-params))
sorting-by (and sortable? (:sort-by sort-params))
has-row-actions? (seq row-actions)
sui-header-class (suo/get-rendering-options report-instance suo/report-table-header-class)
sui-table-class (?! (suo/get-rendering-options report-instance suo/report-table-class) report-instance)]
(dom/table {:className (or sui-table-class "ui selectable table") :classes [table-class]}
(dom/thead
(dom/tr
(map-indexed (fn [idx {:keys [label help column]}]
(let [alignment-class (column-alignment-class report-instance column)]
(dom/th {:key idx
:classes [alignment-class (?! sui-header-class report-instance idx)]}
(if (sortable? column)
(dom/a {:onClick (fn [evt]
(evt/stop-propagation! evt)
(report/sort-rows! report-instance column))}
(str label)
(when (= sorting-by (::attr/qualified-key column))
(if ascending?
(dom/i :.angle.down.icon)
(dom/i :.angle.up.icon))))
(str label))
#?(:cljs
(when help
(ui-popup {:trigger (dom/i :.ui.circle.info.icon)}
(ui-popup-content {}
help)))))))
column-headings)
(when has-row-actions? (dom/th {:classes [(or
(?! sui-header-class report-instance (count column-headings))
"collapsing")]} ""))))
(when (seq rows)
(dom/tbody
(map-indexed
(fn [idx row]
(let [highlighted-row-idx (report/currently-selected-row report-instance)]
(render-report-body-item row (merge query-inclusion-props
{:report-instance report-instance
:row-class BodyItem
:highlighted? (= idx highlighted-row-idx)
::report/idx idx}))))
rows))))))
(defn render-rotated-table [_ {:keys [report-instance] :as env}]
(let [{report-column-headings ::report/column-headings
::report/keys [columns row-actions compare-rows table-class]} (comp/component-options report-instance)
props (comp/props report-instance)
sort-params (-> props :ui/parameters ::report/sort)
sortable? (if-not (boolean compare-rows)
(constantly false)
(if-let [sortable-columns (some-> sort-params :sortable-columns set)]
(fn [{::attr/keys [qualified-key]}] (contains? sortable-columns qualified-key))
(constantly true)))
ascending? (and sortable? (:ascending? sort-params))
sorting-by (and sortable? (:sort-by sort-params))
row-headings (mapv (fn [{::report/keys [column-heading]
::attr/keys [qualified-key] :as attr}]
(let [label (or
(?! (get report-column-headings qualified-key) report-instance)
(?! column-heading report-instance)
(some-> qualified-key name str/capitalize)
"")]
(if (sortable? attr)
(dom/a {:onClick (fn [evt]
(evt/stop-propagation! evt)
(report/sort-rows! report-instance attr))}
label
(when (= sorting-by (::attr/qualified-key attr))
(if ascending?
(dom/i :.angle.down.icon)
(dom/i :.angle.up.icon))))
label)))
columns)
rows (report/current-rows report-instance)
has-row-actions? (seq row-actions)
sui-table-class (?! (suo/get-rendering-options report-instance suo/report-rotated-table-class) report-instance)
sui-cell-class (suo/get-rendering-options report-instance suo/report-table-cell-class)]
(dom/table {:className (or sui-table-class "ui compact collapsing definition selectable table")
:classes [table-class]}
(when (seq rows)
(comp/fragment
(dom/thead
(let [col (first columns)]
(dom/tr {:key "hrow"}
(dom/th
(get row-headings 0))
(map-indexed
(fn [idx row]
(dom/th {:key idx}
(report/formatted-column-value report-instance row col))) rows)
(when has-row-actions?
(dom/td {:key "actions"}
(row-action-buttons report-instance col))))))
(dom/tbody
(map-indexed
(fn [idx col]
(dom/tr {:key idx}
(dom/td (get row-headings (inc idx)))
(map-indexed
(fn [idx row]
(let [user-defined-cell-class (?! sui-cell-class report-instance idx)]
(dom/td {:key idx :className (or user-defined-cell-class "right aligned")}
(report/formatted-column-value report-instance row col)))) rows)
(when has-row-actions?
(let [user-defined-cell-class (when sui-cell-class (sui-cell-class report-instance (count rows)))]
(dom/td {:key "actions" :classes [user-defined-cell-class]}
(row-action-buttons report-instance col))))))
(rest columns))))))))
(comp/defsc TableReportLayout [this {:keys [report-instance] :as env}]
{:initLocalState (fn [this] {:row-factory (memoize (fn [cls] (comp/computed-factory cls
{:keyfn (fn [props]
(some-> props (comp/get-computed ::report/idx)))})))})
:shouldComponentUpdate (fn [_ _ _] true)}
(let [{::report/keys [rotate?]} (comp/component-options report-instance)
rotate? (?! rotate? report-instance)
render-controls (report/control-renderer report-instance)
loading? (report/loading? report-instance)
props (comp/props report-instance)
busy? (:ui/busy? props)
layout-class (?! (suo/get-rendering-options report-instance suo/layout-class) report-instance)
body-class (or
(?! (suo/get-rendering-options report-instance suo/body-class) report-instance)
"ui attached segment")]
(div {:className layout-class}
(when render-controls
(render-controls report-instance))
(div {:className body-class}
(div :.ui.orange.loader {:classes [(when (or busy? loading?) "active")]})
(if rotate?
(render-rotated-table this env)
(render-standard-table this env))))))
(let [ui-table-report-layout (comp/factory TableReportLayout {:keyfn ::report/idx})]
(defn render-table-report-layout [this]
(ui-table-report-layout {:report-instance this})))