-
Notifications
You must be signed in to change notification settings - Fork 16
/
MMM-AfterShip.js
264 lines (211 loc) · 10.1 KB
/
MMM-AfterShip.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
/* Magic Mirror
* Module: MMM-AfterShip
*
* By Mykle1
*
*/
Module.register("MMM-AfterShip", {
// Module config defaults. // Make all changes in your config.js file
defaults: {
apiKey: '', // Your free API Key from aftership.com
useHeader: false, // false if you don't want a header
header: "", // Change in config file. useHeader must be true
maxWidth: "300px",
animationSpeed: 3000, // fade speed
initialLoadDelay: 3250,
retryDelay: 2500,
rotateInterval: 30 * 1000, // 30 second rotation of items
updateInterval: 10 * 60 * 1000, // 10 minutes
apiLanguage: "en",
dateTimeFormat: "ddd, MMM DD, YYYY, h:mm a",
dateFormat: "ddd, MMM DD, YYYY"
},
getStyles: function() {
return ["MMM-AfterShip.css"];
},
start: function() {
Log.info("Starting module: " + this.name);
this.sendSocketNotification('CONFIG', this.config);
requiresVersion: "2.1.0",
// Set locale.
this.url = "";
this.AfterShip = {};
this.activeItem = 0;
this.rotateInterval = null;
this.scheduleUpdate();
},
getDom: function() {
var wrapper = document.createElement("div");
wrapper.className = "wrapper";
wrapper.style.maxWidth = this.config.maxWidth;
if (!this.loaded) {
wrapper.innerHTML = "Where's my shit?";
wrapper.classList.add("bright", "light", "small");
return wrapper;
}
if (this.config.useHeader != false) {
var header = document.createElement("header");
header.classList.add("xsmall", "bright", "header");
header.innerHTML = this.config.header;
wrapper.appendChild(header);
}
// If there are deliveries pending, go through all the data
if (this.AfterShip.length != 0){
// Rotating my data
var AfterShip = this.AfterShip;
var AfterShipKeys = Object.keys(this.AfterShip);
if (AfterShipKeys.length > 0) {
if (this.activeItem >= AfterShipKeys.length) {
this.activeItem = 0;
}
var AfterShip = this.AfterShip[AfterShipKeys[this.activeItem]];
var checkpoints = AfterShip['checkpoints']; // another array inside the first array
// console.log(checkpoints); // for checking
// console.log(this.AfterShip); // for checking
// My data begins here
var top = document.createElement("div");
top.classList.add("list-row");
// Title of shipment (if any)
var Title = document.createElement("div");
Title.classList.add("xsmall", "bright", "Title");
Title.innerHTML = "Title: " + AfterShip.title;
wrapper.appendChild(Title);
// ID of shipment
var ID = document.createElement("div");
ID.classList.add("xsmall", "bright", "ID");
ID.innerHTML = "ID # : " + AfterShip.id;
wrapper.appendChild(ID);
// Last update on shipment
var lastUpdate = document.createElement("div");
lastUpdate.classList.add("xsmall", "bright", "lastUpdate");
lastUpdate.innerHTML = "Last update: " + moment(AfterShip.last_updated_at).local().format(this.config.dateTimeFormat);
wrapper.appendChild(lastUpdate);
// tracking number of shipment
var tracking_number = document.createElement("div");
tracking_number.classList.add("xsmall", "bright", "tracking_number");
tracking_number.innerHTML = "Tracking #: " + AfterShip.tracking_number;
wrapper.appendChild(tracking_number);
// Courier name
var slug = document.createElement("div");
slug.classList.add("xsmall", "bright", "courier");
slug.innerHTML = "Courier: " + (AfterShip.slug.toUpperCase());
wrapper.appendChild(slug);
// expected_delivery date
var expected_delivery = document.createElement("div");
expected_delivery.classList.add("xsmall", "bright", "expected_delivery");
if (AfterShip.expected_delivery != null) {
expected_delivery.innerHTML = "Expected delivery on: " + moment(AfterShip.expected_delivery).local().format(this.config.dateFormat);
wrapper.appendChild(expected_delivery);
} else {
expected_delivery.innerHTML = "No expected delivery date!";
wrapper.appendChild(expected_delivery);
}
// shipment_type
var shipment_type = document.createElement("div");
shipment_type.classList.add("xsmall", "bright", "shipment_type");
if (AfterShip.shipment_type != null) {
shipment_type.innerHTML = "Shipping: " + AfterShip.shipment_type;
wrapper.appendChild(shipment_type);
} else {
shipment_type.innerHTML = "Shipping: If you're lucky!";
wrapper.appendChild(shipment_type);
}
// status oh shipment
var tag = document.createElement("div");
tag.classList.add("xsmall", "bright", "status");
tag.innerHTML = "Status: " + AfterShip.tag;
wrapper.appendChild(tag);
// objects that are inside an array that is inside an object
// checkpoint location // only the last object in the array = checkpoints[checkpoints.length -1] @Cowboysdude //
var location = document.createElement("div");
location.classList.add("xsmall", "bright", "location");
if (AfterShip.checkpoints.length != 0) {
location.innerHTML = "Location: " + AfterShip.checkpoints[checkpoints.length - 1].location; // only the last object in the array = checkpoints[checkpoints.length -1] //
wrapper.appendChild(location);
} else {
location.innerHTML = "Location: Who the fuck knows!";
wrapper.appendChild(location);
}
// objects that are inside an array that is inside an object
// checkpoint_time // only the last object in the array //
var checkpoint_time = document.createElement("div");
checkpoint_time.classList.add("xsmall", "bright", "checkpoint_time");
if (AfterShip.checkpoints.length != 0) {
checkpoint_time.innerHTML = "When: " + moment(AfterShip.checkpoints[checkpoints.length - 1].checkpoint_time).local().format(this.config.dateTimeFormat);
wrapper.appendChild(checkpoint_time);
} else {
checkpoint_time.innerHTML = "When: Who the fuck cares!";
wrapper.appendChild(checkpoint_time);
}
// objects that are inside an array that is inside an object
// message from checkpoint // only the last object in the array //
var message = document.createElement("div");
message.classList.add("xsmall", "bright", "message");
if (AfterShip.checkpoints.length != 0) {
message.innerHTML = "Message: " + AfterShip.checkpoints[checkpoints.length - 1].message;
wrapper.appendChild(message);
} else {
message.innerHTML = "Message: No data from courier!";
wrapper.appendChild(message);
}
} // <-- closes rotation
return wrapper;
} else { // From deliveries pending if statement above
// When there are no pending deliveries, do the following
var top = document.createElement("div");
top.classList.add("list-row");
// When no deliveries are pending
var nothing = document.createElement("div");
nothing.classList.add("small", "bright", "nothing");
nothing.innerHTML = "No deliveries pending!";
wrapper.appendChild(nothing);
// Current date and time (wherever you are)
var date = document.createElement("div");
date.classList.add("small", "bright", "date");
date.innerHTML = moment().local().format(this.config.dateTimeFormat);
wrapper.appendChild(date);
} // Closes else statement from deliveries pending if statement above
return wrapper;
}, // <-- closes getDom
///// Add this function to the modules you want to control with voice //////
notificationReceived: function(notification, payload) {
if (notification === 'HIDE_SHIPPING') {
this.hide(1000);
// this.updateDom(300);
} else if (notification === 'SHOW_SHIPPING') {
this.show(1000);
// this.updateDom(300);
}
},
processAfterShip: function(data) {
this.AfterShip = data;
// console.log(this.AfterShip); // for checking //
this.loaded = true;
},
scheduleCarousel: function() {
// console.log("Carousel of AfterShip fucktion!"); // for cheking //
this.rotateInterval = setInterval(() => {
this.activeItem++;
this.updateDom(this.config.animationSpeed);
}, this.config.rotateInterval);
},
scheduleUpdate: function() {
setInterval(() => {
this.getAfterShip();
}, this.config.updateInterval);
this.getAfterShip(this.config.initialLoadDelay);
},
getAfterShip: function() {
this.sendSocketNotification('GET_AFTERSHIP', this.url);
},
socketNotificationReceived: function(notification, payload) {
if (notification === "AFTERSHIP_RESULT") {
this.processAfterShip(payload);
if (this.rotateInterval == null) {
this.scheduleCarousel();
}
this.updateDom(this.config.animationSpeed);
}
this.updateDom(this.config.initialLoadDelay);
},
});