-
Notifications
You must be signed in to change notification settings - Fork 0
/
MMM-ShoppingList.js
71 lines (60 loc) · 2.25 KB
/
MMM-ShoppingList.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
/* MagicMirror²
* Module: MMM-ShoppingList
*
* By [Miguel Flores]
*/
Module.register('MMM-ShoppingList', {
shoppingList: [],
defaults: {
columnCount: 1 // Default column count
},
start: function () {
this.sendSocketNotification('GET_SHOPPING_LIST');
},
socketNotificationReceived: function (notification, payload) {
console.log(`Received notification: ${notification}`);
if (notification === 'SHOPPING_LIST') {
console.log('Received shopping list:', JSON.stringify(payload));
this.shoppingList = payload;
this.updateDom();
} else if (notification === 'ADD_TO_SHOPPING_LIST') {
console.log(`Received new item: ${payload.item}`);
this.shoppingList.push(payload.item);
this.updateDom();
} else if (notification === 'REMOVE_FROM_SHOPPING_LIST') {
console.log(`Removing item: ${payload.item}`);
const index = this.shoppingList.indexOf(payload.item);
if (index !== -1) {
this.shoppingList.splice(index, 1);
this.updateDom();
}
} else if (notification === 'CLEAR_SHOPPING_LIST') {
console.log('Clearing shopping list');
this.shoppingList = [];
this.updateDom();
}
},
getStyles: function () {
return ['MMM-ShoppingList.css'];
},
getDom: function () {
const wrapper = document.createElement('div');
wrapper.className = 'MMM-ShoppingList-wrapper'; // Add a custom class for styling
// Apply column count style
wrapper.style.columnCount = this.config.columnCount || this.defaults.columnCount;
if (this.shoppingList.length === 0) {
wrapper.innerHTML = '<b>Shopping list is empty.</b>';
} else {
// Create the list and populate it with items
const list = document.createElement('ul');
this.shoppingList.forEach(item => {
const listItem = document.createElement('li');
listItem.innerHTML = item;
list.appendChild(listItem);
});
// Append the list to the wrapper
wrapper.appendChild(list);
}
return wrapper;
},
});