-
-
Notifications
You must be signed in to change notification settings - Fork 201
/
utils.go
60 lines (54 loc) · 1.36 KB
/
utils.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
package formbuilder
import (
"github.com/coscms/forms/common"
"github.com/coscms/forms/fields"
"github.com/webx-top/com"
"github.com/webx-top/echo"
)
func ClearCache() {
common.ClearCachedConfig()
common.ClearCachedTemplate()
}
func DelCachedConfig(file string) bool {
return common.DelCachedConfig(file)
}
func AddChoiceByKV(field fields.FieldInterface, kvData *echo.KVData, checkedKeys ...string) fields.FieldInterface {
for _, kv := range kvData.Slice() {
var checked bool
if kv.H != nil {
checked = kv.H.Bool(`checked`) || kv.H.Bool(`selected`)
}
if len(checkedKeys) > 0 {
checked = com.InSlice(kv.K, checkedKeys)
}
field.AddChoice(kv.K, kv.V, checked)
}
return field
}
func SetChoiceByKV(field fields.FieldInterface, kvData *echo.KVData, checkedKeys ...string) fields.FieldInterface {
choices := []fields.InputChoice{}
if len(checkedKeys) == 0 {
switch f := field.(type) {
case *fields.Field:
if len(f.Value) > 0 {
checkedKeys = append(checkedKeys, f.Value)
}
}
}
for _, kv := range kvData.Slice() {
var checked bool
if kv.H != nil {
checked = kv.H.Bool(`checked`) || kv.H.Bool(`selected`)
}
if len(checkedKeys) > 0 {
checked = com.InSlice(kv.K, checkedKeys)
}
choices = append(choices, fields.InputChoice{
ID: kv.K,
Val: kv.V,
Checked: checked,
})
}
field.SetChoices(choices)
return field
}