-
Notifications
You must be signed in to change notification settings - Fork 64
/
Copy pathclock.js
53 lines (44 loc) · 1.76 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
const moment = require('moment');
const ServerTime = require('../../_common/base/server_time');
const elementInnerHtml = require('../../_common/common_functions').elementInnerHtml;
const applyToAllElements = require('../../_common/utility').applyToAllElements;
const Clock = (() => {
let fncExternalTimer;
const el_clock_selector = '.gmt-clock';
const startClock = () => {
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`;
applyToAllElements(el_clock_selector, (el) => {
elementInnerHtml(el, time_str);
});
showLocalTimeOnHover(el_clock_selector);
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'));
}
});
};
const getLocalTime = (time) => {
const gmt_time_str = time.replaceAll('\n', ' ');
const local_time = moment.utc(gmt_time_str, 'YYYY-MM-DD HH:mm:ss').local();
return local_time.format('YYYY-MM-DD HH:mm:ss Z');
};
return {
startClock,
showLocalTimeOnHover,
getLocalTime,
setExternalTimer: (func) => { fncExternalTimer = func; },
};
})();
module.exports = Clock;