Core

/views/components/Form.html (8.8 KB)

  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
{% import (
"strings"
"time"

"github.com/google/uuid"
"github.com/samber/lo"

"{{{ .Package }}}/app/controller/cutil"
"{{{ .Package }}}/app/util"
"{{{ .Package }}}/views/vutil"
) %}

{% func FormInput(key string, id string, value string, placeholder ...string) %}{% stripspace %}
{% if id == "" %}
<input name="{%s key %}" value="{%s value %}"{%= phFor(placeholder) %} />
{% else %}
<input id="{%s id %}" name="{%s key %}" value="{%s value %}"{%= phFor(placeholder) %} />
{% endif %}
{% endstripspace %}{% endfunc %}

{% func FormInputPassword(key string, id string, value string, placeholder ...string) %}{% stripspace %}
{% if id == "" %}
<input name="{%s key %}" type="password" value="{%s value %}"{%= phFor(placeholder) %} />
{% else %}
<input id="{%s id %}" name="{%s key %}" type="password" value="{%s value %}"{%= phFor(placeholder) %} />
{% endif %}
{% endstripspace %}{% endfunc %}

{% func FormInputNumber(key string, id string, value any, placeholder ...string) %}{% stripspace %}
{% if id == "" %}
<input name="{%s key %}" type="number" value="{%v value %}"{%= phFor(placeholder) %} />
{% else %}
<input id="{%s id %}" name="{%s key %}" type="number" value="{%v value %}"{%= phFor(placeholder) %} />
{% endif %}
{% endstripspace %}{% endfunc %}

{% func FormInputFloat(key string, id string, value any, placeholder ...string) %}{% stripspace %}
{% if id == "" %}
<input name="{%s key %}" type="number" value="{%v value %}" step="0.001"{%= phFor(placeholder) %} />
{% else %}
<input id="{%s id %}" name="{%s key %}" type="number" value="{%v value %}" step="0.001"{%= phFor(placeholder) %} />
{% endif %}
{% endstripspace %}{% endfunc %}

{% func FormInputTimestamp(key string, id string, value *time.Time, placeholder ...string) %}{% stripspace %}
{% if id == "" %}
<input name="{%s key %}" type="datetime-local" value="{%s util.TimeToFull(value) %}"{%= phFor(placeholder) %} />
{% else %}
<input id="{%s id %}" name="{%s key %}" type="datetime-local" value="{%s util.TimeToFull(value) %}"{%= phFor(placeholder) %} />
{% endif %}
{% endstripspace %}{% endfunc %}

{% func FormInputTimestampDay(key string, id string, value *time.Time, placeholder ...string) %}{% stripspace %}
{% if id == "" %}
<input name="{%s key %}" type="date" value="{%s util.TimeToYMD(value) %}"{%= phFor(placeholder) %} />
{% else %}
<input id="{%s id %}" name="{%s key %}" type="date" value="{%s util.TimeToYMD(value) %}"{%= phFor(placeholder) %} />
{% endif %}
{% endstripspace %}{% endfunc %}

{% func FormInputUUID(key string, id string, value *uuid.UUID, placeholder ...string) %}{% stripspace %}
{%- code
v := ""
if value != nil {
v = value.String()
}
-%}
{%= FormInput(key, id, v, placeholder...) %}
{% endstripspace %}{% endfunc %}

{% func FormTextarea(key string, id string, rows int, value string, placeholder ...string) %}{% stripspace %}
{% if id == "" %}
<textarea rows="{%d rows %}" name="{%s key %}"{%= phFor(placeholder) %}>{%s value %}</textarea>
{% else %}
<textarea rows="{%d rows %}" id="{%s id %}" name="{%s key %}"{%= phFor(placeholder) %}>{%s value %}</textarea>
{% endif %}
{% endstripspace %}{% endfunc %}

{% func FormSelect(key string, id string, value string, opts []string, titles []string, indent int) %}{% stripspace %}
<select name="{%s key %}"{% if id != `` %}{% space %}id="{%s id %}"{% endif %}>
{% for idx, opt := range opts %}
{% code
title := opt
if idx < len(titles) {
title = titles[idx]
}
%}
{%= vutil.Indent(true, indent + 1) %}
{% if opt == value %}
<option selected="selected" value="{%s opt %}">{%s title %}</option>
{% else %}
<option value="{%s opt %}">{%s title %}</option>
{% endif %}
{% endfor %}
{%= vutil.Indent(true, indent) %}
</select>
{% endstripspace %}{% endfunc %}

{% func FormDatalist(key string, id string, value string, opts []string, titles []string, indent int, placeholder ...string) %}{% stripspace %}
{%= vutil.Indent(true, indent ) %}
<input id="{%s id %}" list="{%s id %}-list" name="{%s key %}" value="{%s value %}"{%= phFor(placeholder) %} />
{%= vutil.Indent(true, indent ) %}
{%- if len(opts) > 0 -%}
<datalist id="{%s id %}-list">
{% for idx, opt := range opts %}
{% code
title := opt
if idx < len(titles) {
title = titles[idx]
}
%}
{%= vutil.Indent(true, indent + 1) %}
<option value="{%s opt %}">{%s title %}</option>
{% endfor %}
{%= vutil.Indent(true, indent) %}
</datalist>
{%- endif -%}
{% endstripspace %}{% endfunc %}

{% func FormFileUpload(key string, id string, indent int, placeholder ...string) %}{% stripspace %}
{%= vutil.Indent(true, indent ) %}
<input id="{%s id %}" name="{%s key %}" type="file" multiple="multiple" {%= phFor(placeholder) %} />
{% endstripspace %}{% endfunc %}

{% func FormRadio(key string, value string, opts []string, titles []string, indent int) %}{% stripspace %}
{% for idx, opt := range opts %}
{% code
title := opt
if idx < len(titles) {
title = titles[idx]
}
%}
{%= vutil.Indent(true, indent) %}
{% if opt == value %}
<label class="radio-label"><input type="radio" name="{%s key %}" value="{%s opt %}" checked="checked" />{% space %}{%s title %}</label>
{% else %}
<label class="radio-label"><input type="radio" name="{%s key %}" value="{%s opt %}" />{% space %}{%s title %}</label>
{% endif %}
{% endfor %}
{% endstripspace %}{% endfunc %}

{% func FormCheckbox(key string, values []string, opts []string, titles []string, indent int) %}{% stripspace %}
{% for idx, opt := range opts %}
{% code
title := opt
if idx < len(titles) {
title = titles[idx]
}
%}
{%= vutil.Indent(true, indent) %}
{% if lo.Contains(values, opt) %}
<label><input type="checkbox" name="{%s key %}" value="{%s opt %}" checked="checked" />{% space %}{%s title %}</label>
{% else %}
<label><input type="checkbox" name="{%s key %}" value="{%s opt %}" />{% space %}{%s title %}</label>
{% endif %}
{% if lo.Contains(values, opt) %}
<br />
{% endif %}
{% endfor %}
{% endstripspace %}{% endfunc %}

{% func FormInputTags(key string, id string, values []string, ps *cutil.PageState, placeholder ...string) %}{% stripspace %}
{%- code
ps.AddIcon("times")
ps.AddIcon("plus")
-%}
<div class="tag-editor">
{% if id == "" %}
<input class="result" name="{%s key %}" value="{%s strings.Join(values, `, `) %}"{%= phFor(placeholder) %} />
{% else %}
<input class="result" id="{%s id %}" name="{%s key %}" value="{%s strings.Join(values, `, `) %}"{%= phFor(placeholder) %} />
{% endif %}
<div class="tags"></div>
<div class="clear"></div>
</div>
{% endstripspace %}{% endfunc %}

{% func FormInputFile(key string, id string, label string, value string) %}{% stripspace %}
{% if id == "" %}
<label for="key"><input type="file" name="{%s key %}" value="{%s value %}"/>{%s label %}</label>
{% else %}
<label id="{%s id %}" for="{%s key %}"><input type="file" name="{%s key %}" value="{%s value %}"/>{%s label %}</label>
{% endif %}
{% endstripspace %}{% endfunc %}

{% func FormRichEditor(key string, id string, columns []*util.FieldDesc, value []any, placeholder ...string) %}{% stripspace %}
{% code if value == nil { value = []any{} } %}
<div class="rich-editor" data-key="{%s key %}" data-columns="{%j util.ToJSONCompact(columns) %}">
{%= FormTextarea(key, id, 8, util.ToJSON(value), placeholder...) %}
</div>
{% endstripspace %}{% endfunc %}

{% func SearchForm(action string, fieldKey string, placeholder string, value string, ps *cutil.PageState) %}{% stripspace %}
{% code
if fieldKey == "" {
fieldKey = "q"
}
m := map[string]string{}
ps.URI.QueryArgs().VisitAll(func(key []byte, value []byte) {
k, v := string(key), string(value)
if k == fieldKey {
return
}
curr, ok := m[k]
if ok {
curr += ","
}
curr += v
m[k] = curr
})
%}
<form action="{%s action %}" method="get" class="">
{% for k, v := range m %}
<input type="hidden" name="{%s k %}" value="{%s v %}" />
{% endfor %}
<button class="right" type="submit">{%= SVGRef("search", 22, 22, `icon`, ps) %}</button>
<input class="right br0" type="search" placeholder="{%s placeholder %}" name="{%s fieldKey %}" value="{%s value %}">
<div class="clear"></div>
</form>
{% endstripspace %}{% endfunc %}

{% func phFor(phs []string) %}{% stripspace %}
{% if len(phs) > 0 %}{% space %}placeholder="{%s strings.Join(phs, "; ") %}"{% endif %}
{% endstripspace %}{% endfunc %}