@@ -12,6 +12,7 @@ import {
12
12
ChangeDetectorRef ,
13
13
Component ,
14
14
EventEmitter ,
15
+ Host ,
15
16
Inject ,
16
17
Input ,
17
18
OnChanges ,
@@ -30,7 +31,78 @@ import {MatMonthView} from './month-view';
30
31
import { MatMultiYearView , yearsPerPage } from './multi-year-view' ;
31
32
import { MatYearView } from './year-view' ;
32
33
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
+ }
34
106
35
107
/**
36
108
* A calendar that is used as part of the datepicker.
0 commit comments