-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
63 lines (49 loc) · 1.68 KB
/
main.js
File metadata and controls
63 lines (49 loc) · 1.68 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
//======= Global Constants ========
const timeframes = {
daily: "day",
weekly: "week",
monthly: "month",
};
const tasks = document.querySelector(".tasks-container");
let data;
//======= Functions ========
function getTaskClassName(from) {
return from.toLowerCase().replace(" ", "-");
}
function updateTask(index, timeframe, taskData) {
const el = tasks.children[index];
el.classList.add( `${getTaskClassName(taskData.title)}-task` );
el.querySelector(".task-item-title > div").textContent = taskData.title;
el.querySelector(".task-item-time").textContent =
taskData.timeframes[timeframe].current + "hrs";
el.querySelector(".task-item-prev").textContent =
`Last ${timeframes[timeframe]} - ${taskData.timeframes[timeframe].previous}hrs`;
}
function updateTasks(timeframe, data) {
data.forEach( (task, i) => updateTask(i, timeframe, task) );
}
//======= Data Loading ========
const request = new XMLHttpRequest();
request.onload = () => {
data = request.response;
if(data) {
updateTasks("weekly", data);
} else {
console.log("Couldn't load task data");
}
};
request.open("GET", "./data.json");
request.responseType = "json";
request.send();
//======= Links' click event ========
document.querySelectorAll("a").forEach( el =>
el.addEventListener('click', e => {
e.preventDefault();
//Only for timeframe links
if(e.target.getAttribute("data-frame")) {
document.querySelector(".active-link").classList.toggle("active-link");
e.target.classList.toggle("active-link");
updateTasks(e.target.getAttribute("data-frame"), data);
}
})
);