-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
171 lines (148 loc) · 5.09 KB
/
script.js
File metadata and controls
171 lines (148 loc) · 5.09 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
function addHTMLAttributes(htmlElement, attributes) {
for (let attributeValue in attributes)
htmlElement[attributeValue] = attributes[attributeValue];
return htmlElement;
}
function addChildElements(htmlElement, children) {
children.forEach((child) => {
// checks whether to append the child as a text or an element node
typeof child === "string"
? htmlElement.appendChild(document.createTextNode(child))
: htmlElement.appendChild(child);
});
return htmlElement;
}
/**
* createHTMLElement is a helper function for creating HTML elements
* it takes the elementTagName of HTML element to be created as first
* argument, the second argument is an object of attributes of the element
* while the last argument can either be another HTML element to nest
* within it or a text
* @param {String} elementTagName
* @param {String} Attributes
* @param {...any} children
*/
function createHTMLElement(elementTagName, attributes, ...children) {
let htmlElement = document.createElement(elementTagName);
htmlElement = addHTMLAttributes(htmlElement, attributes);
htmlElement = addChildElements(htmlElement, children);
return htmlElement;
}
class FlipComponent {
constructor(label) {
this._top = createHTMLElement("b", { className: "card__top" }, "");
this._bottom = createHTMLElement("b", { className: "card__bottom" }, "");
this._backBottom = createHTMLElement(
"b",
{ className: "card__bottom" },
""
);
this._back = createHTMLElement(
"b",
{ className: "card__back" },
this._backBottom
);
this._flipCard = createHTMLElement(
"b",
{ className: "flip__card card" },
this._top,
this._bottom,
this._back
);
this._flipCardName = createHTMLElement(
"div",
{ className: "flip__name" },
label
);
const div = createHTMLElement(
"div",
{ className: "flip__wrapper" },
createHTMLElement("div", { className: "left flip__dot" }),
createHTMLElement("div", { className: "right flip__dot" }),
this._flipCard
);
this._flipComponent = createHTMLElement(
"div",
{ className: "flip flip-animate", id: `${label}` },
div,
this._flipCardName
);
this._value = undefined;
}
get create() {
return this._flipComponent;
}
/**
* @param {Number} value
*/
set update(value) {
const paddedValue = `${value}`.padStart(2, "0"),
currentValue = `${this._value}`.padStart(2, "0");
if (value !== this._value) {
if (this._value >= 0) {
this._back.setAttribute("data-value", currentValue);
this._bottom.setAttribute("data-value", currentValue);
}
this._value = value;
this._top.innerText = paddedValue;
this._backBottom.setAttribute("data-value", paddedValue);
this._flipComponent.classList.remove("flip-animate");
this._flipComponent.offsetWidth;
this._flipComponent.classList.add("flip-animate");
}
}
}
const countdown = document.querySelector(".main__countdown");
const dailyFlipCard = new FlipComponent("Days");
const hourlyFlipCard = new FlipComponent("Hours");
const minFlipCard = new FlipComponent("Minutes");
const secFlipCard = new FlipComponent("Seconds");
countdown.append(dailyFlipCard.create);
countdown.append(hourlyFlipCard.create);
countdown.append(minFlipCard.create);
countdown.append(secFlipCard.create);
const getSecondsFromIncrementalTime = (incrementalTime) => incrementalTime % 60;
const getMinutesFromIncrementalTime = (incrementalTime) =>
Math.floor((incrementalTime / 60) % 60);
const getHoursFromIncrementalTime = (incrementalTime) =>
Math.floor((incrementalTime / 3600) % 24);
const getDaysFromIncrementalTime = (incrementalTime) =>
Math.floor(incrementalTime / 86400);
/**
* updateCountDownDisplay takes an incrementalTime as argument
* and converts it to number of days, hours, minutes and secondes
* also updating the filp card component of each of the respective fields
* @param {Number} incrementalTime
*/
function updateCountDownDisplay(incrementalTime) {
dailyFlipCard.update = getDaysFromIncrementalTime(incrementalTime);
hourlyFlipCard.update = getHoursFromIncrementalTime(incrementalTime);
minFlipCard.update = getMinutesFromIncrementalTime(incrementalTime);
secFlipCard.update = getSecondsFromIncrementalTime(incrementalTime);
}
// count variable is increment very 1 second
// this value is then converted into sec/min/hours/days
let incrementalTime = 86400 * 8 + 3600 * 23 + 60 * 55 + 41;
/**
* counter_1 and counter_2 functions both call each other
* leading to a recursive process.
* This approach of having two setTimout method is used to ensure
* that the incrementalTime is decreated exactly after 1 second as opposed
* to using setInterval method which perform the opperation
* any given moment after 1 second
*/
const counter_1 = () => {
updateCountDownDisplay(incrementalTime);
// setTimeout(() => {
// counter_2();
// }, 1000);
};
const counter_2 = () => {
setTimeout(() => {
counter_1();
updateCountDownDisplay(incrementalTime);
}, 0);
return incrementalTime--;
};
// init
counter_1();