-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMMM-NASA-APOD.js
47 lines (39 loc) · 1.25 KB
/
MMM-NASA-APOD.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
//Main code file //
Module.register('MMM-NASA-APOD', {
defaults: {
apiKey: 'DEMO_KEY', // Default API key
},
start: function () {
this.apiKey = this.config.apiKey || this.defaults.apiKey;
this.sendSocketNotification('CONFIG', this.apiKey);
},
notificationReceived: function (notification, payload) {
if (notification === 'DOM_OBJECTS_CREATED') {
this.sendSocketNotification('FETCH_APOD');
}
},
socketNotificationReceived: function (notification, payload) {
if (notification === 'APOD_RESULT') {
this.apodData = payload;
this.updateDom(); // Trigger UI update
}
},
getDom: function () {
const wrapper = document.createElement('div');
wrapper.className = 'nasa-apod';
if (this.apodData) {
const title = document.createElement('div');
title.className = 'apod-title';
title.innerHTML = this.apodData.title;
const image = document.createElement('img');
image.className = 'apod-image';
image.src = this.apodData.url;
image.alt = this.apodData.title;
wrapper.appendChild(title);
wrapper.appendChild(image);
} else {
wrapper.innerHTML = 'Loading APOD...';
}
return wrapper;
},
});