-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCustom Alpine.js Filter
225 lines (203 loc) · 9.28 KB
/
Custom Alpine.js Filter
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
// Import necessary libraries
import imagesLoaded from 'imagesloaded';
import Masonry from 'masonry-layout';
// Listen for the Alpine.js initialization event
document.addEventListener('alpine:init', () => {
// Define Alpine.js data component named 'studyFilter'
Alpine.data('studyFilter', () => ({
// Data properties for filtering and displaying posts
selectedAge: '',
selectedCompensation: '',
selectedCompAmount: '',
selectedInvolvement: '',
selectedNeurodevelopment: '',
selectedStudentLed: '',
selectedType: '',
posts: postsData,
filterData: filterData,
showDialog: null,
dropdowns: {
// Data to track the visibility of dropdown menus
'age': false,
'study-compensation': false,
'study-comp-amount': false,
'study-involvement': false,
'neurodevelopment': false,
'study-led': false,
'study-type': false
},
// Initialization function
init() {
// Initialize filter taxonomy counts and Masonry layout
this.filterTaxonomyCounts();
this.initMasonry();
// Watch for changes in filter selections and trigger filtering
this.$watch('selectedAge', () => {
this.filterStudies();
});
this.$watch('selectedCompensation', () => {
this.filterStudies();
});
this.$watch('selectedCompAmount', () => {
this.filterStudies();
});
this.$watch('selectedInvolvement', () => {
this.filterStudies();
});
this.$watch('selectedNeurodevelopment', () => {
this.filterStudies();
});
this.$watch('selectedStudentLed', () => {
this.filterStudies();
});
this.$watch('selectedType', () => {
this.filterStudies();
});
},
// Toggle visibility of dropdown menus
toggleDropdown(name) {
if (this.dropdowns[name]) {
return this.toggleClose(name)
}
this.dropdowns[name] = true;
},
// Close a dropdown menu
toggleClose(name) {
if (!this.dropdowns[name]) return
this.dropdowns[name] = false;
let focusName = name + '-button';
name && document.getElementById(focusName).focus()
},
// Select key or toggle
filterClick(name, value) {
if (this['selected' + name] == "") {
this['selected' + name] = value;
} else {
this['selected' + name] = "";
}
},
// Filter posts based on selected filters
filterStudies() {
// Reset post visibility and filter counts
this.resetPostVisible();
this.resetFilterCounts();
this.filterTaxonomyCounts();
// If all filters are empty, reset posts and return
if ((this.selectedAge == "") && (this.selectedCompensation == "") && (this.selectedCompAmount == "") && (this.selectedInvolvement == "")
&& (this.selectedNeurodevelopment == "") && (this.selectedStudentLed == "") && (this.selectedType == "")) {
this.initMasonry();
return;
}
// Loop through posts and determine visibility based on selected filters
for (let post in this.posts) {
if (this.posts.hasOwnProperty(post)) {
let visible = false;
const ageArray = Object.values(this.posts[post].taxonomies?.['study-age'] ?? []);
const compensationArray = Object.values(this.posts[post].taxonomies?.['study-compensation'] ?? []);
const compAmountArray = Object.values(this.posts[post].taxonomies?.['study-comp-amount'] ?? []);
const involvementArray = Object.values(this.posts[post].taxonomies?.['study-involvement'] ?? []);
const neurodevelopmentArray = Object.values(this.posts[post].taxonomies?.['neurodevelopment'] ?? []);
const studenLedArray = Object.values(this.posts[post].taxonomies?.['study-led'] ?? []);
const typeArray = Object.values(this.posts[post].taxonomies?.['study-type'] ?? []);
// Logic to determine post visibility based on selected filters
visible = (
(this.selectedAge === "" || ageArray.includes(parseInt(this.selectedAge))) &&
(this.selectedCompensation === "" || compensationArray.includes(parseInt(this.selectedCompensation))) &&
(this.selectedCompAmount === "" || compAmountArray.includes(parseInt(this.selectedCompAmount))) &&
(this.selectedInvolvement === "" || involvementArray.includes(parseInt(this.selectedInvolvement))) &&
(this.selectedNeurodevelopment === "" || neurodevelopmentArray.includes(parseInt(this.selectedNeurodevelopment))) &&
(this.selectedStudentLed === "" || studenLedArray.includes(parseInt(this.selectedStudentLed))) &&
(this.selectedType === "" || typeArray.includes(parseInt(this.selectedType)))
);
this.posts[post].visible = visible;
}
}
// Update filter counts and Masonry layout
this.filterTaxonomyCounts();
this.initMasonry();
},
// Reset filter counts to zero
resetFilterCounts() {
for (let taxonomy in this.filterData) {
if (this.filterData.hasOwnProperty(taxonomy)) {
for (let term in this.filterData[taxonomy]) {
if (this.filterData[taxonomy].hasOwnProperty(term)) {
this.filterData[taxonomy][term].count = 0;
}
}
}
}
},
// Update filter counts based on visible posts
filterTaxonomyCounts() {
//Reset counts
this.resetFilterCounts();
//Loop through posts
for (let post in this.posts) {
if (this.posts.hasOwnProperty(post)) {
if (this.posts[post].visible) {
// Loop through post taxonomies and count visible for each taxonomy
for (let taxonomy in this.posts[post].taxonomies) {
if (this.posts[post].taxonomies.hasOwnProperty(taxonomy)) {
this.posts[post].taxonomies[taxonomy].forEach((term) => {
// Increase term count
let count = this.filterData[taxonomy][term].count;
this.filterData[taxonomy][term].count = count + 1;
})
}
}
}
}
}
this.initMasonry();
},
// Reset all filter selections and post visibility
resetFilters() {
this.selectedAge = '';
this.selectedCompensation = '';
this.selectedCompAmount = '';
this.selectedInvolvement = '';
this.selectedNeurodevelopment = '';
this.selectedStudentLed = '';
this.selectedType = '';
this.resetPostVisible();
this.resetFilterCounts();
this.filterTaxonomyCounts();
},
// Reset post visibility to true for all posts
resetPostVisible() {
for (let post in this.posts) {
if (this.posts.hasOwnProperty(post)) {
this.posts[post].visible = true;
// post.count = 0;
}
}
},
// Initialize Masonry layout
initMasonry() {
console.log('initMasonry');
var cardGrids = document.querySelectorAll('.masonry-cards');
console.log(cardGrids);
// Check if there is at least one post with the class name "masonry-cards"
if (cardGrids.length > 0) {
// Iterate through each post with the class name "masonry-cards"
cardGrids.forEach(function (cardGrid) {
// Wait for all images to load before initializing the Masonry grid
imagesLoaded(cardGrid, function () {
// Create a new Masonry object with the "cardGrid" post as the container, and specify the "itemSelector", "columnWidth", "gutter", and "percentPosition" options
var masonry = new Masonry(cardGrid, {
itemSelector: '.masonry-cards__column',
columnWidth: '.masonry-cards__column',
gutter: 16,
percentPosition: true,
});
});
});
}
},
// Get the name of a filter term
getFilterName(filter, key) {
return this.filterData[filter][key].name;
},
}));
});