6
6
* found in the LICENSE file at https://angular.io/license
7
7
*/
8
8
9
- import { FactoryProvider , Injectable , OnDestroy , Optional , SkipSelf } from '@angular/core' ;
10
- import { DateAdapter } from '@angular/material/core ' ;
9
+ import { FactoryProvider , Injectable , Optional , SkipSelf , OnDestroy } from '@angular/core' ;
10
+ import { DateAdapter } from './date-adapter ' ;
11
11
import { Observable , Subject } from 'rxjs' ;
12
12
13
13
/** A class representing a range of dates. */
14
14
export class DateRange < D > {
15
- /**
16
- * Ensures that objects with a ` start` and `end` property can't be assigned to a variable that
17
- * expects a `DateRange`
18
- */
19
- // tslint:disable-next-line:no-unused-variable
20
- private _disableStructuralEquivalency : never ;
15
+ constructor (
16
+ /** The start date of the range. */
17
+ readonly start : D | null ,
18
+ /** The end date of the range. */
19
+ readonly end : D | null ) { }
20
+ }
21
21
22
- /** The start date of the range. */
23
- readonly start : D | null ;
22
+ type ExtractDateTypeFromSelection < T > = T extends DateRange < infer D > ? D : NonNullable < T > ;
24
23
25
- /** The end date of the range. */
26
- readonly end : D | null ;
24
+ /** Event emitted by the date selection model when its selection changes. */
25
+ export interface DateSelectionModelChange < S > {
26
+ /** New value for the selection. */
27
+ selection : S ;
27
28
28
- constructor ( range ?: { start ?: D | null , end ?: D | null } | null ) {
29
- this . start = range && range . start || null ;
30
- this . end = range && range . end || null ;
31
- }
29
+ /** Object that triggered the change. */
30
+ source : any ;
32
31
}
33
32
34
- type ExtractDateTypeFromSelection < T > = T extends DateRange < infer D > ? D : NonNullable < T > ;
35
-
36
33
/** A selection model containing a date selection. */
37
34
export abstract class MatDateSelectionModel < S , D = ExtractDateTypeFromSelection < S > >
38
35
implements OnDestroy {
39
- /** Subject used to emit value change events. */
40
- private _valueChangesSubject = new Subject < void > ( ) ;
36
+ private _selectionChanged = new Subject < DateSelectionModelChange < S > > ( ) ;
41
37
42
- /** Observable of value change events. */
43
- valueChanges : Observable < void > = this . _valueChangesSubject . asObservable ( ) ;
38
+ /** Emits when the selection has changed. */
39
+ selectionChanged : Observable < DateSelectionModelChange < S > > =
40
+ this . _selectionChanged . asObservable ( ) ;
44
41
45
42
/** The current selection. */
46
- get selection ( ) : S { return this . _selection ; }
47
- set selection ( s : S ) {
48
- this . _selection = s ;
49
- this . _valueChangesSubject . next ( ) ;
43
+ readonly selection : S ;
44
+
45
+ protected constructor ( protected readonly adapter : DateAdapter < D > , selection : S ) {
46
+ this . selection = selection ;
50
47
}
51
48
52
- protected constructor ( protected readonly adapter : DateAdapter < D > , private _selection : S ) { }
49
+ updateSelection ( value : S , source : any ) {
50
+ ( this as { selection : S } ) . selection = value ;
51
+ this . _selectionChanged . next ( { selection : value , source} ) ;
52
+ }
53
53
54
54
ngOnDestroy ( ) {
55
- this . _valueChangesSubject . complete ( ) ;
55
+ this . _selectionChanged . complete ( ) ;
56
56
}
57
57
58
58
/** Adds a date to the current selection. */
@@ -64,7 +64,7 @@ export abstract class MatDateSelectionModel<S, D = ExtractDateTypeFromSelection<
64
64
/**
65
65
* Checks whether the current selection is the same as the selection in the given selection model.
66
66
*/
67
- abstract isSame ( other : MatDateSelectionModel < D > ) : boolean ;
67
+ abstract isSame ( other : S ) : boolean ;
68
68
69
69
/** Checks whether the current selection is valid. */
70
70
abstract isValid ( ) : boolean ;
@@ -76,16 +76,16 @@ export abstract class MatDateSelectionModel<S, D = ExtractDateTypeFromSelection<
76
76
/** A selection model that contains a single date. */
77
77
@Injectable ( )
78
78
export class MatSingleDateSelectionModel < D > extends MatDateSelectionModel < D | null , D > {
79
- constructor ( adapter : DateAdapter < D > , date ?: D | null ) {
80
- super ( adapter , date || null ) ;
79
+ constructor ( adapter : DateAdapter < D > ) {
80
+ super ( adapter , null ) ;
81
81
}
82
82
83
83
/**
84
84
* Adds a date to the current selection. In the case of a single date selection, the added date
85
85
* simply overwrites the previous selection
86
86
*/
87
87
add ( date : D | null ) {
88
- this . selection = date ;
88
+ super . updateSelection ( date , this ) ;
89
89
}
90
90
91
91
/**
@@ -97,9 +97,8 @@ export class MatSingleDateSelectionModel<D> extends MatDateSelectionModel<D | nu
97
97
/**
98
98
* Checks whether the current selection is the same as the selection in the given selection model.
99
99
*/
100
- isSame ( other : MatDateSelectionModel < any > ) : boolean {
101
- return other instanceof MatSingleDateSelectionModel &&
102
- this . adapter . sameDate ( other . selection , this . selection ) ;
100
+ isSame ( other : D ) : boolean {
101
+ return this . adapter . sameDate ( other , this . selection ) ;
103
102
}
104
103
105
104
/**
@@ -122,8 +121,8 @@ export class MatSingleDateSelectionModel<D> extends MatDateSelectionModel<D | nu
122
121
/** A selection model that contains a date range. */
123
122
@Injectable ( )
124
123
export class MatRangeDateSelectionModel < D > extends MatDateSelectionModel < DateRange < D > , D > {
125
- constructor ( adapter : DateAdapter < D > , range ?: { start ?: D | null , end ?: D | null } | null ) {
126
- super ( adapter , new DateRange ( range ) ) ;
124
+ constructor ( adapter : DateAdapter < D > ) {
125
+ super ( adapter , new DateRange < D > ( null , null ) ) ;
127
126
}
128
127
129
128
/**
@@ -143,7 +142,7 @@ export class MatRangeDateSelectionModel<D> extends MatDateSelectionModel<DateRan
143
142
end = null ;
144
143
}
145
144
146
- this . selection = new DateRange < D > ( { start, end} ) ;
145
+ super . updateSelection ( new DateRange < D > ( start , end ) , this ) ;
147
146
}
148
147
149
148
/**
@@ -157,12 +156,9 @@ export class MatRangeDateSelectionModel<D> extends MatDateSelectionModel<DateRan
157
156
/**
158
157
* Checks whether the current selection is the same as the selection in the given selection model.
159
158
*/
160
- isSame ( other : MatDateSelectionModel < any > ) : boolean {
161
- if ( other instanceof MatRangeDateSelectionModel ) {
162
- return this . adapter . sameDate ( this . selection . start , other . selection . start ) &&
163
- this . adapter . sameDate ( this . selection . end , other . selection . end ) ;
164
- }
165
- return false ;
159
+ isSame ( other : DateRange < D > ) : boolean {
160
+ return this . adapter . sameDate ( this . selection . start , other . start ) &&
161
+ this . adapter . sameDate ( this . selection . end , other . end ) ;
166
162
}
167
163
168
164
/**
0 commit comments