Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Darick Carpenter committed Aug 1, 2018
1 parent 58cd90b commit da8fef8
Show file tree
Hide file tree
Showing 4 changed files with 328 additions and 0 deletions.
21 changes: 21 additions & 0 deletions BackgroundSlideshow.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
.backgroundSlideShow {
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
background-position: center;
background-repeat: no-repeat;
opacity: 0;
transition: opacity 1s ease-in-out;
}

.gradient {
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
background-image: linear-gradient( to bottom, rgba(0, 0, 0, 0.75) 0%, rgba(0, 0, 0, 0.75) 25%, rgba(0, 0, 0, 0) 40%, rgba(0, 0, 0, 0) 75%, rgba(0, 0, 0, 0.75) 85%, rgba(0, 0, 0, 0.75) 100%);
opacity: 0.6;
}
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2017 Adam Moses

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
150 changes: 150 additions & 0 deletions MMM-BackgroundSlideshow.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
/* global Module */

/* MMM-BackgroundSlideshow.js
*
* Magic Mirror
* Module: MMM-ImageSlideshow
*
* Magic Mirror By Michael Teeuw http://michaelteeuw.nl
* MIT Licensed.
*
* Module MMM-Slideshow By Darick Carpenter
* MIT Licensed.
*/

Module.register("MMM-BackgroundSlideshow", {
// Default module config.
defaults: {
// an array of strings, each is a path to a directory with images
imagePaths: ["modules/MMM-BackgroundSlideshow/exampleImages"],
// the speed at which to switch between images, in milliseconds
slideshowSpeed: 10 * 1000,
// if true randomize image order, otherwise do alphabetical
randomizeImageOrder: false,
// if true combine all images in all the paths
// if false each path with be viewed seperately in the order listed
treatAllPathsAsOne: false,
// list of valid file extensions, seperated by commas
validImageFileExtensions: "bmp,jpg,gif,png",
backgroundOpacity: "1",
transitionSpeed: 1000,
backgroundSize: "contain",
transitionImages: false
},
// load function
start: function() {
// add identifier to the config
this.config.identifier = this.identifier;
// ensure file extensions are lower case
this.config.validImageFileExtensions = this.config.validImageFileExtensions.toLowerCase();
// set no error
this.errorMessage = null;
if (this.config.imagePaths.length == 0) {
this.errorMessage =
"MMM-BackgroundSlideshow: Missing required parameter.";
} else {
// create an empty image list
this.imageList = [];
// set beginning image index to 0, as it will auto increment on start
this.imageIndex = 0;
this.updateImageList();
}
},
// Define required scripts.
getStyles: function() {
// the css contains the make grayscale code
return ["BackgroundSlideshow.css"];
},
// the socket handler
socketNotificationReceived: function(notification, payload) {
// if an update was received
if (notification === "BACKGROUNDSLIDESHOW_FILELIST") {
// check this is for this module based on the woeid
if (payload.identifier === this.identifier) {
// set the image list
this.imageList = payload.imageList;
// if image list actually contains images
// set loaded flag to true and update dom
if (this.imageList.length > 0) {
this.resume();
}
}
}
},
// Override dom generator.
getDom: function() {
var wrapper = document.createElement("div");
this.div1 = this.createDiv("big1");
this.div2 = this.createDiv("big2");
var div3 = document.createElement("div");
div3.className = "gradient";

wrapper.appendChild(this.div1);
wrapper.appendChild(this.div2);
wrapper.appendChild(div3);

return wrapper;
},

createDiv: function(name) {
var div = document.createElement("div");
div.id = name + this.identifier;
div.style.backgroundSize = this.config.backgroundSize;
div.className = "backgroundSlideShow";
return div;
},

updateImage: function() {
if (this.imageList && this.imageList.length) {
if (this.imageIndex < this.imageList.length) {
if (this.config.transitionImages) {
this.swapDivs();
}
var div1 = this.div1;
var div2 = this.div2;

// div2.style.backgroundImage = div1.style.backgroundImage;

var image = new Image();
image.onload = function() {
div1.style.backgroundImage = "url('" + this.src + "')";
div1.style.opacity = "1";
div2.style.opacity = "0";
};
image.src = this.imageList[this.imageIndex];

this.imageIndex += 1;
} else {
this.imageIndex = 0;
this.updateImageList();
}
}
},

swapDivs: function() {
var temp = this.div1;
this.div1 = this.div2;
this.div2 = temp;
},

suspend: function() {
if (this.timer) {
clearInterval(this.timer);
}
},
resume: function() {
this.updateImage();
var self = this;
this.timer = setInterval(function() {
self.updateImage();
}, self.config.slideshowSpeed);
},
updateImageList: function() {
this.suspend();
// ask helper function to get the image list
this.sendSocketNotification(
"BACKGROUNDSLIDESHOW_REGISTER_CONFIG",
this.config
);
}
});
136 changes: 136 additions & 0 deletions node_helper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
/* global Module */

/* node_helper.js
*
* Magic Mirror
* Module: MMM-ImageSlideshow
*
* Magic Mirror By Michael Teeuw http://michaelteeuw.nl
* MIT Licensed.
*
* Module MMM-BackgroundSlideshow By Darick Carpenter
* MIT Licensed.
*/

// call in the required classes
var NodeHelper = require("node_helper");
var FileSystemImageSlideshow = require("fs");

// the main module helper create
module.exports = NodeHelper.create({
// subclass start method, clears the initial config array
start: function() {
this.moduleConfigs = [];
},
// shuffles an array at random and returns it
shuffleArray: function(array) {
var currentIndex = array.length,
temporaryValue,
randomIndex;
while (0 !== currentIndex) {
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex -= 1;
temporaryValue = array[currentIndex];
array[currentIndex] = array[randomIndex];
array[randomIndex] = temporaryValue;
}
return array;
},
// sort by filename attribute
sortByFilename: function(a, b) {
aL = a.filename.toLowerCase();
bL = b.filename.toLowerCase();
if (aL > bL) return 1;
else return -1;
},
// checks there's a valid image file extension
checkValidImageFileExtension: function(filename, extensions) {
var extList = extensions.split(",");
for (var extIndex = 0; extIndex < extList.length; extIndex++) {
if (filename.toLowerCase().endsWith(extList[extIndex])) return true;
}
return false;
},
// gathers the image list
gatherImageList: function(config) {
var self = this;
// create an empty main image list
var imageList = [];
// for each of the paths specified
for (var pathIndex = 0; pathIndex < config.imagePaths.length; pathIndex++) {
var currentPath = config.imagePaths[pathIndex];
var currentPathImageList = FileSystemImageSlideshow.readdirSync(
(path = currentPath)
);
// for each file in the current path
if (currentPathImageList.length > 0) {
// create an empty list for images in the current path
var currentImageList = [];
// for each file
for (
var imageIndex = 0;
imageIndex < currentPathImageList.length;
imageIndex++
) {
// seperate into path and filename
var currentImage = {
path: currentPath,
filename: currentPathImageList[imageIndex]
};
// check if file has a valid image file extension
var isValidImageFileExtension = this.checkValidImageFileExtension(
currentImage.filename,
config.validImageFileExtensions
);
// if file is valid, add it to the list
if (isValidImageFileExtension) currentImageList.push(currentImage);
}
// if not set to combine all paths, do random or alphabetical sort
if (!config.treatAllPathsAsOne) {
if (config.randomizeImageOrder)
currentImageList = this.shuffleArray(currentImageList);
else currentImageList = currentImageList.sort(this.sortByFilename);
}
// add current list main list
imageList = imageList.concat(currentImageList);
}
}
// if set to combine all paths, sort all images randomly or alphabetically by filename
if (config.treatAllPathsAsOne) {
if (config.randomizeImageOrder) imageList = this.shuffleArray(imageList);
else imageList = imageList.sort(this.sortByFilename);
}
// create a file image list combining paths and filenames
var imageListComplete = [];
for (var index = 0; index < imageList.length; index++) {
imageListComplete.push(
imageList[index].path + "/" + imageList[index].filename
);
}
// return final list
return imageListComplete;
},
// subclass socketNotificationReceived, received notification from module
socketNotificationReceived: function(notification, payload) {
if (notification === "BACKGROUNDSLIDESHOW_REGISTER_CONFIG") {
// add the current config to an array of all configs used by the helper
this.moduleConfigs.push(payload);
// this to self
var self = this;
// get the image list
var imageList = this.gatherImageList(payload);
// build the return payload
var returnPayload = {
identifier: payload.identifier,
imageList: imageList
};
// send the image list back
self.sendSocketNotification(
"BACKGROUNDSLIDESHOW_FILELIST",
returnPayload
);
}
}
});

//------------ end -------------

0 comments on commit da8fef8

Please sign in to comment.