forked from PJTewkesbury/MMM-PlexSlideshow
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMMM-PlexSlideshow.js
More file actions
223 lines (209 loc) · 7.07 KB
/
MMM-PlexSlideshow.js
File metadata and controls
223 lines (209 loc) · 7.07 KB
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/* global Module */
/* MMM-PlexSlideshow.js
*
* Magic Mirror
* Module: MMM-PlexSlideshow - Modifications by Peter Tewkesbury, Original code by Adam Moses and Darick Carpenter.
*
* Magic Mirror By Michael Teeuw http://michaelteeuw.nl
* MIT Licensed.
*
* Based Module MMM-BackgroundSlideShow by Darick Carpenter
* and that is based on MMM-ImageSlideShow by Adam Moses
* MIT Licensed.
*/
Module.register('MMM-PlexSlideshow', {
// Default module config.
defaults: {
plex: {
hostname:"localhost",
port: 32400,
username:"",
password:"",
apiToken:""
},
// the speed at which to switch between images, in milliseconds
slideshowSpeed: 10 * 1000,
// if true randomize image order, otherwise do alphabetical
randomizeImageOrder: false,
// transition speed from one image to the other, transitionImages must be true
transitionSpeed: '1s',
// the sizing of the background image
// cover: Resize the background image to cover the entire container, even if it has to stretch the image or cut a little bit off one of the edges
// contain: Resize the background image to make sure the image is fully visible
backgroundSize: 'cover', // cover or contain
// transition from one image to the other (may be a bit choppy on slower devices, or if the images are too big)
transitionImages: false,
// the gradient to make the text more visible
gradient: [
'rgba(0, 0, 0, 0.75) 0%',
'rgba(0, 0, 0, 0) 40%',
'rgba(0, 0, 0, 0) 80%',
'rgba(0, 0, 0, 0.75) 100%'
],
horizontalGradient: [
'rgba(0, 0, 0, 0.75) 0%',
'rgba(0, 0, 0, 0) 40%',
'rgba(0, 0, 0, 0) 80%',
'rgba(0, 0, 0, 0.75) 100%'
],
// the direction the gradient goes, vertical or horizontal
gradientDirection: 'vertical'
},
// load function
start: function() {
// add identifier to the config
this.config.identifier = this.identifier;
// set no error
this.errorMessage = null;
if (this.config.plex.hostname.length == 0 || this.config.plex.username.length==0 || this.config.plex.password.length==0) {
this.errorMessage =
'MMM-PlexSlideshow: 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 ['PlexSlideshow.css'];
},
// generic notification handler
notificationReceived: function(notification, payload, sender) {
if (sender) {
Log.log(this.name + " received a module notification: " + notification + " from sender: " + sender.name);
if(notification === 'BACKGROUNDSLIDESHOW_IMAGE_UPDATE'){
Log.log("MMM-PlexSlideshow: Changing Background");
this.suspend();
this.updateImage();
this.resume();
}
else if (notification === 'BACKGROUNDSLIDESHOW_NEXT'){ // Change to next image
this.updateImage();
if(this.timer){ // Restart timer only if timer was already running
this.resume();
}
}
else if (notification === 'BACKGROUNDSLIDESHOW_PLAY'){ // Change to next image and start timer.
this.updateImage();
this.resume();
}
else if (notification === 'BACKGROUNDSLIDESHOW_PAUSE'){ // Stop timer.
this.suspend();
}
else {
Log.log(this.name + " received a system notification: " + notification);
}
}
},
// 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) {
// console.info('Returning Images, payload:' + JSON.stringify(payload));
// 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.updateImage(); //Added to show the image at least once, but not change it within this.resume()
this.resume();
}
}
}
},
// Override dom generator.
getDom: function() {
var wrapper = document.createElement('div');
this.div1 = this.createDiv('big1');
this.div2 = this.createDiv('big2');
wrapper.appendChild(this.div1);
wrapper.appendChild(this.div2);
if (
this.config.gradientDirection === 'vertical' ||
this.config.gradientDirection === 'both'
) {
this.createGradientDiv('bottom', this.config.gradient, wrapper);
}
if (
this.config.gradientDirection === 'horizontal' ||
this.config.gradientDirection === 'both'
) {
this.createGradientDiv('right', this.config.gradient, wrapper);
}
return wrapper;
},
createGradientDiv: function(direction, gradient, wrapper) {
var div = document.createElement('div');
div.style.backgroundImage =
'linear-gradient( to ' + direction + ', ' + gradient.join() + ')';
div.className = 'gradient';
wrapper.appendChild(div);
},
createDiv: function(name) {
var div = document.createElement('div');
div.id = name + this.identifier;
div.style.backgroundSize = this.config.backgroundSize;
div.style.transition =
'opacity ' + this.config.transitionSpeed + ' ease-in-out';
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 = encodeURI(this.imageList[this.imageIndex]);
this.sendNotification('BACKGROUNDSLIDESHOW_IMAGE_UPDATED', {url:image.src});
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);
this.timer = null;
}
},
resume: function() {
//this.updateImage(); //Removed to prevent image change whenever MMM-Carousel changes slides
this.suspend();
var self = this;
this.timer = setInterval(function() {
self.updateImage();
}, self.config.slideshowSpeed);
},
updateImageList: function() {
this.suspend();
// console.info('Getting Images');
// ask helper function to get the image list
this.sendSocketNotification(
'BACKGROUNDSLIDESHOW_REGISTER_CONFIG',
this.config
);
}
});