-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathMMM-Worldclock.js
202 lines (171 loc) · 4.91 KB
/
MMM-Worldclock.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
/* global Log, Module, moment, config */
/*
* MagicMirror²
* Module: MMM-Worldclock
*
* By eouia
*/
Module.register("MMM-Worldclock", {
// Module config defaults.
defaults: {
timeFormat: "LT", // defined in moment.js format()
style: "left", // where the time could be located; 'top', 'left','right','bottom'
offsetTimezone: null, // or "Europe/Berlin" to get difference from this timezone to each clock.
clocks: [
{
title: "Seoul",
timezone: "Asia/Seoul",
flag: "kr"
}
]
},
// Define required scripts.
getScripts () {
return ["moment.js", "moment-timezone.js"]
},
// Define styles.
getStyles () {
return ["MMM-Worldclock.css"]
},
getCommands (commander) {
if (commander.constructor.name === "TelegramBotCommandRegister") {
return [
{
command: "worldclock",
description: "Tell the time of worldclock",
callback: "TLGBOT_worldclock"
}
]
}
},
TLGBOT_worldclock (command, handler) {
let text = ""
const m = moment()
for (const c in this.config.clocks) {
const clock = this.config.clocks[c]
const title = clock.title
? clock.title
: clock.timezone
if (clock.timezone) {
m.tz(clock.timezone)
} else {
m.local()
}
text += `\`${m.format(this.config.timeFormat)}\``
text += ` in *${title}*\n`
}
text = text
? text
: "I cannot answer now, sorry."
handler.reply("TEXT", text, {parse_mode: "Markdown"})
},
start () {
this.loadCSS()
const self = this
setInterval(() => {
self.updateDom()
}, 1000)
// Set locale.
moment.locale(config.language)
},
clockFormat (c, index) {
const worldWrapper = document.createElement("div")
worldWrapper.className = `world world-${index}`
const clock = moment()
if (c.timezone == null || undefined) {
clock.local()
} else {
clock.tz(c.timezone)
}
let timeFormat
if (c.timeFormat == null || undefined) {
timeFormat = this.config.timeFormat
} else {
timeFormat = c.timeFormat
}
let timeString
timeString = clock.format(timeFormat)
const timeWrapper = document.createElement("div")
timeWrapper.innerHTML = timeString
timeWrapper.className = "time"
// timeWrapper.className = "time bright medium"
const captionWrapper = document.createElement("div")
captionWrapper.className = "caption"
// captionWrapper.className = 'caption small normal'
const zoneWrapper = document.createElement("div")
zoneWrapper.className = "zone"
if (c.title != null) {
zoneWrapper.innerHTML = c.title
} else {
zoneWrapper.innerHTML = c.timezone
}
captionWrapper.appendChild(zoneWrapper)
const gapWrapper = document.createElement("div")
gapWrapper.className = "gap"
let gap = ""
if (this.config.offsetTimezone) {
let ori = "+"
const oclock = moment().tz(this.config.offsetTimezone)
let os = clock.utcOffset() - oclock.utcOffset()
if (os < 0) {
os = oclock.utcOffset() - clock.utcOffset()
ori = "-"
}
const dur = moment.duration(os, "minutes")
gap = ori + moment.utc(dur.asMilliseconds()).format("HH:mm")
} else {
gap = `UTC ${clock.format("Z")}`
}
gapWrapper.innerHTML = gap
captionWrapper.appendChild(gapWrapper)
const mainWrapper = document.createElement("div")
mainWrapper.className = "main"
if (c.flag) {
const flagWrapper = document.createElement("div")
flagWrapper.className = "flag"
const flagIconWrapper = document.createElement("span")
flagIconWrapper.className = "flag-icon flag-icon-squared"
flagIconWrapper.className += ` flag-icon-${c.flag}`
flagWrapper.appendChild(flagIconWrapper)
mainWrapper.appendChild(flagWrapper)
}
mainWrapper.appendChild(timeWrapper)
worldWrapper.appendChild(mainWrapper)
worldWrapper.appendChild(captionWrapper)
return worldWrapper
},
notificationReceived (noti, payload) {
},
// Override dom generator.
getDom () {
const wrapper = document.createElement("div")
wrapper.className =
"worldtime" +
` style-${this.config.style}`
let c
for (c in this.config.clocks) {
wrapper.appendChild(this.clockFormat(this.config.clocks[c], c))
}
return wrapper
},
loadCSS () {
const css = [
{
id: "flag-icon-CSS",
href: "https://cdnjs.cloudflare.com/ajax/libs/flag-icon-css/2.8.0/css/flag-icon.min.css"
}
]
css.forEach((c) => {
if (!document.getElementById(c.id)) {
const head = document.getElementsByTagName("head")[0]
const link = document.createElement("link")
link.id = c.id
link.rel = "stylesheet"
link.type = "text/css"
link.href = c.href
link.media = "all"
head.appendChild(link)
}
})
}
})