-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbattery.js
141 lines (114 loc) · 3.71 KB
/
battery.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
const DEFAULT_CAPACITY_MINS = 90;
const DEFAULT_BREAK_DURATION = 10;
const FULL_BATTERY = 100;
const MODE_REST = 0;
const MODE_DRAIN = 1;
const MODE_CHARGE = 2;
let batteryMode = MODE_REST;
let currentLevel = FULL_BATTERY;
let breakDurationMinutes = DEFAULT_BREAK_DURATION;
let capacityMinutes;
let drainInterval;
let chargeInterval;
const red = "linear-gradient(40deg, rgba(231,15,15,1) 0%, rgba(241,77,77,1) 100%)";
const green = "linear-gradient(40deg, rgba(19,231,59,1) 0%, rgba(0,255,155,1) 100%)";
const yellow = "linear-gradient(259deg, rgba(231,226,15,1) 0%, rgba(221,188,17,1) 100%)";
const blue = null; // we set the gradient in the css
function setMode(newMode) {
batteryMode = newMode;
if (newMode == MODE_DRAIN) {
// DRAINING
document.getElementById('capacity-slider').disabled = true;
document.getElementById('break-duration').disabled = false;
updateBatteryColor();
clearAllIntervals();
drainInterval = window.setInterval(function() {
drainOnePercent();
}, calcDrainIntervalTime());
} else if (newMode == MODE_CHARGE) {
// CHARGING
document.getElementById('capacity-slider').disabled = false;
document.getElementById('break-duration').disabled = true;
updateBatteryColor();
clearAllIntervals();
chargeInterval = window.setInterval(function() {
rechargeOnePercent();
}, calcChargeIntervalTime());
} else {
// OFF
clearAllIntervals();
document.getElementById('capacity-slider').disabled = false;
document.getElementById('break-duration').disabled = false;
}
}
function clearAllIntervals() {
if (drainInterval) {
clearInterval(drainInterval);
}
if (chargeInterval) {
clearInterval(chargeInterval);
}
}
function setBatteryColor(color) {
document.getElementById('battery-level').style.background = color;
}
function rechargeOnePercent() {
setBatteryLevel(currentLevel + 1)
}
function drainOnePercent() {
setBatteryLevel(currentLevel - 1)
}
function setBatteryLevel(newLevel) {
currentLevel = newLevel;
if (currentLevel < 0) {
currentLevel = 0;
} else if (currentLevel > 100) {
currentLevel = 100;
}
updateBatteryColor();
let levelElement = document.getElementById('battery-level');
levelElement.style.width = currentLevel + '%';
}
function updateBatteryColor() {
if (batteryMode == MODE_CHARGE) {
setBatteryColor(blue);
document.getElementById('battery-level').classList.add('charging');
} else {
document.getElementById('battery-level').classList.remove('charging');
if (currentLevel < 25) {
setBatteryColor(red);
} else if (currentLevel < 60) {
setBatteryColor(yellow);
} else {
setBatteryColor(green);
}
}
}
function setCapacitySlider(val) {
capacityMinutes = val;
document.getElementById('capacity-slider').value = capacityMinutes;
}
function updateCapacitySlider() {
capacityMinutes = document.getElementById('capacity-slider').value;
document.getElementById('capacity-label').innerHTML = `${capacityMinutes} minutes`;
}
function setBreakDuration() {
breakDurationMinutes = document.getElementById('break-duration').value;
}
function calcDrainIntervalTime() {
let capacityMs = capacityMinutes * 1000 * 60;
return capacityMs / currentLevel;
}
function calcChargeIntervalTime() {
let percentToCharge = 100 - currentLevel;
let msOfBreak = breakDurationMinutes * 1000 * 60;
return msOfBreak / percentToCharge;
}
function initializeBattery() {
document.getElementById('break-duration').value = DEFAULT_BREAK_DURATION;
setBatteryLevel(FULL_BATTERY);
setCapacitySlider(DEFAULT_CAPACITY_MINS);
updateCapacitySlider();
document.querySelector('#capacity-slider').addEventListener('input', updateCapacitySlider, false);
}
initializeBattery();