-
Notifications
You must be signed in to change notification settings - Fork 5
/
time-classes.js
94 lines (82 loc) · 2.42 KB
/
time-classes.js
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
/**
* An array of valid time classes.
* @type {string[]}
*/
const VALID_TIME_CLASSES = Object.freeze(['bullet', 'blitz', 'rapid', 'daily']);
/**
* The ID for the filter select element.
* @type {string}
*/
const FILTER_SELECT_ID = 'filter-select';
/**
* The value representing any option.
* @type {string}
*/
const ANY_OPTION_VALUE = '';
/**
*
* @param {array} games - Array of games (objects) returned from API call
* @param {string} timeCLass - Time class. One of a VALID_TIME_CLASSES
* @returns {array} - New array filtered by passed time class
*/
function filterGamesByTimeClass(games, timeCLass) {
return games.filter((game) => game.time_class === timeCLass);
}
/**
* Generates a select element and appends it to the filter box DOM element.
*/
function generate() {
/**
* The DOM element representing the filter box.
* @type {Element}
*/
const filterBoxDOM = document.querySelector('.filter');
/**
* The select element to be appended to the filter box DOM.
* @type {HTMLSelectElement}
*/
const select = document.createElement('select');
select.name = 'filter';
select.id = FILTER_SELECT_ID;
const optionNodes = [];
/**
* Creates an option element with the specified value and text content,
* and pushes it to the optionNodes array.
* @param {string} value - The value of the option.
* @param {string} text - The text content of the option.
*/
function createOption(value, text) {
const option = document.createElement('option');
option.value = value;
option.textContent = text;
optionNodes.push(option);
}
createOption(ANY_OPTION_VALUE, 'any');
VALID_TIME_CLASSES.forEach((timeClass) => createOption(timeClass, timeClass));
select.append(...optionNodes);
filterBoxDOM.append(select);
}
function reset() {
document.getElementById(FILTER_SELECT_ID).value = ANY_OPTION_VALUE;
}
function disable() {
document.getElementById(FILTER_SELECT_ID).disabled = true;
}
function enable() {
document.getElementById(FILTER_SELECT_ID).disabled = false;
}
/**
* Creates a change event listener on the filter select element.
* @param {function} func - The function to be called when the change event is triggered.
*/
function createChangeEventListener(func) {
document.getElementById(FILTER_SELECT_ID).addEventListener('change', func);
}
export const FilterOptions = {
filterGamesByTimeClass,
generate,
reset,
disable,
enable,
createChangeEventListener,
};