/
columnDecor.qtpl
318 lines (294 loc) · 11.7 KB
/
columnDecor.qtpl
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
{% import (
"fmt"
"go/types"
"sort"
"strings"
"github.com/ruslanBik4/gotools"
"github.com/ruslanBik4/httpgo/views/templates/json"
)
%}
{% collapsespace %}
{% stripspace %}
{% func (col *ColumnDecor) ToJSON() %}
{
{% code
values := col.GetValues()
%}
"name": "{%s= col.InputName(-1) %}",
"required": {%v col.Required() %},
{% if col.patternDesc > "" %}
"error": {
"pattern": "{%s= col.patternDesc %}",
"message": "{%s= col.patternDesc %}"
},
{% endif %}
{% if p := col.Suggestions; p > "" %}
"suggestions":{
"url":"{%j= p %}"
{% for name, val := range col.SuggestionsParams %}
, "{%j= name %}": {%= json.Element(val)%}
{% endfor %}
},
{% if d := col.DefaultInputValue; d > "" %}
"defaultInputValue": "{%j= d %}",
{% endif %}
{% elseif p := col.Pattern(); p > "" %}
"pattern":"{%j= p %}",
{% endif %}
{% if col.IsSlice %}
{% if col.multiple %}
"type": "select",
"multiple": true
{% else %}
"type": "array",
"typeField": "{%= col.InputTypeForJSON()%}",
"action": "add-field",
"list": [
{% for i, val := range values %}
{% if i > 0 %} , {% endif %}
{% if val != nil %}
{
"value": "{%v val %}"
}
{% endif %}
{% endfor %}
]
{% endif %}
{% else %}
"type": "{%= col.InputTypeForJSON()%}"
{% switch len(values) %}
{% case 0 %}
{% case 1 %}
{% if values[0] != nil %}
, "value": {%= json.Element(values[0]) %}
{% endif %}
{% default %}
, "value": {%= json.Slice(values) %}
{% endswitch %}
{% endif %}
{% if col.CharacterMaximumLength() > 0 %}
, "maxLength": {%d col.CharacterMaximumLength() %}
{% endif %}
{%= col.DataForJSON()%}
, "title": "{%s= col.Label %}"
{% if len(col.Attachments) > 0 %}
, "list": {%= json.Element(col.Attachments) %}
{% endif %}
{% if col.IsReadOnly %}
, "readOnly": true
{% endif %}
{% if col.IsDisabled %}
, "disabled": true
{% endif %}
{% if col.IsHidden %}
, "hidden": true
{% endif %}
}
{% endfunc %}
{% func (col *ColumnDecor) DataForJSON() %}
{% if col.SelectOptions != nil %}
, "data": [
{%code
sortList := make([]string, 0, len(col.SelectOptions))
for title := range col.SelectOptions {
sortList = append(sortList, title)
}
sort.Strings(sortList)
%}
{% for i, title := range sortList %}
{% if i > 0 %},{% endif %}
{
"title": "{%s title %}",
"label": "{%s title %}",
"value": "{%s col.SelectOptions[title].Value %}",
{%- if col.SelectOptions[title].Disabled -%} "disabled": true {%- endif -%}
{%- if col.SelectOptions[title].Selected -%} "selected": true {%- endif -%}
}
{% endfor %}
{% if col.SelectWithNew %}
,
{
"title": "add new item",
"label": "add new item",
"value": "new"
}
{% endif %}
]
{% endif %}
{% endfunc %}
{% func (col *ColumnDecor) InputTypeForJSON() %}
{% switch %}
{% case col.InputType == "checkbox" %}
switch
{% case col.InputType == "textarea" %}
textarea
{% case col.SelectOptions != nil || col.Suggestions > "" %}
select
{% default %}
{%s= col.InputType %}
{% endswitch %}
{% endfunc %}
{%- func (col *ColumnDecor) RenderValue(value any) -%}
{%- switch vv := value.(type) -%}
{%- case nil -%}
{%- case []byte -%}
{% if col.InputType == "textarea" %}{%s= gotools.BytesToString(vv) %} {% else %} "{%= json.Element(value)%}" {% endif %}
{%- case string -%}
{% if col.InputType == "textarea" %}{%s= vv %} {% else %} "{%s vv %}" {% endif %}
{%- default -%}{%= json.Element(value) %}
{%- endswitch -%}
{%- endfunc -%}
{% endstripspace %}
{% func (col *ColumnDecor) RenderAttr(i int) %}
name="{%s col.InputName(i) %}"
id="id_{%s col.InputName(i) %}"
{% if col.Required() %} required="true" {% endif %}
{% if col.IsReadOnly %} readonly disabled {% endif %}
{%- if col.CharacterMaximumLength() > 0 -%}maxlength="{%d col.CharacterMaximumLength() %}"{%- endif -%}
{% if strings.HasPrefix(col.Type(), "json") %} raw="json" {% space %} {%- endif -%}
{% if col.Max != nil %} max={%v col.Max %} {% space %} {%- endif -%}
{% if col.Min != nil %} min={%v col.Min %} {% space %} {%- endif -%}
{% if col.Step != nil %}step={%v col.Step %} {% space %} {%- endif -%}
{% if col.Classes > "" %} class="{%s col.Classes %}" {% endif %}
{% if col.PlaceHolder > "" %} placeholder="{%s col.PlaceHolder %}" {% endif %}
{% if p := col.Suggestions; p > "" %}
{% elseif col.Pattern() > "" %}
pattern="{%s= col.Pattern() %}" onkeyup="return validatePattern(this);"
{% endif %}
{% for key, event := range col.Events %} {%s key %}="{%s= event %}"
{% endfor %}
{% endfunc %}
{% func (col *ColumnDecor) RenderInputs(data map[string]any) %}
{% if col.IsSlice %}
{%s col.Label %}
<ul class="form-field">
{% endif %}
{% for i, val := range col.GetValues() %}
{% if col.IsSlice %}
<li class="form-field">
{% endif %}
{% switch %}
{% case col.IsReadOnly %}
{%s col.Label %}: <i>{%= col.RenderValue(val) %}</i>
{% case col.SelectOptions != nil %}
{%- if !col.IsSlice -%}{%s col.Label %} {%- endif -%}
<select {%= col.RenderAttr(i) %}
{% if d, ok := data[ col.InputName(i) ].(map[string]any); ok %}
{% comment %} must have '' for internal presentation strings with "" {% endcomment %}
data-show-blocks='{%= json.Element(d) %}'
onchange="ShowBlocks(this);"
{% endif %}
>
{%code
sortList := make([]string, 0, len(col.SelectOptions))
for title := range col.SelectOptions {
sortList = append(sortList, title)
}
sort.Strings(sortList)
%}
{% for _, title := range sortList %}
<option value="{%s col.SelectOptions[title].Value %}"
{% if fmt.Sprintf("%v", val) == col.SelectOptions[title].Value || col.SelectOptions[title].Selected %} selected {% endif %}
{% if col.SelectOptions[title].Disabled %} disabled {% endif %}
>{%s title %}</option>
{% endfor %}
</select>
{% case col.InputType == "slider" %}
<input class="slider" type="hidden" {%= col.RenderAttr(i) %}
{% if val != nil %} value={%= col.RenderValue(val) %} {% endif %}
>
<span class="label-text slider" data-placeholder="{%s col.Label %}" data-value="{%v col.Value %}"></span>
<div class="slider"></div>
{% case col.InputType == "textarea" || col.InputType == "markdown" %}
<textarea {%= col.RenderAttr(i) %}{% if val != nil %} value={%= col.RenderValue(val) %} {% endif %} >{%= col.RenderValue(val) %}</textarea>
{%- if !col.IsSlice -%}
<span {% if val != nil || col.PlaceHolder > "" %}class="label-text"{% endif %} data-placeholder="{%s col.Label %}"></span>
{%- endif -%}
{% case col.InputType == "file" %}
{%- if !col.IsSlice -%}{%s col.Label %} {%- endif -%}
<span class="hiddenInput">
<input {%= col.RenderAttr(i) %} type="file" accept="{%s col.Accept %}" data-placeholder="{%s col.Label %}"
onchange="handleFileOnForm(this);" >
</span>
{% case col.Suggestions > "" %}
<input type="text" src="{%j= col.Suggestions %}" data="{%s col.InputName(i) %}-sel"
autocapitalize="none" autocorrect="off" autocomplete="off"
{% if val != nil %} value={%= col.RenderValue(val) %} {% endif %}
onkeyup="return inputSearchKeyUp(this,event);" />
{%- if !col.IsSlice -%}
<span {% if val != nil || col.PlaceHolder > "" %}class="label-text"{% endif %} data-placeholder="{%s col.Label %}"></span>
{%- endif -%}
<div class="suggestions-wrapper">
<span class="suggestions-addon" data-addon-type="spinner"></span>
<select size="10" {%= col.RenderAttr(i) %}
class="suggestions-constraints suggestions-select-hide {%s col.InputName(i) %}-sel"
data-placeholder="{%s col.Label %}">
</select>
<div class="suggestions-suggestions">
</div>
</div>
{% default %}
<input type="{%s col.InputType %}" {%= col.RenderAttr(i) %}
{% if val != nil %} value={%= col.RenderValue(val) %} {% endif %}
{% if col.PlaceHolder > "" %} placeholder="{%s col.PlaceHolder %}" {% endif %}
{% if col.BasicType() == types.Bool %}
onclick="this.value = this.checked;"
{% if val == true %}checked{% endif %}
{% endif %}
>
{%- if !col.IsSlice -%}
<span {% if val != nil || col.PlaceHolder > "" %}class="label-text"{% endif %} data-placeholder="{%s col.Label %}"></span>
{%- endif -%}
{% endswitch %}
{% if col.IsSlice %}
<button class="removeField" type="button" onclick="this.parentNode.remove(); return false;">-</button>
</li>
{% endif %}
{% if col.special == InputMap %}
<div id="map" class="map_showing"></div>
<script>
var elem = $("input[name={%s col.InputName(i) %}]");
var val = elem.val();
var map = L.map('map');
var marker;
map.on('load', function onMapClick(e) {
marker = L.marker(map.getCenter(), {draggable:true}).addTo(map);
marker.bindPopup("<b>Hello world!</b><br>I am her.").openPopup().
on('move' , function () {
marker.savePoint();
FormIsModified(event, elem.parents('form'));
});
marker.savePoint = function() {
marker.bindPopup("It;s my new place.").openPopup();
let geo = marker.getLatLng();
elem.val(`(${geo.lat},${geo.lng})`);
};
});
if (val > "") {
let arr = val.match(/\((\d*\.\d*)\s*,\s*(\d*\.\d*)\)/);
map.setView([arr[1], arr[2]], 13);
} else {
map.locate({setView: true, maxZoom: 16});
}
L.tileLayer('https://tile.openstreetmap.org/{z}/{x}/{y}.png', {
maxZoom: 19,
attribution: '© <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>'
}).addTo(map);
map.on('click', function onMapClick(e) {
if (confirm(`Do you want to set new position at ${e.latlng}?`)) {
marker.setLatLng(e.latlng);
marker.savePoint();
}
});
</script>
{% endif %}
{% endfor %}
{% if col.IsSlice %}
</ul>
<button class="main-btn" type="button"
onclick="l = this.parentNode.firstElementChild.lastElementChild; l.parentNode.insertBefore(l.cloneNode(true), l); return false;">
Add new one
</button>
{% endif %}
{% endfunc %}
{% endcollapsespace %}