-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathscheduleBar.js
More file actions
321 lines (180 loc) · 7.39 KB
/
scheduleBar.js
File metadata and controls
321 lines (180 loc) · 7.39 KB
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
/****************************************************************************
* scheduleBar.js
* openacousticdevices.info
* November 2019
*****************************************************************************/
'use strict';
/* global window, document */
const ui = require('./ui.js');
const timeHandler = require('./timeHandler.js');
const schedule = require('./schedule/schedule.js');
const constants = require('./constants.js');
const uiSun = require('./schedule/uiSun.js');
const timeCanvas = document.getElementById('time-canvas');
const timeContext = timeCanvas.getContext('2d');
const labelCanvas = document.getElementById('label-canvas');
const labelContext = labelCanvas.getContext('2d');
let scaleFactor;
const sunCanvas = document.getElementById('sun-canvas');
let clickCallback;
let selectedPeriod = null;
/* Function to rescale */
function rescale (canvas) {
scaleFactor = 1;
if (Object.prototype.hasOwnProperty.call(window, 'devicePixelRatio')) {
if (window.devicePixelRatio > 1) {
scaleFactor = window.devicePixelRatio;
}
}
if (scaleFactor > 1) {
canvas.width = canvas.width * scaleFactor;
canvas.height = canvas.height * scaleFactor;
canvas.style.width = canvas.width / scaleFactor + 'px';
canvas.style.height = canvas.height / scaleFactor + 'px';
}
}
function drawPeriod (startMins, endMins, timeCanvas) {
/* width / 1440 minutes */
const recX = startMins * timeCanvas.width / constants.MINUTES_IN_DAY;
const recLen = (endMins - startMins) * timeCanvas.width / constants.MINUTES_IN_DAY;
timeContext.fillRect(Math.round(recX), 0, Math.round(recLen), timeCanvas.height);
}
function updateCanvas () {
let timePeriods = schedule.getTimePeriods();
const timeZoneMode = ui.getTimeZoneMode();
if (timeZoneMode !== constants.TIME_ZONE_MODE_UTC) {
timePeriods = timeHandler.shiftTimePeriods(timePeriods, false);
}
const currentTimeDate = new Date();
timeContext.clearRect(0, 0, timeCanvas.width, timeCanvas.height);
if (uiSun.usingSunSchedule()) {
uiSun.updateSunUI();
} else {
for (let i = 0; i < timePeriods.length; i++) {
const startMins = timePeriods[i].startMins;
let endMins = timePeriods[i].endMins;
endMins = endMins === 0 ? constants.MINUTES_IN_DAY : endMins;
if (selectedPeriod !== null && (selectedPeriod.startMins === startMins && selectedPeriod.endMins === endMins)) {
timeContext.fillStyle = '#007BFF';
} else {
timeContext.fillStyle = '#FF0000';
}
if (startMins > endMins) {
drawPeriod(startMins, constants.MINUTES_IN_DAY, timeCanvas);
drawPeriod(0, endMins, timeCanvas);
} else if (startMins === endMins) {
drawPeriod(0, constants.MINUTES_IN_DAY, timeCanvas);
} else {
drawPeriod(startMins, endMins, timeCanvas);
}
}
}
/* 6am, midday and 6pm markers */
if (ui.isNightMode()) {
timeContext.fillStyle = '#FFFFFF';
} else {
timeContext.fillStyle = '#000000';
}
timeContext.fillRect(Math.round(0.25 * timeCanvas.width), 0, 1 * scaleFactor, timeCanvas.height);
timeContext.fillRect(Math.round(0.5 * timeCanvas.width), 0, 1 * scaleFactor, timeCanvas.height);
timeContext.fillRect(Math.round(0.75 * timeCanvas.width), 0, 1 * scaleFactor, timeCanvas.height);
let currentMins = (currentTimeDate.getUTCHours() * constants.MINUTES_IN_HOUR) + currentTimeDate.getUTCMinutes();
if (timeZoneMode !== constants.TIME_ZONE_MODE_UTC) {
currentMins += timeHandler.getTimeZoneOffset();
}
let currentX = currentMins * timeCanvas.width / constants.MINUTES_IN_DAY;
/* Wrap time around midnight at both ends */
currentX = currentX % timeCanvas.width;
currentX = currentX < 0 ? currentX + timeCanvas.width : currentX;
timeContext.fillStyle = '#00AF00';
timeContext.fillRect(Math.floor(currentX), 0, 2 * scaleFactor, timeCanvas.height);
}
exports.updateCanvas = updateCanvas;
/* Regularly update time period canvas so green line reflects current time */
function updateCanvasTimer () {
updateCanvas();
setTimeout(updateCanvasTimer, 60000);
}
/* Update which period is selected when schedule bar is clicked */
function updateSelectedPeriod (event) {
let timePeriods = schedule.getTimePeriods();
/* If there's only one possible time period and it covers the entire length of the schedule, don't bother with the full check */
if (timePeriods.length === 1 && timePeriods[0].startMins === timePeriods[0].endMins) {
const selectedIndex = 0;
if (clickCallback) {
clickCallback(selectedIndex);
}
return;
}
const rect = timeCanvas.getBoundingClientRect();
const clickMins = (event.clientX - rect.left) / timeCanvas.width * constants.MINUTES_IN_DAY;
if (ui.getTimeZoneMode() !== constants.TIME_ZONE_MODE_UTC) {
timePeriods = timeHandler.shiftTimePeriods(timePeriods, false);
}
let selectedIndex = -1;
for (let i = 0; i < timePeriods.length; i++) {
const startMins = timePeriods[i].startMins;
const endMins = timePeriods[i].endMins;
if (startMins > endMins) {
if ((clickMins >= startMins && clickMins < constants.MINUTES_IN_DAY) || (clickMins >= 0 && clickMins < endMins)) {
selectedIndex = i;
}
} else {
if (clickMins >= startMins && clickMins < endMins) {
selectedIndex = i;
}
}
}
if (clickCallback) {
clickCallback(selectedIndex);
}
}
/* Set clicked period to specific index */
exports.setSelectedPeriod = (period) => {
selectedPeriod = period;
updateCanvas();
};
function clearSelectedPeriod () {
selectedPeriod = null;
updateCanvas();
};
exports.clearSelectedPeriod = clearSelectedPeriod;
/* Draw labels below time period canvas */
function drawTimeLabels () {
const fontSize = 0.32 * timeCanvas.height;
labelContext.clearRect(0, 0, labelCanvas.width, labelCanvas.height);
labelContext.font = fontSize + 'pt Helvetica';
if (ui.isNightMode()) {
labelContext.fillStyle = '#FFFFFF';
} else {
labelContext.fillStyle = '#000000';
}
labelContext.fillText('00:00', 0, fontSize);
labelContext.fillText('06:00', 0.25 * timeCanvas.width, fontSize);
labelContext.fillText('12:00', 0.5 * timeCanvas.width, fontSize);
labelContext.fillText('18:00', 0.751 * timeCanvas.width, fontSize);
labelContext.fillText('24:00', timeCanvas.width, fontSize);
}
exports.drawTimeLabels = drawTimeLabels;
exports.prepareScheduleCanvas = (callback) => {
clickCallback = callback;
timeCanvas.addEventListener('click', (event) => {
if (!uiSun.usingSunSchedule()) {
updateSelectedPeriod(event);
}
});
/* Rescale for resolution of screen */
rescale(timeCanvas);
rescale(labelCanvas);
rescale(sunCanvas);
/* Draw labels below timeline */
drawTimeLabels();
/* Start recursive loop which keep canvas up to date */
updateCanvasTimer();
};
exports.setSchedule = (timePeriods) => {
let tps = timePeriods;
tps = timeHandler.sortPeriods(tps);
schedule.setTimePeriods(tps);
updateCanvas();
};