MMM-pages/MMM-pages.js

87 lines
3.4 KiB
JavaScript
Raw Normal View History

2017-06-18 13:24:04 -07:00
Module.register("MMM-pages", {
defaults: {
modules: [],
excludes: ["MMM-page-indicator"],
animationTime: 1000,
rotationTime: 0,
rotationDelay: 10000
2017-06-18 13:24:04 -07:00
},
getStyles: function () {
2017-06-18 13:24:04 -07:00
return ["pages.css"];
},
start: function () {
2017-06-18 13:24:04 -07:00
this.curPage = 0;
// 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);
Log.log(this.config.rotationTime + "|" + this.config.rotationDelay);
2017-06-18 13:24:04 -07:00
},
notificationReceived: function (notification, payload, sender) {
2017-06-18 13:24:04 -07:00
if (notification === "PAGE_CHANGED") {
Log.log(this.name + " recieved a notification to change to page " + payload);
this.curPage = payload;
this.updatePages(true);
2017-06-18 13:24:04 -07:00
} else if (notification === "PAGE_INCREMENT") {
Log.log(this.name + " recieved a notification to increment pages!");
this.curPage = (this.curPage++) % this.config.modules.length;
this.updatePages(true);
2017-06-18 13:24:04 -07:00
} else if (notification === "PAGE_DECREMENT") {
Log.log(this.name + " recieved a notification to decrement pages!");
// Javascript doesn't support negative modulo (-1 % 5 != 4)
2017-06-18 13:24:04 -07:00
if (this.curPage === 0) {
this.curPage = this.config.modules.length - 1;
} else { this.curPage-- }
this.updatePages(true);
2017-06-18 13:24:04 -07:00
} else if (notification === "DOM_OBJECTS_CREATED") {
Log.log(this.name + " recieved that all objects are created; will now hide things!");
this.updatePages(true);
2017-06-18 13:24:04 -07:00
this.sendNotification("MAX_PAGES_CHANGED", this.config.modules.length);
}
},
2017-06-18 14:16:45 -07:00
// TODO: Add slide-left/right animation
updatePages: function (manuallyCalled) {
2017-06-18 13:24:04 -07:00
if (this.config.modules.length !== 0) {
Log.log("updatePages was called with manuallyCalled = " + manuallyCalled);
const self = this;
MM.getModules()
2017-06-18 13:24:04 -07:00
.exceptWithClass(this.config.excludes)
.exceptWithClass(this.config.modules[this.curPage])
.enumerate(module => { module.hide(this.config.animationTime / 2, { lockString: this.identifier }) });
setTimeout(function () {
2017-06-18 13:24:04 -07:00
MM.getModules()
.withClass(self.config.modules[self.curPage])
.enumerate(module => {
module.show(self.config.animationTime / 2,
{ lockString: self.identifier });
});
2017-06-18 13:24:04 -07:00
}, this.config.animationTime / 2);
if (manuallyCalled && this.config.rotationTime > 0) {
Log.log("manually updated page! setting delay before resume timer!");
clearInterval(this.timer);
setTimeout(() => {
self.timer = setInterval(() => {
// Incrementing page
self.curPage = (self.curPage++) % self.config.modules.length;
self.sendNotification("PAGE_INCREMENT");
self.updatePages(false);
}, self.config.rotationTime, false);
}, this.config.rotationDelay);
}
2017-06-18 13:24:04 -07:00
} else { Log.error("Pages aren't properly defined!") }
2017-06-18 13:24:04 -07:00
},
2017-06-18 13:24:04 -07:00
});