-
Notifications
You must be signed in to change notification settings - Fork 10
/
MMM-Bring.js
251 lines (220 loc) · 10.6 KB
/
MMM-Bring.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
/*jshint esversion: 6 */
Module.register("MMM-Bring", {
defaults: {
email: "",
password: "",
updateInterval: 15,
listName: undefined,
showListName: true,
activeItemColor: "#EE524F",
latestItemColor: "#4FABA2",
showLatestItems: false,
maxItems: 0,
maxLatestItems: 0,
locale: "de-DE",
useKeyboard: false,
customTitle: undefined,
listDropdown: true
},
getStyles: function () {
return [this.file('css/styles.css')];
},
start: function () {
this.sendSocketNotification("GET_LIST", this.config);
setInterval(() => {
this.sendSocketNotification("GET_LIST", this.config);
}, this.config.updateInterval * 60 * 1000);
this.lists = ["Liste1", "Liste2"];
},
createDropDown: function () {
const drop = document.createElement("div");
drop.className = "bring-dropdown-title";
const titleBtn = document.createElement("input");
titleBtn.setAttribute("type", "button");
titleBtn.className = "bring-titleBtn bright";
titleBtn.value = this.config.listName + " \u2BC6";
titleBtn.addEventListener("click", function () {
document.getElementById("bring-dropItems").classList.toggle("show");
});
const dropList = document.createElement("div");
dropList.id = "bring-dropList";
const dropItems = document.createElement("div");
dropItems.id = "bring-dropItems";
var self = this;
for (var i = 0; i < this.lists.length; i++) {
var dropItem = document.createElement("div");
dropItem.className = "bring-dropItem";
dropItem.innerHTML = this.lists[i].name;
dropItem.addEventListener("click", function () {
self.config.listName = this.innerHTML;
self.sendSocketNotification("GET_LIST", self.config);
});
dropItems.appendChild(dropItem);
}
dropList.appendChild(dropItems);
drop.appendChild(titleBtn);
drop.appendChild(dropList);
return drop;
},
getDom: function () {
// if the user doesn't want to show the recently bought items and has no items in the list --> hide list
if (!this.currentList ||
((!this.currentList.purchase || !this.currentList.purchase.length || this.currentList.purchase.length === 0) &&
!this.config.showLatestItems)) {
return document.createElement("span");
}
const container = document.createElement("div");
container.className = "bring-list-container bring-" + this.data.position;
if (!!this.config.customTitle) {
const headerElem = document.createElement("header");
headerElem.className = "module-header";
headerElem.innerText = this.config.customTitle + " (" + (this.currentList.purchase || []).length + ")";
container.appendChild(headerElem);
}
if (this.config.showListName && this.currentList && this.currentList.name) {
if (this.config.listDropdown && (!!this.lists && this.lists.length > 1)) {
const dropTitle = this.createDropDown();
document.addEventListener("click", event => {
if (!event.target.matches('.bring-titleBtn')) {
var dropDown = document.getElementById("bring-dropItems");
if (dropDown.classList.contains('show')) {
dropDown.classList.remove('show');
}
}
});
container.appendChild(dropTitle);
} else {
const title = document.createElement("h3");
title.innerText = this.config.listName;
container.appendChild(title);
}
}
// Purchase
if (this.currentList && this.currentList.purchase) {
const bringList = document.createElement("div");
bringList.className = "bring-list";
let max = this.currentList.purchase.length;
if (this.config.maxItems !== 0 && max > this.config.maxItems) {
max = this.config.maxItems;
}
for (let i = 0, len = max; i < len; i++) {
const bringListItem = document.createElement("div");
bringListItem.className = "bring-list-item-content";
bringListItem.style = "background-color: " + this.config.activeItemColor;
bringListItem.onclick = () => this.itemClicked({
name: this.currentList.purchase[i].name,
purchase: true,
listId: this.currentList.uuid
});
const upperPartContainer = document.createElement("div");
upperPartContainer.className = "bring-list-item-upper-part-container";
const imageContainer = document.createElement("div");
imageContainer.className = "bring-list-item-image-container";
const image = document.createElement("img");
image.src = this.currentList.purchase[i].imageSrc;
imageContainer.appendChild(image);
upperPartContainer.appendChild(imageContainer);
bringListItem.appendChild(upperPartContainer);
const itemTextContainer = document.createElement("div");
itemTextContainer.className = "bring-list-item-text-container";
const itemName = document.createElement("span");
itemName.className = "bring-list-item-name";
itemName.innerText = this.currentList.purchase[i].name;
itemTextContainer.appendChild(itemName);
const itemSpec = document.createElement("span");
itemSpec.className = "bring-list-item-specification-label";
itemSpec.innerText = this.currentList.purchase[i].specification;
itemTextContainer.appendChild(itemSpec);
bringListItem.appendChild(itemTextContainer);
bringList.appendChild(bringListItem);
}
if (this.config.useKeyboard) {
//include add button
const bringListAdd = document.createElement("div");
bringListAdd.className = "bring-list-item-add";
var self = this;
bringListAdd.onclick = function () {
self.openKeyboard();
};
bringListAdd.innerHTML = "+";
bringList.appendChild(bringListAdd);
}
container.appendChild(bringList);
}
if (this.config.showLatestItems && this.currentList && this.currentList.recently) {
const bringListRecent = document.createElement("div");
bringListRecent.className = "bring-list";
let max = this.currentList.recently.length;
if (this.config.maxLatestItems !== 0 && max > this.config.maxLatestItems) {
max = this.config.maxLatestItems;
}
for (let i = 0, len = max; i < len; i++) {
const bringListItem = document.createElement("div");
bringListItem.className = "bring-list-item-content";
bringListItem.style = "background-color: " + this.config.latestItemColor;
bringListItem.onclick = () => this.itemClicked({
name: this.currentList.recently[i].name,
purchase: false,
listId: this.currentList.uuid
});
const upperPartContainer = document.createElement("div");
upperPartContainer.className = "bring-list-item-upper-part-container";
const imageContainer = document.createElement("div");
imageContainer.className = "bring-list-item-image-container";
const image = document.createElement("img");
image.src = this.currentList.recently[i].imageSrc;
imageContainer.appendChild(image);
upperPartContainer.appendChild(imageContainer);
bringListItem.appendChild(upperPartContainer);
const itemTextContainer = document.createElement("div");
itemTextContainer.className = "bring-list-item-text-container";
const itemName = document.createElement("div");
itemName.className = "bring-list-item-name";
itemName.innerText = this.currentList.recently[i].name;
itemTextContainer.appendChild(itemName);
const itemSpec = document.createElement("div");
itemSpec.className = "bring-list-item-specification-label";
itemSpec.innerText = this.currentList.recently[i].specification;
itemTextContainer.appendChild(itemSpec);
bringListItem.appendChild(itemTextContainer);
bringListRecent.appendChild(bringListItem);
container.appendChild(bringListRecent);
}
}
return container;
},
openKeyboard: function () {
console.log("MMM-Bring opening keyboard");
this.sendNotification("KEYBOARD", {key: "mmm-bring", style: "default"});
},
socketNotificationReceived: function (notification, payload) {
if (notification === "LIST_DATA") {
this.currentList = payload.currentList;
this.lists = payload.lists;
if (!this.config.listName) {
this.config.listName = this.currentList.name;
}
this.updateDom(1000);
} else if (notification === "RELOAD_LIST") {
this.sendSocketNotification("GET_LIST", this.config);
}
},
notificationReceived: function (notification, payload) {
if (notification === "KEYBOARD_INPUT" && payload.key === "mmm-bring" && payload.message != '') {
var item = {
name: payload.message[0].toUpperCase() + payload.message.substring(1),
purchase: false,
listId: this.currentList.uuid
};
console.log("MMM-Bring received Keyboard input: " + item.name);
this.sendSocketNotification("PURCHASED_ITEM", item);
} else if (notification === "HIDE_SHIPPING") {
this.hide(1000, {lockString: "LOCKEDBYMODULE"});
} else if (notification === "SHOW_SHIPPING") {
this.show(1000, {lockString: "LOCKEDBYMODULE"});
}
},
itemClicked: function (item) {
this.sendSocketNotification("PURCHASED_ITEM", item);
}
});