blob: b4c0795de380295736baa88eda4d684d24d36352 (
plain)
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
|
(import-macros {:compile-html HTML} :macros)
(local lib (require :lib))
(local required-marker
(HTML [:span {:class "form-required-marker"} " (обяз.)"]))
(fn textarea-input [name label required? minlength maxlength help]
(local minlength (or minlength 0))
(local maxlength (or maxlength 1000))
{: name : label : required? : help
:validate
(fn [value]
(if (<= minlength (# value) maxlength)
nil
"Некорректная длина текста."))
:html
(fn [value error]
(HTML
[:div {:class "form-row"}
[:label {:class "form-label" :for name}
label (if required? required-marker "")]
[:textarea (fn [] {:name name :id name :class "form-input"
:minlength (tostring minlength)
:maxlength (tostring maxlength)
:required required?})
(or value "")]
(if error (HTML [:div {:class "form-error"} error]) "")
(if help (HTML [:div {:class "form-help"} help]) "")]))})
(fn text-input [name label required? minlength maxlength help]
(local minlength (or minlength 0))
(local maxlength (or maxlength 200))
{: name : label : required? : help
:validate
(fn [value]
(if (<= minlength (# value) maxlength)
nil
"Некорректная длина текста."))
:html
(fn [value error]
(HTML
[:div {:class "form-row"}
[:label {:class "form-label" :for name}
label (if required? required-marker "")]
[:input (fn [] {:type "text" :name name :id name :class "form-input"
:minlength (tostring minlength)
:maxlength (tostring maxlength)
:required required?
:value value})]
(if error (HTML [:div {:class "form-error"} error]) "")
(if help (HTML [:div {:class "form-help"} help]) "")]))})
(fn password-input [name label required? minlength maxlength help]
(local minlength (or minlength 0))
(local maxlength (or maxlength 200))
{: name : label : required? : help
:validate
(fn [value]
(if (<= minlength (# value) maxlength)
nil
"Некорректная длина текста."))
:html
(fn [value error]
(HTML
[:div {:class "form-row"}
[:label {:class "form-label" :for name}
label (if required? required-marker "")]
[:input (fn [] {:type "password" :name name :id name :class "form-input"
:minlength (tostring minlength)
:maxlength (tostring maxlength)
:required required?
:value value})]
(if error (HTML [:div {:class "form-error"} error]) "")
(if help (HTML [:div {:class "form-help"} help]) "")]))})
(fn url-input [name label required? help]
{: name : label : required? : help
:validate
(fn [value]
(if (<= 0 (# value) 1000)
nil
"Некорректная длина ссылки."))
:html
(fn [value error]
(HTML
[:div {:class "form-row"}
[:label {:class "form-label" :for name}
label (if required? required-marker "")]
[:input (fn [] {:type "url" :name name :id name :class "form-input"
:required required? :value value})]
(if error (HTML [:div {:class "form-error"} error]) "")
(if help (HTML [:div {:class "form-help"} help]) "")]))})
(fn number-input [name label required? min max help]
(local min (or min 0))
(local max (or max 100))
{: name : label : required? : help
:validate
(fn [value]
(if (<= min (tonumber value) max)
nil
"Некорректное число."))
:html
(fn [value error]
(HTML
[:div {:class "form-row"}
[:label {:class "form-label" :for name} label (if required? required-marker "")]
[:input (fn [] {:type "number" :name name :id name :class "form-input"
:required required?
:min (tostring min) :max (tostring max)
:value (tostring value)})]
(if error (HTML [:div {:class "form-error"} error]) "")
(if help (HTML [:div {:class "form-help"} help]) "")]))})
(fn checkbox-input [name label required? help]
{: name : label : required? : help
:value-from-html
(fn [value] (= value "on"))
:html
(fn [value error]
(HTML
[:div {:class "form-row"}
[:input (fn [] {:type "checkbox" :name name :id name :class "form-input"
:required required?
:checked (or (= value "on") (= value true))})]
[:label {:class "form-label" :for name} label (if required? required-marker "")]
(if error (HTML [:div {:class "form-error"} error]) "")
(if help (HTML [:div {:class "form-help"} help]) "")]))})
(fn file-input [name label required? accept thumbnail-width help]
(local onchange-input-js
"const file = event.target.files[0];
const containerEl = event.target.parentElement.parentElement;
if (file && file.name.toLowerCase().endsWith('.jpg')) {
const reader = new FileReader();
reader.onload = function () {
const imgEl = containerEl.querySelector('.form-file-img')
imgEl.src = reader.result;
imgEl.classList.add('d-block');
}
reader.readAsDataURL(event.target.files[0]);
}
containerEl.querySelector('.form-file-reset')
.classList.add('d-block');")
(local reset-button-js
"const parentEl = event.target.parentElement;
for (const inputEl of parentEl.querySelectorAll('input')) {
inputEl.value = ''
}
parentEl.parentElement.querySelector('.form-file-img')
.classList.remove('d-block');
event.target.classList.remove('d-block');")
{:type "file" : name : label : required? : help
:value-from-html
(fn [value {: data : db}]
(if (= (type value) "table")
(lib.handle-upload db value nil thumbnail-width)
(not (lib.empty? value))
value
(let [previous-value (. data (.. name "_previous"))]
(if (not (lib.empty? previous-value))
previous-value
nil))))
:html
(fn [value error]
(local empty-value? (lib.empty? value))
(local required? (and empty-value? required?))
(HTML
[:div {:class "form-row"}
[:div {:class "d-flex gap-1"}
(HTML [:img (fn []
{:class (.. "form-file-img"
(if (and value (lib.ends-with? value ".jpg"))
" d-block"
""))
:src (if value
(.. "/static/files/" value "-thumbnail.jpg")
"")})])
[:div {}
[:label {:class "form-label" :for name} label (if required? required-marker "")]
[:input (fn [] {:type "file" :name name :id name :class "form-input"
:onchange onchange-input-js
:required required? :accept accept})]
(if (not empty-value?)
(HTML [:input {:type "hidden" :name (.. name "_previous")
:value value}])
"")
[:button
(fn []
{:type "button"
:style "display: none"
:class (.. "form-file-reset" (if value " d-block" ""))
:onclick reset-button-js})
[:NO-ESCAPE "× Сбросить"]]]]
(if error (HTML [:div {:class "form-error"} error]) "")
(if help (HTML [:div {:class "form-help"} help]) "")]))})
(fn select-input [name label required? options help]
{: name : label : required? : options : help
:validate
(fn [value]
(var exists? false)
(each [_ option (ipairs options)]
(when (= option.value value)
(set exists? true)))
(if exists? nil "Некорректное значение."))
:html
(fn [value error]
(HTML
[:div {:class "form-row"}
[:label {:class "form-label" :for name} label (if required? required-marker "")]
[:select (fn [] {:name name :id name
:required required?})
[:option [:selected "selected"] ""]
(table.concat
(icollect [_ option (ipairs options)]
(HTML
[:option
(fn [] {:value option.value :selected (= value option.value)})
option.label])))]
(if error [:div {:class "form-error"} error] "")
(if help [:div {:class "form-help"} help] "")]))})
(fn render-form [form data errors]
(HTML
[:form {:class "form" :enctype "multipart/form-data" :method "POST"}
(table.concat
(lib.append
(icollect [_ group (ipairs form)]
(HTML
[:div {:class "form-group"}
[:h3 {:class "form-subheader"} group.title]
(table.concat
(icollect [_ field (ipairs group.fields)]
(field.html (. data field.name) (. errors field.name))))]))
(HTML [:button {:type "submit"} "Сохранить"])))]))
(fn convert-values-from-html [form data db]
(each [_ group (ipairs form)]
(each [_ field (ipairs group.fields)]
(local value (. data field.name))
(when field.value-from-html
(tset data field.name (field.value-from-html value {: data : db})))))
data)
(fn validate-form [form data]
(var errors [])
(each [_ group (ipairs form)]
(each [_ field (ipairs group.fields)]
(local value (. data field.name))
(local empty-value? (lib.empty? value))
(when (and field.required? empty-value?)
(tset errors field.name "Поле должно быть заполнено."))
(when (and value (not empty-value?) field.validate)
(local err (field.validate value))
(when err (tset errors field.name err)))))
errors)
(fn form-insert-sql-statement [table-name form data extra-data]
(var columns [])
(var args [])
(var i 1)
(each [_ group (ipairs form)]
(each [_ field (ipairs group.fields)]
(table.insert columns field.name)
(tset args i (. data field.name))
(set i (+ 1 i))))
(when extra-data
(each [key value (pairs extra-data)]
(table.insert columns key)
(tset args i value)
(set i (+ 1 i))))
;; specify count of args so go knows how many there are args including nils
(tset args :n (- i 1))
(if (< 0 (# columns))
[(.. "INSERT INTO " table-name " (" (table.concat columns ", ") ") VALUES "
"(" (table.concat (icollect [_ _ (ipairs columns)] "?") ", ") ")")
args]
nil))
(fn form-update-sql-statement [table-name form data extra-data where]
(var columns [])
(var args [])
(var i 1)
(each [_ group (ipairs form)]
(each [_ field (ipairs group.fields)]
(table.insert columns field.name)
(tset args i (. data field.name))
(set i (+ 1 i))))
(when extra-data
(each [key value (pairs extra-data)]
(table.insert columns key)
(tset args i value)
(set i (+ 1 i))))
(var where-columns [])
(when where
(each [key value (pairs where)]
(table.insert where-columns key)
(tset args i value)
(set i (+ 1 i))))
;; specify count of args so go knows how many there are args including nils
(tset args :n (- i 1))
(if (< 0 (# columns))
[(.. "UPDATE " table-name " SET " (table.concat columns " = ?, ") " = ? "
"WHERE " (table.concat where-columns " = ?, ") " = ?")
args]
nil))
{: textarea-input
: text-input
: password-input
: number-input
: checkbox-input
: url-input
: file-input
: select-input
: render-form
: convert-values-from-html
: validate-form
: form-insert-sql-statement
: form-update-sql-statement}
|