Skip to content

Commit 70ff16f

Browse files
author
Tobias Schweizer
committed
refactor (MatCalendarHeader): move header related methods
1 parent da49938 commit 70ff16f

File tree

2 files changed

+44
-38
lines changed

2 files changed

+44
-38
lines changed

src/lib/datepicker/calendar-header.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
<div class="mat-calendar-header">
22
<div class="mat-calendar-controls">
33
<button mat-button class="mat-calendar-period-button"
4-
(click)="calendar.currentPeriodClicked()" [attr.aria-label]="periodButtonLabel">
4+
(click)="currentPeriodClicked()" [attr.aria-label]="periodButtonLabel">
55
{{periodButtonText}}
66
<div class="mat-calendar-arrow" [class.mat-calendar-invert]="calendar.currentView != 'month'"></div>
77
</button>
88

99
<div class="mat-calendar-spacer"></div>
1010

1111
<button mat-icon-button class="mat-calendar-previous-button"
12-
[disabled]="!calendar.previousEnabled()" (click)="calendar.previousClicked()"
12+
[disabled]="!previousEnabled()" (click)="previousClicked()"
1313
[attr.aria-label]="prevButtonLabel">
1414
</button>
1515

1616
<button mat-icon-button class="mat-calendar-next-button"
17-
[disabled]="!calendar.nextEnabled()" (click)="calendar.nextClicked()"
17+
[disabled]="!nextEnabled()" (click)="nextClicked()"
1818
[attr.aria-label]="nextButtonLabel">
1919
</button>
2020
</div>

src/lib/datepicker/calendar.ts

Lines changed: 41 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,46 @@ export class MatCalendarHeader implements OnDestroy {
9898
}[this.calendar.currentView];
9999
}
100100

101+
/** Handles user clicks on the period label. */
102+
currentPeriodClicked(): void {
103+
this.calendar.currentView = this.calendar.currentView == 'month' ? 'multi-year' : 'month';
104+
}
105+
106+
/** Handles user clicks on the previous button. */
107+
previousClicked(): void {
108+
this.calendar.activeDate = this.calendar.currentView == 'month' ?
109+
this._dateAdapter.addCalendarMonths(this.calendar.activeDate, -1) :
110+
this._dateAdapter.addCalendarYears(
111+
this.calendar.activeDate,
112+
this.calendar.currentView == 'year' ? -1 : -yearsPerPage
113+
);
114+
}
115+
116+
/** Handles user clicks on the next button. */
117+
nextClicked(): void {
118+
this.calendar.activeDate = this.calendar.currentView == 'month' ?
119+
this._dateAdapter.addCalendarMonths(this.calendar.activeDate, 1) :
120+
this._dateAdapter.addCalendarYears(
121+
this.calendar.activeDate,
122+
this.calendar.currentView == 'year' ? 1 : yearsPerPage
123+
);
124+
}
125+
126+
/** Whether the previous period button is enabled. */
127+
previousEnabled(): boolean {
128+
if (!this.calendar.minDate) {
129+
return true;
130+
}
131+
return !this.calendar.minDate ||
132+
!this.calendar.isSameView(this.calendar.activeDate, this.calendar.minDate);
133+
}
134+
135+
/** Whether the next period button is enabled. */
136+
nextEnabled(): boolean {
137+
return !this.calendar.maxDate ||
138+
!this.calendar.isSameView(this.calendar.activeDate, this.calendar.maxDate);
139+
}
140+
101141
ngOnDestroy() {
102142
this._destroyed.next();
103143
this._destroyed.complete();
@@ -273,42 +313,8 @@ export class MatCalendar<D> implements AfterContentInit, OnDestroy, OnChanges {
273313
this.currentView = view;
274314
}
275315

276-
/** Handles user clicks on the period label. */
277-
currentPeriodClicked(): void {
278-
this.currentView = this.currentView == 'month' ? 'multi-year' : 'month';
279-
}
280-
281-
/** Handles user clicks on the previous button. */
282-
previousClicked(): void {
283-
this.activeDate = this.currentView == 'month' ?
284-
this._dateAdapter.addCalendarMonths(this.activeDate, -1) :
285-
this._dateAdapter.addCalendarYears(
286-
this.activeDate, this.currentView == 'year' ? -1 : -yearsPerPage);
287-
}
288-
289-
/** Handles user clicks on the next button. */
290-
nextClicked(): void {
291-
this.activeDate = this.currentView == 'month' ?
292-
this._dateAdapter.addCalendarMonths(this.activeDate, 1) :
293-
this._dateAdapter.addCalendarYears(
294-
this.activeDate, this.currentView == 'year' ? 1 : yearsPerPage);
295-
}
296-
297-
/** Whether the previous period button is enabled. */
298-
previousEnabled(): boolean {
299-
if (!this.minDate) {
300-
return true;
301-
}
302-
return !this.minDate || !this._isSameView(this.activeDate, this.minDate);
303-
}
304-
305-
/** Whether the next period button is enabled. */
306-
nextEnabled(): boolean {
307-
return !this.maxDate || !this._isSameView(this.activeDate, this.maxDate);
308-
}
309-
310316
/** Whether the two dates represent the same view in the current view mode (month or year). */
311-
private _isSameView(date1: D, date2: D): boolean {
317+
isSameView(date1: D, date2: D): boolean {
312318
if (this.currentView == 'month') {
313319
return this._dateAdapter.getYear(date1) == this._dateAdapter.getYear(date2) &&
314320
this._dateAdapter.getMonth(date1) == this._dateAdapter.getMonth(date2);

0 commit comments

Comments
 (0)