-
Notifications
You must be signed in to change notification settings - Fork 0
/
script2.js
117 lines (101 loc) · 3.79 KB
/
script2.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
const selectElem = document.getElementById("json-select");
const dayHeaderElem = document.getElementById("day-header");
const mapElem = document.getElementById("map");
const progressElem = document.getElementById("progress");
const classNameElem = document.getElementById("class-name");
const timeLeftElem = document.getElementById("time-left");
let data; // Store JSON data
let currentDate; // Store current date
// Fetch JSON data
function fetchData(url) {
return axios.get(url).then((response) => response.data);
}
// Populate dropdown menu with available JSON files
fetchData("json-files.json").then((jsonData) => {
const optionsHTML = jsonData
.map(
(option, index) =>
`<option value="${option.file}"${index === 0 ? " selected" : ""}>${
option.name
}</option>`
)
.join("");
selectElem.innerHTML = optionsHTML;
loadJSON(jsonData[0].file);
});
// When a new JSON file is selected from the dropdown menu
selectElem.addEventListener("change", (event) => {
loadJSON(event.target.value);
});
// Load JSON file and update the webpage accordingly
function loadJSON(filename) {
fetchData(filename).then((jsonData) => {
data = jsonData;
// Update the current date
currentDate = moment();
// Update the day header
const days = [
"Sunday",
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday",
];
const currentDayIndex = parseInt(currentDate.format("d"));
dayHeaderElem.textContent = `${days[currentDayIndex]} (${currentDate.format(
"MMMM Do"
)})`;
// Filter events based on the current day of the week
const filteredData = _.filter(data, { day: `${currentDayIndex}` });
// Sort events by their start time
filteredData.sort((a, b) => (a.start_time > b.start_time ? 1 : -1));
// Generate the map
let mapHTML = "";
const mapWidth = 1000;
const mapHeight = 500;
let totalDuration = 0;
let currentEvent = null;
filteredData.forEach((event, index) => {
const startTime = moment(event.start_time, "HH:mm");
const endTime = moment(event.end_time, "HH:mm");
const duration = moment.duration(endTime.diff(startTime)).asMinutes();
const xPos =
(mapWidth *
startTime.diff(moment(startTime).startOf("day"), "minutes")) /
1440;
const yPos = (mapHeight / filteredData.length) * index;
const width = (mapWidth * duration) / 1440;
const height = mapHeight / filteredData.length;
mapHTML += `<rect x="${xPos}" y="${yPos}" width="${width}" height="${height}" fill="#AAAAAA"/>`;
if (currentDate.isBetween(startTime, endTime, null, "[]")) {
const progressWidth =
(mapWidth * currentDate.diff(startTime, "minutes")) / duration;
progressElem.style.width = `${progressWidth}px`;
progressElem.style.left = `${xPos + progressWidth}px`;
currentEvent = event;
}
totalDuration += duration;
});
mapElem.innerHTML = `` + mapHTML + ``;
// Display information about the current event
if (currentEvent) {
classNameElem.textContent = currentEvent.class;
const endTime = moment(currentEvent.end_time, "HH:mm");
const timeLeft = moment.duration(endTime.diff(currentDate)).asMinutes();
if (timeLeft > 1) {
timeLeftElem.textContent = `${Math.floor(
timeLeft
)} minutes left in class`;
} else if (timeLeft > 0) {
timeLeftElem.textContent = `Less than a minute left in class`;
} else {
timeLeftElem.textContent = `Class is over`;
}
} else {
classNameElem.textContent = "No classes currently in session";
timeLeftElem.textContent = "";
}
});
}