-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompliments.js
191 lines (159 loc) · 4.61 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
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/* global Log, Module, moment */
/* Magic Mirror
* Module: Compliments
*
* By Michael Teeuw http://michaelteeuw.nl
* MIT Licensed.
*/
Module.register("compliments", {
// Module config defaults.
defaults: {
compliments: {
anytime: [
"You are my mirror-cle",
"Got asked to leave a halls of mirrors the other week. I said 'Don't worry, I'll see myself out.'"
],
morning: [
"I see a lot of myself in you"
],
afternoon: [
"Inspecting mirrors is a job I could really see myself doing"
],
evening: [
"I need some time to reflect"
]
},
updateInterval: 30000,
remoteFile: null,
fadeSpeed: 4000
},
// Set currentweather from module
currentWeatherType: "",
// Define required scripts.
getScripts: function() {
return ["moment.js"];
},
// Define start sequence.
start: function() {
Log.info("Starting module: " + this.name);
this.lastComplimentIndex = -1;
if (this.config.remoteFile != null) {
this.complimentFile((response) => {
this.config.compliments = JSON.parse(response);
});
}
// Schedule update timer.
var self = this;
setInterval(function() {
self.updateDom(self.config.fadeSpeed);
}, this.config.updateInterval);
},
/* randomIndex(compliments)
* Generate a random index for a list of compliments.
*
* argument compliments Array<String> - Array with compliments.
*
* return Number - Random index.
*/
randomIndex: function(compliments) {
if (compliments.length === 1) {
return 0;
}
var generate = function() {
return Math.floor(Math.random() * compliments.length);
};
var complimentIndex = generate();
while (complimentIndex === this.lastComplimentIndex) {
complimentIndex = generate();
}
this.lastComplimentIndex = complimentIndex;
return complimentIndex;
},
/* complimentArray()
* Retrieve an array of compliments for the time of the day.
*
* return compliments Array<String> - Array with compliments for the time of the day.
*/
complimentArray: function() {
var hour = moment().hour();
var compliments;
if (hour >= 3 && hour < 12 && this.config.compliments.hasOwnProperty("morning")) {
compliments = this.config.compliments.morning.slice(0);
} else if (hour >= 12 && hour < 17 && this.config.compliments.hasOwnProperty("afternoon")) {
compliments = this.config.compliments.afternoon.slice(0);
} else if(this.config.compliments.hasOwnProperty("evening")) {
compliments = this.config.compliments.evening.slice(0);
}
if (typeof compliments === "undefined") {
compliments = new Array();
}
if (this.currentWeatherType in this.config.compliments) {
compliments.push.apply(compliments, this.config.compliments[this.currentWeatherType]);
}
compliments.push.apply(compliments, this.config.compliments.anytime);
return compliments;
},
/* complimentFile(callback)
* Retrieve a file from the local filesystem
*/
complimentFile: function(callback) {
var xobj = new XMLHttpRequest();
xobj.overrideMimeType("application/json");
xobj.open("GET", this.file(this.config.remoteFile), true);
xobj.onreadystatechange = function() {
if (xobj.readyState == 4 && xobj.status == "200") {
callback(xobj.responseText);
}
};
xobj.send(null);
},
/* complimentArray()
* Retrieve a random compliment.
*
* return compliment string - A compliment.
*/
randomCompliment: function() {
var compliments = this.complimentArray();
var index = this.randomIndex(compliments);
return compliments[index];
},
// Override dom generator.
getDom: function() {
var complimentText = this.randomCompliment();
var compliment = document.createTextNode(complimentText);
var wrapper = document.createElement("div");
wrapper.className = this.config.classes ? this.config.classes : "thin xlarge bright";
wrapper.appendChild(compliment);
return wrapper;
},
// From data currentweather set weather type
setCurrentWeatherType: function(data) {
var weatherIconTable = {
"01d": "day_sunny",
"02d": "day_cloudy",
"03d": "cloudy",
"04d": "cloudy_windy",
"09d": "showers",
"10d": "rain",
"11d": "thunderstorm",
"13d": "snow",
"50d": "fog",
"01n": "night_clear",
"02n": "night_cloudy",
"03n": "night_cloudy",
"04n": "night_cloudy",
"09n": "night_showers",
"10n": "night_rain",
"11n": "night_thunderstorm",
"13n": "night_snow",
"50n": "night_alt_cloudy_windy"
};
this.currentWeatherType = weatherIconTable[data.weather[0].icon];
},
// Override notification handler.
notificationReceived: function(notification, payload, sender) {
if (notification == "CURRENTWEATHER_DATA") {
this.setCurrentWeatherType(payload.data);
}
},
});