Skip to content

Commit b61265d

Browse files
committed
update
1 parent 5f9bc93 commit b61265d

File tree

9 files changed

+48
-49
lines changed

9 files changed

+48
-49
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,13 @@ go get github.com/coscms/forms
3434
Forms
3535
=====
3636

37-
There are two predefined styles for forms: base HTML forms and Bootstrap forms: they have different structures and predefined classes.
37+
There are two predefined themes for forms: base HTML forms and Bootstrap forms: they have different structures and predefined classes.
3838
Style aside, forms can be created from scratch or starting from a base instance.
3939

4040
From scratch
4141
------------
4242

43-
You can create a form instance by simply deciding its style and providing its method and action:
43+
You can create a form instance by simply deciding its theme and providing its method and action:
4444

4545
```go
4646
form := NewWithConfig(&config.Config{

common/types.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ import (
3535
"golang.org/x/sync/singleflight"
3636
)
3737

38-
// Available form styles
38+
// Available form themes
3939
const (
4040
BASE = "base"
4141
BOOTSTRAP = "bootstrap3"
@@ -93,14 +93,14 @@ const (
9393
STATIC = "static"
9494
)
9595

96-
func SetTmplDir(style, tmplDir string) {
96+
func SetTmplDir(theme, tmplDir string) {
9797
lockTmplDir.Lock()
98-
tmplDirs[style] = tmplDir
98+
tmplDirs[theme] = tmplDir
9999
lockTmplDir.Unlock()
100100
}
101101

102-
func TmplDir(style string) (tmplDir string) {
103-
tmplDir, _ = tmplDirs[style]
102+
func TmplDir(theme string) (tmplDir string) {
103+
tmplDir, _ = tmplDirs[theme]
104104
return
105105
}
106106

fields/field.go

+9-10
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ type Field struct {
5454
Choices interface{} `json:"choices,omitempty" xml:"choices,omitempty"`
5555
ChoiceKeys map[string]ChoiceIndex `json:"choiceKeys,omitempty" xml:"choiceKeys,omitempty"`
5656
AppendData map[string]interface{} `json:"appendData,omitempty" xml:"appendData,omitempty"`
57-
Style string `json:"style" xml:"style"`
57+
Theme string `json:"theme" xml:"theme"`
5858
Format string `json:"format,omitempty" xml:"format,omitempty"`
5959
Language string `json:"language,omitempty" xml:"language,omitempty"`
6060
widget widgets.WidgetInterface // Public Widget field for widget customization
@@ -81,18 +81,17 @@ func FieldWithType(name, t string) *Field {
8181
Choices: nil,
8282
ChoiceKeys: map[string]ChoiceIndex{},
8383
AppendData: map[string]interface{}{},
84-
Style: "",
8584
}
8685
}
8786

88-
func (f *Field) SetTemplate(tmpl string, style ...string) FieldInterface {
87+
func (f *Field) SetTemplate(tmpl string, theme ...string) FieldInterface {
8988
f.Template = tmpl
9089
if len(f.Template) > 0 && f.widget != nil && f.Template != tmpl {
9190
var s string
92-
if len(style) > 0 {
93-
s = style[0]
91+
if len(theme) > 0 {
92+
s = theme[0]
9493
} else {
95-
s = f.Style
94+
s = f.Theme
9695
}
9796
f.widget = widgets.BaseWidget(s, f.Type, f.Template)
9897
}
@@ -120,10 +119,10 @@ func (f *Field) Lang() string {
120119
return f.Language
121120
}
122121

123-
// SetStyle sets the style (e.g.: BASE, BOOTSTRAP) of the field, correctly populating the Widget field.
124-
func (f *Field) SetStyle(style string) FieldInterface {
125-
f.Style = style
126-
f.widget = widgets.BaseWidget(style, f.Type, f.Template)
122+
// SetTheme sets the theme (e.g.: BASE, BOOTSTRAP) of the field, correctly populating the Widget field.
123+
func (f *Field) SetTheme(theme string) FieldInterface {
124+
f.Theme = theme
125+
f.widget = widgets.BaseWidget(theme, f.Type, f.Template)
127126
return f
128127
}
129128

fields/field_interface.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,14 @@ type FieldInterface interface {
2424
DeleteParam(key string) FieldInterface
2525
AddCSS(key, value string) FieldInterface
2626
RemoveCSS(key string) FieldInterface
27-
SetStyle(style string) FieldInterface
27+
SetTheme(theme string) FieldInterface
2828
SetLabel(label string) FieldInterface
2929
AddLabelClass(class string) FieldInterface
3030
RemoveLabelClass(class string) FieldInterface
3131
SetValue(value string) FieldInterface
3232
Disabled() FieldInterface
3333
Enabled() FieldInterface
34-
SetTemplate(tmpl string, style ...string) FieldInterface
34+
SetTemplate(tmpl string, theme ...string) FieldInterface
3535
SetHelptext(text string) FieldInterface
3636
AddError(err string) FieldInterface
3737
MultipleChoice() FieldInterface

fields/field_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111

1212
func TestTextField(t *testing.T) {
1313
f := FieldWithType(`title`, common.TEXT)
14-
f.SetStyle(`base`)
14+
f.SetTheme(`base`)
1515
f.AddClass(`form-control`).AddClass(`row`)
1616

1717
assert.Equal(t, common.HTMLAttrValues([]string{`form-control`, `row`}), f.Classes)
@@ -31,7 +31,7 @@ func TestTextField(t *testing.T) {
3131

3232
func TestCheckboxField(t *testing.T) {
3333
f := FieldWithType(`title`, common.CHECKBOX)
34-
f.SetStyle(`base`)
34+
f.SetTheme(`base`)
3535
f.AddChoice(`value1`, `text1`)
3636
f.AddChoice(`value2`, `text2`, true)
3737
f.AddChoice(`value3`, `text3`)
@@ -42,7 +42,7 @@ func TestCheckboxField(t *testing.T) {
4242

4343
func TestSelectField(t *testing.T) {
4444
f := FieldWithType(`title`, common.SELECT)
45-
f.SetStyle(`base`)
45+
f.SetTheme(`base`)
4646
f.AddChoice(`value1`, `text1`)
4747
f.AddChoice(`value2`, `text2`, true)
4848
f.AddChoice(`value3`, `text3`)

fieldset.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ type FieldSetType struct {
4040
Tags common.HTMLAttrValues `json:"tags" xml:"tags"`
4141
FieldList []fields.FieldInterface `json:"fieldList" xml:"fieldList"`
4242
AppendData map[string]interface{} `json:"appendData,omitempty" xml:"appendData,omitempty"`
43-
FormStyle string `json:"formStyle" xml:"formStyle"`
43+
FormTheme string `json:"formTheme" xml:"formTheme"`
4444
Language string `json:"language,omitempty" xml:"language,omitempty"`
4545
Template string `json:"template" xml:"template"`
4646
fieldMap map[string]int
@@ -89,7 +89,7 @@ func (f *FieldSetType) Data() map[string]interface{} {
8989

9090
func (f *FieldSetType) render() string {
9191
buf := bytes.NewBuffer(nil)
92-
tpf := common.TmplDir(f.FormStyle) + "/" + f.FormStyle + "/" + f.Template + ".html"
92+
tpf := common.TmplDir(f.FormTheme) + "/" + f.FormTheme + "/" + f.Template + ".html"
9393
tpl, err := common.GetOrSetCachedTemplate(tpf, func() (*template.Template, error) {
9494
return common.ParseFiles(common.LookupPath(tpf))
9595
})
@@ -132,7 +132,7 @@ func (f *FieldSetType) SetTemplate(tmpl string) *FieldSetType {
132132

133133
// FieldSet creates and returns a new FieldSetType with the given name and list of fields.
134134
// Every method for FieldSetType objects returns the object itself, so that call can be chained.
135-
func FieldSet(name string, label string, style string, elems ...fields.FieldInterface) *FieldSetType {
135+
func FieldSet(name string, label string, theme string, elems ...fields.FieldInterface) *FieldSetType {
136136
ret := &FieldSetType{
137137
Template: "fieldset",
138138
CurrName: name,
@@ -143,7 +143,7 @@ func FieldSet(name string, label string, style string, elems ...fields.FieldInte
143143
FieldList: elems,
144144
fieldMap: map[string]int{},
145145
AppendData: map[string]interface{}{},
146-
FormStyle: style,
146+
FormTheme: theme,
147147
}
148148
for i, elem := range elems {
149149
ret.fieldMap[elem.OriginalName()] = i
@@ -183,7 +183,7 @@ func (f *FieldSetType) Elements(elems ...config.FormElement) *FieldSetType {
183183
}
184184

185185
func (f *FieldSetType) addField(field fields.FieldInterface) *FieldSetType {
186-
field.SetStyle(f.FormStyle)
186+
field.SetTheme(f.FormTheme)
187187
field.SetData(`container`, `fieldset`)
188188
f.FieldList = append(f.FieldList, field)
189189
f.fieldMap[field.OriginalName()] = len(f.FieldList) - 1

forms.go

+9-9
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ func New() *Form {
6262
FieldList: []config.FormElement{},
6363
fieldMap: make(map[string]int),
6464
containerMap: make(map[string]string),
65-
Style: common.BASE,
65+
Theme: common.BASE,
6666
Class: common.HTMLAttrValues{},
6767
ID: "",
6868
Params: map[string]string{},
@@ -88,7 +88,7 @@ type Form struct {
8888
AppendData map[string]interface{} `json:"appendData,omitempty" xml:"appendData,omitempty"`
8989
Model interface{} `json:"model" xml:"model"`
9090
FieldList []config.FormElement `json:"fieldList" xml:"fieldList"`
91-
Style string `json:"style" xml:"style"`
91+
Theme string `json:"theme" xml:"theme"`
9292
Class common.HTMLAttrValues `json:"class" xml:"class"`
9393
ID string `json:"id" xml:"id"`
9494
Params map[string]string `json:"params" xml:"params"`
@@ -202,7 +202,7 @@ func (f *Form) Init(c *config.Config, model ...interface{}) *Form {
202202
if len(c.Theme) == 0 {
203203
c.Theme = common.BASE
204204
}
205-
f.Style = c.Theme
205+
f.Theme = c.Theme
206206
f.Method = c.Method
207207
f.Action = template.HTML(c.Action)
208208
if len(model) > 0 {
@@ -217,8 +217,8 @@ func (f *Form) HTMLTemplate() (*template.Template, error) {
217217
tmpl = f.config.Template
218218
}
219219
if len(tmpl) == 0 {
220-
tmpl = common.TmplDir(f.Style) + `/baseform.html`
221-
//tmpl = common.TmplDir(f.Style) + `/allfields.html`
220+
tmpl = common.TmplDir(f.Theme) + `/baseform.html`
221+
//tmpl = common.TmplDir(f.Theme) + `/allfields.html`
222222
}
223223
return common.GetOrSetCachedTemplate(tmpl, func() (*template.Template, error) {
224224
return common.ParseFiles(common.LookupPath(tmpl))
@@ -629,15 +629,15 @@ func (f *Form) Elements(elems ...config.FormElement) {
629629
}
630630

631631
func (f *Form) addField(field fields.FieldInterface) *Form {
632-
field.SetStyle(f.Style)
632+
field.SetTheme(f.Theme)
633633
f.FieldList = append(f.FieldList, field)
634634
f.fieldMap[field.OriginalName()] = len(f.FieldList) - 1
635635
return f
636636
}
637637

638638
func (f *Form) addFieldSet(fs *FieldSetType) *Form {
639639
for _, v := range fs.FieldList {
640-
v.SetStyle(f.Style)
640+
v.SetTheme(f.Theme)
641641
v.SetData("container", "fieldset")
642642
f.containerMap[v.OriginalName()] = fs.OriginalName()
643643
}
@@ -760,7 +760,7 @@ func (f *Form) LangSet(name string) *LangSetType {
760760
}
761761

762762
func (f *Form) NewLangSet(name string, langs []*config.Language) *LangSetType {
763-
return LangSet(name, f.Style, langs...)
763+
return LangSet(name, f.Theme, langs...)
764764
}
765765

766766
// FieldSet returns the fieldset identified by name. It returns an empty field if it is missing.
@@ -780,7 +780,7 @@ func (f *Form) FieldSet(name string) *FieldSetType {
780780
// NewFieldSet creates and returns a new FieldSetType with the given name and list of fields.
781781
// Every method for FieldSetType objects returns the object itself, so that call can be chained.
782782
func (f *Form) NewFieldSet(name string, label string, elems ...fields.FieldInterface) *FieldSetType {
783-
return FieldSet(name, label, f.Style, elems...)
783+
return FieldSet(name, label, f.Theme, elems...)
784784
}
785785

786786
// SortAll SortAll("field1,field2") or SortAll("field1","field2")

langset.go

+8-8
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ type LangSetType struct {
3939
Tags common.HTMLAttrValues `json:"tags" xml:"tags"`
4040
AppendData map[string]interface{} `json:"appendData,omitempty" xml:"appendData,omitempty"`
4141
Alone bool `json:"alone,omitempty" xml:"alone,omitempty"`
42-
FormStyle string `json:"formStyle" xml:"formStyle"`
42+
FormTheme string `json:"formTheme" xml:"formTheme"`
4343

4444
langMap map[string]int //{"zh-CN":1}
4545
fieldMap map[string]config.FormElement //{"zh-CN:title":0x344555}
@@ -104,7 +104,7 @@ func (f *LangSetType) Data() map[string]interface{} {
104104

105105
func (f *LangSetType) render() string {
106106
buf := bytes.NewBuffer(nil)
107-
tpf := common.TmplDir(f.FormStyle) + "/" + f.FormStyle + "/" + f.Template + ".html"
107+
tpf := common.TmplDir(f.FormTheme) + "/" + f.FormTheme + "/" + f.Template + ".html"
108108
tpl, err := common.GetOrSetCachedTemplate(tpf, func() (*template.Template, error) {
109109
return common.ParseFiles(common.LookupPath(tpf))
110110
})
@@ -134,7 +134,7 @@ func (f *LangSetType) SetTemplate(tmpl string) *LangSetType {
134134

135135
// FieldSet creates and returns a new FieldSetType with the given name and list of fields.
136136
// Every method for FieldSetType objects returns the object itself, so that call can be chained.
137-
func LangSet(name string, style string, languages ...*config.Language) *LangSetType {
137+
func LangSet(name string, theme string, languages ...*config.Language) *LangSetType {
138138
ret := &LangSetType{
139139
Languages: languages,
140140
langMap: map[string]int{},
@@ -146,7 +146,7 @@ func LangSet(name string, style string, languages ...*config.Language) *LangSetT
146146
Params: map[string]interface{}{},
147147
Tags: common.HTMLAttrValues{},
148148
AppendData: map[string]interface{}{},
149-
FormStyle: style,
149+
FormTheme: theme,
150150
}
151151
for i, language := range languages {
152152
ret.langMap[language.ID] = i
@@ -187,7 +187,7 @@ func (f *LangSetType) Elements(elems ...config.FormElement) {
187187
}
188188

189189
func (f *LangSetType) addField(field fields.FieldInterface) *LangSetType {
190-
field.SetStyle(f.FormStyle)
190+
field.SetTheme(f.FormTheme)
191191
if f.Alone {
192192
if ind, ok := f.langMap[field.Lang()]; ok {
193193
field.SetLang(f.Languages[ind].ID)
@@ -219,7 +219,7 @@ func (f *LangSetType) addFieldSet(fs *FieldSetType) *LangSetType {
219219
if f.Alone {
220220
if ind, ok := f.langMap[fs.Lang()]; ok {
221221
for _, v := range fs.FieldList {
222-
v.SetStyle(f.FormStyle)
222+
v.SetTheme(f.FormTheme)
223223
v.SetData("container", "langset")
224224
v.SetLang(f.Languages[ind].ID)
225225
v.SetName(f.Languages[ind].Name(v.OriginalName()))
@@ -239,7 +239,7 @@ func (f *LangSetType) addFieldSet(fs *FieldSetType) *LangSetType {
239239
if k == 0 {
240240
for _, v := range fs.FieldList {
241241
v.SetLang(language.ID)
242-
v.SetStyle(f.FormStyle)
242+
v.SetTheme(f.FormTheme)
243243
v.SetData("container", "langset")
244244
key := v.Lang() + `:` + v.OriginalName()
245245
f.fieldMap[key] = v
@@ -313,7 +313,7 @@ func (f *LangSetType) FieldSet(name string) *FieldSetType {
313313
// NewFieldSet creates and returns a new FieldSetType with the given name and list of fields.
314314
// Every method for FieldSetType objects returns the object itself, so that call can be chained.
315315
func (f *LangSetType) NewFieldSet(name string, label string, elems ...fields.FieldInterface) *FieldSetType {
316-
return FieldSet(name, label, f.FormStyle, elems...)
316+
return FieldSet(name, label, f.FormTheme, elems...)
317317
}
318318

319319
//Sort Sort("field1:1,field2:2") or Sort("field1:1","field2:2")

widgets/widgets.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
*/
1818

1919
//Package widgets This package contains the base logic for the creation and rendering of field widgets. Base widgets are defined for most input fields,
20-
// both in classic and Bootstrap3 style; custom widgets can be defined and associated to a field, provided that they implement the
20+
// both in classic and Bootstrap3 theme; custom widgets can be defined and associated to a field, provided that they implement the
2121
// WidgetInterface interface.
2222
package widgets
2323

@@ -52,11 +52,11 @@ func (w *Widget) Render(data interface{}) string {
5252
return buf.String()
5353
}
5454

55-
// BaseWidget creates a Widget based on style and inpuType parameters, both defined in the common package.
56-
func BaseWidget(style, inputType, tmplName string) *Widget {
57-
cachedKey := style + ", " + inputType + ", " + tmplName
55+
// BaseWidget creates a Widget based on theme and inpuType parameters, both defined in the common package.
56+
func BaseWidget(theme, inputType, tmplName string) *Widget {
57+
cachedKey := theme + ", " + inputType + ", " + tmplName
5858
tmpl, err := common.GetOrSetCachedTemplate(cachedKey, func() (*template.Template, error) {
59-
fpath := common.TmplDir(style) + "/" + style + "/"
59+
fpath := common.TmplDir(theme) + "/" + theme + "/"
6060
urls := []string{common.LookupPath(fpath + "generic.html")}
6161
tpath := widgetTmpl(inputType, tmplName)
6262
urls = append(urls, common.LookupPath(fpath+tpath+".html"))

0 commit comments

Comments
 (0)