-
Notifications
You must be signed in to change notification settings - Fork 0
/
scheduler.component.ts
81 lines (62 loc) · 1.86 KB
/
scheduler.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
import { Component, OnInit, Output, EventEmitter } from '@angular/core';
import { Moment } from 'moment';
import * as moment from 'moment';
@Component({
selector: 'app-scheduler',
templateUrl: './scheduler.component.html',
styleUrls: ['./scheduler.component.scss']
})
export class SchedulerComponent implements OnInit {
@Output() selectedHours: EventEmitter<Moment[]> = new EventEmitter();
get startDate() {
return this._date.clone().startOf('week');
}
get endDate() {
return this._date.clone().endOf('week');
}
get weekDays() {
return Array.from(Array(6))
.map((_, i) => i + 1)
.map((i) => this.startDate.clone().add(i, 'days'));
}
private _date = moment();
private _selectedHours: Moment[] = [];
constructor() { }
ngOnInit() {
}
nextWeek() {
this._date = this._date.add(7, 'days');
}
prevWeek() {
this._date = this._date.subtract(7, 'days');
}
getHours(date: Moment) {
const startDate = date.clone().set({hour: 9, minutes: 0});
const endDate = date.clone().set({hour: 23, minutes: 59}).endOf('minutes');
const hours = [];
while (startDate <= endDate) {
startDate.add(30, 'minutes');
hours.push(startDate.clone());
}
return hours;
}
onClick(hour: Moment): void {
return this.isSelected(hour)
? this.deselectHour(hour)
: this.selectHour(hour);
}
isSelected(hour: Moment) {
return this._selectedHours.some((date) => date.isSame(hour));
}
private selectHour(hour: Moment): void {
this._selectedHours = [...this._selectedHours, hour];
this.emitSelected();
}
private deselectHour(hour: Moment): void {
this._selectedHours = this._selectedHours.filter((selectedHour) => !selectedHour.isSame(hour));
this.emitSelected();
}
private emitSelected() {
this.selectedHours.emit(this._selectedHours);
}
}