forked from icza/gowut
-
Notifications
You must be signed in to change notification settings - Fork 0
/
textbox_pwbox.go
256 lines (218 loc) · 6.79 KB
/
textbox_pwbox.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
// Copyright (C) 2013 Andras Belicza. All rights reserved.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
// Defines the TextBox component.
package gwu
import (
"net/http"
"strconv"
)
// TextBox interface defines a component for text input purpose.
//
// Suggested event type to handle actions: ETypeChange
//
// By default the value of the TextBox is synchronized with the server
// on ETypeChange event which is when the TextBox loses focus
// or when the ENTER key is pressed.
// If you want a TextBox to synchronize values during editing
// (while you type in characters), add the ETypeKeyUp event type
// to the events on which synchronization happens by calling:
// AddSyncOnETypes(ETypeKeyUp)
//
// Default style class: "gwu-TextBox"
type TextBox interface {
// TextBox is a component.
Comp
// TextBox has text.
HasText
// TextBox can be enabled/disabled.
HasEnabled
// ReadOnly returns if the text box is read-only.
ReadOnly() bool
// SetReadOnly sets if the text box is read-only.
SetReadOnly(readOnly bool)
// Rows returns the number of displayed rows.
Rows() int
// SetRows sets the number of displayed rows.
// rows=1 will make this a simple, one-line input text box,
// rows>1 will make this a text area.
SetRows(rows int)
// Cols returns the number of displayed columns.
Cols() int
// SetCols sets the number of displayed columns.
SetCols(cols int)
// MaxLength returns the maximum number of characters
// allowed in the text box.
// -1 is returned if there is no maximum length set.
MaxLength() int
// SetMaxLength sets the maximum number of characters
// allowed in the text box.
// Pass -1 to not limit the maximum length.
SetMaxLength(maxLength int)
}
// PasswBox interface defines a text box for password input purpose.
//
// Suggested event type to handle actions: ETypeChange
//
// By default the value of the PasswBox is synchronized with the server
// on ETypeChange event which is when the PasswBox loses focus
// or when the ENTER key is pressed.
// If you want a PasswBox to synchronize values during editing
// (while you type in characters), add the ETypeKeyUp event type
// to the events on which synchronization happens by calling:
// AddSyncOnETypes(ETypeKeyUp)
//
// Default style class: "gwu-PasswBox"
type PasswBox interface {
// PasswBox is a TextBox.
TextBox
}
// TextBox implementation.
type textBoxImpl struct {
compImpl // Component implementation
hasTextImpl // Has text implementation
hasEnabledImpl // Has enabled implementation
isPassw bool // Tells if the text box is a password box
rows, cols int // Number of displayed rows and columns.
}
var (
strEncURIThisV = []byte("encodeURIComponent(this.value)") // "encodeURIComponent(this.value)"
)
// NewTextBox creates a new TextBox.
func NewTextBox(text string) TextBox {
c := newTextBoxImpl(strEncURIThisV, text, false)
c.Style().AddClass("gwu-TextBox")
return &c
}
// NewPasswBox creates a new PasswBox.
func NewPasswBox(text string) TextBox {
c := newTextBoxImpl(strEncURIThisV, text, true)
c.Style().AddClass("gwu-PasswBox")
return &c
}
// newTextBoxImpl creates a new textBoxImpl.
func newTextBoxImpl(valueProviderJs []byte, text string, isPassw bool) textBoxImpl {
c := textBoxImpl{newCompImpl(valueProviderJs), newHasTextImpl(text), newHasEnabledImpl(), isPassw, 1, 20}
c.AddSyncOnETypes(ETypeChange)
return c
}
func (c *textBoxImpl) ReadOnly() bool {
ro := c.Attr("readonly")
return len(ro) > 0
}
func (c *textBoxImpl) SetReadOnly(readOnly bool) {
if readOnly {
c.SetAttr("readonly", "readonly")
} else {
c.SetAttr("readonly", "")
}
}
func (c *textBoxImpl) Rows() int {
return c.rows
}
func (c *textBoxImpl) SetRows(rows int) {
c.rows = rows
}
func (c *textBoxImpl) Cols() int {
return c.cols
}
func (c *textBoxImpl) SetCols(cols int) {
c.cols = cols
}
func (c *textBoxImpl) MaxLength() int {
if ml := c.Attr("maxlength"); len(ml) > 0 {
if i, err := strconv.Atoi(ml); err == nil {
return i
}
}
return -1
}
func (c *textBoxImpl) SetMaxLength(maxLength int) {
if maxLength < 0 {
c.SetAttr("maxlength", "")
} else {
c.SetAttr("maxlength", strconv.Itoa(maxLength))
}
}
func (c *textBoxImpl) preprocessEvent(event Event, r *http.Request) {
// Empty string for text box is a valid value.
// So we have to check whether it is supplied, not just whether its len() > 0
value := r.FormValue(paramCompValue)
if len(value) > 0 {
c.text = value
} else {
// Empty string might be a valid value, if the component value param is present:
values, present := r.Form[paramCompValue] // Form is surely parsed (we called FormValue())
if present && len(values) > 0 {
c.text = values[0]
}
}
}
func (c *textBoxImpl) Render(w Writer) {
if c.rows <= 1 || c.isPassw {
c.renderInput(w)
} else {
c.renderTextArea(w)
}
}
var (
strInputOp = []byte(`<input type="`) // `<input type="`
strPassword = []byte("password") // "password"
strText = []byte("text") // "text"
strSize = []byte(`" size="`) // `" size="`
strValue = []byte(` value="`) // ` value="`
strInputCl = []byte(`"/>`) // `"/>`
)
// renderInput renders the component as an input HTML tag.
func (c *textBoxImpl) renderInput(w Writer) {
w.Write(strInputOp)
if c.isPassw {
w.Write(strPassword)
} else {
w.Write(strText)
}
w.Write(strSize)
w.Writev(c.cols)
w.Write(strQuote)
c.renderAttrsAndStyle(w)
c.renderEnabled(w)
c.renderEHandlers(w)
w.Write(strValue)
c.renderText(w)
w.Write(strInputCl)
}
var (
strTextareaOp = []byte("<textarea") // "<textarea"
strRows = []byte(` rows="`) // ` rows="`
strCols = []byte(`" cols="`) // `" cols="`
strTextAreaOpCl = []byte("\">\n") // "\">\n"
strTextAreaCl = []byte("</textarea>") // "</textarea>"
)
// renderTextArea renders the component as an textarea HTML tag.
func (c *textBoxImpl) renderTextArea(w Writer) {
w.Write(strTextareaOp)
c.renderAttrsAndStyle(w)
c.renderEnabled(w)
c.renderEHandlers(w)
// New line char after the <textarea> tag is ignored.
// So we must render a newline after textarea, else if text value
// starts with a new line, it will be omitted!
w.Write(strRows)
w.Writev(c.rows)
w.Write(strCols)
w.Writev(c.cols)
w.Write(strTextAreaOpCl)
c.renderText(w)
w.Write(strTextAreaCl)
}