-
Notifications
You must be signed in to change notification settings - Fork 17
/
forms.html.jinja2
384 lines (371 loc) · 17.7 KB
/
forms.html.jinja2
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
{%- from "baseframe/mui/components.html.jinja2" import alertbox, faicon -%}
{% macro rendererrors(field) -%}
{%- if field.errors -%}
{%- for error in field.errors %}
<div><p class="mui-form__error">{{ error }}</p></div>
{%- endfor %}
{%- endif %}
{%- endmacro %}
{% macro renderfield(field, css_class="", widget_css_class="", sidetext=none, tabindex=none, autofocus=false, nolabel=false, style='horiz', rows=none) -%}
{%- if field.widget.html_tag in ['ul', 'ol'] %}
{%- set listwidget = true %}
{%- elif field.widget.input_type == 'checkbox' %}
{%- set listwidget = false %}
{%- else %}
{%- set listwidget = false %}
{%- endif -%}
{%- if field.type == 'FormField' %}
<fieldset>
<legend>{{ field.label.text }}</legend>
{%- for subfield in field.form %}
{%- if not subfield.type in ['CSRFTokenField', 'HiddenField', 'NonceField'] -%}
{{ renderfield(field=subfield, css_class=css_class, widget_css_class=widget_css_class, sidetext=sidetext, tabindex=tabindex, autofocus=false, nolabel=nolabel, style=style, rows=rows) }}
{%- endif %}
{%- endfor %}
</fieldset>
{%- else %}
<div class="mui-form__fields {%- if field.errors %} error has-error {%- endif -%}
{%- if css_class %} {{ css_class }} {%- endif -%}
{%- if listwidget %} listwidget {%- if style == 'horizlist' %} horizlist {%- endif %}{%- endif -%}
" id="field-{{ field.id }}">
{%- if field.type == 'RadioMatrixField' %}
<div class="mui-form__controls">
{{ field | render_field_options(class="field-" + field.id + " " + widget_css_class) }}
{%- if field.description %}
<p class="mui-form__helptext">{{ field.description }}</p>
{%- endif %}
</div>
{%- elif field.type == 'BooleanField' %}
<div class="mui-form__controls">
<div class="mui-checkbox">
<label>
{{ field | render_field_options(class="field-" + field.id + " " + widget_css_class, tabindex=tabindex, rows=rows) }} {{ field.label.text }}
{{ rendererrors(field) }}
</label>
</div>
{%- if field.description %}
<p class="mui-form__helptext">{{ field.description }}</p>
{%- endif %}
</div>
{%- else %}
<div class="mui-form__controls">
{%- if field.type in ['AnnotatedTextField', 'AnnotatedNullTextField'] %}
<div class="mui-textfield mui-textfield--addon">
{% if field.prefix %}<span class="mui-form__addon">{{ field.prefix }}</span>{% endif %}
{{ field | render_field_options(class="field-" + field.id + " " + widget_css_class, tabindex=tabindex, autofocus=autofocus, rows=rows)}}
{% if field.suffix %}<span class="mui-form__addon">{{ field.suffix }}</span>{% endif %}
{%- if not nolabel %}
<label>{{ field.label.text }}</label>
{%- endif %}
</div>
{%- elif field.type == 'CoordinatesField' %}
<div class="map">
<div class="mui-textfield no-jshidden">
<input type="text" class="map__search-field" id="{{ field.id }}_search" placeholder="Enter a location">
<a href="javascript:void(0)" id="{{ field.id }}_clear" class="map__clear" aria-label="clear location">{{ faicon(icon='times', icon_size='title') }}</a>
{%- if not nolabel %}
<label>{{ field.label.text }}</label>
{%- endif %}
</div>
<div id="{{ field.id }}_map" class="no-jshidden map__marker"></div>
<div class="jshidden">{{ field(class="field-" + field.id + " form-control") }}</div>
</div>
{%- elif field.widget.input_type in ['text', 'email', 'search', 'url', 'number', 'tel'] and field.widget.html_tag not in ['ul', 'ol'] %}
<div class="mui-textfield mui-textfield--float-label">
{{ field | render_field_options(class="field-" + field.id + " " + widget_css_class, tabindex=tabindex, autofocus=autofocus, rows=rows)}}
{%- if not nolabel %}
<label>{{ field.label.text }}</label>
{%- endif %}
</div>
{%- elif field.widget.input_type == 'password' and field.widget.html_tag not in ['ul', 'ol'] %}
<div class="mui-textfield mui-textfield--password mui-textfield--float-label">
{{ field | render_field_options(class="field-" + field.id + " " + widget_css_class, tabindex=tabindex, autofocus=autofocus, rows=rows)}}
<a href="javascript:void(0)" class="js-show-password password-toggle" aria-label="show password">{{ faicon(icon='eye', icon_size='title', css_class='mui--text-light') }}</a>
<a href="javascript:void(0)" class="js-hide-password password-toggle mui--hide" aria-label="hide password">{{ faicon(icon='eye-slash', icon_size='title', css_class='mui--text-light') }}</a>
{%- if not nolabel %}
<label>{{ field.label.text }}</label>
{%- endif %}
<div class="progress">
<div class="progress__bar">
<span class="progress__txt mui--text-body2"></span>
</div>
</div>
</div>
{%- elif field.widget.input_type in ['color', 'date', 'datetime', 'datetime-local', 'month', 'time', 'week'] and field.widget.html_tag not in ['ul', 'ol'] %}
<div class="mui-textfield">
{{ field | render_field_options(class="field-" + field.id + " " + widget_css_class, tabindex=tabindex, autofocus=autofocus, rows=rows)}}
{%- if not nolabel %}
<label>{{ field.label.text }}</label>
{%- endif %}
</div>
{%- elif field.type in ['SelectField', 'QuerySelectField', 'SelectMultipleField', 'QuerySelectMultipleField', 'UserSelectField', 'UserSelectMultiField', 'AutocompleteField', 'AutocompleteMultipleField', 'GeonameSelectField', 'GeonameSelectMultiField'] and field.widget.html_tag not in ['ul', 'ol'] %}
<div class="mui-select">
{{ field | render_field_options(class="field-" + field.id + " " + widget_css_class, tabindex=tabindex, autofocus=autofocus, rows=rows)}}
<label>{{ field.label.text }}</label>
</div>
{%- else %}
<div class="mui-textfield">
{%- if not nolabel %}<label class="mui-form__label">{{ field.label.text }}</label>{%- endif %}
{{ field | render_field_options(class="field-" + field.id + " " + widget_css_class, tabindex=tabindex, autofocus=autofocus, rows=rows)}}
</div>
{%- endif %}
{%- if sidetext %}
<span class="mui-form__sidetext">{{sidetext}}</span>
{%- endif %}
{{ rendererrors(field) }}
{%- if field.description %}
<p class="mui-form__helptext">{{ field.description }}</p>
{%- endif %}
</div>
{%- endif %}
</div>
{%- endif %}
{%- endmacro %}
{% macro rendersubmit(buttons=[], css_class="", tabindex='', cancel_url='', style='horiz', csrf_error=false) -%}
<div class="mui-form form-actions clearfix{% if css_class %} {{ css_class}}{% endif %}">
<div>
{% for name, value, widget_class in buttons -%}
<button type="submit"
{%- if name %} name="{{ name|e }}"{% else %} {% endif -%}
class="mui-btn mui-btn--raised {% if widget_class %} {{ widget_class|e }}{% else %} mui-btn--primary{% endif -%}"
{%- if tabindex %} tabindex="{{ tabindex }}" {% endif -%}
>{{ value|e }}</button>
{% endfor %}
{% if cancel_url %}<a href="{{ cancel_url }}" class="mui-btn mui-btn--raised mui-btn">{% trans %}Cancel{% endtrans %}</a>{% endif %}
<span class="loading mui--hide"> </span>
{% if csrf_error -%}
<div><p class="mui-form__error">{% trans -%}
This form has timed out. Please submit again to confirm
{%- endtrans %}</p></div>
{%- endif %}
</div>
</div>
{%- endmacro %}
{% macro renderform_inner(form, formid, style='horiz', autosave=False, draft_revision=None) -%}
{%- if form.errors %}
{{ alertbox('error', 'Please review the indicated issues') }}
{%- endif %}
{% if message %}<p>{{ message }}</p>{% endif %}
<div style="display:none;"><input type="hidden" name="form.id" value="{{ formid }}" /></div>
{{ form.hidden_tag() }}
{%- if autosave %}
<input type="hidden" name="form.revision" value="{{ draft_revision if draft_revision is not none }}" />
{%- endif %}
{%- set autofocus = true %}
{% for field in form -%}
{%- if field.type in ['CSRFTokenField', 'HiddenField', 'NonceField'] -%}
{# Don't show hidden #}
{%- else -%}
{{ renderfield(field, autofocus=autofocus, style=style) }}
{%- if autofocus %}{% set autofocus = false %}{% endif %}
{%- endif %}
{% endfor %}
{%- endmacro %}
{%- macro renderform(form, formid, submit, ref_id='form', message='', action=None, cancel_url='', multipart=false, style='', autosave=False, draft_revision=None) %}
<form data-parsley-validate="true" id="{{ ref_id }}" method="POST" {%- if action %} action="{{ action }}" {%- endif %}{%- if multipart %} enctype="multipart/form-data" {%- endif %} accept-charset="UTF-8" class="mui-form mui-form--margins hg-form {%- if style == 'horiz' %} mui-form--inline{% endif %}">
{{ renderform_inner(form, formid or none, style=style, autosave=autosave, draft_revision=draft_revision) }}
{{ rendersubmit([(none, submit or _("Submit"), 'mui-btn--primary')], cancel_url=cancel_url, style=style, csrf_error=form.csrf_token.errors if form.csrf_token else "") }}
</form>
{% endmacro %}
{%- macro ajaxform(ref_id, request, force=false) %}
{%- if force or request_is_xhr() %}
<script type="text/javascript">
$(function() {
$("#{{ ref_id }}").ajaxForm({
target: '#{{ ref_id }}',
replaceTarget: true,
beforeSubmit: function(formdata, form, options) {
form.find('input[type="submit"]').prop('disabled', true).addClass('submit-disabled');
form.find('button[type="submit"]').prop('disabled', true).addClass('submit-disabled');
form.find('.loading').removeClass('mui--hide');
return true;
},
error: function(context, xhr, status, errMsg) {
var form = $("#{{ ref_id }}");
form.find('input[type="submit"]').prop('disabled', false).removeClass('submit-disabled');
form.find('button[type="submit"]').prop('disabled', false).removeClass('submit-disabled');
form.find('.loading').addClass('mui--hide');
form.append({{ alertbox('error', 'An error occured when submitting the form') | tojson }});
} // No comma or semicolon here
});
});
</script>
{%- else %}
<script type="text/javascript">
$(function() {
// Disable submit button when clicked. Prevent double click.
$('#{{ ref_id }}').submit(function() {
if(!$(this).data('parsley-validate') || $(this).data('parsley-validate') && $(this).hasClass('parsley-valid')) {
$(this).find('button[type="submit"]').prop('disabled', true);
$(this).find('input[type="submit"]').prop('disabled', true);
$(this).find(".loading").removeClass('mui--hide');
}
});
});
</script>
{%- endif %}
{%- endmacro %}
{% macro tinymce3_editor(field, script=true) -%}
{%- if script %}
<script type="text/javascript">
$(function() {
{%- endif %}
if (typeof(tinyMCE) != 'undefined') {
tinymce.remove('textarea#{{ field.id }}');
}
$('textarea#{{ field.id }}').removeClass('richtext').tinymce({
// Location of TinyMCE script
script_url: {{ url_for("baseframe.static", filename="js/tinymce3/tiny_mce.js")|tojson }},
{%- for k, v in field.tinymce_options.items() %}
{{ k|tojson }}: {{ v|tojson }},
{%- endfor %}
// Content CSS
content_css: {{ (field.content_css or url_for('baseframe.editorcss'))|tojson }},
// Focus/blur indicators
setup: function(ed) {
ed.onInit.add(function(ed) {
var container = $('#'+ed.id+'_tbl');
tinymce.dom.Event.add(ed.getWin(), "blur", function() {
container.removeClass('focus');
});
tinymce.dom.Event.add(ed.getWin(), "focus", function() {
container.addClass('focus');
});
});
ed.on('change', function(e) {
ed.save();
});
} // No comma or semicolon here
});
{%- if script %}
});
</script>
{%- endif %}
{% endmacro %}
{% macro tinymce4_editor(field, script=true) -%}
{%- if script %}
<script type="text/javascript">
$(function() {
{%- endif %}
if (typeof(tinyMCE) != 'undefined') {
tinymce.remove('textarea#{{ field.id }}');
}
$('textarea#{{ field.id }}').removeClass('richtext').tinymce({
script_url: {{ url_for("baseframe.static", filename="js/tinymce4/tinymce.min.js")|tojson }},
{%- for k, v in field.tinymce_options.items() %}
{{ k|tojson }}: {{ v|tojson }},
{%- endfor %}
// Content CSS
content_css: {{ (field.content_css or url_for('baseframe.editorcss'))|tojson }},
// Focus/blur indicators
setup: function(ed) {
ed.on('focus', function(e) {
$(ed.editorContainer).addClass('active');
});
ed.on('blur', function(e) {
$(ed.editorContainer).removeClass('active');
});
ed.on('change', function(e) {
ed.save();
});
} // End of setup keyval
});
{%- if script %}
});
</script>
{%- endif %}
{% endmacro %}
{%- macro map_marker_script(field) %}
new window.Baseframe.MapMarker({
location_id: "{{ field.id}}_search",
map_id: "{{ field.id }}_map",
latitude_id: "{{ field.id }}_latitude",
longitude_id: "{{ field.id}}_longitude",
clear_id: "{{ field.id}}_clear"
});
{% endmacro %}
{% macro widget_ext_scripts(form) %}
{%- with scripts = {} %}
{%- for field in form %}
{%- if field.type == 'CoordinatesField' %}
{#- We're using setdefault instead of a simple = because the do extension doesn't support assignment #}
{%- do scripts.setdefault('gmap', ('https://maps.googleapis.com/maps/api/js?key=' + config['GOOGLE_MAPS_API_KEY'] + '&libraries=places')) %}
{%- do scripts.setdefault('locationpicker', 'jquery.locationpicker.js'|ext_asset_url) %}
{%- endif %}
{%- endfor %}
{%- for key in scripts %}
<script type="text/javascript" src="{{ scripts[key] }}"></script>
{%- endfor %}
{%- endwith %}
{% endmacro %}
{% macro widgetscripts(form, script=true, ref_id='') -%}
{%- if script %}
<script type="text/javascript">
$(function() {
{%- endif %}
$('.js-show-password').click(function (argument) {
$(this).parent().find('.password-toggle').toggleClass('mui--hide');
$(this).parent().find('input').attr('type', 'text');
});
$('.js-hide-password').click(function (argument) {
$(this).parent().find('.password-toggle').toggleClass('mui--hide');
$(this).parent().find('input').attr('type', 'password');
});
{%- for field in form -%}
{%- if field.type == 'FormField' %}
{{ widgetscripts(field.form, script=false) }}
{%- elif field.widget.input_type == 'tinymce3' -%}
{{ tinymce3_editor(field, script=false) }}
{%- elif field.widget.input_type == 'tinymce4' -%}
{{ tinymce4_editor(field, script=false) }}
{%- elif field.type in ['AutocompleteField', 'AutocompleteMultipleField'] %}
Baseframe.Forms.textAutocomplete({
id: {{ field.id|tojson }},
multiple: {{ field.multiple|tojson }},
separator: {{ field.separator|tojson }},
autocomplete_endpoint: {{ field.autocomplete_endpoint|tojson }},
key: {{ field.results_key|tojson }}
});
{%- elif field.type == 'RecaptchaField' and ref_id != '' %}
window.onInvisibleRecaptchaSubmit = function (recaptcha_response) {
document.getElementById("{{ ref_id }}").submit();
};
$('#{{ ref_id }}').submit(function (event) {
event.preventDefault();
if (typeof grecaptcha !== "undefiend" && grecaptcha.getResponse() === '') {
grecaptcha.execute();
} else {
document.getElementById("{{ ref_id }}").submit();
}
})
{%- elif field.type in ['UserSelectField', 'UserSelectMultiField'] %}
{%- if config['LASTUSER_CLIENT_ID'] and current_auth.cookie and 'sessionid' in current_auth.cookie %}
Baseframe.Forms.lastuserAutocomplete({
id: {{ field.id|tojson }},
multiple: {{ field.multiple|tojson }},
separator: {{ field.separator|tojson }},
autocomplete_endpoint: {{ field.autocomplete_endpoint|tojson }},
getuser_endpoint: {{ field.getuser_endpoint|tojson }},
client_id: {{ config['LASTUSER_CLIENT_ID']|tojson }},
session_id: {{ current_auth.cookie['sessionid']|tojson }}
});
{%- else %}
Baseframe.Forms.lastuserAutocomplete({
id: {{ field.id|tojson }},
multiple: {{ field.multiple|tojson }},
separator: {{ field.separator|tojson }},
autocomplete_endpoint: {{ field.autocomplete_endpoint|tojson }},
getuser_endpoint: {{ field.getuser_endpoint|tojson }}
});
{%- endif %}
{%- elif field.type in ['GeonameSelectField', 'GeonameSelectMultiField'] %}
activate_geoname_autocomplete({{ ('#'+field.id)|tojson }}, {{ field.autocomplete_endpoint|tojson }}, {{ field.getname_endpoint|tojson }}, {{ field.separator|tojson }});
{%- elif field.type == 'CoordinatesField' -%}
{{ map_marker_script(field) }}
{%- endif -%}
{%- endfor -%}
{%- if script %}
});
</script>
{%- endif -%}
{% endmacro %}