-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclock.js
212 lines (184 loc) · 6.85 KB
/
clock.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
const path = require('path');
const {Modes} = require(path.join(__dirname, 'modes.js'));
const {Once} = require(path.join(__dirname, 'once.js'));
const {Stopwatch} = require(path.join(__dirname, 'stopwatch.js'));
const {Countdown} = require(path.join(__dirname, 'countdown.js'));
var modes = new Modes();
var stopwatch = new Stopwatch();
var countdown = new Countdown();
global.instance = this;
var animationRunning = false; // Process / Suspend ticks
var setDisplay = function(){};
/*[ Helper: Avoid jQuery ]*******************************************
* Searches the HTML for a class or id
* @param {String} search string representation of the HTML query
* @return {Object}
*/
function $(search){
return document.querySelector(search);
}
/*[ HTML: Time ]*****************************************************
* Returns an HTML string with the current time
* @return {String}
*/
function updateTime() {
var time = new Date();
var amt = "am";
var hrs = time.getHours().toString();
var min = time.getMinutes().toString();
var sec = time.getSeconds().toString();
var ndx = time.getSeconds() % 2;
if (time.getHours() > 12) {
amt = "pm";
hrs = (time.getHours()-12).toString();
}
if (hrs == "0") hrs = "12";
if (min.length == 1) min = '0' + min;
if (sec.length == 1) sec = '0' + sec;
return `${hrs}<span class="accent${ndx}">:</span>${min}<span class="accent${ndx}">.</span>${sec} <span class="${amt}">${amt}</span>`;
}
/*[ HTML: Date ]*****************************************************
* Returns an HTML string with the current date
* @return {String}
*/
function updateDate() {
var date = new Date();
var mth = (date.getMonth()+1).toString();
var day = date.getDate().toString();
var yer = date.getFullYear().toString();
var ndx = date.getSeconds() % 2;
return `${mth}<span class="accent${ndx}">-</span>${day}<span class="accent${ndx}">-</span>${yer}`;
}
/*[ Transition: Promises ]*******************************************
* Transition an objects opacity
* @param {Number} op The opacity you want to transition to
* @param {Number} delay Wait x microseconds after the animation
* @return {Promise}
*/
function transition(op, delay=0) {
var ele = $('#mainContent');
var set = (op == 1) ? [1, 0] : [0, 1];
return new Promise(function(resolve) {
ele.velocity({opacity: set}, {
duration: 250,
complete: function() {
setTimeout(function() {
resolve(ele);
}, 250 + delay);
}
});
});
}
/*[ Transition: Change the display ]*********************************
* Execute a function that changes the on-screen display in a transition
* @param {Number} fn Function to execute
*/
function mainTransition(fn) {
var fnSave = setDisplay;
if (!animationRunning) {
animationRunning = true;
$('#mainContent').style.cursor = "wait";
$('#footer').style.cursor = "wait";
transition(0).then(function(ele) {
setDisplay = fn;
transition(1, 1500).then(function() {
transition(0).then(function(ele) {
setDisplay = fnSave;
transition(1);
animationRunning = false;
$('#mainContent').style.cursor = "pointer";
$('#footer').style.cursor = "pointer";
});
})
})
}
}
/*[ Transition: Change the mode ]************************************
* Fancy Transition functions to use when changing display modes
*/
function changeMode() {
var mainContent = $('#mainContent');
var footer = $('#footer');
var footerContent = $('#footerContent');
// Animations & Change the mode
var hideFooter = new Once(function() {
mainContent.velocity({lineHeight: ["120px", "90px"]}, {duration:250});
footer.velocity({top: ["120px", "90px"]}, {delay:250, duration:250});
footerContent.velocity({left: ["-340px", "0px"]}, {duration:250, complete: function() { modes.next(); }});
});
var showFooter = new Once(function() {
modes.next();
mainContent.velocity({lineHeight: ["90px", "120px"]}, {duration:250});
footer.velocity({top: ["90px", "120px"]}, {delay:250, duration:250});
footerContent.velocity({left: ["0px", "-340px"]}, {delay:250, duration:250});
});
var changeFooter = new Once(function() {
footerContent.velocity({left: ["340px", "0px"]}, {
delay:250,
duration:250,
complete: function() {
modes.next();
footerContent.velocity({left: ["0px", "340px"]}, { delay:250, duration:250 });
}
});
});
// Preview the next mode index (don't change it yet)
var index = modes.previewNextIndex();
// What animation to use
var once = changeFooter;
if (index == 1) once = showFooter;
if (index == 0) once = hideFooter;
// Execute
mainTransition(function() {
once.exec();
mainContent.innerHTML = `<span class="modes">${modes.name(index)}</span>`;
footerContent.innerHTML = modes.display()();
});
}
/*[ Start Here ]******************************************/
document.addEventListener('DOMContentLoaded', function() {
var mainContent = $('#mainContent');
var footer = $('#footer');
var footerContent = $('#footerContent');
// Add Display Modes
modes.addMode([
{
name: "Stopwatch",
reset: function() { },
click: function(ev) { stopwatch.click(ev) },
display: function() { return stopwatch.display() }
},{
name: "Countdown",
reset: function() { },
click: function(ev) { countdown.click(ev) },
display: function() { return countdown.display() }
}
]);
// Display function, What to display on-screen
setDisplay = function() {
mainContent.innerHTML = updateTime();
footerContent.innerHTML = modes.display()();
}
// Listener : Update the date
mainContent.addEventListener("click", function(){
if (!animationRunning)
mainTransition(function() {
mainContent.innerHTML = updateDate();
footerContent.innerHTML = modes.display()();
});
});
// Listener : Change the Display Mode
mainContent.addEventListener("contextmenu", function(ev){
ev.preventDefault();
if (!animationRunning)
changeMode();
});
// Listener : Footer Clicks
footer.addEventListener("mouseup", function(ev){
ev.preventDefault();
if (!animationRunning)
modes.click()(ev);
});
// Update the display every ms
setInterval(function() { setDisplay() }, 1);
}, false);