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