forked from edward-shen/MMM-pages
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMMM-pages.js
340 lines (320 loc) · 11.6 KB
/
MMM-pages.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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
Module.register('MMM-pages', {
/**
* By default, we have don't pseudo-paginate any modules. We also exclude
* the page indicator by default, in case people actually want to use the
* sister module. We also don't rotate out modules by default.
*/
defaults: {
modules: [],
excludes: [], // Keep for compatibility
fixed: ['MMM-page-indicator'],
hiddenPages: {},
animationTime: 1000,
rotationTime: 0,
rotationFirstPage: 0, // Keep for compatibility
rotationHomePage: 0,
rotationDelay: 10000,
homePage: 0,
useLockString: true,
pageTimeout: []
},
timer:null,
/**
* Apply any styles, if we have any.
*/
getStyles() {
return ['pages.css'];
},
/**
* Modulo that also works with negative numbers.
*
* @param {number} x The dividend
* @param {number} n The divisor
*/
mod(x, n) {
return ((x % n) + n) % n;
},
/**
* Pseudo-constructor for our module. Makes sure that values aren't negative,
* and sets the default current page to 0.
*/
start() {
// Clamp homePage value to [0, num pages).
if (this.config.homePage >= this.config.modules.length || this.config.homePage < 0) {
this.config.homePage = 0;
}
this.curPage = this.config.homePage;
this.rotationPaused = false;
// Compatibility
if (this.config.excludes.length) {
Log.warn('[MMM-pages] The config option "excludes" is deprecated. Please use "fixed" instead.');
this.config.fixed = this.config.excludes;
}
if (this.config.rotationFirstPage) {
Log.warn('[MMM-pages] The config option "rotationFirstPage" is deprecated. Please used "rotationHomePage" instead.');
this.config.rotationHomePage = this.config.rotationFirstPage;
}
// Disable rotation if an invalid input is given
this.config.rotationTime = Math.max(this.config.rotationTime, 0);
this.config.rotationDelay = Math.max(this.config.rotationDelay, 0);
this.config.rotationHomePage = Math.max(this.config.rotationHomePage, 0);
if (!this.config.useLockString) {
Log.log('[MMM-pages] User opted to not use lock strings!');
}
},
/**
* Handles incoming notifications. Responds to the following:
* 'PAGE_CHANGED' - Set the page to the specified payload page.
* 'PAGE_INCREMENT' - Move to the next page.
* 'PAGE_DECREMENT' - Move to the previous page.
* 'DOM_OBJECTS_CREATED' - Starts the module.
* 'QUERY_PAGE_NUMBER' - Requests the current page number
* 'PAUSE_ROTATION' - Stops rotation
* 'RESUME_ROTATION' - Resumes rotation
* 'HOME_PAGE' - Calls PAGED_CHANGED with the default home page.
* 'SHOW_HIDDEN_PAGE' - Shows the (in the payload) specified hidden
* page by name
* 'LEAVE_HIDDEN_PAGE' - Hides the currently showing hidden page and
* resumes showing the last page
*
* @param {string} notification the notification ID
* @param {number|string} payload the page to change to/by
*/
notificationReceived(notification, payload) {
switch (notification) {
case 'PAGE_CHANGED':
Log.log(`[MMM-pages] received a notification to change to page ${payload} of type ${typeof payload}.`);
this.curPage = payload;
this.updatePages();
break;
case 'PAGE_INCREMENT':
Log.log('[MMM-pages] received a notification to increment pages!');
this.changePageBy(payload, 1);
this.updatePages();
break;
case 'PAGE_DECREMENT':
Log.log('[MMM-pages] received a notification to decrement pages!');
// We can't just pass in -payload for situations where payload is null
// JS will coerce -payload to -0.
this.changePageBy(payload ? -payload : payload, -1);
this.updatePages();
break;
case 'DOM_OBJECTS_CREATED':
Log.log('[MMM-pages] received that all objects are created; will now hide things!');
this.sendNotification('MAX_PAGES_CHANGED', this.config.modules.length);
this.sendNotification('NEW_PAGE', this.curPage);
this.animatePageChange();
this.resetTimerWithDelay(0);
break;
case 'QUERY_PAGE_NUMBER':
this.sendNotification('PAGE_NUMBER_IS', this.curPage);
break;
case 'PAUSE_ROTATION':
this.setRotation(false);
break;
case 'RESUME_ROTATION':
this.setRotation(true);
break;
case 'HOME_PAGE':
this.notificationReceived('PAGE_CHANGED', this.config.homePage);
break;
case 'SHOW_HIDDEN_PAGE':
Log.log(`[MMM-pages] received a notification to change to the hidden page "${payload}" of type "${typeof payload}".`);
this.setRotation(false);
this.showHiddenPage(payload);
break;
case 'LEAVE_HIDDEN_PAGE':
Log.log('[MMM-pages] received a notification to leave the current hidden page.');
this.animatePageChange();
this.setRotation(true);
break;
default: // Do nothing
}
},
/**
* Changes the internal page number by the specified amount. If the provided
* amount is invalid, use the fallback amount. If the fallback amount is
* missing or invalid, do nothing.
*
* @param {number} amt the amount of pages to move forward by. Accepts
* negative numbers.
* @param {number} fallback the fallback value to use. Accepts negative
* numbers.
*/
changePageBy(amt, fallback) {
if (typeof amt !== 'number' && typeof fallback === 'undefined') {
Log.warn(`[MMM-pages] ${amt} is not a number!`);
}
if (typeof amt === 'number' && !Number.isNaN(amt)) {
this.curPage = this.mod(
this.curPage + amt,
this.config.modules.length
);
} else if (typeof fallback === 'number') {
this.curPage = this.mod(
this.curPage + fallback,
this.config.modules.length
);
}
},
/**
* Handles hiding the current page's elements and showing the next page's
* elements.
*/
updatePages() {
// Update if there's at least one page.
if (this.config.modules.length !== 0) {
this.animatePageChange();
if (!this.rotationPaused) {
this.resetTimerWithDelay(0);
}
this.sendNotification('NEW_PAGE', this.curPage);
} else { Log.error('[MMM-pages] Pages are not properly defined!'); }
},
/**
* Animates the page change from the previous page to the current one. This
* assumes that there is a discrepancy between the page currently being shown
* and the page that is meant to be shown.
*
* @param {string} [targetPageName] the name of the hiddenPage we want to show.
* Optional and only used when we want to switch to a hidden page
*/
animatePageChange(targetPageName) {
let lockStringObj = { lockString: this.identifier };
if (!this.config.useLockString) {
// Passing in an undefined object is equivalent to not passing it in at
// all, effectively providing only one arg to the hide and show calls
lockStringObj = undefined;
}
// Hides all modules not on the current page. This hides any module not
// meant to be shown.
const self = this;
let modulesToShow;
if (typeof targetPageName !== 'undefined') {
modulesToShow = this.config.hiddenPages[targetPageName];
} else {
modulesToShow = this.config.fixed.concat(this.config.modules[this.curPage]);
}
const animationTime = self.config.animationTime / 2;
MM.getModules()
.exceptWithClass(modulesToShow)
.enumerate(module => module.hide(animationTime, () => {}, lockStringObj));
// Shows all modules meant to be on the current page, after a small delay.
setTimeout(() => {
MM.getModules()
.withClass(modulesToShow)
.enumerate(module => module.show(animationTime, () => {}, lockStringObj));
}, animationTime);
},
/**
* Resets the page changing timer with a delay.
*
* @param {number} delay the delay, in milliseconds.
*/
resetTimerWithDelay(delay) {
if (this.config.rotationTime > 0) {
// This timer is the auto rotate function.
if(this.timer){
(this.config.pageTimeout.length?clearTimeout:clearInterval)(this.timer);
this.timer=null
}
// This is delay timer after manually updating.
if(this.delayTimer){
clearTimeout(this.delayTimer);
this.delayTimer=null
}
let rotationTimeout=this.config.rotationTime
if(this.config.pageTimeout.length){
for(let pageInfo of this.config.pageTimeout){
if((pageInfo.pageNumber) -1 == this.curPage){
rotationTimeout= pageInfo.timeout
break;
}
}
}
const self = this;
this.delayTimer = setTimeout(() => {
self.timer = (this.config.pageTimeout.length?setTimeout:setInterval)(() => {
// Inform other modules and page change.
// MagicMirror automatically excludes the sender from receiving the
// message, so we need to trigger it for ourselves.
self.sendNotification('PAGE_INCREMENT');
self.notificationReceived('PAGE_INCREMENT');
}, rotationTimeout);
}, delay, this);
} else if (this.config.rotationHomePage > 0) {
// This timer is the auto rotate function.
if(this.timer){
(this.config.pageTimeout.length?clearTimeout:clearInterval)(this.timer);
this.timer = null
}
// This is delay timer after manually updating.
if(this.delayTimer){
clearTimeout(this.delayTimer);
this.delayTimer=null
}
let rotationTimeout=this.config.rotationHomePage
if(this.config.pageTimeout.length){
for(let pageInfo of this.config.pageTimeout){
if((pageInfo.pagenumber) -1 == this.curPage){
rotationTimeout= pageInfo.timeout
break;
}
}
}
const self = this;
this.delayTimer = setTimeout(() => {
this.delayTimer=null
self.timer = (this.config.pageTimeout.length?setTimeout:setInterval)(() => {
// Inform other modules and page change.
// MagicMirror automatically excludes the sender from receiving the
// message, so we need to trigger it for ourselves.
self.sendNotification('PAGE_CHANGED', 0);
self.notificationReceived('PAGE_CHANGED', self.config.homePage);
}, rotationTimeout);
}, delay);
}
},
/**
* Pause or resume the page rotation. If the provided isRotating value is
* set to true, it will resume the rotation. If the requested
* state (f.e. isRotating === true) equals the current state, print a warning
* and do nothing.
*
* @param {boolean} isRotating the parameter, if you want to pause or resume.
*/
setRotation(isRotating) {
const stateBaseString = isRotating ? 'resum' : 'paus';
if (isRotating === this.rotationPaused) {
Log.warn(`[MMM-pages] was asked to ${stateBaseString}e but rotation is already ${stateBaseString}ed!`);
} else {
Log.log(`[MMM-pages] ${stateBaseString}ing rotation`);
if (!isRotating) {
if(this.timer){
this.timer_clear_function(this.timer);
this.timer=null
}
if(this.delayTimer){
clearTimeout(this.delayTimer);
this.delayTimer=null
}
} else {
this.resetTimerWithDelay(this.rotationDelay);
}
this.rotationPaused = isRotating;
}
},
/**
* Handles hidden pages.
*
* @param {string} name the name of the hiddenPage we want to show
*/
showHiddenPage(name) {
// Only proceed if the named hidden page actually exists
if (name in this.config.hiddenPages) {
this.animatePageChange(name);
} else {
Log.error(`[MMM-pages] Hidden page "${name}" does not exist!`);
}
},
});