-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresource-list.js
More file actions
113 lines (93 loc) · 3.94 KB
/
resource-list.js
File metadata and controls
113 lines (93 loc) · 3.94 KB
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
function updateResourceList(data) {
const activeTags = Array.from(document.querySelectorAll(".tag-filter input:checked"))
.map(input => input.value);
const exclusiveToggle = document.getElementById("exclusive-filter-toggle").checked;
console.log("Active tags:", activeTags); // Debugging line
console.log("Exclusive mode:", exclusiveToggle); // Debugging line
// Filter resources based on active tags
const filteredData = data.filter(d => {
const resourceTags = Object.keys(d)
.filter(key => !["Name", "Link", "Description"].includes(key) && d[key].toLowerCase() === "yes");
if (exclusiveToggle) {
// In exclusive mode, include only resources with EXACTLY the active tags
return resourceTags.length === activeTags.length && activeTags.every(tag => resourceTags.includes(tag));
} else {
// Default mode: include resources with ANY of the active tags
return activeTags.some(tag => resourceTags.includes(tag));
}
});
// If no resources match, show a message
if (filteredData.length === 0) {
document.getElementById("resources-list").innerHTML = "<em>No resources match the selected filters.</em>";
return;
}
// Generate HTML for the filtered list
let html = "<ul>";
filteredData.forEach(d => {
const tags = Object.keys(d)
.filter(key => !["Name", "Link", "Description"].includes(key) && d[key].toLowerCase() === "yes");
html += `<li>
<strong><a href="${d.Link}" target="_blank">${d.Name}</a></strong>
<span>${d.Description}</span><br>
<span>${tags.map(tag => `<span class="tag">${tag}</span>`).join(" ")}</span>
</li><br>`;
});
html += "</ul>";
// Update the resource list with filtered resources
document.getElementById("resources-list").innerHTML = html;
}
// Generate filter checkboxes based on unique tag headers
function generateFilterCheckboxes(data) {
const tagHeaders = Object.keys(data[0]).filter(key => !["Name", "Link", "Description"].includes(key));
console.log("Tag Headers:", tagHeaders); // Debugging line
let filterHTML = "";
tagHeaders.forEach(tag => {
filterHTML += `
<div>
<label>
<input type="checkbox" value="${tag}" checked> ${tag}
</label>
</div>`;
});
document.getElementById("filter-controls").innerHTML = filterHTML;
}
// Attach event listeners to checkboxes
function attachFilterListeners(data) {
const checkboxes = document.querySelectorAll(".tag-filter input");
checkboxes.forEach(checkbox => {
checkbox.addEventListener("change", () => updateResourceList(data));
});
const exclusiveToggle = document.getElementById("exclusive-filter-toggle");
exclusiveToggle.addEventListener("change", () => updateResourceList(data));
const selectAllButton = document.getElementById("select-all-btn");
selectAllButton.addEventListener("click", () => {
// Check all checkboxes
checkboxes.forEach(checkbox => checkbox.checked = true);
// Update the resource list after selecting all
updateResourceList(data);
});
const deselectAllButton = document.getElementById("deselect-all-btn");
deselectAllButton.addEventListener("click", () => {
// Uncheck all checkboxes
checkboxes.forEach(checkbox => checkbox.checked = false);
// Update the resource list after deselecting all
updateResourceList(data);
});
}
// Load data and initialize filters
d3.csv("resources/resources.csv").then(function (data) {
// If no data, show an empty state message
if (!data || data.length === 0) {
document.getElementById("resources-list").innerHTML = "<em>No resources listed yet.</em>";
return;
}
// Generate and populate filters
generateFilterCheckboxes(data);
// Populate initial resource list
updateResourceList(data);
// Attach filter listeners
attachFilterListeners(data);
}).catch(function (error) {
console.error("Error loading resources:", error);
document.getElementById("resources-list").innerHTML = "<em>Could not load resources list.</em>";
});