-
Notifications
You must be signed in to change notification settings - Fork 1
/
script.js
145 lines (126 loc) · 4.45 KB
/
script.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
//initializing variables
var currentMonth = dayjs().month();
var currentYear = dayjs().year();
var daysInOffice = 0;
var percentageInOffice = 0;
var storedDays = [];
var state;
var number;
console.log(currentMonth);
console.log(currentYear);
//get the number of days in a month
function getDaysInMonth(month, year) {
return 32 - new Date(year, month, 32).getDate();
}
//function to see if the day is a weekday
function isWeekday(year, month, day) {
var currentDay = new Date(year, month, day).getDay();
console.log("day: " + currentDay);
return currentDay != 0 && currentDay != 6;
}
//get the number of working days in a month
function getWeekdaysInMonth(month, year) {
var weekdays = 0;
for (var i = 0; i < daysInMonth; i++) {
if (isWeekday(year, month, i + 1)) weekdays++;
}
return weekdays;
}
var daysInMonth = getDaysInMonth(currentMonth, currentYear);
var numberOfWeekdays = getWeekdaysInMonth(currentMonth, currentYear);
//displaying the number of Working days in a month
workingDaysEl = document.getElementById("working-days");
numberOfWeekdays = numberOfWeekdays - 1; //hardcoding for the Juneteenth holiday in June;
workingDaysEl.textContent = numberOfWeekdays;
daysEl = document.getElementById("days");
dayEl = document.querySelectorAll(".day");
init();
//Event listener function to capture days in/not in the office
daysEl.addEventListener("click", function (event) {
//getting the element to know which day was clicked
var element = event.target;
state = "";
number = "";
if (element.matches(".day")) {
//getting the elements data attributes
state = element.getAttribute("data-state");
number = element.getAttribute("data-number");
if (
isWeekday(currentYear, currentMonth, number) &&
state !== null &&
number !== null
) {
//if the day is not clicked (state = off), set the state to on and change the color to indicate day has been selected
//increment days in office and update the local storage
if (element.dataset.state === "off") {
daysInOffice++;
element.dataset.state = "on";
element.classList.add("has-background-warning");
storedDays[number - 1].ststate = "on";
setStorage();
}
//if the day is clicked (state = on), set the state to off and change the color to indicate day has been unselected
//decrement days in office and update the local storage
else if (daysInOffice > 0) {
daysInOffice--;
element.dataset.state = "off";
element.classList.remove("has-background-warning");
storedDays[number - 1].ststate = "off";
setStorage();
}
renderNumbers();
}
}
});
//function to display Days in office and the calculation and display of Percentage in Office
function renderNumbers() {
officeDaysEl = document.getElementById("office-days");
officeDaysEl.textContent = daysInOffice;
percentageEl = document.getElementById("percentage");
percentageInOffice = ((daysInOffice / numberOfWeekdays) * 100).toFixed(1);
percentageEl.textContent = percentageInOffice + " %";
}
// function to set localStorage
function setStorage() {
localStorage.setItem("storedDays", JSON.stringify(storedDays));
}
//function to display the month from local storage
function renderMonth() {
var currentState = "";
var currentNumber = 0;
for (i = 0; i < dayEl.length; i++) {
currentState = dayEl[i].getAttribute("data-state");
currentNumber = dayEl[i].getAttribute("data-number");
if (currentState !== null && currentNumber !== null) {
for (j = 0; j < storedDays.length; j++) {
if (storedDays[j].stday == currentNumber) {
dayEl[i].dataset.state = storedDays[j].ststate;
if (storedDays[j].ststate == "on") {
dayEl[i].classList.add("has-background-warning");
daysInOffice++;
}
}
}
}
}
renderNumbers();
}
//initial function to get info from local storage and to set local storage if it does not exist
function init() {
// Get stored days from localStorage
var storedDay;
var storedMonth = JSON.parse(localStorage.getItem("storedDays"));
// If days were retrieved from localStorage, update the storedDays array to it
if (storedMonth !== null) {
console.log("not null");
storedDays = storedMonth;
} else {
console.log("null");
for (i = 1; i <= daysInMonth; i++) {
storedDay = { stday: i, ststate: "off" };
storedDays.push(storedDay);
}
setStorage();
}
renderMonth();
}