-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlivebar.js
460 lines (405 loc) · 12.6 KB
/
livebar.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
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
(function () {
// styling
let backgroundColor = document.currentScript.getAttribute(
'data-background-color'
);
let buttonColor = document.currentScript.getAttribute('data-button-color');
let buttonTextColor = document.currentScript.getAttribute(
'data-button-text-color'
);
let textColor = document.currentScript.getAttribute('data-text-color');
let layout = document.currentScript.getAttribute('data-layout');
if (!layout) {
layout = 'header';
} // default to header
// content
let buttonText = document.currentScript.getAttribute('data-button-text');
let headerText = document.currentScript.getAttribute('data-header-text');
let dismissable = document.currentScript.getAttribute('data-dismissable');
// services
let servicesNum = 0;
Array.from(document.currentScript.attributes).map(function (attribute, idx) {
if (attribute.name.includes('day-of-week')) {
servicesNum++;
}
});
let allServices = [];
for (let i = 0; i < servicesNum; i++) {
currentIndex = i + 1;
allServices.push(
getCountdownDataForService(
parseInt(
document.currentScript.getAttribute(
`data-service-${currentIndex}-day-of-week`
)
),
document.currentScript.getAttribute(
`data-service-${currentIndex}-hours`
),
document.currentScript.getAttribute(
`data-service-${currentIndex}-minutes`
),
document.currentScript.getAttribute(
`data-service-${currentIndex}-duration-minutes`
)
)
);
}
// timing & links
let liveUrl = document.currentScript.getAttribute('data-live-url');
if (!liveUrl) {
liveUrl = 'https://livebar.church/no-url.html';
}
let loadLivebarImmediately = document.currentScript.getAttribute(
'data-load-livebar-immediately'
);
// define style
let style = document.createElement('style');
style.type = 'text/css';
style.innerHTML = `
.livebar-colors {
color: ${textColor};
background: ${backgroundColor};
}
.livebar-header {
padding: 20px;
z-index: 1000;
position: relative;
}
.livebar-close {
font-weight: 700;
font-family: Tahoma;
cursor: pointer;
}
.livebar-header .livebar-text {
padding: 10px;
float: left;
}
.livebar-header .livebar-button {
float: right;
}
.livebar-header .livebar-close {
padding: 10px 14px;
float: right;
}
.livebar-sidebar .livebar-close {
position: absolute;
right: 10px;
top: 10px;
font-size: 12px;
}
.livebar-header .livebar-close:hover {
filter: brightness(150%);
}
.livebar-sidebar .livebar-text {
padding: 0px 0px 10px 0px;
max-width: 200px;
}
.livebar-sidebar .livebar-button {
display: block;
text-align: center;
}
.livebar-sidebar {
position: fixed;
top: 100px;
right:0;
padding: 20px;
-webkit-transition-duration: 0.3s;
-moz-transition-duration: 0.3s;
-o-transition-duration: 0.3s;
transition-duration: 0.3s;
border-top-left-radius: 7px;
border-bottom-left-radius: 7px;
z-index: 1000;
}
.livebar-text {
font-size: 16px;
font-weight: 500;
font-family: 'Helvetica', 'Arial';
color: ${textColor} !important;
}
.livebar-button {
padding: 10px 20px;
background: ${buttonColor};
border-radius: 5px;
color: ${buttonTextColor} !important;
font-size: 15px;
text-decoration: none;
font-family: 'Helvetica', 'Arial';
}
.livebar-button:hover {
filter: brightness(85%);
color: ${buttonTextColor} !important;
text-decoration: none;
}
@media only screen and (max-width: 800px) {
.livebar-header .livebar-close {
position: absolute;
right: 10px;
top: 10px;
font-size: 12px;
}
.livebar-header .livebar-text {
float: none;
text-align: center;
}
.livebar-header .livebar-button {
float: none;
display: block;
text-align: center;
}
.livebar-sidebar {
position: fixed;
left: 0;
bottom: 0;
width: 100%;
text-align: center;
top: initial;
border-radius: 0px;
padding: 0px;
}
.livebar-sidebar .livebar-text {
float: none;
width: 100%;
max-width: 100%;
padding: 20px 20px 0px 20px;
}
.livebar-sidebar .livebar-button {
float: none;
display: block;
text-align: center;
margin: 20px;
}
}
`;
// when loaded
document.addEventListener('DOMContentLoaded', function () {
loadLivebar();
});
function loadLivebar() {
// if existing livebar, don't load
let existingLivebar = document.getElementsByTagName('livebar_container');
if (existingLivebar.length > 0) {
return false;
}
// head and body containers
let livebarHead = document.getElementsByTagName('head')[0];
let livebarBody = document.getElementsByTagName('body')[0];
// add style to head
livebarHead.appendChild(style);
// create livebar and add class to it
let livebarBar = document.createElement('div');
livebarBar.classList.add('livebar_container');
livebarBar.classList.add('livebar-colors');
// select layout
if (layout == 'sidebar') {
livebarBar.classList.add('livebar-sidebar');
} else {
livebarBar.classList.add('livebar-header');
}
// add content to livebar
let livebarText = document.createElement('div');
livebarText.classList.add('livebar-text');
livebarText.innerHTML = headerText;
livebarBar.appendChild(livebarText);
if (dismissable === 'yes') {
let livebarClose = document.createElement('div');
livebarClose.classList.add('livebar-close');
livebarClose.innerHTML = '✕';
livebarBar.appendChild(livebarClose);
}
let livebarButton = document.createElement('a');
livebarButton.classList.add('livebar-button');
livebarButton.id = 'button-text';
livebarButton.target = '_blank';
livebarButton.href = liveUrl;
livebarButton.innerHTML = '...';
livebarBar.appendChild(livebarButton);
let clearFloat = document.createElement('div');
clearFloat.style.clear = 'both';
livebarBar.appendChild(clearFloat);
// prepend livebar to body
closeLivebar(); // close right before inserting to prevent duplicates
livebarBody.insertBefore(livebarBar, livebarBody.firstChild);
// now that the container is added, fetch it
let timerCountdownContainer = document.getElementById('button-text');
// listen for close
if (dismissable == 'yes') {
let livebarClose = document.getElementsByClassName('livebar-close');
livebarClose[0].addEventListener('click', function (e) {
closeLivebar();
});
}
// update the container every 1 second
let timerCountdown = setInterval(function () {
timerCountdownContainer.innerHTML = getCountdownString();
}, 1000);
trackLivebarUsage('loaded');
}
function closeLivebar() {
let livebar_container = document.getElementsByClassName(
'livebar_container'
);
if (livebar_container.length > 0) {
livebar_container[0].style.display = 'none';
}
}
function trackLivebarUsage(action) {
let domain = window.location.hostname;
if (domain && action && domain != 'livebar.church') {
let http = new XMLHttpRequest();
let url = 'https://app.breezechms.com/livebar/ping';
let params = 'domain=' + domain + '&action=' + action;
http.open('POST', url, true);
http.setRequestHeader(
'Content-type',
'application/x-www-form-urlencoded'
); //Send the proper header information along with the request
http.send(params);
}
}
// determines how much time is left or has elapsed relative to `countDownDate`
function getCountdownTime(countDownDate) {
let distance;
// Get today's date and time
let now = new Date().getTime();
// if counting down
if (countDownDate > now) {
distance = countDownDate - now;
// if counting up
} else {
distance = now - countDownDate;
}
// Time calculations for days, hours, minutes and seconds
let days = Math.floor(distance / (1000 * 60 * 60 * 24));
let hours = Math.floor(
(distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60)
);
let minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
let seconds = Math.floor((distance % (1000 * 60)) / 1000);
// format output and exclude portions of date that aren't needed
let countdownString = '';
if (days) {
countdownString += days + 'd ';
}
if (hours || days) {
countdownString += hours + 'h ';
}
if (minutes || hours || days) {
countdownString += minutes + 'm ';
}
countdownString += seconds + 's ';
return countdownString;
}
function getCountdownDataForService(dayOfWeek, hours, minutes, duration) {
// get now
let timezone = document.currentScript.getAttribute('data-timezone');
let now = timezone ? new Date((new Date()).toLocaleString("en-US", {timeZone: timezone})) : new Date();
let completeThresholdMinutes = 5;
// define default view
let view = 'countdown';
// get next service start time
let nextServiceTimeStarts = new Date();
nextServiceTimeStarts.setDate(
nextServiceTimeStarts.getDate() +
((dayOfWeek + 7 - nextServiceTimeStarts.getDay()) % 7)
);
nextServiceTimeStarts.setHours(hours, minutes, 00);
// get next service end time
let nextServiceTimeEnds = new Date(
nextServiceTimeStarts.getTime() + duration * 60000
);
// get time since service ended
let serviceEndedMinutesAgo =
(now.getTime() - nextServiceTimeEnds.getTime()) / 1000 / 60;
// if now is past the service end time
if (now.getTime() > nextServiceTimeEnds.getTime()) {
// if it's within x minutes, show complete
if (serviceEndedMinutesAgo < completeThresholdMinutes) {
view = 'complete';
// otherwise jump forward a week and show next countdown
} else {
view = 'countdown';
// get next service start time
nextServiceTimeStarts = new Date(
nextServiceTimeStarts.getTime() + 24 * (60000 * 60)
); // jump forward 24 hours and then recalculate based on next day
nextServiceTimeStarts.setDate(
nextServiceTimeStarts.getDate() +
((dayOfWeek + 7 - nextServiceTimeStarts.getDay()) % 7)
);
nextServiceTimeStarts.setHours(hours, minutes, 00);
}
// if now is before end of service, show live
} else if (now.getTime() > nextServiceTimeStarts.getTime()) {
view = 'live';
// default to countdown
} else {
view = 'countdown';
}
let response = {};
response.view = view;
response.nextServiceTimeStarts = nextServiceTimeStarts;
response.nextServiceTimeEnds = nextServiceTimeEnds;
response.timeUntilStart = nextServiceTimeStarts.getTime() - now.getTime();
return response;
}
function pickMostRelevantService(services) {
// if only one service, return it
if (services.length == 1) {
return services[0];
}
// for each service, if a service is live, return it as live trumps all
for (index = 0; index < services.length; index++) {
if (services[index].view == 'live') {
return services[index];
}
}
// get next service to start
let nextServiceTimeToStart;
let nextService = services[0]; // default to first
// for each service, if a service is complete, return it
for (index = 0; index < services.length; index++) {
if (services[index].view == 'complete') {
return services[index];
}
// keep track of what is the next service to start
if (services[index].timeUntilStart) {
nextService = services[index];
nextServiceTimeToStart = services[index].timeUntilStart;
}
}
// default to next service to start
return nextService;
}
function getCountdownString() {
let service = pickMostRelevantService(allServices);
// show output depending on view
switch (service.view) {
case 'countdown': // service has not yet started
return (
buttonText +
' ' +
getCountdownTime(service.nextServiceTimeStarts.getTime())
);
break;
case 'complete': // service recently completed
return (
'Live Finished ' +
getCountdownTime(service.nextServiceTimeEnds.getTime()) +
' Ago'
);
break;
case 'live': // service is live
return (
'Live Now (+' +
getCountdownTime(service.nextServiceTimeStarts.getTime()) +
')'
);
break;
}
}
if (loadLivebarImmediately == 'yes') {
loadLivebar();
}
})();