-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcount-down.js
132 lines (115 loc) · 5.29 KB
/
count-down.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
'use strict';
function Util(){};
Util.setAttributes = function (el, attrs) {
for (var key in attrs) {
el.setAttribute(key, attrs[key]);
}
};
(function () {
var CountDown = function (element) {
this.element = element;
this.labels = this.element.getAttribute('data-labels') ? this.element.getAttribute('data-labels').split(',') : [];
this.intervalId;
// set visible labels
this.setVisibleLabels();
//create countdown HTML
this.createCountDown();
//store time elements
this.days = this.element.getElementsByClassName('js-countdown__value--0')[0];
this.hours = this.element.getElementsByClassName('js-countdown__value--1')[0];
this.mins = this.element.getElementsByClassName('js-countdown__value--2')[0];
this.secs = this.element.getElementsByClassName('js-countdown__value--3')[0];
this.endTime = this.getEndTime();
//init counter
this.initCountDown();
};
CountDown.prototype.setVisibleLabels = function () {
this.visibleLabels = this.element.getAttribute('data-visible-labels') ? this.element.getAttribute('data-visible-labels').split(',') : [];
this.visibleLabels = this.visibleLabels.map(function (label) {
return label.trim();
});
};
CountDown.prototype.createCountDown = function () {
var wrapper = document.createElement("div");
Util.setAttributes(wrapper, {'aria-hidden': 'true', 'class': 'countdown__timer'});
for (var i = 0; i < 4; i++) {
var timeItem = document.createElement("span"),
timeValue = document.createElement("span"),
timeLabel = document.createElement('span');
timeItem.setAttribute('class', 'countdown__item');
timeValue.setAttribute('class', 'countdown__value countdown__value--' + i + ' js-countdown__value--' + i);
timeItem.appendChild(timeValue);
if (this.labels && this.labels.length > 0) {
timeLabel.textContent = this.labels[i].trim();
timeLabel.setAttribute('class', 'countdown__label');
timeItem.appendChild(timeLabel);
}
wrapper.appendChild(timeItem);
}
// append new content to countdown element
this.element.insertBefore(wrapper, this.element.firstChild);
// this.element.appendChild(wrapper);
};
CountDown.prototype.getEndTime = function () {
// get number of remaining seconds
if (this.element.getAttribute('data-timer')) return Number(this.element.getAttribute('data-timer')) * 1000 + new Date().getTime();
else if (this.element.getAttribute('data-countdown')) return Number(new Date(this.element.getAttribute('data-countdown')).getTime());
};
CountDown.prototype.initCountDown = function () {
var self = this;
this.intervalId = setInterval(function () {
self.updateCountDown(false);
}, 1000);
this.updateCountDown(true);
};
CountDown.prototype.updateCountDown = function (bool) {
// original countdown function
// https://gist.github.com/adriennetacke/f5a25c304f1b7b4a6fa42db70415bad2
var time = parseInt((this.endTime - new Date().getTime()) / 1000),
days = 0,
hours = 0,
mins = 0,
seconds = 0;
if (isNaN(time) || time < 0) {
clearInterval(this.intervalId);
this.emitEndEvent();
} else {
days = parseInt(time / 86400);
time = (time % 86400);
hours = parseInt(time / 3600);
time = (time % 3600);
mins = parseInt(time / 60);
time = (time % 60);
seconds = parseInt(time);
}
// hide days/hours/mins if not available
if (bool && days == 0 && this.visibleLabels.indexOf('d') < 0) this.days.parentElement.style.display = "none";
if (bool && days == 0 && hours == 0 && this.visibleLabels.indexOf('h') < 0) this.hours.parentElement.style.display = "none";
if (bool && days == 0 && hours == 0 && mins == 0 && this.visibleLabels.indexOf('m') < 0) this.mins.parentElement.style.display = "none";
this.days.textContent = days;
this.hours.textContent = this.getTimeFormat(hours);
this.mins.textContent = this.getTimeFormat(mins);
this.secs.textContent = this.getTimeFormat(seconds);
};
CountDown.prototype.getTimeFormat = function (time) {
return ('0' + time).slice(-2);
};
CountDown.prototype.emitEndEvent = function (time) {
var event = new CustomEvent('countDownFinished');
this.element.dispatchEvent(event);
};
// Functions calling
window.addEventListener ('load', function () {
//initialize the CountDown objects
/* window.setTimeout(() => {*/
var countDown = document.getElementsByClassName('js-countdown');
if (countDown.length > 0) {
for (var i = 0; i < countDown.length; i++) {
(function (i) {
new CountDown(countDown[i]);
})(i);
}
}
/* }, 1000);*/
});
}());