-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMMM-OnlineImagesViewer.js
92 lines (77 loc) · 2.16 KB
/
MMM-OnlineImagesViewer.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
/* global Module */
Module.register("MMM-OnlineImagesViewer",{
defaults: {
opacity: 1,
animationSpeed: 500,
updateInterval: 60000,
maxWidth: "100%",
maxHeight: "100%",
showAll: true,
random: false,
numColumns: 3
},
start: function() {
var self = this;
this.images = this.config.images;
Log.info(this.config.updateInterval);
this.loaded = false;
this.lastImageIndex = 0;
this.gridColumns = Array(this.config.numColumns).fill('auto').join(' ');
// Schedule update timer
setInterval(function() {
self.updateDom(self.config.animationSpeed);
}, this.config.updateInterval);
},
randomIndex: function(images) {
if (images.length === 1) {
return 0;
}
var generate = function() {
return Math.floor(Math.random() * images.length);
};
var ImageIndex = generate();
while (this.images.length > 1 && ImageIndex == this.lastImageIndex) {
ImageIndex = generate();
}
this.lastImageIndex = ImageIndex;
return ImageIndex;
},
randomPhoto: function() {
var images = this.images;
var index = this.randomIndex(images);
return images[index];
},
getDom: function() {
var wrapper = document.createElement("div");
wrapper.style.display = "grid";
wrapper.style.gridTemplateColumns = this.gridColumns;
if (this.config.showAll) {
this.images.forEach(photoImageUrl => {
this.appendImage(wrapper, photoImageUrl);
});
} else {
if (this.config.random) {
var photoImageUrl = this.randomPhoto();
} else {
var photoImageUrl = this.images[this.lastImageIndex];
this.lastImageIndex = (this.lastImageIndex+1) % this.images.length;
}
this.appendImage(wrapper, photoImageUrl);
}
return wrapper;
},
appendImage: function(wrapper, photoImageUrl) {
var img = document.createElement("img");
// get the photo image url and add a t parameter
let imgSrcURL = new URL(photoImageUrl);
imgSrcURL.searchParams.append('t', new Date().getTime());
img.src = imgSrcURL.href;
img.style.maxWidth = this.config.maxWidth;
img.style.maxHeight = this.config.maxHeight;
img.style.opacity = this.config.opacity;
wrapper.appendChild(img);
},
getScripts: function() {
return ["MMM-OnlineImagesViewer.css"]
},
});