-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathbars.html
246 lines (208 loc) · 9.76 KB
/
bars.html
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>1440 Clock</title>
<link rel="shortcut icon" href="favicon.ico" />
<link
href="https://fonts.googleapis.com/css?family=Source+Code+Pro:400,700&display=swap"
rel="stylesheet"
/>
<style>
body {
background-color: black;
color: white;
font-family: 'Source Code Pro', monospace;
letter-spacing: 2px;
margin: 0px;
}
.hour-prog-bar-wrap {
margin-bottom: 0px;
width: 100%;
}
.hour-prog-bar-wrap .hour-prog-bar-label {
float: left;
font-size: 10pt;
margin-left: 10px;
margin-top: 5px;
text-align: left;
}
.hour-prog-bar-wrap .hour-prog-bar {
background-color: #0e3a5c;
width: 100%;
}
.hour-prog-bar-wrap .hour-prog-bar-fill-complete {
background-color: black; /* #346388 */
height: 30px;
width: 100%;
}
</style>
</head>
<body>
<div id="clock"></div>
<script>
// Consts ----------------------------------------------------------
const d = document;
const w = window;
// Time constants
const HOURS_IN_DAY = 24;
const MINS_IN_HOUR = 60;
const SECS_IN_MIN = 60;
const MS_IN_SECS = 1000;
// User settings ---------------------------------------------------
// Gather user settings from the query string
const getIntQuerySetting = (key, defaultVal = 0) => {
const match = location.search.match(
new RegExp(`\\b(${key})=(\\d+)\\b`),
'i',
);
return match ? parseInt(match[2]) : defaultVal;
};
// Starting hour, default 0
const START_HOUR = getIntQuerySetting('sh');
// How many hours to show. Can be used to show partial days, e.g.,
// working hours. Default and max value is 24.
const rawHoursToDisplay = getIntQuerySetting('dh', HOURS_IN_DAY);
const HOURS_TO_DISPLAY =
rawHoursToDisplay <= HOURS_IN_DAY
? rawHoursToDisplay
: HOURS_IN_DAY;
// Show milliseconds in clock
const SHOW_MS = getIntQuerySetting('ms');
// How often to update the clock, in milliseconds
const DISPLAY_UPDATE_INTERVAL = SHOW_MS ? 100 : 1000;
// Helper functions ------------------------------------------------
// Prefix targetNum with zeros up to the specified digits
const zeroPad = (targetNum, digits = 2) =>
'0'.repeat(digits - String(targetNum).length) + targetNum;
// Shift given hour by start hour wrapping if too large
const shiftHours = h => (h + START_HOUR) % HOURS_IN_DAY;
// Calculate the vertical margins of an element
const getElementVertMarginHeight = el => {
var styles = window.getComputedStyle(el);
return (
parseFloat(styles['marginTop']) +
parseFloat(styles['marginBottom'])
);
};
// Get the total height of an element, including the margins
const getElementTotalHeight = el =>
Math.ceil(getElementVertMarginHeight(el) + el.offsetHeight);
// Clock -----------------------------------------------------------
// Create the HTML for a single hour progress bar
const createHourBar = hour => {
// Create the progress bar filler
const hourProgBarFill = d.createElement('div');
hourProgBarFill.className = 'hour-prog-bar-fill-complete';
// Create the progress bar itself, append the filler
const hourProgBar = d.createElement('div');
hourProgBar.className = 'hour-prog-bar';
hourProgBar.appendChild(hourProgBarFill);
// Create the label with the hour
const hourProgBarLabel = d.createElement('div');
hourProgBarLabel.className = 'hour-prog-bar-label';
hourProgBarLabel.innerText = zeroPad(hour);
// Create the hour's wrapper, append the label and prog bar
const hourProgBarWrap = d.createElement('div');
hourProgBarWrap.id = 'hour-' + zeroPad(hour);
hourProgBarWrap.className = 'hour-prog-bar-wrap';
hourProgBarWrap.appendChild(hourProgBarLabel);
hourProgBarWrap.appendChild(hourProgBar);
return hourProgBarWrap;
};
// Generate the clock HTML and add it to the DOM
const generateClock = targetElQuery => {
const targetEl = d.querySelector(targetElQuery);
for (let h = 0; h < HOURS_TO_DISPLAY; ++h) {
targetEl.appendChild(createHourBar(shiftHours(h)));
}
};
// Update the clock
const updateClock = date => {
const hours = date.getHours();
const mins = date.getMinutes();
const secs = date.getSeconds();
const ms = date.getMilliseconds();
for (let h = 0; h < HOURS_TO_DISPLAY; ++h) {
const shiftedHours = shiftHours(h);
const hourProgBarFill = d.querySelector(
`#hour-${zeroPad(
shiftedHours,
)} .hour-prog-bar-fill-complete`,
);
const hourProgBarLabel = d.querySelector(
`#hour-${zeroPad(shiftedHours)} .hour-prog-bar-label`,
);
// Past - Start by assuming the hour's 100% over
if (shiftedHours < hours) {
hourProgBarFill.style.width = '100%';
hourProgBarLabel.innerText = zeroPad(shiftedHours);
hourProgBarLabel.style.color = 'darkgrey';
hourProgBarLabel.style.fontWeight = '400';
}
// Present - If it's the current hour, calculate the percent
// of the hour that's complete
else if (shiftedHours === hours) {
const fractionOfSec = ms / MS_IN_SECS;
const fractionOfMin =
(secs + fractionOfSec) / SECS_IN_MIN;
const fractionOfHour =
(mins + fractionOfMin) / MINS_IN_HOUR;
const tenthOfSec = Math.floor(ms / 100);
const percentHourOver = fractionOfHour * 100;
hourProgBarFill.style.width = percentHourOver + '%';
// Set the label to the current time
hourProgBarLabel.innerText = `${zeroPad(
shiftedHours,
)}:${zeroPad(mins)}:${zeroPad(secs)}`;
hourProgBarLabel.style.color = 'white';
hourProgBarLabel.style.fontWeight = '700';
if (SHOW_MS) {
hourProgBarLabel.innerText += `.${tenthOfSec}`;
}
}
// Future - If it's a future hour, it's completely full
else if (shiftedHours > hours) {
hourProgBarFill.style.width = '0';
hourProgBarLabel.innerText = zeroPad(shiftedHours);
hourProgBarLabel.style.color = 'lightgrey';
hourProgBarLabel.style.fontWeight = '400';
}
// Set the bar height such that the clock height completely
// fills the screen without getting smashed
const bodyVertMargins = getElementVertMarginHeight(
d.querySelector('body'),
);
const hourProgBarFillVertMargins = getElementVertMarginHeight(
hourProgBarFill,
);
const progBarLabelTotalHeight = getElementTotalHeight(
hourProgBarLabel,
);
const newProgBarHeight =
(w.innerHeight -
bodyVertMargins -
HOURS_TO_DISPLAY * hourProgBarFillVertMargins) /
HOURS_TO_DISPLAY;
hourProgBarFill.style.height =
newProgBarHeight >= progBarLabelTotalHeight
? newProgBarHeight
: progBarLabelTotalHeight;
}
};
// Main ------------------------------------------------------------
// Generate clock HTML
generateClock('#clock');
// Update display immediately, then at a regular interval
const updateDisplay = () => {
const date = new Date();
updateClock(date);
};
updateDisplay();
setInterval(
requestAnimationFrame.bind(this, updateDisplay),
DISPLAY_UPDATE_INTERVAL,
);
</script>
</body>
</html>