-
Notifications
You must be signed in to change notification settings - Fork 0
/
MMM-StorH.js
executable file
·228 lines (194 loc) · 5.33 KB
/
MMM-StorH.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
/* Magic Mirror
* Module: StorH
*
* By Petter Skog (https://skogdev.no)
* MIT Licensed.
*/
var baseUrl = 'https://storhapi.skogdev.no/v1/';
var accessToken = null;
var groupId = null;
var performHttp = function(requestUrl) {
return new Promise((resolve, reject) => {
var hr = new XMLHttpRequest();
hr.onreadystatechange = () => {
// Success --> resolve
if (hr.readyState == 4 && hr.status == 200) {
resolve(hr.responseText);
}
// Token expired --> reauthenticate
else if (hr.readyState == 4 && hr.status === 401) {
authenticate()
.then((result) => {
if (result)
resolve(result);
else
reject('unable to authenticate');
})
}
}
hr.open('GET', requestUrl, true);
hr.setRequestHeader('Authorization', 'Bearer ' + accessToken);
hr.send(null);
})
}
var authenticate = function(config) {
return new Promise((resolve) => {
var hr = new XMLHttpRequest();
hr.open('POST', baseUrl + 'auth/login', true);
hr.setRequestHeader('Content-Type', 'application/json');
hr.onreadystatechange = function () {
if (hr.readyState == 4 && hr.status == 200) {
var result = JSON.parse(hr.responseText).result;
accessToken = result.accessToken;
resolve(true);
}
else if (hr.readyState == 4)
resolve(false)
}
hr.send(JSON.stringify({
email: config.email,
password: config.password
}));
})
}
var getGroup = function() {
var url = baseUrl + 'group/getgroups';
return getData(url)
.then((result) => {
if (result.length > 0) {
groupId = result[0].id;
return;
}
else
throw 'User has no group';
});
}
var getData = function(url) {
return performHttp(url)
.then((response) => {
var parsedResponse = JSON.parse(response);
return parsedResponse.result;
})
}
var startPolling = function(self) {
var url = baseUrl + 'item/getitemsingroup?groupId=' + groupId + '&isShoppingList=true'
getData(url)
.then((result) => {
self.items = result.slice(0, this.config.maxItems);
self.updateDom(0);
})
}
var tran = {
MANUFACTURER: '',
ITEMNAME: '',
COUNT: '',
LOADING: ''
}
Module.register('MMM-StorH', {
defaults: {
showHeader: true,
maxItems: 10,
},
getStyles: () => {
return ["storh.css"];
},
getTranslations: () => {
return {
en: "translations/en.json",
nb: "translations/nb.json"
}
},
start: function () {
Log.log('starting');
console.log(this);
this.items = [];
Log.info('Starting module: ' + this.name);
var translator = this.Translator;
tran.MANUFACTURER = this.translate('MANUFACTURER');
tran.ITEMNAME = this.translate('ITEMNAME');
tran.COUNT = this.translate('COUNT');
tran.LOADING = this.translate('LOADING');
// Set locale and time format based on global config
Log.log('setting locale to', config.language);
// Setup
authenticate(this.config)
.then(() => getGroup())
.then(() => startPolling(this))
.then(() => {
setInterval(() => {
startPolling(this);
}, 60000);
})
.catch((err) => {
throw new Error(err);
})
},
updateDomIfNeeded: function(self) {
self.updateDom(this.config.animationSpeed);
},
getTableHeaderRow: function() {
var thBrand = document.createElement('th');
thBrand.className = 'light';
thBrand.appendChild(document.createTextNode(tran.MANUFACTURER));
var thItemName = document.createElement('th');
thItemName.className = 'light';
thItemName.appendChild(document.createTextNode(tran.ITEMNAME));
var thCount = document.createElement('th');
thCount.className = 'light';
thCount.appendChild(document.createTextNode(tran.COUNT));
var thead = document.createElement('thead');
thead.addClass = 'xsmall dimmed';
thead.appendChild(thBrand);
thead.appendChild(thItemName);
thead.appendChild(thCount);
return thead;
},
getTableRow: function(item) {
var tdItemManu = document.createElement('td');
tdItemManu.className = 'manu';
var txtLine = document.createTextNode(item.itemManu);
tdItemManu.appendChild(txtLine);
var tdItemName = document.createElement('td');
tdItemName.className = 'itemname bright';
tdItemName.appendChild(document.createTextNode(item.itemName));
var tdCount = document.createElement('td');
tdCount.className = 'count center';
tdCount.appendChild(document.createTextNode(item.count));
var tr = document.createElement('tr');
tr.appendChild(tdItemManu);
tr.appendChild(tdItemName);
tr.appendChild(tdCount);
return tr;
},
getDom: function() {
if (this.items.length > 0) {
var table = document.createElement('table');
table.className = 'storh small';
if (this.config.showHeader) {
table.appendChild(this.getTableHeaderRow());
}
for (var i = 0; i < this.items.length; i++) {
var item = this.items[i];
var tr = this.getTableRow(item);
if (this.config.fade && this.config.fadePoint < 1) {
if (this.config.fadePoint < 0) {
this.config.fadePoint = 0;
}
var startingPoint = this.items.length * this.config.fadePoint;
var steps = this.items.length - startingPoint;
if (i >= startingPoint) {
var currentStep = i - startingPoint;
tr.style.opacity = 1 - (1 / steps * currentStep);
}
}
table.appendChild(tr);
}
return table;
} else {
var wrapper = document.createElement('div');
wrapper.innerHTML = tran.LOADING;
wrapper.className = 'small dimmed';
}
return wrapper;
}
});