-
Notifications
You must be signed in to change notification settings - Fork 0
/
attribute-editor-internal.ss
224 lines (175 loc) · 7.17 KB
/
attribute-editor-internal.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
#lang scheme/base
(require "base.ss")
(require (smoke-in)
(snooze-in)
(unlib-in string symbol)
"check-label.ss"
"editor-internal.ss")
; Configuration ----------------------------------
; (parameter xml+quotable)
(define default-required-label
(make-parameter " (required)"))
; Interfaces -------------------------------------
(define attribute-editor<%>
(interface (editor<%> labelled-element<%> check-label<%>)
get-attributes ; -> (listof attribute)
set-attributes! ; (listof attribute) -> void
destructure!
restructure))
; Mixins -----------------------------------------
(define attribute-editor-mixin
(mixin/cells (form-element<%> labelled-element<%> editor<%> check-label<%>) (attribute-editor<%>)
(inherit get-component-id
get-id
set-id!
get-value
set-value!
render-check-label
set-label!
value-changed?)
; Fields -------------------------------------
; Have to call super-new first to make sure the default component-id is initialised:
(super-new)
; (listof attribute)
(init-cell attributes null #:accessor #:mutator)
; boolean
(init-field required?
(for/or ([attr (in-list attributes)])
(not (type-allows-null? (attribute-type attr))))
#:accessor)
(init-field required-label
(default-required-label)
#:accessor
#:mutator)
(init [id (or (attributes->id attributes) (gensym/interned 'smoke))]
[label (or (attributes->label attributes) (xml-quote id))])
(set-id! id)
(set-label! label)
; Methods ------------------------------------
; check-result -> boolean
(define/override (report-result? result)
(or (super report-result? result)
(ormap (cut check-result-has-attribute? result <>)
(get-attributes))))
; -> symbol
(define/public (get-wrapper-id)
(symbol-append (get-id) '-wrapper))
; seed -> xml
(define/override (render-label-content seed)
(if required?
(xml ,(super render-label-content seed)
,(opt-xml required-label ,required-label))
(super render-label-content seed)))
; seed -> xml
(define/override (render seed)
(xml (span (@ [id ,(get-wrapper-id)])
,(super render seed) " "
,(render-check-label seed))))
; snooze-struct -> snooze-struct
(define/public (destructure! struct)
(match (get-attributes)
[(list-rest (? attribute? attr) _)
(set-value! (snooze-struct-ref struct attr))]
[attrs (raise-type-error 'attribute-editor.destructure! "(list attribute attribute ...)" attrs)]))
; snooze-struct -> snooze-struct
(define/public (restructure struct)
(match (get-attributes)
[(list-rest (? attribute? attr) _)
(snooze-struct-set struct attr (get-value))]
[attrs (raise-type-error 'attribute-editor.restructure "(list attribute attribute ...)" attrs)]))
; -> (listof check-result)
(define/override (parse)
(check/annotate ([ann:form-elements (list this)])
(check-until-problems
(cut super parse)
(cut with-handlers ([exn:smoke:form? (lambda (exn) (check-fail (exn-message exn)))])
(if (and required? (not (get-value)))
(check-fail "Value is required.")
(check-pass))))))
; -> (listof check-result)
(define/override (validate)
(check/annotate ([ann:attrs (get-attributes)])
(check-until-problems
(cut super validate)
(cut with-handlers ([exn:smoke:form? (lambda (exn) (check-fail (exn-message exn)))])
(if (and required? (not (get-value)))
(check-fail "Value is required.")
(check-pass))))))
; seed -> js
(define/override (get-on-render seed)
(js (!dot Smoke (insertHTML (!dot Smoke (findById ,(get-wrapper-id)))
"replace"
,(xml->string (render seed))))))))
(define complete-attribute-editor-mixin
(compose attribute-editor-mixin
check-label-mixin
simple-editor-mixin
labelled-element-mixin))
; Classes ----------------------------------------
(define simple-attribute-editor%
(class/cells (labelled-element-mixin (check-label-mixin (simple-editor-mixin form-element%))) (attribute-editor<%>)
(inherit core-html-attributes
get-component-id
set-id!
render-check-label
set-label!)
; Fields -------------------------------------
; (listof attribute)
(init-cell attributes null #:accessor #:mutator)
; boolean
(init-field required?
(and (pair? attributes)
(let ([attr (car attributes)])
(not (type-allows-null? (attribute-type attr)))))
#:accessor)
(init-field required-label
(default-required-label)
#:accessor
#:mutator)
(init [id (or (attributes->id attributes) (get-component-id))]
[label (if (pair? attributes)
(let ([attr (car attributes)])
(xml-quote (string-sentencecase (attribute-pretty-name attr))))
(xml-quote id))])
(super-new [id id] [label label])
; Methods ------------------------------------
; seed -> xml
(define/override (render-label-content seed)
(if required?
(xml ,(super render-label-content seed)
,(opt-xml required-label " " ,required-label))
(super render-label-content seed)))
; check-result -> boolean
(define/override (report-result? result)
(or (super report-result? result)
(ormap (cut check-result-has-attribute? result <>)
(get-attributes))))
; seed -> xml
(define/overment (render seed)
(xml (span (@ ,(core-html-attributes seed))
,(inner (xml) render seed) " "
,(render-check-label seed))))
; snooze-struct -> snooze-struct
(define/public (destructure! struct)
(error "simple-attribute-editor.destructure! must be overridden"))
; snooze-struct -> snooze-struct
(define/public (restructure struct)
(error "simple-attribute-editor.restructure must be overridden"))))
; Helpers ----------------------------------------
; (listof attribute) -> (U symbol #f)
(define (attributes->id attributes)
(and (pair? attributes)
(let ([attr (car attributes)])
(gensym/interned (html-id-encode (format "~a-~a" (entity-name (attribute-entity attr)) (attribute-name attr)))))))
; (listof attribute) -> (U xml #f)
(define (attributes->label attributes)
(and (pair? attributes)
(let ([attr (car attributes)])
(xml-quote (string-sentencecase (attribute-pretty-name attr))))))
; Provide statements -----------------------------
(provide attribute-editor<%>
attribute-editor-mixin
complete-attribute-editor-mixin
simple-attribute-editor%)
(provide/contract
[default-required-label (parameter/c xml+quotable?)])