|
| 1 | +/* global Module */ |
| 2 | + |
| 3 | +/* MMM-PenPlotter.js |
| 4 | + * |
| 5 | + * Magic Mirror |
| 6 | + * Module: MMM-PenPlotter |
| 7 | + * |
| 8 | + * |
| 9 | + * Magic Mirror By Michael Teeuw http://michaelteeuw.nl |
| 10 | + * MIT Licensed. |
| 11 | + * |
| 12 | + * MMM-PenPlotter.js by Jason Bamford https://jbamford.github.io/ |
| 13 | + * |
| 14 | + * Derivate work of |
| 15 | + * Module MMM-ImageSlideshow By Adam Moses http://adammoses.com |
| 16 | + * MIT Licensed. |
| 17 | + */ |
| 18 | + |
| 19 | +Module.register("MMM-ImageSlideshow", { |
| 20 | + // Default module config. |
| 21 | + defaults: { |
| 22 | + // an array of strings, each is a path to a directory with images |
| 23 | + // imagePaths: [ 'modules/MMM-ImageSlideshow/exampleImages' ], |
| 24 | + imagePaths:[], |
| 25 | + // the speed at which to switch between images, in milliseconds 300000 = 5mins |
| 26 | + slideshowSpeed: 65000, |
| 27 | + // if zero do nothing, otherwise set width to a pixel value |
| 28 | + fixedImageWidth: 0, |
| 29 | + // if zero do nothing, otherwise set height to a pixel value |
| 30 | + fixedImageHeight: 0, |
| 31 | + // if true randomize image order, otherwise do alphabetical |
| 32 | + randomizeImageOrder: false, |
| 33 | + // if true combine all images in all the paths |
| 34 | + // if false each path with be viewed seperately in the order listed |
| 35 | + treatAllPathsAsOne: false, |
| 36 | + // if true, all images will be made grayscale, otherwise as they are |
| 37 | + makeImagesGrayscale: false, |
| 38 | + // list of valid file extensions, seperated by commas |
| 39 | + validImageFileExtensions: 'bmp,jpg,gif,png,svg', |
| 40 | + // a delay timer after all images have been shown, to wait to restart (in ms) |
| 41 | + delayUntilRestart: 0, |
| 42 | + }, |
| 43 | + // load function |
| 44 | + start: function () { |
| 45 | + |
| 46 | + |
| 47 | + // add identifier to the config |
| 48 | + this.config.identifier = this.identifier; |
| 49 | + // ensure file extensions are lower case |
| 50 | + this.config.validImageFileExtensions = this.config.validImageFileExtensions.toLowerCase(); |
| 51 | + // set no error |
| 52 | + this.errorMessage = null; |
| 53 | + if (this.config.imagePaths.length == 0) { |
| 54 | + this.errorMessage = "MMM-ImageSlideshow: Missing required parameter." |
| 55 | + } |
| 56 | + else { |
| 57 | + // create an empty image list |
| 58 | + this.imageList = []; |
| 59 | + // set beginning image index to -1, as it will auto increment on start |
| 60 | + this.imageIndex = -1; |
| 61 | + // ask helper function to get the image list |
| 62 | + this.sendSocketNotification('IMAGESLIDESHOW_REGISTER_CONFIG', this.config); |
| 63 | + // do one update time to clear the html |
| 64 | + this.updateDom(); |
| 65 | + // set a blank timer |
| 66 | + this.interval = null; |
| 67 | + } |
| 68 | + }, |
| 69 | + // Define required scripts. |
| 70 | + getStyles: function() { |
| 71 | + // the css contains the make grayscale code |
| 72 | + return ["imageslideshow.css"]; |
| 73 | + }, |
| 74 | + // the socket handler |
| 75 | + socketNotificationReceived: function(notification, payload) { |
| 76 | + // if an update was received |
| 77 | + if (notification === "IMAGESLIDESHOW_FILELIST") { |
| 78 | + // check this is for this module based on the woeid |
| 79 | + if (payload.identifier === this.identifier) |
| 80 | + { |
| 81 | + // set the image list |
| 82 | + this.imageList = payload.imageList; |
| 83 | + // if image list actually contains images |
| 84 | + // set loaded flag to true and update dom |
| 85 | + if (this.imageList.length > 0) { |
| 86 | + this.loaded = true; |
| 87 | + this.updateDom(); |
| 88 | + // set the timer schedule to the slideshow speed |
| 89 | + var self = this; |
| 90 | + this.interval = setInterval(function() { |
| 91 | + self.updateDom(); |
| 92 | + }, this.config.slideshowSpeed); |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | + }, |
| 97 | + // Override dom generator. |
| 98 | + getDom: function () { |
| 99 | + var wrapper = document.createElement("div"); |
| 100 | + // if an error, say so (currently no errors can occur) |
| 101 | + if (this.errorMessage != null) { |
| 102 | + wrapper.innerHTML = this.errorMessage; |
| 103 | + } |
| 104 | + // if no errors |
| 105 | + else { |
| 106 | + // if the image list has been loaded |
| 107 | + if (this.loaded === true) { |
| 108 | + // if was delayed until restart, reset index reset timer |
| 109 | + if (this.imageIndex == -2) { |
| 110 | + this.imageIndex = -1; |
| 111 | + clearInterval(this.interval); |
| 112 | + var self = this; |
| 113 | + this.interval = setInterval(function() { |
| 114 | + self.updateDom(0); |
| 115 | + }, this.config.slideshowSpeed); |
| 116 | + } |
| 117 | + // iterate the image list index |
| 118 | + this.imageIndex += 1; |
| 119 | + // set flag to show stuff |
| 120 | + var showSomething = true; |
| 121 | + // if exceeded the size of the list, go back to zero |
| 122 | + if (this.imageIndex == this.imageList.length) { |
| 123 | + // if delay after last image, set to wait |
| 124 | + if (this.config.delayUntilRestart > 0) { |
| 125 | + this.imageIndex = -2; |
| 126 | + wrapper.innerHTML = " "; |
| 127 | + showSomething = false; |
| 128 | + clearInterval(this.interval); |
| 129 | + var self = this; |
| 130 | + this.interval = setInterval(function() { |
| 131 | + self.updateDom(0); |
| 132 | + }, this.config.delayUntilRestart); |
| 133 | + } |
| 134 | + // if not reset index |
| 135 | + else |
| 136 | + this.imageIndex = 0; |
| 137 | + } |
| 138 | + if (showSomething) { |
| 139 | + |
| 140 | + // create the image dom bit |
| 141 | + |
| 142 | + var obj = document.createElement('object'); |
| 143 | + |
| 144 | + // obj.data = "modules/MMM-ImageSlideshow/example_svg/spiralbezier.svg" |
| 145 | + obj.data = encodeURI(this.imageList[this.imageIndex]); |
| 146 | + obj.type = "image/svg+xml" |
| 147 | + obj.width ="100%" |
| 148 | + obj.height ="100%" |
| 149 | + obj.id = "alphasvg" |
| 150 | + |
| 151 | + wrapper.appendChild(obj); |
| 152 | + |
| 153 | + } |
| 154 | + } |
| 155 | + else { |
| 156 | + // if no data loaded yet, empty html |
| 157 | + wrapper.innerHTML = " "; |
| 158 | + } |
| 159 | + } |
| 160 | + // return the dom |
| 161 | + return wrapper; |
| 162 | + } |
| 163 | +}); |
0 commit comments