forked from ljmerza/calendar-card
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcalendar-card.js
598 lines (513 loc) · 16 KB
/
calendar-card.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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
/**
*
*/
class CalendarCard extends HTMLElement {
/**
* called by hass - creates card, sets up any conmfig settings, and generates card
* @param {[type]} hass [description]
* @return {[type]} [description]
*/
set hass(hass) {
// if we don't have the card yet then create it
if (!this.content) {
const card = document.createElement('ha-card');
card.header = this.config.title;
this.content = document.createElement('div');
this.content.className = 'calendar-card';
card.appendChild(this.content);
this.appendChild(card);
}
// save an instance of hass for later
this._hass = hass;
this.wait();
}
wait() {
if(typeof(moment) == "undefined") {
setTimeout(() => this.wait(), 250);
return;
}
moment.locale(this._hass.language);
// save css rules
this.cssRules = `
<style>
.calendar-card {
display: flex;
padding: 0 16px 4px;
flex-direction: column;
}
.day-wrapper {
border-bottom: 1px solid;
}
.day-wrapper:last-child {
border-bottom: none;
}
.day-wrapper .calendar-day {
display: flex;
flex-direction: row;
width: 100%;
}
.day-wrapper .date {
display: flex;
flex-direction: column;
align-items: center;
justify-content: top;
flex: 0 0 40px;
padding-top: 10px;
}
.day-wrapper .events {
flex: 1 1 auto;
}
.day-wrapper .summary {
font-size: ${this.config.textSizeSummary}%;
cursor: pointer;
}
.day-wrapper .event-wrapper {
margin-left: 10px;
padding-top: 10px;
}
.day-wrapper .event-wrapper:last-child {
padding-bottom: 10px;
}
.day-wrapper .event {
flex: 0 1 auto;
display: flex;
flex-direction: column;
}
.day-wrapper .info {
display: flex;
width: 100%;
justify-content: space-between;
flex-direction: row;
}
.day-wrapper .time {
color: var(--primary-color);
font-size: ${this.config.textSizeTime}%;
}
.day-wrapper hr.now {
border-style: solid;
border-color: var(--primary-color);
border-width: 1px 0 0 0;
margin-top: -8px;
margin-left: 5px;
width: 100%;
}
.day-wrapper ha-icon {
height: 16px;
width: 16px;
color: ${this.config.mapIconColor};
}
.day-wrapper ha-icon.now {
height: 12px;
width: 12px;
color: var(--paper-item-icon-color, #44739e);
}
.day-wrapper ha-icon.current {
height: 12px;
width: 12px;
color: rgb(223, 76, 30);
}
</style>
`;
// update card with calendars
this
.getAllEvents(this.config.entities)
.then(events => this.updateHtmlIfNecessary(events))
.catch(error => console.log('error', error));
}
/**
* [getAllEvents description]
* @param {[type]} entities [description]
* @return {[type]} [description]
*/
async getAllEvents(entities) {
// don't update if it's only been 15 min
if(this.lastUpdate && moment().diff(this.lastUpdate, 'minutes') <= 15) {
return this.events;
}
// create url params
const dateFormat = "YYYY-MM-DDTHH:mm:ss";
const today = moment().startOf('day');
const start = today.format(dateFormat);
const end = today.add(this.config.numberOfDays, 'days').format(dateFormat);
// generate urls for calendars and get each calendar data
const urls = entities.map(entity => `calendars/${entity.entity}?start=${start}Z&end=${end}Z`);
let allResults = await this.getAllUrls(urls);
// creating CalendarEvents and passing color settings for different calendars
let events= [].concat.apply([], (allResults.map((result,i) => {
return result.map(r => {
return(new CalendarEvent(r,this.config.entities[i].color===undefined ? 'var(--primary-text-color)' :this.config.entities[i].color )
)
});
})))
// show progress bar if turned on
if (this.config.showProgressBar && events.length > 0 && moment().format('DD') === moment(events[0].startDateTime).format('DD')) {
//checks if any event is running, if no, show the default progress bar
let noEventRunning=true;
events.forEach(function(element, i) {
if(!element.isFullDayEvent && moment()>=moment(element.startDateTime) && moment()<=moment(element.endDateTime))
noEventRunning=false;
});
//show standard progress bar
if(noEventRunning || !this.config.showCurrentProgress) {
let now = {startDateTime: moment().format(), type: 'now'}
events.push(now);}
}
// sort events by date starting with soonest
events.sort((a, b) => new Date(a.startDateTime) - new Date(b.startDateTime));
// see if anything changed since last time, save events, and update last time we updated
const isSomethingChanged = this.isSomethingChanged(events);
this.events = events;
this.lastUpdate = moment();
return { events, isSomethingChanged };
}
/**
* given a list of urls get the data from them
* @param {Array<string>} urls
* @return {Array<any>}
*/
async getAllUrls(urls) {
try {
return await Promise.all(urls.map(url => this._hass.callApi('get', url)));
} catch (error) {
throw error;
}
}
/**
* updates the entire card if we need to
* @param {[type]} eventList [description]
* @return {[type]} [description]
*/
updateHtmlIfNecessary(eventList) {
if(!eventList.isSomethingChanged) return;
// save CSS rules then group events by day
this.content.innerHTML = this.cssRules;
const events = eventList.events;
const groupedEventsPerDay = this.groupBy(events, event => moment(event.startDateTime).format('YYYY-MM-DD'));
// for each group event create a UI 'day'
groupedEventsPerDay.forEach((events, day) => {
const eventStateCardContentElement = document.createElement('div');
eventStateCardContentElement.classList.add('day-wrapper');
eventStateCardContentElement.innerHTML = this.getDayHtml(day, events);
this.content.append(eventStateCardContentElement);
});
}
/**
* check if event is today
* @param {*} event
* @return {Boolean}
*/
isEventToday(event){
return moment(event.startDateTime).isSame(moment(), 'day') ? true : false;
}
/**
* check if event is tomorrow
* @param {*} event
* @return {Boolean}
*/
isEventTomorrow(event){
return moment(event.startDateTime).isSame(moment().add(1,'day'), 'day') ? true : false;
}
/**
* generates the HTML for a single day
* @param {[type]} day [description]
* @param {[type]} events [description]
* @return {[type]} [description]
*/
getDayHtml(day, events) {
const className = moment().format('DD') === moment(day).format('DD') ? 'date now' : 'date';
let momentDay = moment(day);
return `
<div class="calendar-day">
<div class="${className}">
${this.config.showMonth ? `<div>${momentDay.format('MMM')}</div>` : ''}
<div>${momentDay.format('DD')}</div>
<div>${momentDay.format('ddd')}</div>
</div>
<div class="events">
${events.map(event => this.getEventHtml(event)).join('')}
</div>
</div>`;
}
/**
* generate HTML for a single event
* @param {[type]} event [description]
* @return {[type]} [description]
*/
getEventHtml(event) {
//show standard progress bar
if(event.type) {
return `<ha-icon icon="mdi:circle" class="now"></ha-icon><hr class="now" />`;
}
// show current progress bar if event is running
let progress=''
let start = moment(event.startDateTime);
let end = moment(event.endDateTime);
let now = moment();
if (this.config.showCurrentProgress && this.config.showProgressBar && moment().format('DD') === moment(event.startDateTime).format('DD') && !event.isFullDayEvent) {
if(now>=start && now<=end){
let eventDuration = end.diff(start, 'minutes');
let eventProgress = now.diff(start, 'minutes');
let eventPercentProgress= Math.floor((eventProgress * 100)/eventDuration);
progress=`<ha-icon icon="mdi:circle" class="current" style="margin-left:${eventPercentProgress}%;"></ha-icon><hr class="now" />`;
}
}
// setting text color, if todayColor or tomorrowColor is set in config
let todayClass;
if (this.isEventToday(event) && this.config.todayColor!='')
todayClass = `<div class="time" style="color: ${this.config.todayColor}">`
else if (this.isEventTomorrow(event) && this.config.tomorrowColor!='')
todayClass = `<div class="time" style="color: ${this.config.tomorrowColor}">`
else
todayClass = `<div class="time">`
return `
<div class="event-wrapper">
<div class="event" >
<div class="info">
<div class="summary" ${this.getLinkHtml(event)}>
${this.getTitleHtml(event)}
</div>
${this.getLocationHtml(event)}
</div>
${todayClass}${this.getTodayHtml(event)}${this.getTimeHtml(event)}</div>
</div>
</div>
${progress}`;
}
/**
* gets the ebent title with a colored marker if user wants
* @return {[type]} [description]
*/
getTitleHtml(event){
let showDot = this.config.showDot ? `● ` : ``
return this.config.showColors ? `<span style="color: ${event.color || ''};">${showDot}${event.title}</span>` : `${event.title}`;
}
/**
* generates HTML for opening an event
* @param {*} event
*/
getLinkHtml(event){
return event.htmlLink ? `onClick="(function(){window.open('${event.htmlLink}');return false;})();return false;"` : '';
}
/**
* generates HTML for showing an event times
* @param {*} event
*/
getTimeHtml(event){
if (event.isFullDayEvent) return this.config.textAllDay
const start = moment(event.startDateTime).format(this.config.timeFormat);
const end = moment(event.endDateTime).format(this.config.timeFormat);
return `${start} - ${end}`;
}
/**
* generates HTML for showing if event is today or tomorrow
* @param {*} event
*/
getTodayHtml(event){
if(this.config.showTodayText){
if(this.isEventToday(event)) return `${this.config.textToday} `;
else if(this.isEventTomorrow(event)) return `${this.config.textTomorrow} `;
else return ''
} else return ''
}
/**
* generate the html for showing an event location
* @param {*} event
*/
getLocationHtml(event){
let locationHtml = ``;
if (event.location) {
locationHtml += `
<div class="location">
<ha-icon icon="mdi:map-marker"></ha-icon> `
}
if (event.location && event.locationAddress) {
let linkStyle=''
this.config.linkColor!='' ? linkStyle=` color: ${this.config.linkColor} ` : ''
locationHtml += `
<a href="https://maps.google.com/?q=${event.locationAddress}" target="_blank" style="${linkStyle}; font-size: ${this.config.textSizeLocation}%">
${event.location}
</a>
</div>`;
} else if (event.location) {
locationHtml += `</div>`
}
return locationHtml;
}
/**
* merge the user configuration with default configuration
* @param {[type]} config [description]
*/
setConfig(config) {
if (!config.entities) {
throw new Error('You need to define at least one calendar entity via entities');
}
this.config = {
title: 'Calendar',
showProgressBar: true,
numberOfDays: 7,
showColors: false,
timeFormat: 'HH:mm',
textAllDay: 'All day',
textToday: '',
textTomorrow: '',
showTodayText: true,
showDot: true,
showCurrentProgress: false,
textSizeSummary: '100',
textSizeTime: '90',
textSizeLocation: '90',
linkColor: '',
mapIconColor: 'var(--paper-item-icon-color, #44739e)',
showMonth: false,
...config
};
if (typeof this.config.entities === 'string')
this.config.entities = [{entity: config.entities}];
this.config.entities.forEach((entity, i) => {
if (typeof entity === 'string')
this.config.entities[i] = { entity: entity };
});
}
/**
* get the size of the card
* @return {[type]} [description]
*/
getCardSize() {
return 3;
}
/**
* did any event change since the last time we checked?
* @param {[type]} events [description]
* @return {Boolean} [description]
*/
isSomethingChanged(events) {
let isSomethingChanged = JSON.stringify(events) !== JSON.stringify(this.events);
return isSomethingChanged;
}
/**
* ddep clone a js object
* @param {[type]} obj [description]
* @return {[type]} [description]
*/
deepClone(obj) {
return JSON.parse(JSON.stringify(obj));
}
/**
* group evbents by a givenkey
* @param {[type]} list [description]
* @param {[type]} keyGetter [description]
* @return {[type]} [description]
*/
groupBy(list, keyGetter) {
const map = new Map();
list.forEach(item => {
const key = keyGetter(item);
const collection = map.get(key);
if (!collection) {
map.set(key, [item]);
} else {
collection.push(item);
}
});
return map;
}
}
/**
* Creaates an generalized Calendar Event to use when creating the calendar card
* There can be Google Events and CalDav Events. This class normalizes those
*/
class CalendarEvent {
/**
* [constructor description]
* @param {[type]} calendarEvent [description]
* @return {[type]} [description]
*/
constructor(calendarEvent,color) {
this.calendarEvent = calendarEvent;
this.color = color;
}
/**
* get the start time for an event
* @return {[type]} [description]
*/
get startDateTime() {
if (this.calendarEvent.start.date) {
let dateTime = moment(this.calendarEvent.start.date);
return dateTime.toISOString();
}
return this.calendarEvent.start && this.calendarEvent.start.dateTime || this.calendarEvent.start || '';
}
/**
* get the end time for an event
* @return {[type]} [description]
*/
get endDateTime() {
return this.calendarEvent.end && this.calendarEvent.end.dateTime || this.calendarEvent.end || '';
}
/**
* get the URL for an event
* @return {[type]} [description]
*/
get htmlLink(){
return this.calendarEvent.htmlLink;
}
/**
* get the title for an event
* @return {[type]} [description]
*/
get title() {
return this.calendarEvent.summary || this.calendarEvent.title;
}
/**
* get the description for an event
* @return {[type]} [description]
*/
get description() {
return this.calendarEvent.description;
}
/**
* parse location for an event
* @return {[type]} [description]
*/
get location() {
if(this.calendarEvent.location) {
return this.calendarEvent.location.split(',')[0]
}
return undefined;
}
/**
* get location address for an event
* @return {[type]} [description]
*/
get locationAddress() {
if(this.calendarEvent.location) {
//let address = this.calendarEvent.location.substring(this.calendarEvent.location.indexOf(',') + 1);
let address = this.calendarEvent.location;
return address.split(' ').join('+');
}
return undefined;
}
/**
* is the event a full day event?
* @return {Boolean} [description]
*/
get isFullDayEvent() {
if (this.calendarEvent.start && this.calendarEvent.start.date){
return this.calendarEvent.start.date;
}
let start = moment(this.startDateTime);
let end = moment(this.endDateTime);
let diffInHours = end.diff(start, 'hours');
return diffInHours >= 24;
}
get color() {
return this._color;
}
set color(color) {
this._color = color;
}
}
/**
* add card definition to hass
*/
customElements.define('calendar-card', CalendarCard);