/
te-calendar.component.ts
565 lines (465 loc) · 19.2 KB
/
te-calendar.component.ts
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
import {Component, ElementRef, Input, OnDestroy, OnInit, SecurityContext, ViewChild, AfterViewInit, Output, EventEmitter, Injector, ViewEncapsulation, ChangeDetectionStrategy} from "@angular/core";
import {FullCalendarComponent} from '@fullcalendar/angular';
import {States} from "core-components/states.service";
import * as moment from "moment";
import { Moment } from 'moment';
import {StateService} from "@uirouter/core";
import {I18nService} from "core-app/modules/common/i18n/i18n.service";
import {DomSanitizer} from "@angular/platform-browser";
import timeGrid from '@fullcalendar/timegrid';
import { EventInput, EventApi, Duration, View } from '@fullcalendar/core';
import { EventSourceError } from '@fullcalendar/core/structs/event-source';
import { ToolbarInput } from '@fullcalendar/core/types/input-types';
import {ConfigurationService} from "core-app/modules/common/config/configuration.service";
import {TimeEntryDmService} from "core-app/modules/hal/dm-services/time-entry-dm.service";
import {FilterOperator} from "core-components/api/api-v3/api-v3-filter-builder";
import {TimeEntryResource} from "core-app/modules/hal/resources/time-entry-resource";
import {TimezoneService} from "core-components/datetime/timezone.service";
import {CollectionResource} from "core-app/modules/hal/resources/collection-resource";
import {TimeEntryCacheService} from "core-components/time-entries/time-entry-cache.service";
import interactionPlugin from '@fullcalendar/interaction';
import {HalResourceEditingService} from "core-app/modules/fields/edit/services/hal-resource-editing.service";
import {TimeEntryEditService} from "core-app/modules/time_entries/edit/edit.service";
import {TimeEntryCreateService} from "core-app/modules/time_entries/create/create.service";
import {ColorsService} from "core-app/modules/common/colors/colors.service";
import {BrowserDetector} from "core-app/modules/common/browser/browser-detector.service";
import { HalResourceNotificationService } from 'core-app/modules/hal/services/hal-resource-notification.service';
interface CalendarViewEvent {
el:HTMLElement;
event:EventApi;
jsEvent:MouseEvent;
}
interface CalendarMoveEvent {
el:HTMLElement;
event:EventApi;
oldEvent:EventApi;
delta:Duration;
revert:() => void;
jsEvent:Event;
view:View;
}
const TIME_ENTRY_CLASS_NAME = 'te-calendar--time-entry';
const DAY_SUM_CLASS_NAME = 'te-calendar--day-sum';
const ADD_ENTRY_CLASS_NAME = 'te-calendar--add-entry';
const ADD_ICON_CLASS_NAME = 'te-calendar--add-icon';
const ADD_ENTRY_PROHIBITED_CLASS_NAME = '-prohibited';
@Component({
templateUrl: './te-calendar.template.html',
styleUrls: ['./te-calendar.component.sass'],
selector: 'te-calendar',
encapsulation: ViewEncapsulation.None,
changeDetection: ChangeDetectionStrategy.OnPush,
providers: [
TimeEntryEditService,
TimeEntryCreateService,
HalResourceEditingService
]
})
export class TimeEntryCalendarComponent implements OnInit, OnDestroy, AfterViewInit {
@ViewChild(FullCalendarComponent, { static: false }) ucCalendar:FullCalendarComponent;
@Input() projectIdentifier:string;
@Input() static:boolean = false;
@Output() entries = new EventEmitter<CollectionResource<TimeEntryResource>>();
// Not used by the calendar but rather is the maximum/minimum of the graph.
public minHour = 1;
public maxHour = 12;
public labelIntervalHours = 2;
public scaleRatio = 1;
public calendarPlugins = [timeGrid, interactionPlugin];
public calendarEvents:Function;
public calendarHeader:ToolbarInput|boolean = {
right: '',
center: 'title',
left: 'prev,next today'
};
public calendarSlotLabelFormat = (info:any) => (this.maxHour - info.date.hour) / this.scaleRatio;
public calendarSlotLabelInterval = `${this.labelIntervalHours}:00:00`;
public calendarContentHeight = 605;
public calendarAllDaySlot = false;
public calendarDisplayEventTime = false;
public calendarSlotEventOverlap = false;
public calendarEditable = false;
public calendarMinTime = `${this.minHour - 1}:00:00`;
public calendarMaxTime = `${this.maxHour}:00:00`;
public calendarEventOverlap = (stillEvent:any) => !stillEvent.classNames.includes(TIME_ENTRY_CLASS_NAME);
protected memoizedTimeEntries:{start:Date, end:Date, entries:Promise<CollectionResource<TimeEntryResource>>};
protected memoizedCreateAllowed:boolean = false;
public text = {
logTime: this.i18n.t('js.button_log_time')
};
constructor(readonly states:States,
readonly timeEntryDm:TimeEntryDmService,
readonly $state:StateService,
private element:ElementRef,
readonly i18n:I18nService,
readonly injector:Injector,
readonly notifications:HalResourceNotificationService,
private sanitizer:DomSanitizer,
private configuration:ConfigurationService,
private timezone:TimezoneService,
private timeEntryEdit:TimeEntryEditService,
private timeEntryCreate:TimeEntryCreateService,
private timeEntryCache:TimeEntryCacheService,
private colors:ColorsService,
private browserDetector:BrowserDetector) { }
ngOnInit() {
this.initializeCalendar();
}
ngOnDestroy() {
// nothing to do
}
ngAfterViewInit() {
// The full-calendar component's outputs do not seem to work
// see: https://github.com/fullcalendar/fullcalendar-angular/issues/228#issuecomment-523505044
// Therefore, setting the outputs via the underlying API
this.ucCalendar.getApi().setOption('eventRender', (event:CalendarViewEvent) => { this.alterEventEntry(event); });
this.ucCalendar.getApi().setOption('eventDestroy', (event:CalendarViewEvent) => { this.beforeEventRemove(event); });
this.ucCalendar.getApi().setOption('eventClick', (event:CalendarViewEvent) => { this.dispatchEventClick(event); });
this.ucCalendar.getApi().setOption('eventDrop', (event:CalendarMoveEvent) => { this.moveEvent(event); });
}
public calendarEventsFunction(fetchInfo:{ start:Date, end:Date },
successCallback:(events:EventInput[]) => void,
failureCallback:(error:EventSourceError) => void ):void | PromiseLike<EventInput[]> {
this.fetchTimeEntries(fetchInfo.start, fetchInfo.end)
.then((collection) => {
this.entries.emit(collection);
successCallback(this.buildEntries(collection.elements, fetchInfo));
});
}
protected fetchTimeEntries(start:Date, end:Date) {
if (!this.memoizedTimeEntries ||
this.memoizedTimeEntries.start.getTime() !== start.getTime() ||
this.memoizedTimeEntries.end.getTime() !== end.getTime()) {
let promise = this
.timeEntryDm
.list({ filters: this.dmFilters(start, end), pageSize: 500 })
.then(collection => {
this.memoizedCreateAllowed = !!collection.createTimeEntry;
collection.elements.forEach(timeEntry => this.timeEntryCache.updateValue(timeEntry.id!, timeEntry));
return collection;
});
this.memoizedTimeEntries = { start: start, end: end, entries: promise };
}
return this.memoizedTimeEntries.entries;
}
private buildEntries(entries:TimeEntryResource[], fetchInfo:{ start:Date, end:Date }) {
this.setRatio(entries);
return this.buildTimeEntryEntries(entries)
.concat(this.buildAuxEntries(entries, fetchInfo));
}
private setRatio(entries:TimeEntryResource[]) {
let dateSums = this.calculateDateSums(entries);
let maxHours = Math.max(...Object.values(dateSums), 0);
let oldRatio = this.scaleRatio;
if (maxHours > this.maxHour - this.minHour) {
this.scaleRatio = this.smallerSuitableRatio((this.maxHour - this.minHour) / maxHours);
} else {
this.scaleRatio = 1;
}
if (oldRatio !== this.scaleRatio) {
this.ucCalendar.getApi().render();
}
}
private buildTimeEntryEntries(entries:TimeEntryResource[]) {
let hoursDistribution:{ [key:string]:Moment } = {};
return entries.map((entry) => {
let start:Moment;
let end:Moment;
let hours = this.timezone.toHours(entry.hours) * this.scaleRatio;
if (hoursDistribution[entry.spentOn]) {
start = hoursDistribution[entry.spentOn].clone().subtract(hours, 'h');
end = hoursDistribution[entry.spentOn].clone();
} else {
start = moment(entry.spentOn).add(this.maxHour - hours, 'h');
end = moment(entry.spentOn).add(this.maxHour, 'h');
}
hoursDistribution[entry.spentOn] = start;
const color = this.colors.toHsl(this.entryName(entry));
return this.timeEntry(entry, hours, start, end);
}) as EventInput[];
}
private buildAuxEntries(entries:TimeEntryResource[], fetchInfo:{ start:Date, end:Date }) {
let dateSums = this.calculateDateSums(entries);
let calendarEntries:EventInput[] = [];
for (let m = moment(fetchInfo.start); m.diff(fetchInfo.end, 'days') <= 0; m.add(1, 'days')) {
let duration = dateSums[m.format('YYYY-MM-DD')] || 0;
calendarEntries.push(this.sumEntry(m, duration));
if (this.memoizedCreateAllowed) {
calendarEntries.push(this.addEntry(m, duration));
}
}
return calendarEntries;
}
private calculateDateSums(entries:TimeEntryResource[]) {
let dateSums:{ [key:string]:number } = {};
entries.forEach((entry) => {
let hours = this.timezone.toHours(entry.hours);
if (dateSums[entry.spentOn]) {
dateSums[entry.spentOn] += hours;
} else {
dateSums[entry.spentOn] = hours;
}
});
return dateSums;
}
protected timeEntry(entry:TimeEntryResource, hours:number, start:Moment, end:Moment) {
const color = this.colors.toHsl(this.entryName(entry));
let classNames = [TIME_ENTRY_CLASS_NAME];
let span = end.diff(start, 'm');
if (span < 40) {
classNames.push('-no-fadeout');
}
return {
title: span < 20 ? '' : this.entryName(entry),
startEditable: !!entry.update,
start: start.format(),
end: end.format(),
backgroundColor: color,
borderColor: color,
classNames: classNames,
entry: entry
};
}
protected sumEntry(date:Moment, duration:number) {
return {
start: date.clone().add(this.maxHour - Math.min(duration * this.scaleRatio, this.maxHour - 0.5) - 0.5, 'h').format(),
end: date.clone().add(this.maxHour - Math.min(((duration + 0.05) * this.scaleRatio), this.maxHour - 0.5), 'h').format(),
classNames: DAY_SUM_CLASS_NAME,
rendering: 'background' as 'background',
sum: this.i18n.t('js.units.hour', { count: this.formatNumber(duration) })
};
}
protected addEntry(date:Moment, duration:number) {
let classNames = [ADD_ENTRY_CLASS_NAME];
if (duration >= 24) {
classNames.push(ADD_ENTRY_PROHIBITED_CLASS_NAME);
}
return {
start: date.clone().format(),
end: date.clone().add(this.maxHour - Math.min(duration * this.scaleRatio, this.maxHour - 1) - 0.5, 'h').format(),
rendering: "background" as 'background',
classNames: classNames
};
}
protected dmFilters(start:Date, end:Date):Array<[string, FilterOperator, string[]]> {
let startDate = moment(start).format('YYYY-MM-DD');
let endDate = moment(end).subtract(1, 'd').format('YYYY-MM-DD');
return [['spentOn', '<>d', [startDate, endDate]] as [string, FilterOperator, string[]],
['user_id', '=', ['me']] as [string, FilterOperator, [string]]];
}
private initializeCalendar() {
this.calendarEvents = this.calendarEventsFunction.bind(this);
}
public get calendarEventLimit() {
return false;
}
public get calendarLocale() {
return this.i18n.locale;
}
public get calendarFixedWeekCount() {
return false;
}
public get calendarDefaultView() {
return 'timeGridWeek';
}
public get calendarFirstDay() {
return this.configuration.startOfWeek();
}
private get calendarElement() {
return jQuery(this.element.nativeElement).find('.fc-view-container');
}
private dispatchEventClick(event:CalendarViewEvent) {
if (event.event.extendedProps.entry) {
this.editEvent(event.event.extendedProps.entry);
} else if (event.el.classList.contains(ADD_ENTRY_CLASS_NAME) && !event.el.classList.contains(ADD_ENTRY_PROHIBITED_CLASS_NAME)) {
this.addEvent(moment(event.event.start!));
}
}
private editEvent(entry:TimeEntryResource) {
this
.timeEntryEdit
.edit(entry)
.then(modificationAction => {
this.updateEventSet(modificationAction.entry, modificationAction.action);
})
.catch(() => {
// do nothing, the user closed without changes
});
}
private moveEvent(event:CalendarMoveEvent) {
let entry = event.event.extendedProps.entry;
// Use end instead of start as when dragging, the event might be too long and would thus be start
// on the day before by fullcalendar.
entry.spentOn = moment(event.event.end!).format('YYYY-MM-DD');
this
.timeEntryDm
.update(entry, entry.schema)
.then(event => {
this.updateEventSet(event, 'update');
})
.catch((e) => {
this.notifications.handleRawError(e);
event.revert();
});
}
public addEventToday() {
this.addEvent(moment(new Date()));
}
private addEvent(date:Moment) {
if (!this.memoizedCreateAllowed) {
return;
}
this
.timeEntryCreate
.create(date)
.then(modificationAction => {
this.updateEventSet(modificationAction.entry, modificationAction.action);
})
.catch(() => {
// do nothing, the user closed without changes
});
}
private updateEventSet(event:TimeEntryResource, action:'update'|'destroy'|'create') {
this.memoizedTimeEntries.entries.then(collection => {
let foundIndex = collection.elements.findIndex(x => x.id === event.id);
switch (action) {
case 'update':
collection.elements[foundIndex] = event;
break;
case 'destroy':
collection.elements.splice(foundIndex, 1);
break;
case 'create':
this.timeEntryCache.updateValue(event.id!, event);
collection.elements.push(event);
break;
}
this.ucCalendar.getApi().refetchEvents();
});
}
private alterEventEntry(event:CalendarViewEvent) {
this.appendAddIcon(event);
this.appendSum(event);
if (!event.event.extendedProps.entry) {
return;
}
this.addTooltip(event);
this.prependDuration(event);
this.appendFadeout(event);
}
private appendAddIcon(event:CalendarViewEvent) {
if (!event.el.classList.contains(ADD_ENTRY_CLASS_NAME)) {
return;
}
let addIcon = document.createElement('div');
addIcon.classList.add(ADD_ICON_CLASS_NAME);
addIcon.innerText = '+';
event.el.append(addIcon);
}
private appendSum(event:CalendarViewEvent) {
if (event.event.extendedProps.sum) {
event.el.append(event.event.extendedProps.sum);
}
}
private addTooltip(event:CalendarViewEvent) {
if (this.browserDetector.isMobile) {
return;
}
jQuery(event.el).tooltip({
content: this.tooltipContentString(event.event.extendedProps.entry),
items: '.fc-event',
close: function () { jQuery(".ui-helper-hidden-accessible").remove(); },
track: true
});
}
private removeTooltip(event:CalendarViewEvent) {
jQuery(event.el).tooltip('disable');
}
private prependDuration(event:CalendarViewEvent) {
let formattedDuration = this.timezone.formattedDuration(event.event.extendedProps.entry.hours);
jQuery(event.el)
.find('.fc-title')
.prepend(`<div class="fc-duration">${formattedDuration}</div>`);
}
/* Fade out event text to the bottom to avoid it being cut of weirdly.
* Multiline ellipsis with an unknown height is not possible, hence we blur the text.
* The gradient needs to take the background color of the element into account (hashed over the event
* title) which is why the style is set in code.
*
* We do not print anything on short entries (< 0.5 hours),
* which leads to the fc-short class not being applied by full calendar. For other short events, the css rules
* need to deactivate the fc-fadeout.
*/
private appendFadeout(event:CalendarViewEvent) {
let timeEntry = event.event.extendedProps.entry;
if (this.timezone.toHours(timeEntry.hours) < 0.5) {
return;
}
let $element = jQuery(event.el);
let fadeout = jQuery(`<div class="fc-fadeout"></div>`);
let hslaStart = this.colors.toHsla(this.entryName(timeEntry), 0);
let hslaEnd = this.colors.toHsla(this.entryName(timeEntry), 100);
fadeout.css('background', `-webkit-linear-gradient(${hslaStart} 0%, ${hslaEnd} 100%`);
['-moz-linear-gradient', '-o-linear-gradient', 'linear-gradient', '-ms-linear-gradient'].forEach((style => {
fadeout.css('background-image', `${style}(${hslaStart} 0%, ${hslaEnd} 100%`);
}));
$element
.append(fadeout);
}
private beforeEventRemove(event:CalendarViewEvent) {
if (!event.event.extendedProps.entry) {
return;
}
this.removeTooltip(event);
}
private entryName(entry:TimeEntryResource) {
let name = entry.project.name;
if (entry.workPackage) {
name += ` - ${this.workPackageName(entry)}`;
}
return this.sanitizedValue(name) || '-';
}
private workPackageName(entry:TimeEntryResource) {
return `#${entry.workPackage.idFromLink}: ${entry.workPackage.name}`;
}
private tooltipContentString(entry:TimeEntryResource) {
return `
<ul class="tooltip--map">
<li class="tooltip--map--item">
<span class="tooltip--map--key">${this.i18n.t('js.time_entry.project')}:</span>
<span class="tooltip--map--value">${this.sanitizedValue(entry.project.name)}</span>
</li>
<li class="tooltip--map--item">
<span class="tooltip--map--key">${this.i18n.t('js.time_entry.work_package')}:</span>
<span class="tooltip--map--value">${entry.workPackage ? this.sanitizedValue(this.workPackageName(entry)) : this.i18n.t('js.placeholders.default')}</span>
</li>
<li class="tooltip--map--item">
<span class="tooltip--map--key">${this.i18n.t('js.time_entry.activity')}:</span>
<span class="tooltip--map--value">${this.sanitizedValue(entry.activity.name)}</span>
</li>
<li class="tooltip--map--item">
<span class="tooltip--map--key">${this.i18n.t('js.time_entry.duration')}:</span>
<span class="tooltip--map--value">${this.timezone.formattedDuration(entry.hours)}</span>
</li>
<li class="tooltip--map--item">
<span class="tooltip--map--key">${this.i18n.t('js.time_entry.comment')}:</span>
<span class="tooltip--map--value">${entry.comment.raw || this.i18n.t('js.placeholders.default')}</span>
</li>
`;
}
private sanitizedValue(value:string) {
return this.sanitizer.sanitize(SecurityContext.HTML, value);
}
protected formatNumber(value:number):string {
return this.i18n.toNumber(value, { precision: 2 });
}
private smallerSuitableRatio(value:number):number {
for (let divisor = this.labelIntervalHours + 1; divisor < 100; divisor++) {
let candidate = this.labelIntervalHours / divisor;
if (value >= candidate) {
return candidate;
}
}
return 1;
}
}