-
Notifications
You must be signed in to change notification settings - Fork 1
/
concertcalendar.js
215 lines (175 loc) · 5.55 KB
/
concertcalendar.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/* Magic Mirror
* Module: ConcertCalendar
*
* By Marc Pratllusà https://github.com/muilpp
* based on a Script from Benjamin Angst http://www.beny.ch
* MIT Licensed.
*/
Module.register("concertcalendar",{
// Define module defaults
defaults: {
//maximumArtist: 350, // Number of bands to check if they are on tour
concertsPerPage: 12,
updateInterval: 60 * 60 * 24 * 1000, // Once a day.
paginationInterval: 20 * 1000, // Every twenty seconds.
animationSpeed: 2000,
fade: true,
fadePoint: 0.25, // Start on 1/4th of the list.
initialLoadDelay: 0, // start delay seconds.
apiBase: 'http://localhost:8092/concerts/lastfmUser/lastfmKey/latitude/longitude?bandsLimit=limit',
titleReplace: {
"Upcoming Concerts Calendar ": ""
},
},
// Define required scripts.
getStyles: function() {
return ["concertcalendar.css", "font-awesome.css"];
},
// Define required scripts.
getScripts: function() {
return ["moment.js"];
},
// Define start sequence.
start: function() {
Log.info("Starting module: " + this.name);
// Set locale.
moment.locale(config.language);
this.concerts = [];
this.visibleConcerts = [];
this.loaded = false;
this.paginationTimer = null;
this.paginationIndex = 0;
this.scheduleUpdate(this.config.initialLoadDelay);
this.paginationUpdate();
this.updateTimer = null;
},
// Override dom generator.
getDom: function() {
var wrapper = document.createElement("div");
var table = document.createElement("table");
table.className = "small";
for (var t in this.visibleConcerts) {
var concert = this.visibleConcerts[t];
var row = document.createElement("tr");
table.appendChild(row);
var concertArtistCell = document.createElement("td");
concertArtistCell.className = "from";
concertArtistCell.innerHTML = concert.artist;
row.appendChild(concertArtistCell);
var concertCityCell = document.createElement("td");
concertCityCell.innerHTML = " - " + concert.city.trim()+", ";
concertCityCell.className = "align-right trainto";
row.appendChild(concertCityCell);
var concertDate = document.createElement("td");
concertDate.innerHTML = concert.concertDate;
concertDate.className = "align-right trainto";
row.appendChild(concertDate);
}
return table;
},
/* updateTimetable()
* Calls processConcerts on succesfull response.
*/
updateTimetable: function() {
var url = this.config.apiBase; //+ this.getParams();
var self = this;
var retry = true;
var concertRequest = new XMLHttpRequest();
concertRequest.open("GET", url, true);
concertRequest.onreadystatechange = function() {
if (this.readyState === 4) {
if (this.status === 200) {
self.processConcerts(JSON.parse(this.response));
} else if (this.status === 401) {
self.config.id = "";
self.updateDom(self.config.animationSpeed);
Log.error(self.name + ": Incorrect, 401 reponse...");
retry = false;
} else {
Log.error(self.name + ": Could not load concerts.");
}
if (retry) {
self.scheduleUpdate((self.loaded) ? -1 : self.config.retryDelay);
}
}
};
concertRequest.send();
},
/* getParams(compliments)
* Generates an url with api parameters based on the config.
*
* return String - URL params.
*/
getParams: function() {
var params = "/";
params += this.config.user;
params += "/" + this.config.lastFmKey;
params += "/" + this.config.area;
params += "?limit=" + this.config.maximumArtist;
return params;
},
/* processConcerts(data)
* Uses the received data to set the various values.
*
* argument data object - Weather information received form openweather.org.
*/
processConcerts: function(data) {
this.concerts = [];
data.forEach((concert) => {
//console.log(concert.city);
if (concert.city !== null) {
var city = concert.city.split(",");
if (city.length > 0) {
cityToAdd = city[0];
if (city[0].length > 9)
cityToAdd = city[0].substring(0,9)+"..";
artistToAdd = concert.artist;
if (artistToAdd.length > 17)
artistToAdd = artistToAdd.substring(0,17)+"..";
var date = new Date(concert.date);
this.concerts.push({
artist: artistToAdd.trim(),
city: cityToAdd.trim(),
concertDate: date.getDate()+"/"+(date.getMonth()+1)
});
}
}
});
this.loaded = true;
concertsToShow = this.concerts.slice(0,this.config.concertsPerPage);
this.visibleConcerts = this.visibleConcerts.concat(concertsToShow);
this.updateDom(this.config.animationSpeed);
},
paginate: function() {
this.paginationIndex = this.paginationIndex + this.config.concertsPerPage;
if (this.concerts.length > this.paginationIndex) {
this.visibleConcerts = this.concerts.slice(this.paginationIndex, this.paginationIndex+this.config.concertsPerPage);
} else {
this.visibleConcerts = this.concerts.slice(0,this.config.concertsPerPage);
this.paginationIndex = 0;
}
this.updateDom(this.config.animationSpeed);
},
/* scheduleUpdate()
* Schedule next update.
*
* argument delay number - Milliseconds before next update. If empty, this.config.updateInterval is used.
*/
scheduleUpdate: function(delay) {
var nextLoad = this.config.updateInterval;
if (typeof delay !== "undefined" && delay >= 0) {
nextLoad = delay;
}
var self = this;
clearTimeout(this.updateTimer);
this.updateTimer = setTimeout(function() {
self.updateTimetable();
}, nextLoad);
},
paginationUpdate: function() {
var self = this;
setInterval(function() {
self.paginate();
}, this.config.paginationInterval);
},
});