forked from binary-com/binary-static
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclock.js
More file actions
49 lines (39 loc) · 1.48 KB
/
clock.js
File metadata and controls
49 lines (39 loc) · 1.48 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
const moment = require('moment');
const ServerTime = require('../../_common/base/server_time');
const elementInnerHtml = require('../../_common/common_functions').elementInnerHtml;
const getElementById = require('../../_common/common_functions').getElementById;
const Clock = (() => {
let el_clock,
fncExternalTimer;
const startClock = () => {
if (!el_clock) {
el_clock = getElementById('gmt-clock');
}
ServerTime.init(onTimeUpdated);
};
const onTimeUpdated = () => {
const server_time = ServerTime.get();
window.time = server_time;
const time_str = `${server_time.format('YYYY-MM-DD HH:mm:ss')} GMT`;
elementInnerHtml(el_clock, time_str);
showLocalTimeOnHover('#gmt-clock');
if (typeof fncExternalTimer === 'function') {
fncExternalTimer();
}
};
const showLocalTimeOnHover = (selector) => {
document.querySelectorAll(selector || '.date').forEach((el) => {
const gmt_time_str = el.textContent.replace('\n', ' ');
const local_time = moment.utc(gmt_time_str, 'YYYY-MM-DD HH:mm:ss').local();
if (local_time.isValid()) {
el.setAttribute('data-balloon', local_time.format('YYYY-MM-DD HH:mm:ss Z'));
}
});
};
return {
startClock,
showLocalTimeOnHover,
setExternalTimer: (func) => { fncExternalTimer = func; },
};
})();
module.exports = Clock;