]> git.lizzy.rs Git - cheatdb.git/blob - app/templates/macros/forms.html
9c56a4842e6977b495d7cca4b9378c8cd0dff09c
[cheatdb.git] / app / templates / macros / forms.html
1 {% macro render_field(field, label=None, label_visible=true, right_url=None, right_label=None, fieldclass=None) -%}
2         <div class="form-group {% if field.errors %}has-error{% endif %} {{ kwargs.pop('class_', '') }}">
3                 {% if field.type != 'HiddenField' and label_visible %}
4                         {% if not label and label != "" %}{% set label=field.label.text %}{% endif %}
5                         {% if label %}<label for="{{ field.id }}">{{ label|safe }}</label>{% endif %}
6                 {% endif %}
7                 {{ field(class_=fieldclass or 'form-control', **kwargs) }}
8                 {% if field.errors %}
9                         {% for e in field.errors %}
10                                 <p class="help-block">{{ e }}</p>
11                         {% endfor %}
12                 {% endif %}
13         </div>
14 {%- endmacro %}
15
16 {% macro form_scripts() -%}
17         <link href="/static/jquery-ui.min.css" rel="stylesheet" type="text/css">
18         <script src="/static/jquery-ui.min.js"></script>
19         <script src="/static/tagselector.js?v=2"></script>
20 {% endmacro %}
21
22 {% macro package_lists() -%}
23         <script>
24                 meta_packages = [
25                         {% for m in mpackages %}
26                                 {# This is safe as name can only contain `[a-z0-9_]` #}
27                                 {
28                                         id: "{{ m.name }}",
29                                         value: "{{ m.name }}",
30                                         toString: function() { return "{{ m.name }}"; },
31                                 },
32                         {% endfor %}
33                 ]
34
35                 function escape(unsafe) {
36                         return unsafe
37                                 .replace(/&/g, "&amp;")
38                                 .replace(/</g, "&lt;")
39                                 .replace(/>/g, "&gt;")
40                                 .replace(/"/g, "&quot;")
41                                 .replace(/'/g, "&#039;");
42                 }
43
44                 all_packages = meta_packages.slice();
45
46                 {% for p in packages %}
47                         all_packages.push({
48                                 id: "{{ p.author.username }}/{{ p.name }}",
49                                 value: escape({{ p.title | tojson }} + " by " + {{ p.author.display_name | tojson }}),
50                                 toString: function() { return  escape({{ p.title | tojson }} + " by " + {{ p.author.display_name | tojson }} + " only"); },
51                         });
52                 {% endfor %}
53         </script>
54 {% endmacro %}
55
56 {% macro render_multiselect_field(field, label=None, label_visible=true, right_url=None, right_label=None) -%}
57         <div class="form-group {% if field.errors %}has-error{% endif %} {{ kwargs.pop('class_', '') }}">
58                 {% if field.type != 'HiddenField' and label_visible %}
59                         {% if not label %}{% set label=field.label.text %}{% endif %}
60                         <label for="{{ field.id }}">{{ label|safe }}</label>
61                 {% endif %}
62                 <div class="multichoice_selector bulletselector form-control">
63                         <input type="text" placeholder="Start typing to see suggestions">
64                         <div class="clearboth"></div>
65                 </div>
66                 <div class="invalid-remaining invalid-feedback"></div>
67                 {{ field(class_='form-control', **kwargs) }}
68                 {% if field.errors %}
69                         {% for e in field.errors %}
70                                 <div class="invalid-feedback">{{ e }}</div>
71                         {% endfor %}
72                 {% endif %}
73         </div>
74 {% endmacro %}
75
76 {% macro render_mpackage_field(field, label=None, label_visible=true, right_url=None, right_label=None) -%}
77         <div class="form-group {% if field.errors %}has-error{% endif %} {{ kwargs.pop('class_', '') }}">
78                 {% if field.type != 'HiddenField' and label_visible %}
79                         {% if not label %}{% set label=field.label.text %}{% endif %}
80                         <label for="{{ field.id }}">{{ label|safe }}</label>
81                 {% endif %}
82                 <div class="metapackage_selector bulletselector form-control">
83                         <input type="text" placeholder="Comma-seperated values">
84                         <div class="clearboth"></div>
85                 </div>
86                 {{ field(class_='form-control', **kwargs) }}
87                 <div class="invalid-remaining invalid-feedback"></div>
88                 {% if field.errors %}
89                         {% for e in field.errors %}
90                                 <p class="help-block">{{ e }}</p>
91                         {% endfor %}
92                 {% endif %}
93         </div>
94 {% endmacro %}
95
96 {% macro render_deps_field(field, label=None, label_visible=true, right_url=None, right_label=None) -%}
97         <div class="form-group {% if field.errors %}has-error{% endif %} {{ kwargs.pop('class_', '') }}">
98                 {% if field.type != 'HiddenField' and label_visible %}
99                         {% if not label %}{% set label=field.label.text %}{% endif %}
100                         <label for="{{ field.id }}">{{ label|safe }}</label>
101                 {% endif %}
102                 <div class="deps_selector bulletselector form-control">
103                         <input type="text" placeholder="Comma-seperated values">
104                         <div class="clearboth"></div>
105                 </div>
106                 {{ field(class_='form-control', **kwargs) }}
107                 <div class="invalid-remaining invalid-feedback"></div>
108                 {% if field.errors %}
109                         {% for e in field.errors %}
110                                 <p class="help-block">{{ e }}</p>
111                         {% endfor %}
112                 {% endif %}
113         </div>
114 {% endmacro %}
115
116 {% macro render_checkbox_field(field, label=None) -%}
117         {% if not label %}{% set label=field.label.text %}{% endif %}
118         <div class="checkbox {{ kwargs.pop('class_', '') }}">
119                 <label>
120                         {{ field(type='checkbox', **kwargs) }} {{ label }}
121                 </label>
122         </div>
123 {%- endmacro %}
124
125 {% macro render_radio_field(field) -%}
126         {% for value, label, checked in field.iter_choices() %}
127                 <div class="form-check my-1">
128                         <label class="form-check-label">
129                                 <input class="form-check-input" type="radio" name="{{ field.id }}" id="{{ field.id }}" value="{{ value }}"{% if checked %} checked{% endif %}>
130                                 {{ label }}
131                         </label>
132                 </div>
133         {% endfor %}
134 {%- endmacro %}
135
136 {% macro render_toggle_field(field, icons=[]) -%}
137 <div class="btn-group btn-group-toggle" data-toggle="buttons">
138         {% for value, label, checked in field.iter_choices() %}
139                 <label class="btn btn-primary{% if checked %} active{% endif %}">
140                         {% set icon = icons[value] %}
141                         {% if icon %}
142                                 <i class="fas {{ icon }} mr-2"></i>
143                         {% endif %}
144                         <input type="radio" name="{{ field.id }}" id="{{ field.id }}" value="{{ value }}" autocomplete="off" {% if checked %} checked{% endif %}>
145                         {{ label }}
146                 </label>
147         {% endfor %}
148         </div>
149 {%- endmacro %}
150
151 {% macro render_submit_field(field, label=None, tabindex=None) -%}
152         {% if not label %}{% set label=field.label.text %}{% endif %}
153         {#<button type="submit" class="form-control btn btn-default btn-primary">{{label}}</button>#}
154         <input type="submit" value="{{label}}" class="btn btn-primary"
155                    {% if tabindex %}tabindex="{{ tabindex }}"{% endif %}
156                    >
157 {%- endmacro %}