-
Notifications
You must be signed in to change notification settings - Fork 484
/
Copy pathcontent-filter-dropdown.html
229 lines (216 loc) · 12.4 KB
/
content-filter-dropdown.html
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
{%- assign default_component_classname = "content-list--filters--category-selector" -%}
{%- assign default_labeled_dropdown_id = "category-filter-selectore" -%}
{%- assign default_labeled_dropdown_text = "Filter by Category" -%}
{%- assign component_classname = include.component_classname | default: default_component_classname -%}
{%- assign dropdown_component_classname = component_classname | append: "--dropdown" -%}
{%- assign labeled_dropdown_text = include.labeled_dropdown_text | default: default_labeled_dropdown_text -%}
{%- assign labeled_dropdown_component_id = include.labeled_dropdown_component_id | default: default_labeled_dropdown_id -%}
{%- assign filter_chips_container_classname = component_classname | append: "--filter-chips" -%}
{%- assign filterable_content_block_element_classname = include.filterable_content_block_element_classname | default: "" -%}
{%- assign filter_categories_label_value_pairs = include.filter_categories -%}
{%- if filter_categories_label_value_pairs -%}
{%- assign filter_categories_count = filter_categories_label_value_pairs.size -%}
{%- else -%}
{%- assign filter_categories_count = 0 -%}
{%- endif -%}
<template id="community-filter-chip">
{% include filter-chips.html type="community" %}
</template>
<template id="development-filter-chip">
{% include filter-chips.html type="development" %}
</template>
<template id="events-filter-chip">
{% include filter-chips.html type="events" %}
</template>
<template id="author-filter-chip">
{% include filter-chips.html type="author" %}
</template>
<template id="conference_speaker-filter-chip">
{% include filter-chips.html type="conference_speaker" %}
</template>
<template id="osci-filter-chip">
{% include filter-chips.html type="osci" %}
</template>
<template id="search-filter-chip">
{% include filter-chips.html type="search" %}
</template>
<template id="analytics-observability-security-filter-chip">
{% include filter-chips.html type="analytics-observability-security" %}
</template>
<template id="operating-opensearch-filter-chip">
{% include filter-chips.html type="operating-opensearch" %}
</template>
{%- unless filterable_content_block_element_classname == "" or filter_categories_count == 0 -%}
<div class="content-filter-dropdown-component {{ component_classname }}">
<div class="content-filter-dropdown-component--dropdown {{ dropdown_component_classname }}">
{% include labeled-dropdown.html
items=filter_categories_label_value_pairs
label=labeled_dropdown_text
id=labeled_dropdown_component_id
%}
</div>
<div class="content-filter-dropdown-component--filter-chips {{ filter_chips_container_classname }}"></div>
</div>
<script type="module">
document.addEventListener('DOMContentLoaded', () => {
function hasFilterChip(chipType) {
const selector = `.{{ filter_chips_container_classname }} > [data-category=${chipType}]`;
const chipElement = document.querySelector(selector);
return chipElement !== null;
}
function hasAnyFilterChips() {
const selector = '.{{ filter_chips_container_classname }} > .filter-chip';
const chips = document.querySelectorAll(selector);
const chipCount = chips?.length ?? 0;
return chipCount > 0;
}
function updateFilterByCategoryLabelCount() {
const filterChipsParent = document.querySelector('.{{ filter_chips_container_classname }}');
const chipCount = filterChipsParent?.querySelectorAll?.(':scope > .filter-chip')?.length ?? 0;
const defaultLabelText = '{{ labeled_dropdown_text }}';
const labelSelector = '.{{ dropdown_component_classname }} .labeled-dropdown--label';
const label = document.querySelector(labelSelector);
if (chipCount > 0) {
const labelTextWithCount = `${defaultLabelText} (${chipCount})`;
label.textContent = labelTextWithCount;
} else {
label.textContent = defaultLabelText;
}
}
function addFilterChip(chipType) {
const templateId = `${chipType}-filter-chip`;
const chipTemplate = document.getElementById(templateId);
if (chipTemplate) {
const templateContent = chipTemplate.content.cloneNode(true);
const filterChipsParent = document.querySelector('.{{ filter_chips_container_classname }}');
filterChipsParent?.appendChild(templateContent);
updateFilterByCategoryLabelCount();
}
}
function removeFilterChip(chipType) {
const selector = `.{{ filter_chips_container_classname }} > [data-category=${chipType}]`;
const chipElement = document.querySelector(selector);
chipElement?.parentElement?.removeChild?.(chipElement);
updateFilterByCategoryLabelCount();
}
function hideFilterDropdown() {
const selector = '.{{ dropdown_component_classname }} .labeled-dropdown--select';
const dropdown = document.querySelector(selector);
const parent = dropdown.closest('.labeled-dropdown');
if (parent) {
const collapsedClassName = 'labeled-dropdown__collapsed';
const expandedClassName = 'labeled-dropdown__expanded';
parent.classList.replace(expandedClassName, collapsedClassName);
}
}
function onClickAwayCloseDropdown(e) {
const expandedCategoryFilterSelector = '.labeled-dropdown.labeled-dropdown__expanded';
const expandedCategoryFilter = document.querySelector(expandedCategoryFilterSelector);
if (expandedCategoryFilter) {
hideFilterDropdown();
event.preventDefault();
}
}
document.querySelector('.{{ filter_chips_container_classname }}')?.addEventListener?.('click', (e) => {
const { target } = e;
const clickedChipSelector = '.filter-chip';
const clickedChip = target.closest(clickedChipSelector);
if (clickedChip) {
const category = clickedChip.getAttribute('data-category');
const filterableElementClass = '{{ filterable_content_block_element_classname }}';
const filterableElementSelector = `.${filterableElementClass}`;
const filterableElementFilteredModifierClass = `${filterableElementClass}__filtered`;
const filterableElement = document.querySelector(filterableElementSelector);
if (filterableElement) {
const categoryAttribute = 'data-filtercategory';
const filteredCategory = filterableElement.getAttribute(categoryAttribute);
if (filteredCategory) {
const updatedFilters = filteredCategory.split(',').filter(value => value !== category).join(',');
if (updatedFilters.length > 0) {
filterableElement.setAttribute(categoryAttribute, updatedFilters);
} else {
filterableElement.removeAttribute(categoryAttribute);
filterableElement.classList.remove(filterableElementFilteredModifierClass);
}
}
}
removeFilterChip(category);
}
});
document.querySelector('.{{ dropdown_component_classname }} .labeled-dropdown--label-arrow')?.addEventListener?.('click', (e) => {
const collapsedClassName = 'labeled-dropdown__collapsed';
const expandedClassName = 'labeled-dropdown__expanded';
const { currentTarget } = e;
const parent = currentTarget.closest('.labeled-dropdown');
const selectElement = parent?.querySelector(':scope select');
if (selectElement?.hasAttribute?.('disabled')) {
e.stopImmediatePropagation();
e.stopPropagation();
e.preventDefault();
return;
}
if (parent) {
if (parent.classList.contains(collapsedClassName)) {
parent.classList.replace(collapsedClassName, expandedClassName);
} else {
parent.classList.replace(expandedClassName, collapsedClassName);
}
}
e.stopImmediatePropagation();
e.stopPropagation();
e.preventDefault();
});
document.querySelector('.{{ dropdown_component_classname }} .labeled-dropdown--select')?.addEventListener?.('click', (e) => {
const { currentTarget, target : { value = '' } = {} } = e;
if (currentTarget.hasAttribute('disabled')) {
e.preventDefault();
return;
}
const filterCategories = {{ filter_categories_label_value_pairs | map: "value" | jsonify }};
if (filterCategories.includes(value)) {
const contentBlockElementClass = '{{ filterable_content_block_element_classname }}';
const contentBlockElementSelector = `.${contentBlockElementClass}`;
const contentBlockElementFilteredModifierClass = `${contentBlockElementClass}__filtered`;
const contentBlockElement = document.querySelector(contentBlockElementSelector);
const adding = !hasFilterChip(value);
const filterAttribute = 'data-filtercategory';
if (adding) {
addFilterChip(value);
} else {
removeFilterChip(value);
}
if (hasAnyFilterChips()) {
if (contentBlockElement.hasAttribute(filterAttribute)) {
const existingFilters = contentBlockElement.getAttribute(filterAttribute);
if (adding) {
const updatedFilters = [...existingFilters.split(','), value].join(',');
contentBlockElement.setAttribute(filterAttribute, updatedFilters);
} else {
const updatedFilters = existingFilters.split(',').filter(category => category !== value);
if (updatedFilters.length > 0) {
contentBlockElement.setAttribute(filterAttribute, updatedFilters.join(','));
if (!contentBlockElement.classList.contains(contentBlockElementFilteredModifierClass)) {
contentBlockElement.classList.add(contentBlockElementFilteredModifierClass);
}
} else {
contentBlockElement.removeAttribute(filterAttribute);
contentBlockElement.classList.remove(contentBlockElementFilteredModifierClass);
}
}
} else {
contentBlockElement.setAttribute(filterAttribute, value);
if (!contentBlockElement.classList.contains(contentBlockElementFilteredModifierClass)) {
contentBlockElement.classList.add(contentBlockElementFilteredModifierClass);
}
}
} else {
contentBlockElement.removeAttribute(filterAttribute);
contentBlockElement.classList.remove(contentBlockElementFilteredModifierClass);
}
}
hideFilterDropdown();
});
document.body.addEventListener('click', onClickAwayCloseDropdown);
});
</script>
{%- endunless -%}