-
Notifications
You must be signed in to change notification settings - Fork 1
/
node_helper.js
127 lines (123 loc) · 5.13 KB
/
node_helper.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
/* global Module */
/* node_helper.js
*
* Magic Mirror
* Module: MMM-ImageSlideshow
*
* Magic Mirror By Michael Teeuw http://michaelteeuw.nl
* MIT Licensed.
*
* MMM-PenPlotter.js by Jason Bamford https://jbamford.github.io/
* MIT Licensed.
*
* Derivate work of
* Module MMM-ImageSlideshow By Adam Moses http://adammoses.com
* MIT Licensed.
*/
// call in the required classes
var NodeHelper = require("node_helper");
var FileSystemPenPlotter = 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 = FileSystemPenPlotter.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 === "PENPLOTTER_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('PENPLOTTER_FILELIST', returnPayload );
}
},
});
//------------ end -------------