-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
172 lines (147 loc) · 3.49 KB
/
app.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
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
const projectName = location.pathname.split('/').pop(); // get course project name
const STORAGE_KEY = `htmlacademy-${projectName}-checklist`;
/**
* Get and parse state from local storage
*
* @returns {array}
*/
const getState = () => {
return JSON.parse(localStorage.getItem(STORAGE_KEY) || '[]');
}
/**
* Update state in local storage
*
* @param {string[]} state
*/
const updateState = (state) => {
localStorage.setItem(STORAGE_KEY, JSON.stringify(state));
}
/**
* Make item as complete
*
* @param {HTMLElement} item
*/
const completeItem = (item) => {
item.style.textDecoration = 'line-through';
item.classList.add('strikeout');
}
/**
* Make item as uncomplete
*
* @param {HTMLElement} item
*/
const uncompleteItem = (item) => {
item.style.textDecoration = 'none';
item.classList.remove('strikeout');
}
/**
* Create a checkbox element
*
* @returns {HTMLInputElement}
*/
const createCheckbox = () => {
const checkbox = document.createElement('input');
checkbox.type = 'checkbox';
checkbox.style.marginRight = '5px';
return checkbox;
}
/**
* Create Progress Bar Element
*
* @param initValue
* @return {HTMLProgressElement}
*/
const createProgressBar = (initValue = 0) => {
const progress = document.createElement('progress')
progress.id = 'progress'
progress.value = initValue;
progress.max = 100;
progress.style.width = '100%';
progress.style.height = '30px';
return progress;
}
/**
* Get Specification Elements
*
* @returns {HTMLElement[]}
*/
const getSpecificationItems = () => {
return [...document.querySelectorAll('#specification li')];
}
/**
* Handle click on checkbox
*
* @param {HTMLElement} item
* @returns {function(): (void)}
*/
const checkboxClickHandler = (item) => {
return () => {
const state = getState();
const itemText = item.innerText;
if (item.classList.contains('strikeout')) {
uncompleteItem(item);
const newState = state.filter((s) => s !== itemText);
updateState(newState);
updateProgressBar();
return;
}
state.push(itemText);
updateState(state);
updateProgressBar();
completeItem(item);
}
}
/**
* Count Complete Percent
*
* @return {string}
*/
const countCompletePercent = () => {
const items = getSpecificationItems().length;
const completeList = getState().length;
return ((completeList / items) * 100).toFixed(2);
};
/**
* Update Progress Bar
*
* @returns {void}
*/
const updateProgressBar = () => {
const progressBar = document.querySelector('#progress')
progressBar.value = countCompletePercent();
}
/**
* Insert Progress Bar
*
* @returns {void}
*/
const insertProgressBar = () => {
const specification = document.querySelector('#specification');
specification.prepend(createProgressBar(countCompletePercent()));
}
/**
* Append custom checkbox to the specification items
*
* @returns {void}
*/
const init = () => {
const items = getSpecificationItems();
const completeList = getState();
insertProgressBar();
items.forEach((item) => {
const checkbox = createCheckbox();
const itemText = item.innerText;
if (completeList.includes(itemText)) {
checkbox.checked = true;
completeItem(item);
}
checkbox.addEventListener('click', checkboxClickHandler(item));
if (item.querySelector('p')) { // if item has paragraph, prepend checkbox into paragraph
item.querySelector('p').prepend(checkbox);
return;
}
item.prepend(checkbox); // prepend checkbox into li item
})
}
// init extension
init();