aboutsummaryrefslogtreecommitdiffstats
path: root/pydis_site/static/js/resources.js
blob: dee59e520da4502dfdb24f13cef071fa2f8ffa0b (plain) (blame)
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
"use strict";

// Filters that are currently selected
var activeFilters = {
    topics: [],
    type: [],
    "payment-tiers": [],
    complexity: []
};

/* Update the resources to match 'active_filters' */
function update() {
    let resources = $('.resource-box');

    // If there's nothing in the filters, show everything and return.
    if (
        activeFilters.topics.length === 0 &&
        activeFilters.type.length === 0 &&
        activeFilters["payment-tiers"].length === 0 &&
        activeFilters.complexity.length === 0
    ) {
        resources.show();
        return;
    }

    // Otherwise, hide everything and then filter the resources to decide what to show.
    resources.hide();
    resources.filter(function() {
        let validation = {
            topics: false,
            type: false,
            'payment-tiers': false,
            complexity: false
        };
        let resourceBox = $(this);

        // Validate the filters
        $.each(activeFilters, function(filterType, activeFilters) {
            // If the filter list is empty, this passes validation.
            if (activeFilters.length === 0) {
                validation[filterType] = true;
                return;
            }

            // Otherwise, we need to check if one of the classes exist.
            $.each(activeFilters, function(index, filter) {
                if (resourceBox.hasClass(filter)) {
                    validation[filterType] = true;
                }
            });
        });

        // If validation passes, show the resource.
        if (Object.values(validation).every(Boolean)) {
            return true;
        } else {
            return false;
        }
    }).show();
}

// Executed when the page has finished loading.
document.addEventListener("DOMContentLoaded", function () {

    // If you collapse or uncollapse a filter group, swap the icon.
    $('button.collapsible').click(function() {
        let icon = $(this).find(".card-header-icon i");

        if ($(icon).hasClass("fa-window-minimize")) {
            $(icon).removeClass(["far", "fa-window-minimize"]);
            $(icon).addClass(["fas", "fa-angle-down"]);
        } else {
            $(icon).removeClass(["fas", "fa-angle-down"]);
            $(icon).addClass(["far", "fa-window-minimize"]);
        }
    });

    // Update the filters on page load to reflect URL parameters.

    // If you click on the div surrounding the filter checkbox, it clicks the checkbox.
    $('.filter-panel').click(function() {
        let checkbox = $(this).find(".filter-checkbox");
        checkbox.prop("checked", !checkbox.prop("checked"));
        checkbox.change();
    });

    // When checkboxes are toggled, trigger a filter update.
    $('.filter-checkbox').change(function () {
        let filterItem = this.dataset.filterItem;
        let filterName = this.dataset.filterName;
        let cssClass = filterName + "-" + filterItem;
        var filterIndex = activeFilters[filterName].indexOf(cssClass);

        if (this.checked) {
            if (filterIndex === -1) {
                activeFilters[filterName].push(cssClass);
            }
            update();
        } else {
            if (filterIndex !== -1) {
                activeFilters[filterName].splice(filterIndex, 1);
            }
            update();
        }
    });
});



// const initialParams = new URLSearchParams(window.location.search);
// const checkboxOptions = ['topic', 'type', 'payment', 'complexity'];
//
// const createQuerySelect = (opt) => {
//     return "input[name=" + opt + "]"
// }
//
// checkboxOptions.forEach((option) => {
//     document.querySelectorAll(createQuerySelect(option)).forEach((checkbox) => {
//         if (initialParams.get(option).includes(checkbox.value)) {
//             checkbox.checked = true
//         }
//     });
// });
//
// function buildQueryParams() {
//     let params = new URLSearchParams(window.location.search);
//     checkboxOptions.forEach((option) => {
//         let tempOut = ""
//         document.querySelectorAll(createQuerySelect(option)).forEach((checkbox) => {
//             if (checkbox.checked) {
//                 tempOut += checkbox.value + ",";
//             }
//         });
//         params.set(option, tempOut);
//     });
//
//     window.location.search = params;
// }
//
// function clearQueryParams() {
//     checkboxOptions.forEach((option) => {
//         document.querySelectorAll(createQuerySelect(option)).forEach((checkbox) => {
//             checkbox.checked = false;
//         });
//     });
// }
//
// function selectAllQueryParams(column) {
//     checkboxOptions.forEach((option) => {
//         document.querySelectorAll(createQuerySelect(option)).forEach((checkbox) => {
//             if (checkbox.className == column) {
//                 checkbox.checked = true;
//             }
//         });
//     });
// }