@@ -8,42 +8,33 @@ var activeFilters = {
8
8
difficulty : [ ]
9
9
} ;
10
10
11
+ /* Add a filter, and update the UI */
11
12
function addFilter ( filterName , filterItem ) {
12
- // Push the filter into the stack
13
13
var filterIndex = activeFilters [ filterName ] . indexOf ( filterItem ) ;
14
14
if ( filterIndex === - 1 ) {
15
15
activeFilters [ filterName ] . push ( filterItem ) ;
16
16
}
17
17
updateUI ( ) ;
18
+ }
18
19
19
- // Show a corresponding filter box tag
20
- $ ( `.filter-box-tag[data-filter-name=${ filterName } ][data-filter-item=${ filterItem } ]` ) . show ( ) ;
21
-
22
- // Make corresponding resource tags active
23
- $ ( `.resource-tag[data-filter-name=${ filterName } ][data-filter-item=${ filterItem } ]` ) . addClass ( "active" ) ;
24
-
25
- // Hide the "No filters selected" tag.
26
- $ ( ".no-tags-selected.tag" ) . hide ( ) ;
20
+ /* Remove all filters, and update the UI */
21
+ function removeAllFilters ( ) {
22
+ activeFilters = {
23
+ topics : [ ] ,
24
+ type : [ ] ,
25
+ "payment-tiers" : [ ] ,
26
+ difficulty : [ ]
27
+ } ;
28
+ updateUI ( ) ;
27
29
}
28
30
31
+ /* Remove a filter, and update the UI */
29
32
function removeFilter ( filterName , filterItem ) {
30
- // Remove the filter from the stack
31
33
var filterIndex = activeFilters [ filterName ] . indexOf ( filterItem ) ;
32
34
if ( filterIndex !== - 1 ) {
33
35
activeFilters [ filterName ] . splice ( filterIndex , 1 ) ;
34
36
}
35
37
updateUI ( ) ;
36
-
37
- // Hide the corresponding filter box tag
38
- $ ( `.filter-box-tag[data-filter-name=${ filterName } ][data-filter-item=${ filterItem } ]` ) . hide ( ) ;
39
-
40
- // Make corresponding resource tags inactive
41
- $ ( `.resource-tag[data-filter-name=${ filterName } ][data-filter-item=${ filterItem } ]` ) . removeClass ( "active" ) ;
42
-
43
- // Show "No filters selected" tag, if there are no filters active
44
- if ( noFilters ( ) ) {
45
- $ ( ".no-tags-selected.tag" ) . show ( ) ;
46
- }
47
38
}
48
39
49
40
/* Check if there are no filters */
@@ -76,7 +67,7 @@ function deserializeURLParams() {
76
67
window . location . href = "https://www.youtube.com/watch?v=dQw4w9WgXcQ" ;
77
68
} else if ( String ( filter ) === "sneakers" && filterType === "topics" ) {
78
69
window . location . href = "https://www.youtube.com/watch?v=NNZscmNE9QI" ;
79
- } else if ( validFilters [ filterType ] . includes ( String ( filter ) ) ) {
70
+ } else if ( validFilters . hasOwnProperty ( filterType ) && validFilters [ filterType ] . includes ( String ( filter ) ) ) {
80
71
let checkbox = $ ( `.filter-checkbox[data-filter-name='${ filterType } '][data-filter-item='${ filter } ']` ) ;
81
72
let filterTag = $ ( `.filter-box-tag[data-filter-name='${ filterType } '][data-filter-item='${ filter } ']` ) ;
82
73
let resourceTags = $ ( `.resource-tag[data-filter-name='${ filterType } '][data-filter-item='${ filter } ']` ) ;
@@ -91,9 +82,10 @@ function deserializeURLParams() {
91
82
// Ditch all the params from the URL, and recalculate the URL params
92
83
updateURL ( ) ;
93
84
94
- // If we've added a filter, hide the no filters tag.
85
+ // If we've added a filter, hide stuff
95
86
if ( filterAdded ) {
96
87
$ ( ".no-tags-selected.tag" ) . hide ( ) ;
88
+ $ ( ".close-filters-button" ) . show ( ) ;
97
89
}
98
90
}
99
91
} ) ;
@@ -123,15 +115,36 @@ function updateURL() {
123
115
function updateUI ( ) {
124
116
let resources = $ ( '.resource-box' ) ;
125
117
let filterTags = $ ( '.filter-box-tag' ) ;
118
+ let noTagsSelected = $ ( ".no-tags-selected.tag" ) ;
119
+ let closeFiltersButton = $ ( ".close-filters-button" ) ;
126
120
127
121
// Update the URL to match the new filters.
128
122
updateURL ( ) ;
129
123
130
- // If there's nothing in the filters, show everything and return.
124
+ // If there's nothing in the filters, we can return early .
131
125
if ( noFilters ( ) ) {
132
126
resources . show ( ) ;
133
127
filterTags . hide ( ) ;
128
+ noTagsSelected . show ( ) ;
129
+ closeFiltersButton . hide ( ) ;
130
+ $ ( `.filter-checkbox:checked` ) . prop ( "checked" , false )
134
131
return ;
132
+ } else {
133
+ $ . each ( activeFilters , function ( filterType , filters ) {
134
+ $ . each ( filters , function ( index , filter ) {
135
+ // Show a corresponding filter box tag
136
+ $ ( `.filter-box-tag[data-filter-name=${ filterType } ][data-filter-item=${ filter } ]` ) . show ( ) ;
137
+
138
+ // Make corresponding resource tags active
139
+ $ ( `.resource-tag[data-filter-name=${ filterType } ][data-filter-item=${ filter } ]` ) . addClass ( "active" ) ;
140
+
141
+ // Hide the "No filters selected" tag.
142
+ noTagsSelected . hide ( ) ;
143
+
144
+ // Show the close filters button
145
+ closeFiltersButton . show ( ) ;
146
+ } ) ;
147
+ } ) ;
135
148
}
136
149
137
150
// Otherwise, hide everything and then filter the resources to decide what to show.
@@ -147,7 +160,6 @@ function updateUI() {
147
160
let resourceBox = $ ( this ) ;
148
161
149
162
// Validate the filters
150
-
151
163
$ . each ( activeFilters , function ( filterType , filters ) {
152
164
// If the filter list is empty, this passes validation.
153
165
if ( filters . length === 0 ) {
@@ -256,6 +268,11 @@ document.addEventListener("DOMContentLoaded", function () {
256
268
}
257
269
} ) ;
258
270
271
+ // When you click the little gray x, remove all filters.
272
+ $ ( ".close-filters-button" ) . on ( "click" , function ( ) {
273
+ removeAllFilters ( ) ;
274
+ } ) ;
275
+
259
276
// When checkboxes are toggled, trigger a filter update.
260
277
$ ( '.filter-checkbox' ) . on ( "change" , function ( event ) {
261
278
let filterItem = this . dataset . filterItem ;
0 commit comments