Skip to content

Commit da49938

Browse files
author
Tobias Schweizer
committed
refactor (MatCalendarHeader): move MatCalendarHeader component back to calendar.ts (to avoid circular deps)
1 parent 4cee00c commit da49938

File tree

4 files changed

+74
-97
lines changed

4 files changed

+74
-97
lines changed

src/lib/datepicker/calendar-header.ts

Lines changed: 0 additions & 94 deletions
This file was deleted.

src/lib/datepicker/calendar.ts

Lines changed: 73 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
ChangeDetectorRef,
1313
Component,
1414
EventEmitter,
15+
Host,
1516
Inject,
1617
Input,
1718
OnChanges,
@@ -30,7 +31,78 @@ import {MatMonthView} from './month-view';
3031
import {MatMultiYearView, yearsPerPage} from './multi-year-view';
3132
import {MatYearView} from './year-view';
3233
import {ComponentPortal, ComponentType, Portal} from '@angular/cdk/portal';
33-
import {MatCalendarHeader} from './calendar-header';
34+
import {Subject} from 'rxjs/Subject';
35+
import {takeUntil} from 'rxjs/operators/takeUntil';
36+
37+
/** Default header for MatCalendar */
38+
@Component({
39+
moduleId: module.id,
40+
selector: 'mat-calendar-header',
41+
templateUrl: 'calendar-header.html',
42+
encapsulation: ViewEncapsulation.None,
43+
preserveWhitespaces: false,
44+
changeDetection: ChangeDetectionStrategy.OnPush,
45+
})
46+
export class MatCalendarHeader implements OnDestroy {
47+
/** Subject that emits when the component has been destroyed. */
48+
private _destroyed = new Subject<void>();
49+
50+
constructor(private _intl: MatDatepickerIntl,
51+
@Host() public calendar: MatCalendar<any>,
52+
@Optional() private _dateAdapter: DateAdapter<any>,
53+
@Optional() @Inject(MAT_DATE_FORMATS) private _dateFormats: MatDateFormats,
54+
changeDetectorRef: ChangeDetectorRef) {
55+
_intl.changes.pipe(takeUntil(this._destroyed)).subscribe(
56+
() => changeDetectorRef.markForCheck()
57+
);
58+
}
59+
60+
/** The label for the current calendar view. */
61+
get periodButtonText(): string {
62+
if (this.calendar.currentView == 'month') {
63+
return this._dateAdapter
64+
.format(this.calendar.activeDate, this._dateFormats.display.monthYearLabel)
65+
.toLocaleUpperCase();
66+
}
67+
if (this.calendar.currentView == 'year') {
68+
return this._dateAdapter.getYearName(this.calendar.activeDate);
69+
}
70+
const activeYear = this._dateAdapter.getYear(this.calendar.activeDate);
71+
const firstYearInView = this._dateAdapter.getYearName(
72+
this._dateAdapter.createDate(activeYear - activeYear % 24, 0, 1));
73+
const lastYearInView = this._dateAdapter.getYearName(
74+
this._dateAdapter.createDate(activeYear + yearsPerPage - 1 - activeYear % 24, 0, 1));
75+
return `${firstYearInView} \u2013 ${lastYearInView}`;
76+
}
77+
78+
get periodButtonLabel(): string {
79+
return this.calendar.currentView == 'month' ?
80+
this._intl.switchToMultiYearViewLabel : this._intl.switchToMonthViewLabel;
81+
}
82+
83+
/** The label for the the previous button. */
84+
get prevButtonLabel(): string {
85+
return {
86+
'month': this._intl.prevMonthLabel,
87+
'year': this._intl.prevYearLabel,
88+
'multi-year': this._intl.prevMultiYearLabel
89+
}[this.calendar.currentView];
90+
}
91+
92+
/** The label for the the next button. */
93+
get nextButtonLabel(): string {
94+
return {
95+
'month': this._intl.nextMonthLabel,
96+
'year': this._intl.nextYearLabel,
97+
'multi-year': this._intl.nextMultiYearLabel
98+
}[this.calendar.currentView];
99+
}
100+
101+
ngOnDestroy() {
102+
this._destroyed.next();
103+
this._destroyed.complete();
104+
}
105+
}
34106

35107
/**
36108
* A calendar that is used as part of the datepicker.

src/lib/datepicker/datepicker-module.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import {NgModule} from '@angular/core';
1313
import {MatButtonModule} from '@angular/material/button';
1414
import {MatDialogModule} from '@angular/material/dialog';
1515
import {MatCalendar} from './calendar';
16-
import {MatCalendarHeader} from './calendar-header';
16+
import {MatCalendarHeader} from './calendar';
1717
import {MatCalendarBody} from './calendar-body';
1818
import {
1919
MAT_DATEPICKER_SCROLL_STRATEGY_PROVIDER,

src/lib/datepicker/public-api.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
export * from './datepicker-module';
1010
export * from './calendar';
11-
export * from './calendar-header';
1211
export * from './calendar-body';
1312
export * from './datepicker';
1413
export * from './datepicker-animations';

0 commit comments

Comments
 (0)