-
Notifications
You must be signed in to change notification settings - Fork 9
/
MMM-RandomBackground.js
executable file
·161 lines (124 loc) · 4.09 KB
/
MMM-RandomBackground.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 */
/* Magic Mirror
* Module: MMM-RandomBackground
*/
Module.register('MMM-RandomBackground', {
defaults: {
animationSpeed: 1000,
updateInterval: 10 * 60 * 1000, // Update every 10 minutes.
showAdditionalInfo: false,
randomOrder: true,
opacity: 1.0,
photoDirectories: [] // Additional folders to find photos in
},
loaded: false,
start: function() {
console.log('Background module started!');
console.log('Display in order: ' + (this.config.randomOrder ? 'Yes' : 'No'));
this.imageIndex = 0;
this.images = {}
this.sendSocketNotification('RANDOM_IMAGES_GET');
},
getDom: function() {
var wrapper = document.createElement('div');
if (!this.loaded) {
wrapper.innerHTML = this.translate("LOADING");
return wrapper;
}
var image = this.images.photo[this.imageIndex];
var backgroundImage = document.createElement('div');
backgroundImage.className = 'background-image fullscreen';
var backgroundPlaceholder1 = document.createElement('div');
backgroundPlaceholder1.id = 'background-placeholder-1';
backgroundPlaceholder1.className = 'fullscreen';
var backgroundPlaceholder2 = document.createElement('div');
backgroundPlaceholder2.id = 'background-placeholder-2';
backgroundPlaceholder2.className = 'fullscreen';
backgroundImage.appendChild(backgroundPlaceholder1);
backgroundImage.appendChild(backgroundPlaceholder2);
wrapper.appendChild(backgroundImage);
var imageInfo = document.createElement('div');
imageInfo.className = 'image-info';
var imageTitle = document.createElement('div');
imageTitle.className = 'image-title';
var imageOwner = document.createElement('div');
imageOwner.className = 'image-owner';
imageOwner.id = 'image-owner';
imageInfo.appendChild(imageTitle);
imageInfo.appendChild(imageOwner);
if (this.config.showAdditionalInfo) {
wrapper.appendChild(imageInfo);
}
return wrapper;
},
loadImage: function() {
var self = this;
// Refactor this code
var backgroundPlaceholder1 = $('#background-placeholder-1');
var backgroundPlaceholder2 = $('#background-placeholder-2');
if (backgroundPlaceholder1.is(':visible')) {
var top = backgroundPlaceholder1;
var bottom = backgroundPlaceholder2;
} else {
var top = backgroundPlaceholder2;
var bottom = backgroundPlaceholder1;
}
var image = self.images.photo[self.imageIndex];
$('<img/>').attr('src', image.photolink).load(function() {
$('#background-placeholder-1').css({
background: '#000 url("' + image.photolink + '") center center',
backgroundSize: 'cover',
backgroundRepeat: 'no-repeat'
}).animate({
opacity: self.config.opacity
}, self.config.animationSpeed, function() {
$(this).attr('id', 'background-placeholder-2');
$('#image-owner').html('Taken by ' + image.takenBy + ' on ' + image.dateTaken);
});
$('#background-placeholder-2').animate({
opacity: 0
}, self.config.animationSpeed, function() {
$(this).attr('id', 'background-placeholder-1');
});
});
},
scheduleUpdateInterval: function() {
var self = this;
console.log('Scheduled update interval set up...');
self.updateDom(self.config.animationSpeed);
setInterval(function() {
// Get random photo from array
self.nextImageIndex();
self.loadImage();
}, this.config.updateInterval);
},
socketNotificationReceived: function(notification, payload) {
if (notification === 'RANDOM_IMAGE_LIST') {
this.images = payload;
if (!this.loaded) {
this.scheduleUpdateInterval();
}
this.loaded = true;
this.updateDom();
this.loadImage();
}
},
nextImageIndex: function() {
var self = this;
var imageCount = self.images.photo.length;
if (this.config.randomOrder) {
this.imageIndex = Math.round(Math.random() * (imageCount - 1));
} else {
this.imageIndex++;
if (this.imageIndex == imageCount) { // last image, reset counter
this.imageIndex = 0;
}
}
},
getStyles: function(){
return [ 'random-background.css' ]
},
getScripts: function() {
return [ this.file('node_modules/jquery/dist/jquery.min.js') ];
}
});