-
Notifications
You must be signed in to change notification settings - Fork 0
/
textfield.go
176 lines (150 loc) · 4.23 KB
/
textfield.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
package xdommask
import (
"errors"
"fmt"
"regexp"
"strconv"
"github.com/webability-go/wajaf"
"github.com/webability-go/xdominion"
"github.com/webability-go/xamboo/cms/context"
)
const (
TEXTTYPE_TEXT = "text"
TEXTTYPE_MASKED = "masked"
TEXTTYPE_INTEGER = "integer"
TEXTTYPE_FLOAT = "float"
TEXTTYPE_EMAIL = "email"
)
type TextField struct {
*DataField
TextType string
Format string
FormatJS string
MinLength int
MaxLength int
MinWords int
MaxWords int
StatusBadFormat string
StatusTooShort string
StatusTooLong string
StatusTooFewWords string
StatusTooManyWords string
KeyUpJS string
FocusJS string
BlurJS string
Check func(ctx *context.Context, mode Mode, value interface{}) error
Calc func(ctx *context.Context, mode Mode, rec *xdominion.XRecord) (interface{}, error)
}
func NewTextField(name string) *TextField {
tf := &TextField{
DataField: NewDataField(name),
TextType: TEXTTYPE_TEXT,
MinLength: -1,
MaxLength: -1,
MinWords: -1,
MaxWords: -1,
}
tf.Type = FIELD
return tf
}
func (f *TextField) Compile() wajaf.NodeDef {
t := wajaf.NewTextFieldElement(f.ID)
t.SetAttribute("texttype", f.TextType)
t.SetAttribute("style", f.Style)
t.SetAttribute("classname", f.ClassName)
t.SetAttribute("defaultvalue", fmt.Sprint(f.DefaultValue))
t.SetData(f.Title)
t.SetAttribute("size", f.Size)
if f.MinLength >= 0 {
t.SetAttribute("minlength", strconv.Itoa(f.MinLength))
}
if f.MaxLength >= 0 {
t.SetAttribute("maxlength", strconv.Itoa(f.MaxLength))
}
if f.MinWords >= 0 {
t.SetAttribute("minwords", strconv.Itoa(f.MinWords))
}
if f.MaxWords >= 0 {
t.SetAttribute("maxwords", strconv.Itoa(f.MaxWords))
}
t.SetAttribute("format", f.FormatJS)
t.SetAttribute("visible", convertModes(f.AuthModes))
t.SetAttribute("info", convertModes(f.ViewModes))
t.SetAttribute("readonly", convertModes(f.ReadOnlyModes))
t.SetAttribute("notnull", convertModes(f.NotNullModes))
t.SetAttribute("disabled", convertModes(f.DisabledModes))
t.SetAttribute("helpmode", convertModes(f.HelpModes))
if f.Auto {
t.SetAttribute("auto", "yes")
} else {
t.SetAttribute("auto", "no")
}
t.AddHelp("", "", f.HelpDescription)
t.AddMessage("defaultvalue", fmt.Sprint(f.DefaultValue))
t.AddMessage("statusnotnull", f.StatusNotNull)
t.AddMessage("statusbadformat", f.StatusBadFormat)
t.AddMessage("statustooshort", f.StatusTooShort)
t.AddMessage("statustoolong", f.StatusTooLong)
t.AddMessage("statustoofewwords", f.StatusTooFewWords)
t.AddMessage("statustoomanywords", f.StatusTooManyWords)
t.AddMessage("statuscheck", f.StatusCheck)
t.AddMessage("automessage", f.AutoMessage)
t.AddEvent("keyup", f.KeyUpJS)
t.AddEvent("blur", f.BlurJS)
t.AddEvent("focus", f.FocusJS)
return t
}
// GetValue to get the value from the field when needed. return value, ignored bool (true = ignored by construct)
func (f *TextField) GetValue(ctx *context.Context, mode Mode) (interface{}, bool, error) {
if DEBUG {
fmt.Println("xdominion.TextField::GetValue", f.Name, mode)
}
val, ignore, err := f.DataField.GetValue(ctx, mode)
if err != nil || ignore {
return val, ignore, err
}
// FILTER VALUE:
// Lengths, formats
if val != nil {
sval := val.(string)
if f.MinLength > 0 && len(sval) < f.MinLength {
return val, ignore, errors.New(f.StatusTooShort)
}
if f.MaxLength > 0 && len(sval) > f.MaxLength {
return val, ignore, errors.New(f.StatusTooLong)
}
nw := countWords(sval)
if f.MinWords > 0 && nw < f.MinWords {
return val, ignore, errors.New(f.StatusTooFewWords)
}
if f.MaxWords > 0 && nw > f.MaxWords {
return val, ignore, errors.New(f.StatusTooManyWords)
}
if f.Format != "" {
matched, err := regexp.MatchString(f.Format, sval)
if err != nil {
return val, ignore, err
}
if !matched {
return val, ignore, errors.New(f.StatusBadFormat)
}
}
}
// extra code check
if f.Check != nil {
err = f.Check(ctx, mode, val)
}
return val, ignore, err
}
func countWords(s string) int {
/* JS algorith:
text = text.replace(/[\n\t\r]+/g, " ");
text = text.replace(/^[ ]+/, "");
text = text.replace(/[ ]+$/, "");
text = text.replace(/[ ]+/g, " ");
*/
re := regexp.MustCompile(`[\S]+`)
// Find all matches and return count.
results := re.FindAllString(s, -1)
return len(results)
}