-
-
Notifications
You must be signed in to change notification settings - Fork 14
/
MMM-nyc-transit.js
373 lines (329 loc) · 11.9 KB
/
MMM-nyc-transit.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
/* MagicMirror²
* Module: MMM-NYC-transit
*
* By Elan Trybuch https://github.com/elaniobro
* MIT Licensed.
*/
Module.register('MMM-nyc-transit', { /*eslint-disable-line*/
// Default module config.
defaults: {
displayType: 'marquee',
header: 'Next Train',
module: 'MMM-nyc-transit',
mtaType: 'train',
position: 'top_bar',
stations: [
{
dir: {
upTown: true,
downTown: true,
},
stationId: 237,
walkingTime: 5,
},
{
dir: {
upTown: true,
downTown: true,
},
stationId: 238,
walkingTime: 5,
},
],
updateInterval: 300000, // every 5 min
},
getStyles: function () {
return ['MMM-nyc-transit.css']
},
start: function () {
this.getDepartures()
this.scheduleUpdate()
},
getDom: function () {
// Set up targetnode based on position set in config
var targetNode = document.querySelector('.region.' + this.config.position.split('_').join('.') + ' .container')
// set up mutation observer config options
var config = { attributes: true, childList: true, subtree: true }
// call back function for mutation observer
var callback = function (mutationsList, observer) { /*eslint-disable-line*/
// Use traditional 'for loops' for IE 11
for (var mutation of mutationsList) {
if (mutation.type === 'childList') {
var trainTimes = document.querySelectorAll('.mta__train--time span')
trainTimes.forEach((train) => {
// Get the train time as a Number type
var duration = Number(
train.textContent.split(' ')[1].split('min')[0]
)
var timer = duration * 60
var minutes
var seconds
// Compare duration to walkingtime
if (duration <= Number(train.dataset.walkingTime)) {
setInterval(function () {
minutes = parseInt(timer / 60, 10)
seconds = parseInt(timer % 60, 10)
// minutes = minutes < 10 ? minutes : minutes;
seconds = seconds < 10 ? '0' + seconds : seconds
train.textContent = minutes + ':' + seconds + 'min'
if (--timer < 0) {
timer = duration
}
}, 1000)
}
})
}
}
}
var observer = new MutationObserver(callback)
var data = this.result // the data is not ready
var wrapper = document.createElement('div')
var marquee = document.createElement('marquee')
var list = document.createElement('ul')
var isList = this.config.displayType !== 'marquee'
wrapper.className = 'MMM-nyc-transit'
list.className = 'mta__train--list'
marquee.className = 'mta__train--marquee'
if (data) {
var downTown = data[0].downTown
var upTown = data[1].upTown
if (Object.keys(data).length === 0 && data.constructor === Object) {
return wrapper
}
if (isList) {
var trainHashMap = {
downTown: [],
upTown: [],
}
downTown.forEach((train) => {
if (!trainHashMap.downTown[this.isSIR(train.routeId)]) {
trainHashMap.downTown[this.isSIR(train.routeId)] = {
time: [train.time],
dest: train.destination,
walkingTime: train.walkingTime,
}
} else {
trainHashMap.downTown[
this.isSIR(train.routeId)
].time.push(train.time)
}
})
for (var dKey in trainHashMap.downTown) {
var dHtml = ''
var downTownListItem = document.createElement('li')
dHtml =
dHtml +
'<span class="mta mta__train mta__train--logo mta__train--line-' +
dKey.toLowerCase().split("")[0] +
" mta__train--line-" +
this.isExpress(dKey) +
'"><span class="' +
this.isExpress(dKey) +
'">' +
dKey.toLowerCase().split("")[0] +
"</span></span>" +
trainHashMap.downTown[dKey].dest +
'<span class="mta mta_train mta__train--time mta__train-time__' +
dKey.toLowerCase() +
'"> ' +
trainHashMap.downTown[dKey].time
.slice(0, 3)
.map(
(trainTime, i) =>
"<span data-walking-time='" +
trainHashMap.downTown[dKey].walkingTime +
"' class='train-time__downTown-" +
dKey.toLowerCase() +
"--" +
i +
"'> " +
trainTime +
"min</span>"
) +
" </span>"; /*eslint-disable-line*/
downTownListItem.className = 'mta__train--item mta__train--item-' + this.isExpress(dKey)
downTownListItem.innerHTML = dHtml
list.appendChild(downTownListItem)
}
upTown.forEach((train) => {
if (!trainHashMap.upTown[this.isSIR(train.routeId)]) {
trainHashMap.upTown[this.isSIR(train.routeId)] = {
time: [train.time],
dest: train.destination,
walkingTime: train.walkingTime,
}
} else {
trainHashMap.upTown[
this.isSIR(train.routeId)
].time.push(train.time)
}
})
for (var uKey in trainHashMap.upTown) {
var uHtml = ''
var upTownListItem = document.createElement('li')
uHtml =
uHtml +
'<span class="mta mta__train mta__train--logo mta__train--line-' +
uKey.toLowerCase().split("")[0] +
" mta__train--line-" +
this.isExpress(uKey) +
'"><span class="' + this.isExpress(uKey) + '">' +
uKey.toLowerCase().split("")[0] +
"</span></span>" +
trainHashMap.upTown[uKey].dest +
'<span class="mta mta_train mta__train--time mta_train-time__' +
uKey.toLowerCase() +
'"> ' +
trainHashMap.upTown[uKey].time
.slice(0, 3)
.map(
(trainTime, i) =>
"<span data-walking-time='" +
trainHashMap.upTown[uKey].walkingTime +
"' class='train-time__upTown-" +
uKey.toLowerCase() +
"--" +
i +
"'> " +
trainTime +
"min</span>"
) +
" </span>"; /*eslint-disable-line*/
upTownListItem.className = 'mta__train--item mta__train--item-' + this.isExpress(uKey);
upTownListItem.innerHTML = uHtml
list.appendChild(upTownListItem)
}
wrapper.appendChild(list)
return wrapper
} else {
for (var upMarKey in upTown) {
if (
!Object.prototype.hasOwnProperty.call(upTown, upMarKey)
) {
continue
}
var upMarHtml = ''
var upTownMarListItem = document.createElement('span')
upMarHtml = upMarHtml +
'<span class="mta mta__train mta__train--logo mta__train--line-' +
upTown[upMarKey].routeId.toLowerCase().split("")[0] +
' mta__train--line-' + this.isExpress(upTown[upMarKey].routeId) + '">' +
'<span class="' + this.isExpress(upTown[upMarKey].routeId) + '">' +
upTown[upMarKey].routeId.toLowerCase().split("")[0] +
"</span></span>" +
'</span><span class=\'mta mta_train mta__train--time mta_train-time__\'' +
(parseFloat(upMarKey) + 4) +
'">' +
'<span class="mta mta_train mta__train--time mta__train-time__' +
upMarKey.toLowerCase() +
'"> ' +
'<span data-walking-time=\'' +
upTown[upMarKey].walkingTime +
'\' class=\'train-time__upTown-' +
upTown[upMarKey].routeId.toLowerCase() +
'--' +
upMarKey.toLowerCase() +
'\'> ' +
upTown[upMarKey].time +
'min</span>';
(" </span>"); /*eslint-disable-line*/
upTownMarListItem.className = 'mta__train--item'
upTownMarListItem.innerHTML = upMarHtml
marquee.appendChild(upTownMarListItem)
}
for (var downMarKey in downTown) {
if (
!Object.prototype.hasOwnProperty.call(
downTown,
downMarKey
)
) {
continue
}
var downMarHtml = ''
var downTownMarListItem = document.createElement('span')
downMarHtml =
downMarHtml +
'<span class="mta mta__train mta__train--logo mta__train--line-' +
downTown[downMarKey].routeId.toLowerCase().split("")[0] +
" mta__train--line-" +
this.isExpress(downTown[downMarKey].routeId) +
'">' +
'<span class="' +
this.isExpress(downTown[downMarKey].routeId) +
'">' +
downTown[downMarKey].routeId.toLowerCase().split("")[0] +
"</span></span>" +
"</span><span class='mta mta_train mta__train--time mta_train-time__'" +
(parseFloat(downMarKey) + 4) +
'">' +
'<span class="mta mta_train mta__train--time mta__train-time__' +
downMarKey.toLowerCase() +
'"> ' +
"<span data-walking-time='" +
downTown[downMarKey].walkingTime +
"' class='train-time__downTown-" +
downTown[downMarKey].routeId.toLowerCase() +
"--" +
downMarKey.toLowerCase() +
"'> " +
downTown[downMarKey].time +
"min</span>";
(" </span>"); /*eslint-disable-line*/
downTownMarListItem.className = 'mta__train--item'
downTownMarListItem.innerHTML = downMarHtml
marquee.appendChild(downTownMarListItem)
}
wrapper.appendChild(marquee)
return wrapper
}
}
// observer mutation on targetNode with config obj
observer.observe(targetNode, config)
return wrapper
},
isExpress: function (id) {
return id.split('').length === 2 ? 'express' : ''
},
isSIR: function (id) {
return id === 'SI' ? 'SIR' : id === 'SS' ? 'SIR' : id
},
getDepartures: function () {
var config = this.config
this.sendSocketNotification('GET_DEPARTURES', config)
},
scheduleUpdate: function (delay) {
var loadTime = this.config.updateInterval
var that = this
if (typeof delay !== 'undefined' && delay >= 0) {
loadTime = delay
}
setInterval(function () {
that.getDepartures()
}, loadTime)
},
stationArrayEquals: function (stationsLeft, stationsRight) {
if (stationsLeft.length !== stationsRight.length) {
return false
} else {
for (var i = 0; i < stationsLeft.length; i++) {
if (stationsLeft[i] !== stationsRight[i]) {
return false
}
}
return true
}
},
socketNotificationReceived: function (notification, payload) {
var myStations = this.config.stations.map((obj) => obj.stationId)
if (notification === 'TRAIN_TABLE' && this.stationArrayEquals(payload['stations'], myStations)) {
// eslint-disable-next-line no-console
console.log('socketNotificationReceived: "TRAIN_TABLE": ', this.result)
this.result = payload['data']
this.updateDom(this.config.fadeSpeed)
} else if (notification === 'DOM_OBJECTS_CREATED') {
// eslint-disable-next-line no-console
console.log('Dom Objects Created')
}
},
})