-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMMM-Random-local-image.js
176 lines (150 loc) · 4.76 KB
/
MMM-Random-local-image.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/* global Module */
/* Magic Mirror
* Module: MMM-Random-local-image
*/
Module.register("MMM-Random-local-image", {
defaults: {
photoDir: "./modules/MMM-Random-local-image/exampleImages/",
photoUpdateInterval: 30 * 1000,
photoLoadInitialDelay: 1000,
photoLoadUpdateInterval: 12 * 60 * 60 * 1000,
randomOrder: true,
selectFromSubdirectories: false,
ignoreVideos: false,
ignoreDirRegex: "a^", // default matching nothing
opacity: 1.0,
showAdditionalInformation: false,
maxWidth: "100%",
maxHeight: "100%",
},
initialImageLoadingFinished: false,
images: [],
imageIndex: 0,
error: null,
start: function () {
Log.info(`Module ${this.name} started...`);
Log.debug("Configuration: : " + this.config);
// TODO: check it for all configs and check type
if (!this.config.photoDir) {
this.error = "Missing required parameter 'photoDir'";
}
setTimeout(() => this.loadImages(), this.config.photoLoadInitialDelay);
},
/**
* Triggers node_helper.js to load the images from the disk
*/
loadImages: function () {
Log.info("Retrieving images...");
this.sendSocketNotification("RANDOM_IMAGES_GET", this.config);
},
/**
* Receives images from node_helper.js
* @param notification type of the notification
* @param payload images
*/
socketNotificationReceived: function (notification, payload) {
if (notification === "RANDOM_IMAGE_LIST") {
// init
this.images = payload;
this.imageIndex = -1;
if (this.config.randomOrder) {
shuffle(this.images);
}
Log.info(`Received ${this.images.length} images`);
const isFirstTime = !this.initialImageLoadingFinished;
this.initialImageLoadingFinished = true;
this.loadNextImage();
if (isFirstTime) {
setInterval(
() => this.loadNextImage(),
this.config.photoUpdateInterval,
);
setInterval(
() => this.loadImages(),
this.config.photoLoadUpdateInterval,
);
}
}
},
loadNextImage: function () {
const allImagesShown = this.setNextImage();
if (allImagesShown) {
// this.loadImages(); // TODO: add option to add this code line (load new images when all pictures where shown)
}
this.updateDom(); // built-in function
},
setNextImage: function () {
const nextIndex = this.imageIndex + 1;
this.imageIndex = this.imageIndex + 1;
// all images shown? --> reset counter
if (this.imageIndex > this.images.length - 1) {
this.imageIndex = 0;
return true;
}
this.imageIndex = nextIndex;
return false;
},
getDom: function () {
const wrapper = document.createElement("div");
if (this.error) {
wrapper.innerHTML = this.translate(this.error);
return wrapper;
}
if (!this.initialImageLoadingFinished) {
wrapper.innerHTML = this.translate("LOADING");
return wrapper;
}
const image = this.images[this.imageIndex];
if (!image) {
Log.error(`Could not load image (index: ${this.imageIndex})`);
wrapper.innerHTML = this.translate("ERROR LOADING");
return wrapper;
}
wrapper.appendChild(this.createImageElement(image));
if (this.config.showAdditionalInformation) {
wrapper.appendChild(this.createFilePathElement(image));
}
return wrapper;
},
createImageElement: function (image) {
const mediaType = image.mimeType.split("/")[0];
let element = document.createElement("img");
if (mediaType === "video") {
element = document.createElement("video");
element.type = image.mime;
}
element.src = image.fullPath;
element.style.maxWidth = this.config.maxWidth;
element.style.maxHeight = this.config.maxHeight;
element.style.opacity = this.config.opacity;
return element;
},
createFilePathElement: function (image) {
const element = document.createElement("div");
element.className = "dimmed small regular"; // use styles from magic mirrors main.css
const node = document.createTextNode(image.relativePath);
element.appendChild(node);
return element;
},
});
/**
* Randomly shuffle an array
* https://stackoverflow.com/a/2450976/1293256
* @param {Array} array The array to shuffle
* @return {String} The first item in the shuffled array
*/
function shuffle(array) {
let currentIndex = array.length;
let temporaryValue, randomIndex;
// While there remain elements to shuffle...
while (0 !== currentIndex) {
// Pick a remaining element...
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex -= 1;
// And swap it with the current element.
temporaryValue = array[currentIndex];
array[currentIndex] = array[randomIndex];
array[randomIndex] = temporaryValue;
}
return array;
}