-
Notifications
You must be signed in to change notification settings - Fork 1
/
progressbar.js
115 lines (99 loc) · 3.86 KB
/
progressbar.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
const TOTAL_KEY = "total";
const PROGRESS_KEY = "progress";
const BAR_COLOR_KEY = "barColor";
document.addEventListener("DOMContentLoaded", function () {
let total = parseInt(localStorage.getItem(TOTAL_KEY)) || 1;
let progress = parseInt(localStorage.getItem(PROGRESS_KEY)) || 0;
let barColor = localStorage.getItem(BAR_COLOR_KEY) || "#4CAF50";
const progressContainer = document.getElementById("progress-container");
const progressBar = document.getElementById("progress-bar");
const countDisplay = document.getElementById("count-display");
const totalInput = document.getElementById("total-input");
const contextMenu = document.getElementById("context-menu");
const barColorPicker = document.getElementById("bar-color-picker");
const updateProgressBar = () => {
const progressPercentage = (progress / total) * 100;
progressBar.style.width = progressPercentage + "%";
};
const updateCountDisplay = () => {
countDisplay.textContent = progress + " / " + total;
};
const updateBarColor = () => progressBar.style.backgroundColor = barColor;
const resetProgress = () => {
updateProgressBar();
updateCountDisplay();
};
const setTotal = () => {
const newTotal = parseInt(totalInput.value.trim());
if (!isNaN(newTotal) && newTotal > 0 && newTotal !== total) {
total = newTotal;
localStorage.setItem(TOTAL_KEY, total);
totalInput.value = total;
resetProgress();
}
};
const resetBarColor = () => {
barColor = barColorPicker.value = "#4CAF50";
localStorage.setItem(BAR_COLOR_KEY, barColor);
updateBarColor();
};
const decreaseProgress = () => {
if (progress > 0) {
progress--;
localStorage.setItem(PROGRESS_KEY, progress);
resetProgress();
}
};
const increaseProgress = () => {
if (progress < total) {
progress++;
localStorage.setItem(PROGRESS_KEY, progress);
resetProgress();
}
};
const handleClick = (event) => {
const clickPosition = event.clientX - progressContainer.offsetLeft;
const third = progressContainer.clientWidth / 3;
if (clickPosition < third) {
// click left of the progress bar
decreaseProgress();
} else if (clickPosition > 2 * third) {
// click right of the progress bar
increaseProgress();
} else {
// click middle of the progress bar
totalInput.style.display = "block";
totalInput.focus();
}
};
const handleRightClick = (event) => {
event.preventDefault(); // prevent default right click menu
contextMenu.style.display = "block";
contextMenu.style.left = event.pageX + "px";
contextMenu.style.top = event.pageY + "px";
};
const handlBarColor = () => {
barColor = barColorPicker.value;
localStorage.setItem(BAR_COLOR_KEY, barColor);
updateBarColor();
};
// Attach event listeners
progressContainer.addEventListener("click", handleClick);
progressContainer.addEventListener("contextmenu", handleRightClick);
document.addEventListener("click", (event) => contextMenu.style.display = "none");
totalInput.addEventListener("blur", function () {
setTotal();
this.style.display = "none";
});
document.getElementById("reset-progress").addEventListener("click", () => {
progress = 0;
localStorage.setItem(PROGRESS_KEY, progress);
resetProgress();
});
barColorPicker.addEventListener("change", handlBarColor);
document.getElementById("bar-color-reset-btn").addEventListener("click", resetBarColor);
// Initialize display
resetProgress();
updateBarColor();
barColorPicker.value = barColor;
});