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: [],
|
2019-02-12 21:15:20 -08:00
|
|
|
excludes: [], // Keep for compatibility
|
|
|
|
fixed: ['MMM-page-indicator'],
|
2018-04-26 00:25:40 -07:00
|
|
|
animationTime: 1000,
|
|
|
|
rotationTime: 0,
|
2019-04-13 10:40:14 -07:00
|
|
|
rotationDelay: 10000,
|
|
|
|
slideAnimation: false,
|
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-08-10 13:21:52 -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.
|
2018-08-20 13:21:38 -07:00
|
|
|
*
|
2018-04-28 18:14:35 -07:00
|
|
|
* @param {number} x The dividend
|
|
|
|
* @param {number} n The divisor
|
|
|
|
*/
|
2018-08-10 13:21:52 -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-08-10 13:21:52 -07:00
|
|
|
start: function() {
|
2018-04-26 00:25:40 -07:00
|
|
|
this.curPage = 0;
|
2019-02-12 21:15:20 -08:00
|
|
|
|
|
|
|
// Compatibility
|
|
|
|
if (this.config.excludes.length) {
|
|
|
|
Log.warn(`[Pages]: The config option "excludes" is deprecated. Please use "fixed" instead.`);
|
|
|
|
this.config.fixed = this.config.excludes;
|
|
|
|
}
|
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-08-20 13:21:38 -07:00
|
|
|
* 'QUERY_PAGE_NUMBER' - Requests the current page number
|
|
|
|
*
|
2018-04-26 00:25:40 -07:00
|
|
|
* @param {string} notification the notification ID
|
2018-08-20 13:21:38 -07:00
|
|
|
* @param {number} payload the page to change to/by
|
2018-04-26 00:25:40 -07:00
|
|
|
*/
|
2018-05-08 12:32:20 -07:00
|
|
|
notificationReceived: function(notification, payload) {
|
2018-04-26 00:25:40 -07:00
|
|
|
switch (notification) {
|
|
|
|
case 'PAGE_CHANGED':
|
2018-09-01 05:41:08 -07:00
|
|
|
Log.log('[Pages]: received a notification '
|
2018-04-28 18:14:35 -07:00
|
|
|
+ `to change to page ${payload} of type ${typeof payload}`);
|
2018-08-20 13:21:38 -07:00
|
|
|
this.curPage = payload;
|
|
|
|
this.updatePages();
|
2018-04-26 00:25:40 -07:00
|
|
|
break;
|
|
|
|
case 'PAGE_INCREMENT':
|
2018-09-01 05:41:08 -07:00
|
|
|
Log.log('[Pages]: received a notification to increment pages!');
|
2018-08-20 13:21:38 -07:00
|
|
|
this.changePageBy(payload, 1);
|
|
|
|
this.updatePages();
|
2018-04-26 00:25:40 -07:00
|
|
|
break;
|
|
|
|
case 'PAGE_DECREMENT':
|
2018-09-01 05:41:08 -07:00
|
|
|
Log.log('[Pages]: received a notification to decrement pages!');
|
2018-10-16 08:18:09 -07:00
|
|
|
this.changePageBy(-payload, -1);
|
2018-08-20 13:21:38 -07:00
|
|
|
this.updatePages();
|
2018-04-26 00:25:40 -07:00
|
|
|
break;
|
|
|
|
case 'DOM_OBJECTS_CREATED':
|
2018-09-01 05:41:08 -07:00
|
|
|
Log.log('[Pages]: received that all objects are created;'
|
2018-04-26 00:25:40 -07:00
|
|
|
+ 'will now hide things!');
|
2018-04-28 18:14:35 -07:00
|
|
|
this.sendNotification('MAX_PAGES_CHANGED', this.config.modules.length);
|
2018-08-20 13:21:38 -07:00
|
|
|
this.animatePageChange();
|
|
|
|
this.resetTimerWithDelay(0);
|
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-08-20 13:21:38 -07:00
|
|
|
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: function(amt, fallback) {
|
2019-04-13 09:35:47 -07:00
|
|
|
if (typeof amt !== 'number') {
|
2018-09-01 05:41:08 -07:00
|
|
|
Log.warn(`[Pages]: ${amt} is not a number!`);
|
|
|
|
}
|
|
|
|
|
2018-10-16 08:18:09 -07:00
|
|
|
if (typeof amt === 'number' && !isNaN(amt)) {
|
2018-08-20 13:21:38 -07:00
|
|
|
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
|
|
|
|
);
|
2018-04-26 00:25:40 -07:00
|
|
|
}
|
|
|
|
},
|
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 hiding the current page's elements and showing the next page's
|
2018-04-26 00:25:40 -07:00
|
|
|
* elements.
|
|
|
|
*/
|
2018-08-20 13:21:38 -07:00
|
|
|
updatePages: function() {
|
2018-08-20 07:43:21 -07:00
|
|
|
// Update iff there's at least one page.
|
2018-04-26 00:25:40 -07:00
|
|
|
if (this.config.modules.length !== 0) {
|
2018-08-20 13:21:38 -07:00
|
|
|
this.animatePageChange();
|
2018-09-01 05:41:08 -07:00
|
|
|
this.resetTimerWithDelay(this.config.rotationDelay);
|
|
|
|
} else { Log.error("[Pages]: Pages aren't properly defined!"); }
|
2018-04-26 00:25:40 -07:00
|
|
|
},
|
2018-08-20 13:21:38 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
animatePageChange: function() {
|
|
|
|
const self = this;
|
|
|
|
|
2019-04-13 10:40:14 -07:00
|
|
|
if (this.config.slideAnimation) {
|
|
|
|
MM.getModules()
|
|
|
|
.exceptWithClass(this.config.fixed)
|
|
|
|
.exceptWithClass(this.config.modules[this.curPage])
|
|
|
|
.enumerate(module => {
|
|
|
|
const element = document.getElementById(module.data.identifier);
|
|
|
|
if (element) {
|
|
|
|
element.classList.add("mmm-pages-slide-out");
|
|
|
|
element.classList.remove("mmm-pages-slide-in");
|
|
|
|
}
|
|
|
|
setTimeout(() => {
|
|
|
|
module.hide(0, { lockString: self.identifier });
|
|
|
|
if (element) {
|
|
|
|
element.classList.remove("mmm-pages-slide-out");
|
|
|
|
}
|
|
|
|
}, self.config.animationTime);
|
|
|
|
});
|
|
|
|
|
2019-02-19 06:40:43 -08:00
|
|
|
MM.getModules()
|
|
|
|
.withClass(self.config.modules[self.curPage])
|
|
|
|
.enumerate((module) => {
|
2019-04-13 10:40:14 -07:00
|
|
|
const element = document.getElementById(module.data.identifier);
|
|
|
|
if (element) {
|
|
|
|
element.classList.add("mmm-pages-slide-in");
|
|
|
|
}
|
|
|
|
module.show(0, { lockString: self.identifier } );
|
2019-02-19 06:40:43 -08:00
|
|
|
});
|
2019-04-13 10:40:14 -07:00
|
|
|
|
|
|
|
} else {
|
|
|
|
// Hides all modules not on the current page. This hides any module not
|
|
|
|
// meant to be shown.
|
|
|
|
MM.getModules()
|
|
|
|
.exceptWithClass(this.config.fixed)
|
|
|
|
.exceptWithClass(this.config.modules[this.curPage])
|
|
|
|
.enumerate(module => module.hide(
|
|
|
|
self.config.animationTime / 2,
|
|
|
|
{ lockString: self.identifier }
|
|
|
|
));
|
|
|
|
|
|
|
|
// Shows all modules meant to be on the current page, after a small delay.
|
|
|
|
setTimeout(() => {
|
|
|
|
MM.getModules()
|
|
|
|
.withClass(self.config.modules[self.curPage])
|
|
|
|
.enumerate((module) => {
|
|
|
|
module.show(
|
|
|
|
self.config.animationTime / 2,
|
|
|
|
{ lockString: self.identifier }
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}, this.config.animationTime / 2);
|
|
|
|
}
|
2018-08-20 13:21:38 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Resets the page changing timer with a delay.
|
|
|
|
*
|
|
|
|
* @param {number} delay the delay, in milliseconds.
|
|
|
|
*/
|
|
|
|
resetTimerWithDelay: function(delay) {
|
|
|
|
if (this.config.rotationTime > 0) {
|
|
|
|
// This timer is the auto rotate function.
|
|
|
|
clearInterval(this.timer);
|
|
|
|
// This is delay timer after manually updating.
|
|
|
|
clearInterval(this.delayTimer);
|
|
|
|
const self = this;
|
|
|
|
|
|
|
|
this.delayTimer = setTimeout(() => {
|
|
|
|
self.timer = setInterval(() => {
|
2018-08-21 08:53:39 -07:00
|
|
|
self.sendNotification('PAGE_INCREMENT');
|
2018-08-20 13:21:38 -07:00
|
|
|
self.changePageBy(1);
|
|
|
|
self.updatePages();
|
|
|
|
}, self.config.rotationTime);
|
|
|
|
}, delay);
|
|
|
|
}
|
|
|
|
},
|
2017-06-18 13:24:04 -07:00
|
|
|
});
|