This repository has been archived by the owner on Sep 12, 2022. It is now read-only.
forked from ponzu-cms/ponzu
-
Notifications
You must be signed in to change notification settings - Fork 1
/
repeaters.go
498 lines (424 loc) · 15.1 KB
/
repeaters.go
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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
package editor
import (
"bytes"
"fmt"
"log"
"strings"
)
// InputRepeater returns the []byte of an <input> HTML element with a label.
// It also includes repeat controllers (+ / -) so the element can be
// dynamically multiplied or reduced.
// IMPORTANT:
// The `fieldName` argument will cause a panic if it is not exactly the string
// form of the struct field that this editor input is representing
// type Person struct {
// item.Item
// editor editor.Editor
//
// Names []string `json:"names"`
// //...
// }
//
// func (p *Person) MarshalEditor() ([]byte, error) {
// view, err := editor.Form(p,
// editor.Field{
// View: editor.InputRepeater("Names", p, map[string]string{
// "label": "Names",
// "type": "text",
// "placeholder": "Enter a Name here",
// }),
// }
// )
// }
func InputRepeater(fieldName string, p interface{}, attrs map[string]string) []byte {
// find the field values in p to determine pre-filled inputs
fieldVals := ValueFromStructField(fieldName, p)
vals := strings.Split(fieldVals, "__kudzu")
scope := TagNameFromStructField(fieldName, p)
html := bytes.Buffer{}
_, err := html.WriteString(`<span class="__kudzu-repeat ` + scope + `">`)
if err != nil {
log.Println("Error writing HTML string to InputRepeater buffer")
return nil
}
for i, val := range vals {
el := &Element{
TagName: "input",
Attrs: attrs,
Name: TagNameFromStructFieldMulti(fieldName, i, p),
Data: val,
ViewBuf: &bytes.Buffer{},
}
// only add the label to the first input in repeated list
if i == 0 {
el.Label = attrs["label"]
}
_, err := html.Write(DOMElementSelfClose(el))
if err != nil {
log.Println("Error writing DOMElementSelfClose to InputRepeater buffer")
return nil
}
}
_, err = html.WriteString(`</span>`)
if err != nil {
log.Println("Error writing HTML string to InputRepeater buffer")
return nil
}
return append(html.Bytes(), RepeatController(fieldName, p, "input", ".input-field")...)
}
// SelectRepeater returns the []byte of a <select> HTML element plus internal <options> with a label.
// It also includes repeat controllers (+ / -) so the element can be
// dynamically multiplied or reduced.
// IMPORTANT:
// The `fieldName` argument will cause a panic if it is not exactly the string
// form of the struct field that this editor input is representing
func SelectRepeater(fieldName string, p interface{}, attrs, options map[string]string) []byte {
// options are the value attr and the display value, i.e.
// <option value="{map key}">{map value}</option>
scope := TagNameFromStructField(fieldName, p)
html := bytes.Buffer{}
_, err := html.WriteString(`<span class="__kudzu-repeat ` + scope + `">`)
if err != nil {
log.Println("Error writing HTML string to SelectRepeater buffer")
return nil
}
// find the field values in p to determine if an option is pre-selected
fieldVals := ValueFromStructField(fieldName, p)
vals := strings.Split(fieldVals, "__kudzu")
if _, ok := attrs["class"]; ok {
attrs["class"] += " browser-default"
} else {
attrs["class"] = "browser-default"
}
// loop through vals and create selects and options for each, adding to html
if len(vals) > 0 {
for i, val := range vals {
sel := &Element{
TagName: "select",
Attrs: attrs,
Name: TagNameFromStructFieldMulti(fieldName, i, p),
ViewBuf: &bytes.Buffer{},
}
// only add the label to the first select in repeated list
if i == 0 {
sel.Label = attrs["label"]
}
// create options for select element
var opts []*Element
// provide a call to action for the select element
cta := &Element{
TagName: "option",
Attrs: map[string]string{"disabled": "true", "selected": "true"},
Data: "Select an option...",
ViewBuf: &bytes.Buffer{},
}
// provide a selection reset (will store empty string in db)
reset := &Element{
TagName: "option",
Attrs: map[string]string{"value": ""},
Data: "None",
ViewBuf: &bytes.Buffer{},
}
opts = append(opts, cta, reset)
for k, v := range options {
optAttrs := map[string]string{"value": k}
if k == val {
optAttrs["selected"] = "true"
}
opt := &Element{
TagName: "option",
Attrs: optAttrs,
Data: v,
ViewBuf: &bytes.Buffer{},
}
opts = append(opts, opt)
}
_, err := html.Write(DOMElementWithChildrenSelect(sel, opts))
if err != nil {
log.Println("Error writing DOMElementWithChildrenSelect to SelectRepeater buffer")
return nil
}
}
}
_, err = html.WriteString(`</span>`)
if err != nil {
log.Println("Error writing HTML string to SelectRepeater buffer")
return nil
}
return append(html.Bytes(), RepeatController(fieldName, p, "select", ".input-field")...)
}
// FileRepeater returns the []byte of a <input type="file"> HTML element with a label.
// It also includes repeat controllers (+ / -) so the element can be
// dynamically multiplied or reduced.
// IMPORTANT:
// The `fieldName` argument will cause a panic if it is not exactly the string
// form of the struct field that this editor input is representing
func FileRepeater(fieldName string, p interface{}, attrs map[string]string) []byte {
// find the field values in p to determine if an option is pre-selected
fieldVals := ValueFromStructField(fieldName, p)
vals := strings.Split(fieldVals, "__kudzu")
addLabelFirst := func(i int, label string) string {
if i == 0 {
return `<label class="active">` + label + `</label>`
}
return ""
}
tmpl :=
`<div class="file-input %[5]s %[4]s input-field col s12">
%[2]s
<div class="file-field input-field">
<div class="btn">
<span>Upload</span>
<input class="upload %[4]s" type="file" />
</div>
<div class="file-path-wrapper">
<input class="file-path validate" placeholder="Add %[5]s" type="text" />
</div>
</div>
<div class="preview"><div class="img-clip"></div></div>
<input class="store %[4]s" type="hidden" name="%[1]s" value="%[3]s" />
</div>`
// 1=nameidx, 2=addLabelFirst, 3=val, 4=className, 5=fieldName
script :=
`<script>
$(function() {
var $file = $('.file-input.%[2]s'),
upload = $file.find('input.upload'),
store = $file.find('input.store'),
preview = $file.find('.preview'),
clip = preview.find('.img-clip'),
reset = document.createElement('div'),
img = document.createElement('img'),
video = document.createElement('video'),
unknown = document.createElement('div'),
viewLink = document.createElement('a'),
viewLinkText = document.createTextNode('Download / View '),
iconLaunch = document.createElement('i'),
iconLaunchText = document.createTextNode('launch'),
uploadSrc = store.val();
video.setAttribute
preview.hide();
viewLink.setAttribute('href', '%[3]s');
viewLink.setAttribute('target', '_blank');
viewLink.appendChild(viewLinkText);
viewLink.style.display = 'block';
viewLink.style.marginRight = '10px';
viewLink.style.textAlign = 'right';
iconLaunch.className = 'material-icons tiny';
iconLaunch.style.position = 'relative';
iconLaunch.style.top = '3px';
iconLaunch.appendChild(iconLaunchText);
viewLink.appendChild(iconLaunch);
preview.append(viewLink);
// when %[2]s input changes (file is selected), remove
// the 'name' and 'value' attrs from the hidden store input.
// add the 'name' attr to %[2]s input
upload.on('change', function(e) {
resetImage();
});
if (uploadSrc.length > 0) {
var ext = uploadSrc.substring(uploadSrc.lastIndexOf('.'));
ext = ext.toLowerCase();
switch (ext) {
case '.jpg':
case '.jpeg':
case '.webp':
case '.gif':
case '.png':
$(img).attr('src', store.val());
clip.append(img);
break;
case '.mp4':
case '.webm':
$(video)
.attr('src', store.val())
.attr('type', 'video/'+ext.substring(1))
.attr('controls', true)
.css('width', '100%%');
clip.append(video);
break;
default:
$(img).attr('src', '/admin/static/dashboard/img/kudzu-file.png');
$(unknown)
.css({
position: 'absolute',
top: '10px',
left: '10px',
border: 'solid 1px #ddd',
padding: '7px 7px 5px 12px',
fontWeight: 'bold',
background: '#888',
color: '#fff',
textTransform: 'uppercase',
letterSpacing: '2px'
})
.text(ext);
clip.append(img);
clip.append(unknown);
clip.css('maxWidth', '200px');
}
preview.show();
$(reset).addClass('reset %[2]s btn waves-effect waves-light grey');
$(reset).html('<i class="material-icons tiny">clear<i>');
$(reset).on('click', function(e) {
e.preventDefault();
preview.animate({"opacity": 0.1}, 200, function() {
preview.slideUp(250, function() {
resetImage();
});
})
});
clip.append(reset);
}
function resetImage() {
store.val('');
store.attr('name', '');
upload.attr('name', '%[1]s');
clip.empty();
}
});
</script>`
// 1=nameidx, 2=className
name := TagNameFromStructField(fieldName, p)
html := bytes.Buffer{}
_, err := html.WriteString(`<span class="__kudzu-repeat ` + name + `">`)
if err != nil {
log.Println("Error writing HTML string to FileRepeater buffer")
return nil
}
for i, val := range vals {
className := fmt.Sprintf("%s-%d", name, i)
nameidx := TagNameFromStructFieldMulti(fieldName, i, p)
_, err := html.WriteString(fmt.Sprintf(tmpl, nameidx, addLabelFirst(i, attrs["label"]), val, className, fieldName))
if err != nil {
log.Println("Error writing HTML string to FileRepeater buffer")
return nil
}
_, err = html.WriteString(fmt.Sprintf(script, nameidx, className, val))
if err != nil {
log.Println("Error writing HTML string to FileRepeater buffer")
return nil
}
}
_, err = html.WriteString(`</span>`)
if err != nil {
log.Println("Error writing HTML string to FileRepeater buffer")
return nil
}
return append(html.Bytes(), RepeatController(fieldName, p, "input.upload", "div.file-input."+fieldName)...)
}
// RepeatController generates the javascript to control any repeatable form
// element in an editor based on its type, field name and HTML tag name
func RepeatController(fieldName string, p interface{}, inputSelector, cloneSelector string) []byte {
scope := TagNameFromStructField(fieldName, p)
script := `
<script>
$(function() {
// define the scope of the repeater
var scope = $('.__kudzu-repeat.` + scope + `');
var getChildren = function() {
return scope.find('` + cloneSelector + `')
}
var resetFieldNames = function() {
// loop through children, set its name to the fieldName.i where
// i is the current index number of children array
var children = getChildren();
for (var i = 0; i < children.length; i++) {
var preset = false;
var $el = children.eq(i);
var name = '` + scope + `.'+String(i);
$el.find('` + inputSelector + `').attr('name', name);
// ensure no other input-like elements besides ` + inputSelector + `
// get the new name by setting it to an empty string
$el.find('input, select, textarea').each(function(i, elem) {
var $elem = $(elem);
// if the elem is not ` + inputSelector + ` and has no value
// set the name to an empty string
if (!$elem.is('` + inputSelector + `')) {
if ($elem.val() === '' || $elem.is('.file-path')) {
$elem.attr('name', '');
} else {
$elem.attr('name', name);
preset = true;
}
}
});
// if there is a preset value, remove the name attr from the
// ` + inputSelector + ` element so it doesn't overwrite db
if (preset) {
$el.find('` + inputSelector + `').attr('name', '');
}
// reset controllers
$el.find('.controls').remove();
}
applyRepeatControllers();
}
var addRepeater = function(e) {
e.preventDefault();
var add = e.target;
// find and clone the repeatable input-like element
var source = $(add).parent().closest('` + cloneSelector + `');
var clone = source.clone();
// if clone has label, remove it
clone.find('label').remove();
// remove the pre-filled value from clone
clone.find('` + inputSelector + `').val('');
clone.find('input').val('');
// remove controls from clone if already present
clone.find('.controls').remove();
// remove input preview on clone if copied from source
clone.find('.preview').remove();
// add clone to scope and reset field name attributes
scope.append(clone);
resetFieldNames();
}
var delRepeater = function(e) {
e.preventDefault();
// do nothing if the input is the only child
var children = getChildren();
if (children.length === 1) {
return;
}
var del = e.target;
// pass label onto next input-like element if del 0 index
var wrapper = $(del).parent().closest('` + cloneSelector + `');
if (wrapper.find('` + inputSelector + `').attr('name') === '` + scope + `.0') {
wrapper.next().append(wrapper.find('label'))
}
wrapper.remove();
resetFieldNames();
}
var createControls = function() {
// create + / - controls for each input-like child element of scope
var add = $('<button>+</button>');
add.addClass('repeater-add');
add.addClass('btn-flat waves-effect waves-green');
var del = $('<button>-</button>');
del.addClass('repeater-del');
del.addClass('btn-flat waves-effect waves-red');
var controls = $('<span></span>');
controls.addClass('controls');
controls.addClass('right');
// bind listeners to child's controls
add.on('click', addRepeater);
del.on('click', delRepeater);
controls.append(add);
controls.append(del);
return controls;
}
var applyRepeatControllers = function() {
// add controls to each child
var children = getChildren()
for (var i = 0; i < children.length; i++) {
var el = children[i];
$(el).find('` + inputSelector + `').parent().find('.controls').remove();
var controls = createControls();
$(el).append(controls);
}
}
resetFieldNames();
});
</script>
`
return []byte(script)
}