This repository has been archived by the owner on May 29, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
foundation.go
146 lines (120 loc) · 3.12 KB
/
foundation.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
// Foundation Second Layer (http://foundation.zurb.com/)
package foundation
import (
"fmt"
"github.com/cjtoolkit/form"
"io"
"strings"
)
type foundation struct{}
func (f foundation) addClass(src string, classes ...string) (str string) {
if src == "" {
str = strings.Join(classes, " ")
return
}
srcs := strings.Split(src, " ")
srcs = append(srcs, classes...)
str = strings.Join(srcs, " ")
return
}
func (f foundation) Render(w io.Writer, r form.RenderData) {
before := ""
after := ""
r.Fns.Call("html", map[string]interface{}{
"before": &before,
"after": &after,
})
fmt.Fprint(w, before)
fmt.Fprintf(w, `<div id="form-group-%d">`, r.Count)
beforeInput := ""
afterInput := ""
startOfGroup := ""
endOfGroup := ""
r.Fns.Call("foundation", map[string]interface{}{
"beforeInput": &beforeInput,
"afterInput": &afterInput,
"startOfGroup": &startOfGroup,
"endOfGroup": &endOfGroup,
})
fmt.Fprint(w, startOfGroup)
labelContent := ""
labelFor := ""
var labelAttr map[string]string
renderedLabelAttr := ""
if r.Type == form.InputRadio {
goto formField
}
r.Fns.Call("label", map[string]interface{}{
"content": &labelContent,
"for": &labelFor,
"attr": &labelAttr,
})
if labelFor == "" {
goto formField
}
if labelAttr != nil {
delete(labelAttr, "for")
if r.Error != nil {
labelAttr["class"] = f.addClass(labelAttr["class"], "error")
}
renderedLabelAttr = form.RenderAttr(labelAttr)
}
fmt.Fprintf(w, `<label for="%s" %s>%s</label>`, es(labelFor), renderedLabelAttr, es(labelContent))
formField:
fmt.Fprint(w, beforeInput)
// render first layer stacks
for _, field := range r.FirstLayerStacks {
switch field := field.(type) {
case *form.FirstLayerInput:
addErrorAndRender := func() {
if r.Error != nil {
field.Attr["class"] = f.addClass(field.Attr["class"], "error")
}
field.Render(w)
}
switch field.Attr["type"] {
case "radio":
label := strings.TrimSpace(field.Label)
if label != "" {
field.Label = ""
if r.Error == nil {
fmt.Fprint(w, `<label>`)
} else {
fmt.Fprint(w, `<label class="error">`)
}
addErrorAndRender()
fmt.Fprintf(w, ` %s</label>`, es(label))
} else {
addErrorAndRender()
}
default:
addErrorAndRender()
}
case *form.FirstLayerTextarea:
if r.Error != nil {
field.Attr["class"] = f.addClass(field.Attr["class"], "error")
}
field.Render(w)
case *form.FirstLayerSelect:
if r.Error != nil {
field.Attr["class"] = f.addClass(field.Attr["class"], "error")
}
field.Render(w)
}
}
fmt.Fprint(w, afterInput)
if r.Error != nil {
fmt.Fprintf(w, `<small class="error">%s</small>`, es(r.Error.Error()))
} else {
fmt.Fprint(w, `<small class="error" style="display: none;"></small>`)
}
fmt.Fprint(w, endOfGroup)
fmt.Fprint(w, `</div>`) // Close form group
fmt.Fprint(w, after)
}
/*
Foundation Second Layer (http://foundation.zurb.com/)
*/
func SecondLayer() form.RenderSecondLayer {
return foundation{}
}