2018-04-26 00:25:40 -07:00
|
|
|
Module.register('MMM-pages', {
|
2017-10-20 21:51:54 -07:00
|
|
|
|
2018-07-30 08:53:07 -07:00
|
|
|
// We require the older style of function declaration for compatibility
|
2018-05-06 06:33:33 -07:00
|
|
|
// reasons.
|
|
|
|
|
2018-04-26 00:25:40 -07:00
|
|
|
/**
|
2018-07-30 08:53:07 -07:00
|
|
|
* By default, we have don't pseudo-paginate any modules. We also exclude
|
2018-04-26 00:25:40 -07:00
|
|
|
* 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: ['MMM-page-indicator'],
|
|
|
|
animationTime: 1000,
|
|
|
|
rotationTime: 0,
|
2018-05-06 15:11:27 -07:00
|
|
|
rotationDelay: 10000
|
2018-04-26 00:25:40 -07:00
|
|
|
},
|
2017-10-20 21:51:54 -07:00
|
|
|
|
2018-04-28 18:14:35 -07:00
|
|
|
/**
|
|
|
|
* Apply any styles, if we have any.
|
|
|
|
*/
|
2018-07-26 11:11:18 -07:00
|
|
|
getStyles: function () {
|
2018-04-26 00:25:40 -07:00
|
|
|
return ['pages.css'];
|
|
|
|
},
|
2017-10-20 21:51:54 -07:00
|
|
|
|
2018-04-28 18:14:35 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Modulo that also works with negative numbers.
|
|
|
|
* @param {number} x The dividend
|
|
|
|
* @param {number} n The divisor
|
|
|
|
*/
|
2018-07-26 11:11:18 -07:00
|
|
|
mod: function (x, n) {
|
2018-04-28 18:14:35 -07:00
|
|
|
return ((x % n) + n) % n;
|
|
|
|
},
|
|
|
|
|
2018-04-26 00:25:40 -07:00
|
|
|
/**
|
|
|
|
* Pseudo-constructor for our module. Makes sure that values aren't negative,
|
|
|
|
* and sets the default current page to 0.
|
|
|
|
*/
|
2018-07-26 11:11:18 -07:00
|
|
|
start: function () {
|
2018-04-26 00:25:40 -07:00
|
|
|
this.curPage = 0;
|
2017-10-20 21:51:54 -07:00
|
|
|
|
2018-04-26 00:25:40 -07:00
|
|
|
// 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);
|
|
|
|
},
|
2017-10-20 21:51:54 -07:00
|
|
|
|
2018-04-26 00:25:40 -07:00
|
|
|
/**
|
2018-07-30 08:53:07 -07:00
|
|
|
* Handles incoming notifications. Responds to the following:
|
2018-04-26 00:25:40 -07:00
|
|
|
* '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.
|
2018-07-30 08:53:07 -07:00
|
|
|
* 'REQUEST_CURRENT_PAGE' - Requests the current page number
|
2018-04-26 00:25:40 -07:00
|
|
|
* @param {string} notification the notification ID
|
|
|
|
* @param {number} payload the page to change to
|
|
|
|
*/
|
2018-07-26 11:11:18 -07:00
|
|
|
notificationReceived: function (notification, payload) {
|
2018-04-26 00:25:40 -07:00
|
|
|
switch (notification) {
|
|
|
|
case 'PAGE_CHANGED':
|
2018-07-30 08:53:07 -07:00
|
|
|
Log.log(`${this.name} received a notification`
|
2018-04-28 18:14:35 -07:00
|
|
|
+ `to change to page ${payload} of type ${typeof payload}`);
|
2018-05-08 12:34:36 -07:00
|
|
|
if (typeof payload === 'number') {
|
2018-04-26 00:25:40 -07:00
|
|
|
this.curPage = payload;
|
|
|
|
} else {
|
2018-04-28 18:14:35 -07:00
|
|
|
Log.error('Was asked to change to an invalid number!');
|
|
|
|
Log.error(`Payload=${payload}, type=${typeof payload},
|
|
|
|
maxPageIndex=${this.config.modules.length - 1}`);
|
2017-06-18 13:24:04 -07:00
|
|
|
}
|
2018-04-26 00:25:40 -07:00
|
|
|
this.updatePages(true);
|
|
|
|
break;
|
|
|
|
case 'PAGE_INCREMENT':
|
2018-07-30 08:53:07 -07:00
|
|
|
Log.log(`${this.name} received a notification to increment pages!`);
|
2018-05-08 12:34:36 -07:00
|
|
|
this.curPage = this.mod(this.curPage + 1, this.config.modules.length);
|
2018-04-26 00:25:40 -07:00
|
|
|
this.updatePages(true);
|
|
|
|
break;
|
|
|
|
case 'PAGE_DECREMENT':
|
2018-07-30 08:53:07 -07:00
|
|
|
Log.log(`${this.name} received a notification to decrement pages!`);
|
2018-05-08 12:34:36 -07:00
|
|
|
this.curPage = this.mod(this.curPage - 1, this.config.modules.length);
|
2018-04-26 00:25:40 -07:00
|
|
|
this.updatePages(true);
|
|
|
|
break;
|
|
|
|
case 'DOM_OBJECTS_CREATED':
|
2018-07-30 08:53:07 -07:00
|
|
|
Log.log(`${this.name} received that all objects are created;`
|
2018-04-26 00:25:40 -07:00
|
|
|
+ 'will now hide things!');
|
|
|
|
this.updatePages(true);
|
2018-04-28 18:14:35 -07:00
|
|
|
this.sendNotification('MAX_PAGES_CHANGED', this.config.modules.length);
|
2018-04-26 00:25:40 -07:00
|
|
|
break;
|
2018-07-30 08:56:12 -07:00
|
|
|
case 'QUERY_PAGE_NUMBER':
|
|
|
|
this.sendNotification('PAGE_NUMBER_IS', this.curPage);
|
2018-07-30 08:53:07 -07:00
|
|
|
break;
|
2018-04-26 00:25:40 -07:00
|
|
|
default:
|
|
|
|
}
|
|
|
|
},
|
2017-10-20 21:51:54 -07:00
|
|
|
|
2018-04-26 00:25:40 -07:00
|
|
|
// TODO: Add slide-left/right animation
|
|
|
|
/**
|
2018-07-30 08:53:07 -07:00
|
|
|
* Handles hiding the current page's elements and showing the next page's
|
2018-04-26 00:25:40 -07:00
|
|
|
* elements.
|
|
|
|
* @param {boolean} manuallyCalled whether or not to add in an extended delay.
|
|
|
|
*/
|
2018-07-26 11:11:18 -07:00
|
|
|
updatePages: function (manuallyCalled) {
|
2018-04-26 00:25:40 -07:00
|
|
|
if (this.config.modules.length !== 0) {
|
2018-05-05 19:19:51 -07:00
|
|
|
// We need to use self because upstream uses an older electron and thus
|
|
|
|
// older version of node
|
|
|
|
const self = this;
|
|
|
|
|
2018-04-26 00:25:40 -07:00
|
|
|
Log.log(`updatePages was ${manuallyCalled ? '' : 'not'} manually called`);
|
2017-10-20 21:51:54 -07:00
|
|
|
|
2018-04-26 00:25:40 -07:00
|
|
|
// Hides the current page's elements.
|
|
|
|
MM.getModules()
|
|
|
|
.exceptWithClass(this.config.excludes)
|
|
|
|
.exceptWithClass(this.config.modules[this.curPage])
|
|
|
|
.enumerate((module) => {
|
|
|
|
module.hide(
|
2018-05-05 19:19:51 -07:00
|
|
|
self.config.animationTime / 2,
|
2018-05-06 15:11:27 -07:00
|
|
|
{ lockString: self.identifier }
|
2018-04-26 00:25:40 -07:00
|
|
|
);
|
|
|
|
});
|
2017-10-20 21:51:54 -07:00
|
|
|
|
2018-04-26 00:25:40 -07:00
|
|
|
// Shows the next page's elements
|
2018-07-30 08:53:07 -07:00
|
|
|
setTimeout(() => MM.getModules()
|
|
|
|
.withClass(self.config.modules[self.curPage])
|
|
|
|
.enumerate(module => module.show(
|
|
|
|
self.config.animationTime / 2,
|
|
|
|
{ lockString: self.identifier },
|
|
|
|
)), this.config.animationTime / 2);
|
2017-10-20 21:51:54 -07:00
|
|
|
|
2018-04-28 15:50:07 -07:00
|
|
|
if (manuallyCalled && this.config.rotationTime > 0) {
|
2018-04-26 00:25:40 -07:00
|
|
|
Log.log('Manually updated page! setting delay before resuming timer!');
|
2017-10-20 21:51:54 -07:00
|
|
|
|
2018-04-26 00:25:40 -07:00
|
|
|
clearInterval(this.timer);
|
2017-10-20 21:51:54 -07:00
|
|
|
|
2018-04-26 00:25:40 -07:00
|
|
|
setTimeout(() => {
|
2018-05-05 19:19:51 -07:00
|
|
|
self.timer = setInterval(() => {
|
2018-04-26 00:25:40 -07:00
|
|
|
// Incrementing page
|
2018-05-05 19:19:51 -07:00
|
|
|
self.curPage = self.mod(
|
|
|
|
self.curPage + 1,
|
2018-05-06 15:11:27 -07:00
|
|
|
self.config.modules.length
|
2018-04-28 18:14:35 -07:00
|
|
|
);
|
2018-05-05 19:19:51 -07:00
|
|
|
self.sendNotification('PAGE_INCREMENT');
|
|
|
|
self.updatePages(false);
|
|
|
|
}, self.config.rotationTime, false);
|
2018-04-26 00:25:40 -07:00
|
|
|
}, this.config.rotationDelay);
|
|
|
|
}
|
|
|
|
} else { Log.error("Pages aren't properly defined!"); }
|
|
|
|
},
|
2017-06-18 13:24:04 -07:00
|
|
|
});
|