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'],
|
2020-11-05 10:53:44 -08:00
|
|
|
hiddenPages: {},
|
2018-04-26 00:25:40 -07:00
|
|
|
animationTime: 1000,
|
|
|
|
rotationTime: 0,
|
2020-11-06 07:07:21 -08:00
|
|
|
rotationFirstPage: 0, // Keep for compatibility
|
2020-09-07 13:48:19 -07:00
|
|
|
rotationHomePage: 0,
|
2020-08-31 18:24:56 -07:00
|
|
|
rotationDelay: 10000,
|
2020-11-23 15:25:30 -08:00
|
|
|
homePage: 0,
|
|
|
|
useLockString: true,
|
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.
|
|
|
|
*/
|
2020-04-07 08:52:39 -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
|
|
|
|
*/
|
2020-04-07 08:52:39 -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.
|
|
|
|
*/
|
2020-04-07 08:52:39 -07:00
|
|
|
start: function () {
|
2020-08-31 18:24:56 -07:00
|
|
|
// 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;
|
2020-04-16 16:59:19 -07:00
|
|
|
this.rotationPaused = false;
|
2019-08-08 09:58:21 -07:00
|
|
|
|
2019-02-12 21:15:20 -08:00
|
|
|
// Compatibility
|
|
|
|
if (this.config.excludes.length) {
|
2024-12-01 11:29:26 -08:00
|
|
|
Log.warn('[MMM-pages] The config option "excludes" is deprecated. Please use "fixed" instead.');
|
2019-02-12 21:15:20 -08:00
|
|
|
this.config.fixed = this.config.excludes;
|
|
|
|
}
|
2017-10-20 21:51:54 -07:00
|
|
|
|
2020-09-07 13:48:19 -07:00
|
|
|
if (this.config.rotationFirstPage) {
|
2024-12-01 11:29:26 -08:00
|
|
|
Log.warn('[MMM-pages] The config option "rotationFirstPage" is deprecated. Please used "rotationHomePage" instead.');
|
2020-09-07 13:48:19 -07:00
|
|
|
this.config.rotationHomePage = this.config.rotationFirstPage;
|
|
|
|
}
|
|
|
|
|
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);
|
2020-09-07 13:48:19 -07:00
|
|
|
this.config.rotationHomePage = Math.max(this.config.rotationHomePage, 0);
|
2020-11-23 15:25:30 -08:00
|
|
|
|
|
|
|
if (!this.config.useLockString) {
|
2024-12-01 11:29:26 -08:00
|
|
|
Log.info('[MMM-pages] User opted to not use lock strings!');
|
2020-11-23 15:25:30 -08:00
|
|
|
}
|
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 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
|
2020-08-31 18:24:56 -07:00
|
|
|
* 'PAUSE_ROTATION' - Stops rotation
|
|
|
|
* 'RESUME_ROTATION' - Resumes rotation
|
|
|
|
* 'HOME_PAGE' - Calls PAGED_CHANGED with the default home page.
|
2020-11-06 00:42:31 -08:00
|
|
|
* '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
|
2018-08-20 13:21:38 -07:00
|
|
|
*
|
2018-04-26 00:25:40 -07:00
|
|
|
* @param {string} notification the notification ID
|
2020-11-06 00:13:24 -08:00
|
|
|
* @param {number|string} payload the page to change to/by
|
2018-04-26 00:25:40 -07:00
|
|
|
*/
|
2020-04-07 08:52:39 -07:00
|
|
|
notificationReceived: function (notification, payload) {
|
2018-04-26 00:25:40 -07:00
|
|
|
switch (notification) {
|
|
|
|
case 'PAGE_CHANGED':
|
2024-12-01 11:29:26 -08:00
|
|
|
Log.log(`[MMM-pages] received a notification 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':
|
2024-12-01 11:29:26 -08:00
|
|
|
Log.log('[MMM-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':
|
2024-12-01 11:29:26 -08:00
|
|
|
Log.log('[MMM-pages] received a notification to decrement pages!');
|
2019-08-08 09:58:21 -07:00
|
|
|
// 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);
|
2018-08-20 13:21:38 -07:00
|
|
|
this.updatePages();
|
2018-04-26 00:25:40 -07:00
|
|
|
break;
|
|
|
|
case 'DOM_OBJECTS_CREATED':
|
2024-12-01 11:29:26 -08:00
|
|
|
Log.log('[MMM-pages] received that all objects are created; will now hide things!');
|
2018-04-28 18:14:35 -07:00
|
|
|
this.sendNotification('MAX_PAGES_CHANGED', this.config.modules.length);
|
2020-09-07 13:09:37 -07:00
|
|
|
this.sendNotification('NEW_PAGE', this.curPage);
|
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;
|
2020-04-16 16:59:19 -07:00
|
|
|
case 'PAUSE_ROTATION':
|
2020-11-07 07:53:53 -08:00
|
|
|
this.setRotation(false);
|
2020-04-16 16:59:19 -07:00
|
|
|
break;
|
|
|
|
case 'RESUME_ROTATION':
|
2020-11-07 07:53:53 -08:00
|
|
|
this.setRotation(true);
|
2020-04-16 16:59:19 -07:00
|
|
|
break;
|
2020-08-31 18:24:56 -07:00
|
|
|
case 'HOME_PAGE':
|
|
|
|
this.notificationReceived('PAGE_CHANGED', this.config.homePage);
|
|
|
|
break;
|
2020-11-05 10:53:44 -08:00
|
|
|
case 'SHOW_HIDDEN_PAGE':
|
2024-12-01 11:29:26 -08:00
|
|
|
Log.log(`[MMM-pages] received a notification to change to the hidden page "${payload}" of type "${typeof payload}".`);
|
2020-11-07 07:53:53 -08:00
|
|
|
this.setRotation(false);
|
2020-11-05 10:53:44 -08:00
|
|
|
this.showHiddenPage(payload);
|
|
|
|
break;
|
|
|
|
case 'LEAVE_HIDDEN_PAGE':
|
2024-12-01 11:29:26 -08:00
|
|
|
Log.log('[MMM-pages] received a notification to leave the current hidden page.');
|
2020-11-05 10:53:44 -08:00
|
|
|
this.animatePageChange();
|
2020-11-07 07:53:53 -08:00
|
|
|
this.setRotation(true);
|
2020-11-05 10:53:44 -08: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.
|
|
|
|
*/
|
2020-04-07 08:52:39 -07:00
|
|
|
changePageBy: function (amt, fallback) {
|
2020-11-06 07:27:17 -08:00
|
|
|
if (typeof amt !== 'number' && typeof fallback === 'undefined') {
|
2024-12-01 11:29:26 -08:00
|
|
|
Log.warn(`[MMM-pages] ${amt} is not a number!`);
|
2018-09-01 05:41:08 -07:00
|
|
|
}
|
|
|
|
|
2019-08-08 10:14:01 -07:00
|
|
|
if (typeof amt === 'number' && !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.
|
|
|
|
*/
|
2020-04-07 08:52:39 -07:00
|
|
|
updatePages: function () {
|
2024-12-01 11:29:38 -08:00
|
|
|
// Update if 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();
|
2020-04-16 16:59:19 -07:00
|
|
|
if (!this.rotationPaused) {
|
2021-12-09 17:10:47 -08:00
|
|
|
this.resetTimerWithDelay(0);
|
2020-04-16 16:59:19 -07:00
|
|
|
}
|
2020-04-07 08:52:39 -07:00
|
|
|
this.sendNotification('NEW_PAGE', this.curPage);
|
2024-12-02 11:56:04 -08:00
|
|
|
} else { Log.error('[MMM-pages] Pages are not 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.
|
2020-11-23 15:25:30 -08:00
|
|
|
*
|
2020-11-07 01:15:51 -08:00
|
|
|
* @param {string} [targetPageName] the name of the hiddenPage we want to show.
|
2020-11-06 00:42:31 -08:00
|
|
|
* Optional and only used when we want to switch to a hidden page
|
2018-08-20 13:21:38 -07:00
|
|
|
*/
|
2020-11-07 01:15:51 -08:00
|
|
|
animatePageChange: function (targetPageName) {
|
2020-11-23 15:36:20 -08:00
|
|
|
let lockStringObj = { lockString: this.identifier };
|
2020-11-23 15:25:30 -08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-11-23 15:29:50 -08:00
|
|
|
// 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;
|
|
|
|
|
2020-11-06 00:13:24 -08:00
|
|
|
MM.getModules()
|
|
|
|
.exceptWithClass(modulesToShow)
|
2020-11-23 15:29:50 -08:00
|
|
|
.enumerate(module => module.hide(animationTime, lockStringObj));
|
2020-11-06 00:13:24 -08:00
|
|
|
|
|
|
|
// Shows all modules meant to be on the current page, after a small delay.
|
|
|
|
setTimeout(() => {
|
2020-11-05 10:53:44 -08:00
|
|
|
MM.getModules()
|
2020-11-06 00:13:24 -08:00
|
|
|
.withClass(modulesToShow)
|
2020-11-23 15:29:50 -08:00
|
|
|
.enumerate(module => module.show(animationTime, lockStringObj));
|
|
|
|
}, animationTime);
|
2018-08-20 13:21:38 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Resets the page changing timer with a delay.
|
|
|
|
*
|
|
|
|
* @param {number} delay the delay, in milliseconds.
|
|
|
|
*/
|
2020-04-07 08:52:39 -07:00
|
|
|
resetTimerWithDelay: function (delay) {
|
2018-08-20 13:21:38 -07:00
|
|
|
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(() => {
|
2020-11-06 07:28:49 -08:00
|
|
|
// 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');
|
2020-09-07 13:48:19 -07:00
|
|
|
self.notificationReceived('PAGE_INCREMENT');
|
2018-08-20 13:21:38 -07:00
|
|
|
}, self.config.rotationTime);
|
|
|
|
}, delay);
|
2020-09-07 13:48:19 -07:00
|
|
|
} else if (this.config.rotationHomePage > 0) {
|
2020-06-02 17:57:24 -07:00
|
|
|
// 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(() => {
|
2020-11-06 07:28:49 -08:00
|
|
|
// 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);
|
2020-11-06 07:07:21 -08:00
|
|
|
self.notificationReceived('PAGE_CHANGED', self.config.homePage);
|
2020-09-07 13:48:19 -07:00
|
|
|
}, self.config.rotationHomePage);
|
2020-06-02 17:57:24 -07:00
|
|
|
}, delay);
|
2020-08-31 18:24:56 -07:00
|
|
|
}
|
2018-08-20 13:21:38 -07:00
|
|
|
},
|
2020-11-05 10:53:44 -08:00
|
|
|
|
|
|
|
/**
|
2020-11-06 00:13:24 -08:00
|
|
|
* Pause or resume the page rotation. If the provided isRotating value is
|
2020-11-07 07:53:53 -08:00
|
|
|
* set to true, it will resume the rotation. If the requested
|
2020-11-06 00:42:31 -08:00
|
|
|
* state (f.e. isRotating === true) equals the current state, print a warning
|
|
|
|
* and do nothing.
|
2020-11-05 10:53:44 -08:00
|
|
|
*
|
2020-11-06 00:13:24 -08:00
|
|
|
* @param {boolean} isRotating the parameter, if you want to pause or resume.
|
2020-11-05 10:53:44 -08:00
|
|
|
*/
|
2020-11-06 00:13:24 -08:00
|
|
|
setRotation: function (isRotating) {
|
2020-11-07 07:53:53 -08:00
|
|
|
const stateBaseString = (isRotating) ? "resum" : "paus";
|
2020-11-06 00:13:24 -08:00
|
|
|
if (isRotating === this.rotationPaused) {
|
2024-12-01 11:29:26 -08:00
|
|
|
Log.warn(`[MMM-pages] was asked to ${stateBaseString}e but rotation is already ${stateBaseString}ed!`);
|
2020-11-05 10:53:44 -08:00
|
|
|
} else {
|
2024-12-01 11:29:26 -08:00
|
|
|
Log.log(`[MMM-pages] ${stateBaseString}ing rotation`);
|
2020-11-07 07:53:53 -08:00
|
|
|
if (!isRotating) {
|
2020-11-05 10:53:44 -08:00
|
|
|
clearInterval(this.timer);
|
|
|
|
clearInterval(this.delayTimer);
|
|
|
|
} else {
|
|
|
|
this.resetTimerWithDelay(this.rotationDelay);
|
|
|
|
}
|
2020-11-06 00:13:24 -08:00
|
|
|
this.rotationPaused = isRotating;
|
2020-11-05 10:53:44 -08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handles hidden pages.
|
2020-11-23 15:25:30 -08:00
|
|
|
*
|
2020-11-06 00:13:24 -08:00
|
|
|
* @param {string} name the name of the hiddenPage we want to show
|
2020-11-05 10:53:44 -08:00
|
|
|
*/
|
|
|
|
showHiddenPage: function (name) {
|
2020-11-06 00:13:24 -08:00
|
|
|
// Only proceed if the named hidden page actually exists
|
|
|
|
if (name in this.config.hiddenPages) {
|
|
|
|
this.animatePageChange(name);
|
|
|
|
} else {
|
2024-12-01 11:29:26 -08:00
|
|
|
Log.error(`[MMM-pages] Hidden page "${name}" does not exist!`);
|
2020-11-05 10:53:44 -08:00
|
|
|
}
|
|
|
|
},
|
2017-06-18 13:24:04 -07:00
|
|
|
});
|