selectize.html 2.4 KB
Newer Older
C
codecalm 已提交
1
{% assign key = include.key | default: 'people' %}
C
codecalm 已提交
2
{% assign id = include.select-id | default: key %}
C
codecalm 已提交
3 4
{% assign data = site.data.selects[key] %}
{% assign options = data.options %}
C
codecalm 已提交
5

C
codecalm 已提交
6
<select name="{{ key }}" id="select-{{ id }}" class="form-select{% if include.state %} is-{{ include.state }}{% endif %}"{% if data.multiple %} multiple{% endif %}>
7 8
	{% if options == 'people' %}
		{% for person in site.data.people limit: 20 %}
C
codecalm 已提交
9
		{% capture avatar %}
C
codecalm 已提交
10
			{% include ui/avatar.html person=person class="avatar-sm rounded mr-2 ml-n1" %}
C
codecalm 已提交
11
		{% endcapture %}
C
codecalm 已提交
12
		<option value="{{ person.id }}" data-data='{"avatar": "{{ avatar | strip  | replace: '"', '\"' }}"}'{% if person.id == data.value %} selected{% endif %}>{{ person.full_name }}</option>
13
		{% endfor %}
C
codecalm 已提交
14

15 16 17
	{% else %}
		{% for option in options %}
		{% if option[1] %}
C
codecalm 已提交
18 19
			{% assign current-value = option[0] %}
			{% assign current-name = option[1].name %}
20 21
			{% assign all-data = option[1] %}
		{% else %}
C
codecalm 已提交
22 23
			{% assign current-value = option %}
			{% assign current-name = option %}
24
		{% endif %}
C
codecalm 已提交
25 26

		<option value="{{ current-value }}"{% if all-data.image %} data-data='{"image": "{{ site.base }}/{{ all-data.image }}"}'{% elsif all-data.flag %} data-data='{"flag": "{{ all-data.flag }}"}'{% endif %}{% if data.value == current-value or data.value contains current-value %} selected{% endif %}>{{ current-name }}</option>
27
		{% endfor %}
C
codecalm 已提交
28

29
	{% endif %}
C
codecalm 已提交
30 31
</select>

C
codecalm 已提交
32
{% append_lib selectize %}
C
codecalm 已提交
33
{% capture_global scripts %}
C
codecalm 已提交
34
<script>
35
	$(document).ready(function () {
C
codecalm 已提交
36
		$('#select-{{ id }}').selectize({
C
codecalm 已提交
37
			{% if key == "countries" %}
38 39
			render: {
				option: function (data, escape) {
40
					return '<div class="option"><span class="flag flag-country-' + data.flag + ' mr-2 ml-n1"></span>' + escape(data.text) + '</div>';
41 42
				},
				item: function (data, escape) {
43
					return '<div class="d-flex align-items-center"><span class="flag flag-country-' + data.flag + ' mr-2 ml-n1"></span>' + escape(data.text) + '</div>';
44 45
				}
			}
C
codecalm 已提交
46
			{% elsif key == "people" %}
47 48
			render: {
				option: function (data, escape) {
C
codecalm 已提交
49
					return '<div class="option">' + data.avatar + '' + escape(data.text) + '</div>';
50 51
				},
				item: function (data, escape) {
C
codecalm 已提交
52
					return '<div class="d-flex align-items-center">' + data.avatar + '' + escape(data.text) + '</div>';
53 54
				}
			}
C
codecalm 已提交
55 56 57 58 59 60 61 62
			{% endif %}
			{% if data.max-items %}
			maxItems: {{ data.max-items }},
			{% endif %}

			{% if data.removable %}
			plugins: ['remove_button'],
			{% endif %}
63 64
		});
	});
C
codecalm 已提交
65
</script>
C
codecalm 已提交
66
{% endcapture_global %}