-
Notifications
You must be signed in to change notification settings - Fork 6
/
wrapper.go
180 lines (149 loc) · 5.1 KB
/
wrapper.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
package control
import (
"bytes"
"context"
"github.com/goradd/goradd/pkg/html"
"github.com/goradd/goradd/pkg/page"
)
const (
DivWrapper = "bootstrap.Div"
FormGroupWrapper = "bootstrap.FormGroup"
FieldsetWrapper = "bootstrap.Fieldset"
)
// DivWrapperType is a wrapper similar to a form group, but simply without the FormGroup class added. Use this for
// wrapping inline elements and other special situations listed in the Bootstrap doc under the Forms component.
// https://getbootstrap.com/docs/4.1/components/forms/ as of this writing
type DivWrapperType struct {
page.LabelWrapperType
ΩinnerDivAttr *html.Attributes
UseTooltips bool // uses tooltips for the error class
}
func NewDivWrapper() *DivWrapperType {
return &DivWrapperType{}
}
/*
func (w *DivWrapperType) Copy() *DivWrapperType {
wNew := &DivWrapperType{}
wNew.LabelWrapperType = *w.LabelWrapperType.Copy()
wNew.ΩinnerDivAttr = w.ΩinnerDivAttr.Copy()
wNew.UseTooltips = w.UseTooltips
return wNew
}*/
func (w *DivWrapperType) ΩNewI() page.WrapperI {
return NewDivWrapper()
}
func (w *DivWrapperType) ΩWrap(ctx context.Context, ctrl page.ControlI, html string, buf *bytes.Buffer) {
FormGroupTmpl(ctx, w, ctrl, html, buf)
}
func (w DivWrapperType) TypeName() string {
return DivWrapper
}
// InnerDivAttributes returns attributes for the innerDiv. Changes will be remembered, but only drawn if you redraw the
// control. If you set these, the control
// itself will be wrapped with a div with these attributes. This is useful for layouts that have the label next to
// the control.
func (w *DivWrapperType) InnerDivAttributes() *html.Attributes {
if w.ΩinnerDivAttr == nil {
w.ΩinnerDivAttr = html.NewAttributes()
}
return w.ΩinnerDivAttr
}
// HasInnerDivAttributes returns true if the wrapper has attributes on the inner div.
func (w *DivWrapperType) HasInnerDivAttributes() bool {
if w.ΩinnerDivAttr == nil || w.ΩinnerDivAttr.Len() == 0 {
return false
}
return true
}
// SetUseTooltips sets whether to use tooltips to display validation messages.
func (w *DivWrapperType) SetUseTooltips(t bool) *DivWrapperType {
w.UseTooltips = t
return w
}
// ΩAjaxRender is called by the framework to draw any changes to the wrapper that we have recorded.
// This has to work closely with the wrapper template so that it would create the same effect as if that
// entire control had been redrawn
func (w *DivWrapperType) ΩAjaxRender(ctx context.Context, response *page.Response, c page.ControlI) {
var class string
if w.ValidationStateChanged {
switch c.ValidationState() {
case page.ValidationWaiting:
response.ExecuteControlCommand(c.ID(), "removeClass", "is-valid")
response.ExecuteControlCommand(c.ID(), "removeClass", "is-invalid")
if w.UseTooltips {
class = "valid-tooltip"
} else {
class = "valid-feedback"
}
case page.ValidationValid:
response.ExecuteControlCommand(c.ID(), "addClass", "is-valid")
response.ExecuteControlCommand(c.ID(), "removeClass", "is-invalid")
if w.UseTooltips {
class = "valid-tooltip"
} else {
class = "valid-feedback"
}
case page.ValidationInvalid:
response.ExecuteControlCommand(c.ID(), "removeClass", "is-valid")
response.ExecuteControlCommand(c.ID(), "addClass", "is-invalid")
if w.UseTooltips {
class = "invalid-tooltip"
} else {
class = "invalid-feedback"
}
}
response.ExecuteControlCommand(c.ID() + "_err", "attr", "class", class)
}
w.LabelWrapperType.ΩAjaxRender(ctx, response, c)
}
func (w *DivWrapperType) ΩModifyDrawingAttributes(c page.ControlI, attr *html.Attributes) {
switch c.ValidationState() {
case page.ValidationValid:
attr.AddClass("is-valid")
case page.ValidationInvalid:
attr.AddClass("is-invalid")
}
}
type FormGroupWrapperType struct {
DivWrapperType
}
func NewFormGroupWrapper() *FormGroupWrapperType {
w := new(FormGroupWrapperType)
return w
}
func (w *FormGroupWrapperType)ΩNewI() page.WrapperI {
return NewFormGroupWrapper()
}
func (w *FormGroupWrapperType) ΩWrap(ctx context.Context, ctrl page.ControlI, html string, buf *bytes.Buffer) {
ctrl.WrapperAttributes().AddClass("form-group")
FormGroupTmpl(ctx, &w.DivWrapperType, ctrl, html, buf)
}
func (w FormGroupWrapperType) TypeName() string {
return FormGroupWrapper
}
type FieldsetWrapperType struct {
page.LabelWrapperType
UseTooltips bool // uses tooltips for the error class
}
// https://getbootstrap.com/docs/4.1/components/forms/#horizontal-form
func NewFieldsetWrapper() *FieldsetWrapperType {
return new(FieldsetWrapperType)
}
func (w *FieldsetWrapperType) ΩNewI() page.WrapperI {
return NewFieldsetWrapper()
}
func (w *FieldsetWrapperType) ΩWrap(ctx context.Context, ctrl page.ControlI, html string, buf *bytes.Buffer) {
FieldsetTmpl(ctx, w, ctrl, html, buf)
}
func (w *FieldsetWrapperType) SetUseTooltips(t bool) *FieldsetWrapperType {
w.UseTooltips = t
return w
}
func (w *FieldsetWrapperType) TypeName() string {
return FieldsetWrapper
}
func init() {
page.RegisterControlWrapper(DivWrapper, &DivWrapperType{})
page.RegisterControlWrapper(FormGroupWrapper, &FormGroupWrapperType{})
page.RegisterControlWrapper(FieldsetWrapper, &FieldsetWrapperType{})
}