-
Notifications
You must be signed in to change notification settings - Fork 0
/
actionable-entity-report.ss
287 lines (231 loc) · 11.4 KB
/
actionable-entity-report.ss
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
#lang scheme/base
(require "base.ss")
(require (only-in (unlib-in list) list-ref?)
(unlib-in string symbol)
"actionable-entity-report-internal.ss"
"entity-report.ss"
"report-column.ss"
"report-internal.ss")
(define default-report-action
(create-report-action 'default-report-action "With selected ..."))
; Classes ----------------------------------------
(define report-action-combo-box%
(class/cells vanilla-combo-box% ()
(inherit get-id)
; Fields -------------------------------------
; actionable-entity-report%
(init-field report #:accessor)
; Constructor --------------------------------
(super-new)
; Methods ------------------------------------
; -> (listof any)
(define/override (get-options)
(send report get-report-actions))
; any -> (U string #f)
(define/override (option->raw report-action)
(report-action-id report-action))
; (U string #f) -> any
(define/override (raw->option raw)
(let ([raw-sym (and raw (string->symbol raw))])
(for/or ([report-action (in-list (get-options))])
(and (eq? raw-sym (report-action-id report-action)) report-action))))
; any -> string
(define/override (option->xml report-action)
(report-action-xml report-action))
; any -> string
(define/override (option->classes report-action)
(report-action-classes report-action))
(define/override (option-enabled? report-action)
(send report report-action-enabled? report-action))
; seed -> js
(define/augment (get-on-attach seed)
(js (!dot ($ ,(format "#~a" (get-id)))
(change (function (event ui)
(if (!= (!dot ($ this) (val)) ,(report-action-id default-report-action))
(!block (var [dataObj (!object)])
(!dot ($ ,(format "#~a input.report-action:checked" (send report get-id)))
(each (function (elem index)
(= (!index dataObj (!dot ($ this) (attr "id"))) #t))))
(!dot Smoke (doAjax ,(embed seed (callback [report on-run-report-action])) dataObj)))))))))
; seed -> js
(define/augment (get-on-detach seed)
(js (!dot ($ ,(format "#~a" (get-id))) (unbind))))))
; Interfaces -------------------------------------
(define actionable-entity-report<%>
(interface ()
get-report-actions ; -> (listof report-action)
report-action-enabled? ; report-action -> boolean
get-selected-items ; (listof any) -> void
set-selected-items! ; -> (listof any)
render-report-action-selector ; seed -> xml
render-report-action-column ; seed col any -> xml
data->report-action-input-id ; any -> (U symbol string)
on-run-report-action ; -> void
run-report-action)) ; report-action -> void
; Columns ----------------------------------------
(define report-action-column
(make-column 'report-action
"Actions"
#:xml-name (xml (input (@ [type 'checkbox] [id 'toggle-all-selection])))
#:classes '(report-action)
#:display-in-html? #t
#:display-in-csv? #f))
; entity-report subclass -------------------------
(define actionable-entity-report%
(class/cells entity-report% (actionable-entity-report<%>)
(inherit do-queries
get-entity
get-id
get-show-pattern-field?
get-show-pager-top?
get-show-pager-bottom?
get-sort-col
get-sort-dir
get-views
get-visible-columns
render-body
render-pager
render-controller-cell
show-controller-cell?)
; Fields -------------------------------------
; (listof report-action)
(init-field report-actions (error "A list of report-actions must be specified."))
; Child-components ---------------------------
(field report-action-combo
(new report-action-combo-box% [report this])
#:child #:accessor)
; Cells --------------------------------------
; (cellof (listof any))
(cell selected-items null #:accessor #:mutator)
; (cellof boolean)
(cell dirty? #f #:accessor #:mutator)
; Constructor --------------------------------
(init [classes null])
(super-new [classes (list* "actionable-entity-report" classes)])
; Methods ------------------------------------
; -> boolean
(define/override (dirty?)
(begin0 (or (super dirty?)
(send report-action-combo dirty?)
(get-dirty?))
(set-dirty?! #f)))
; -> (listof any)
(define/public (get-report-actions)
(cons default-report-action report-actions))
; -> boolean
(define/public (report-actions-enabled?)
(pair? (get-report-actions)))
; -> (listof any)
(define/public (get-report-action-column)
report-action-column)
; -> boolean
(define/public (report-action-enabled? report-action)
#t)
; seed -> xml
(define/public (render-report-action-selector seed)
(xml (div (@ [class "report-actions"])
,(opt-xml (report-actions-enabled?)
,(send report-action-combo render seed)))))
; Overridden rendering -----------------------
; seed -> xml
(define/override (render-preamble seed start count total)
(xml ,(super render-preamble seed start count total)
,(render-report-action-selector seed)))
; seed (listof column) -> xml
(define/override (render-empty-body seed cols)
(xml (tbody (tr (td (@ [colspan ,(+ (length cols)
(if (show-controller-cell?)
(if (report-actions-enabled?) 2 1)
(if (report-actions-enabled?) 1 0)))]
[class "empty-row"])
"There are no items to display in this list.")))))
; seed (listof column) -> xml
(define/override (render-head seed cols)
(let ([current-col (get-sort-col)]
[current-dir (get-sort-dir)])
(xml (thead (tr (@ [class 'ui-widget-header])
,(opt-xml (report-actions-enabled?)
,(send report-action-column render-head seed #f))
,(opt-xml (show-controller-cell?)
(th (@ [class "controller-cell ui-state-default"])
(& nbsp)))
,@(for/list ([col (in-list (get-visible-columns))])
(send col render-head seed (and (equal? col current-col) current-dir))))))))
; seed (listof column) snooze-struct -> xml
(define/overment (render-item-columns seed cols struct)
(xml ,(render-report-action-column seed struct)
,(inner (super render-item-columns seed cols struct) render-item-columns seed cols struct)))
; seed snooze-struct -> xml
(define/public (render-report-action-column seed struct)
(let ([input-id (data->report-action-input-id struct)])
(opt-xml (report-actions-enabled?)
(td (@ [class "report-action-cell"])
(input (@ [type 'checkbox]
[class "report-action"]
[id ,input-id]
,(opt-xml-attr (memq input-id (get-selected-items))
checked 'checked)))))))
(define/public (get-empty-selection-message)
(xml "You must select one or more " ,(entity-pretty-name-plural (get-entity)) " to do that..."))
; any -> symbol
(define/public (data->report-action-input-id struct)
(string->symbol (format "report-action-~a-~a" (entity-name (snooze-struct-entity struct)) (snooze-struct-id struct))))
; any -> symbol
(define/public (report-action-input-id->data id)
(match (regexp-split #rx"-" (symbol->string id))
[(list "report" "action" the-entity the-id)
(and (string->number the-id) (find-by-id (get-entity) (string->number the-id)))]
[_ #f]))
; Events -------------------------------------
; -> void
(define/public-final #:callback (on-run-report-action)
(let* ([report-action (send report-action-combo get-value)]
[selected-items (for/fold ([selected null])
([binding (in-list (request-bindings (current-request)))])
(match-let* ([(cons id val) binding]
[elem (report-action-input-id->data id)])
(if elem (cons elem selected) selected)))])
(cond [(or (not report-action) (eq? report-action default-report-action))
(void)]
[(null? selected-items)
(set-selected-items! (map (cut data->report-action-input-id <>) selected-items))
(notifications-add! (get-empty-selection-message))
(set-dirty?! #t)]
[else
(set-selected-items! (map (cut data->report-action-input-id <>) selected-items))
(run-report-action report-action selected-items)
(send report-action-combo set-value! default-report-action)
(set-dirty?! #t)])))
; report-action -> void
(define/public (run-report-action report-action selected-items)
(error "run-report-action must be overridden"))
; JavaScript ---------------------------------
; seed -> js
(define/override (get-on-attach seed)
(js ,(super get-on-attach seed)
,(opt-js (report-actions-enabled?)
(var [allCheckboxes ($ ,(format "#~a input.report-action" (get-id)))]
[actionCombo ($ ,(format "#~a" (send report-action-combo get-id)))]
[enableCombo (function ()
(if (== (!dot ($ ,(format "#~a input.report-action:checked" (get-id))) (size)) 0)
(!block (!dot actionCombo (attr "disabled" "disabled")))
(!block (!dot actionCombo (removeAttr "disabled")))))])
(!dot ($ "#toggle-all-selection")
(click (function (event ui)
(if (!dot ($ this) (attr "checked"))
(!block (!dot allCheckboxes (attr "checked" "checked")))
(!block (!dot allCheckboxes (removeAttr "checked"))))
(enableCombo))))
(!dot allCheckboxes (click enableCombo))
(enableCombo))))
; seed -> js
(define/override (get-on-detach seed)
(js ,(super get-on-detach seed)
(!dot ($ "#select-all") (unbind))
(!dot ($ "#select-none") (unbind))
(!dot ($ ,(format "#~a input.report-action" (get-id))) (unbind))))))
; Provide statements -----------------------------
(provide actionable-entity-report%)
(provide (except-out (all-from-out "actionable-entity-report-internal.ss") create-report-action make-report-action)
(rename-out [create-report-action make-report-action])
default-report-action)