-
Notifications
You must be signed in to change notification settings - Fork 0
/
eventprovider.js
258 lines (224 loc) · 10.9 KB
/
eventprovider.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
/* global Class */
/* MagicMirror²
* Module: MMM-Events
* File: eventprovider.js
*
* By Doug Woolridge https://github.com/dWoolridge
* MIT Licensed.
*
* This class is the blueprint for an event provider.
*/
const EventProvider = Class.extend({
// Event Provider Properties
providerName: null,
defaults: {},
// The following properties have accessor methods.
// Try to not access them directly.
currentEventObject: null,
fetchedLocationName: null,
// The following properties will be set automatically.
// You do not need to overwrite these properties.
config: null,
delegate: null,
providerIdentifier: null,
// Event Provider Methods
// All the following methods can be overwritten, although most are good as they are.
// Called when a event provider is initialized.
init: function (config) {
this.config = config;
Log.info(`Event provider: ${this.providerName} initialized.`);
},
// Called to set the config, this config is the same as the event module's config.
setConfig: function (config) {
this.config = config;
Log.info(`Event provider: ${this.providerName} config set.`, this.config);
},
// Called when the event provider is about to start.
start: function () {
Log.info(`Event provider: ${this.providerName} started.`);
},
// This method should start the API request to fetch the events.
// This method should definitely be overwritten in the provider.
fetchEventList: function () {
Log.warn(`Event provider: ${this.providerName} does not subclass the fetchEventList method.`);
},
// this returns an array of Event objects for the current event list.
eventList: function () {
return this.eventArray;
},
// This returns the name of the fetched location or an empty string.
fetchedLocation: function () {
return this.fetchedLocationName || "";
},
// Set the EventList and notify the delegate that new information is available.
setEventList: function (eventArray) {
this.currentEventObject = eventArray;
},
// Set the fetched location name.
setFetchedLocation: function (name) {
this.fetchedLocationName = name;
},
// Notify the delegate that new events are available.
updateAvailable: function () {
this.delegate.updateAvailable(this);
},
getCorsUrl: function () {
if (this.config.mockData || typeof this.config.useCorsProxy === "undefined" || !this.config.useCorsProxy) {
return "";
} else {
return location.protocol + "//" + location.host + "/cors?url=";
}
},
// A convenience function to make requests. It returns a promise.
fetchData: function (url, method = "GET", type = "json", header) {
url = this.getCorsUrl() + url;
const getData = function (mockData) {
return new Promise(function (resolve, reject) {
if (mockData) {
let data = mockData;
data = data.substring(1, data.length - 1);
resolve(JSON.parse(data));
} else {
const request = new XMLHttpRequest();
const headerLabel = header.slice(0,header.search(":")).trim();
const headerData = header.slice(header.search(":")+1).trim();
request.open(method, url, true);
request.setRequestHeader(headerLabel, headerData);
request.onreadystatechange = function () {
if (this.readyState === 4) {
if (this.status === 200) {
if (type === "xml") {
resolve(this.responseXML);
} else {
resolve(JSON.parse(this.response));
}
} else {
reject(request);
}
}
};
request.send();
}
});
};
return getData(this.config.mockData);
},
calculateStartDate: function () {
this.startDate = this.curDate;
this.startDate = this.startDate.startOf('day');
// Calculate the startDate to filter the array
if ( this.config.ignoreToday ) {
this.startDate = this.startDate.add(1, 'days');
}
// else {
// switch ( this.config.type.toLowerCase() ) {
// case "nextweek":
// // Find the next Sunday, that becomes the startDate
// this.startDate = this.startDate.add( 7 - this.startDate.day(), 'days');
// break;
// case "nextmonth":
// // Find the next first of the Month, that becomes the startDate
// this.startDate = moment(this.curDate.format("YYYY-MM-01"));
// this.startDate = this.startDate.add(1,'M');
// break;
// //Make the invalid values use the "Loading..." from events
// default:
// }
// }
this.startDateCalculated = true;
},
addThisEventBasedOnStartDate: function (eventStartDate) {
if ( !this.startDateCalculated ) { this.calculateStartDate(); }
// Skip any dates before startDate
let tMoment = moment(eventStartDate);
tMoment = tMoment.startOf('day');
if ( tMoment.diff(this.startDate, 'days') >=0 ) {
return true;
} else {
return false;
}
},
addFilters() {
this.nunjucksEnvironment().addFilter(
"formatTime",
function (date) {
date = moment(date);
if (this.config.timeFormat !== 24) {
if (this.config.showPeriod) {
if (this.config.showPeriodUpper) {
return date.format("h:mm A");
} else {
return date.format("h:mm a");
}
} else {
return date.format("h:mm");
}
}
return date.format("HH:mm");
}.bind(this)
);
this.nunjucksEnvironment().addFilter(
"calcNumSteps",
function (forecast) {
return Math.min(forecast.length, this.config.maxNumberOfDays);
}.bind(this)
);
this.nunjucksEnvironment().addFilter(
"calcNumEntries",
function (dataArray) {
return Math.min(dataArray.length, this.config.maxEntries);
}.bind(this)
);
this.nunjucksEnvironment().addFilter(
"opacity",
function (currentStep, numSteps) {
if (this.config.fade && this.config.fadePoint < 1) {
if (this.config.fadePoint < 0) {
this.config.fadePoint = 0;
}
const startingPoint = numSteps * this.config.fadePoint;
const numFadesteps = numSteps - startingPoint;
if (currentStep >= startingPoint) {
return 1 - (currentStep - startingPoint) / numFadesteps;
} else {
return 1;
}
} else {
return 1;
}
}.bind(this)
);
}
});
/**
* Collection of registered event providers.
*/
EventProvider.providers = [];
/**
* Static method to register a new event provider.
*
* @param {string} providerIdentifier The name of the event provider
* @param {object} providerDetails The details of the event provider
*/
EventProvider.register = function (providerIdentifier, providerDetails) {
EventProvider.providers[providerIdentifier.toLowerCase()] = EventProvider.extend(providerDetails);
};
/**
* Static method to initialize a new event provider.
*
* @param {string} providerIdentifier The name of the event provider
* @param {object} delegate The event module
* @returns {object} The new event provider
*/
EventProvider.initialize = function (providerIdentifier, delegate) {
providerIdentifier = providerIdentifier.toLowerCase();
const provider = new EventProvider.providers[providerIdentifier]();
const config = Object.assign({}, provider.defaults, delegate.config);
provider.delegate = delegate;
provider.setConfig(config);
provider.providerIdentifier = providerIdentifier;
if (!provider.providerName) {
provider.providerName = providerIdentifier;
}
return provider;
};