forked from MagicMirrorOrg/MagicMirror
-
Notifications
You must be signed in to change notification settings - Fork 0
/
compliments.js
70 lines (54 loc) · 1.92 KB
/
compliments.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
var compliments = {
complimentLocation: '.compliment',
currentCompliment: '',
complimentList: {
'morning': config.compliments.morning,
'afternoon': config.compliments.afternoon,
'evening': config.compliments.evening
},
updateInterval: config.compliments.interval || 30000,
fadeInterval: config.compliments.fadeInterval || 4000,
intervalId: null
};
/**
* Changes the compliment visible on the screen
*/
compliments.updateCompliment = function () {
var _list = [];
var hour = moment().hour();
// In the following if statement we use .slice() on the
// compliments array to make a copy by value.
// This way the original array of compliments stays intact.
if (hour >= 3 && hour < 12) {
// Morning compliments
_list = compliments.complimentList['morning'].slice();
} else if (hour >= 12 && hour < 17) {
// Afternoon compliments
_list = compliments.complimentList['afternoon'].slice();
} else if (hour >= 17 || hour < 3) {
// Evening compliments
_list = compliments.complimentList['evening'].slice();
} else {
// Edge case in case something weird happens
// This will select a compliment from all times of day
Object.keys(compliments.complimentList).forEach(function (_curr) {
_list = _list.concat(compliments.complimentList[_curr]).slice();
});
}
// Search for the location of the current compliment in the list
var _spliceIndex = _list.indexOf(compliments.currentCompliment);
// If it exists, remove it so we don't see it again
if (_spliceIndex !== -1) {
_list.splice(_spliceIndex, 1);
}
// Randomly select a location
var _randomIndex = Math.floor(Math.random() * _list.length);
compliments.currentCompliment = _list[_randomIndex];
$('.compliment').updateWithText(compliments.currentCompliment, compliments.fadeInterval);
}
compliments.init = function () {
this.updateCompliment();
this.intervalId = setInterval(function () {
this.updateCompliment();
}.bind(this), this.updateInterval)
}