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. */
@@ -19,40 +19,52 @@ export class DateRange<D> {
19
19
// tslint:disable-next-line:no-unused-variable
20
20
private _disableStructuralEquivalency : never ;
21
21
22
- /** The start date of the range. */
23
- readonly start : D | null ;
24
-
25
- /** The end date of the range. */
26
- readonly end : D | null ;
27
-
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
- }
22
+ constructor (
23
+ /** The start date of the range. */
24
+ readonly start : D | null ,
25
+ /** The end date of the range. */
26
+ readonly end : D | null ) { }
32
27
}
33
28
34
29
type ExtractDateTypeFromSelection < T > = T extends DateRange < infer D > ? D : NonNullable < T > ;
35
30
31
+ /** Event emitted by the date selection model when its selection changes. */
32
+ export interface DateSelectionModelChange < S > {
33
+ /** New value for the selection. */
34
+ selection : S ;
35
+
36
+ /** Object that triggered the change. */
37
+ source : unknown ;
38
+ }
39
+
36
40
/** A selection model containing a date selection. */
37
41
export abstract class MatDateSelectionModel < S , D = ExtractDateTypeFromSelection < S > >
38
42
implements OnDestroy {
39
- /** Subject used to emit value change events. */
40
- private _valueChangesSubject = new Subject < void > ( ) ;
43
+ private _selectionChanged = new Subject < DateSelectionModelChange < S > > ( ) ;
41
44
42
- /** Observable of value change events . */
43
- valueChanges : Observable < void > = this . _valueChangesSubject . asObservable ( ) ;
45
+ /** Emits when the selection has changed . */
46
+ selectionChanged : Observable < DateSelectionModelChange < S > > = this . _selectionChanged . asObservable ( ) ;
44
47
45
- /** The current selection. */
46
- get selection ( ) : S { return this . _selection ; }
47
- set selection ( s : S ) {
48
- this . _selection = s ;
49
- this . _valueChangesSubject . next ( ) ;
48
+ protected constructor (
49
+ /** Date adapter used when interacting with dates in the model. */
50
+ protected readonly adapter : DateAdapter < D > ,
51
+ /** The current selection. */
52
+ readonly selection : S ) {
53
+ this . selection = selection ;
50
54
}
51
55
52
- protected constructor ( protected readonly adapter : DateAdapter < D > , private _selection : S ) { }
56
+ /**
57
+ * Updates the current selection in the model.
58
+ * @param value New selection that should be assigned.
59
+ * @param source Object that triggered the selection change.
60
+ */
61
+ updateSelection ( value : S , source : unknown ) {
62
+ ( this as { selection : S } ) . selection = value ;
63
+ this . _selectionChanged . next ( { selection : value , source} ) ;
64
+ }
53
65
54
66
ngOnDestroy ( ) {
55
- this . _valueChangesSubject . complete ( ) ;
67
+ this . _selectionChanged . complete ( ) ;
56
68
}
57
69
58
70
/** Adds a date to the current selection. */
@@ -61,10 +73,8 @@ export abstract class MatDateSelectionModel<S, D = ExtractDateTypeFromSelection<
61
73
/** Checks whether the current selection is complete. */
62
74
abstract isComplete ( ) : boolean ;
63
75
64
- /**
65
- * Checks whether the current selection is the same as the selection in the given selection model.
66
- */
67
- abstract isSame ( other : MatDateSelectionModel < D > ) : boolean ;
76
+ /** Checks whether the current selection is identical to the passed-in selection. */
77
+ abstract isSame ( other : S ) : boolean ;
68
78
69
79
/** Checks whether the current selection is valid. */
70
80
abstract isValid ( ) : boolean ;
@@ -76,16 +86,16 @@ export abstract class MatDateSelectionModel<S, D = ExtractDateTypeFromSelection<
76
86
/** A selection model that contains a single date. */
77
87
@Injectable ( )
78
88
export class MatSingleDateSelectionModel < D > extends MatDateSelectionModel < D | null , D > {
79
- constructor ( adapter : DateAdapter < D > , date ?: D | null ) {
80
- super ( adapter , date || null ) ;
89
+ constructor ( adapter : DateAdapter < D > ) {
90
+ super ( adapter , null ) ;
81
91
}
82
92
83
93
/**
84
94
* Adds a date to the current selection. In the case of a single date selection, the added date
85
95
* simply overwrites the previous selection
86
96
*/
87
97
add ( date : D | null ) {
88
- this . selection = date ;
98
+ super . updateSelection ( date , this ) ;
89
99
}
90
100
91
101
/**
@@ -94,12 +104,9 @@ export class MatSingleDateSelectionModel<D> extends MatDateSelectionModel<D | nu
94
104
*/
95
105
isComplete ( ) { return this . selection != null ; }
96
106
97
- /**
98
- * Checks whether the current selection is the same as the selection in the given selection model.
99
- */
100
- isSame ( other : MatDateSelectionModel < any > ) : boolean {
101
- return other instanceof MatSingleDateSelectionModel &&
102
- this . adapter . sameDate ( other . selection , this . selection ) ;
107
+ /** Checks whether the current selection is identical to the passed-in selection. */
108
+ isSame ( other : D ) : boolean {
109
+ return this . adapter . sameDate ( other , this . selection ) ;
103
110
}
104
111
105
112
/**
@@ -122,8 +129,8 @@ export class MatSingleDateSelectionModel<D> extends MatDateSelectionModel<D | nu
122
129
/** A selection model that contains a date range. */
123
130
@Injectable ( )
124
131
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 ) ) ;
132
+ constructor ( adapter : DateAdapter < D > ) {
133
+ super ( adapter , new DateRange < D > ( null , null ) ) ;
127
134
}
128
135
129
136
/**
@@ -143,7 +150,7 @@ export class MatRangeDateSelectionModel<D> extends MatDateSelectionModel<DateRan
143
150
end = null ;
144
151
}
145
152
146
- this . selection = new DateRange < D > ( { start, end} ) ;
153
+ super . updateSelection ( new DateRange < D > ( start , end ) , this ) ;
147
154
}
148
155
149
156
/**
@@ -154,15 +161,10 @@ export class MatRangeDateSelectionModel<D> extends MatDateSelectionModel<DateRan
154
161
return this . selection . start != null && this . selection . end != null ;
155
162
}
156
163
157
- /**
158
- * Checks whether the current selection is the same as the selection in the given selection model.
159
- */
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 ;
164
+ /** Checks whether the current selection is identical to the passed-in selection. */
165
+ isSame ( other : DateRange < D > ) : boolean {
166
+ return this . adapter . sameDate ( this . selection . start , other . start ) &&
167
+ this . adapter . sameDate ( this . selection . end , other . end ) ;
166
168
}
167
169
168
170
/**
@@ -199,11 +201,13 @@ export class MatRangeDateSelectionModel<D> extends MatDateSelectionModel<DateRan
199
201
}
200
202
}
201
203
204
+ /** @docs -private */
202
205
export function MAT_SINGLE_DATE_SELECTION_MODEL_FACTORY (
203
206
parent : MatSingleDateSelectionModel < unknown > , adapter : DateAdapter < unknown > ) {
204
207
return parent || new MatSingleDateSelectionModel ( adapter ) ;
205
208
}
206
209
210
+ /** Used to provide a single selection model to a component. */
207
211
export const MAT_SINGLE_DATE_SELECTION_MODEL_PROVIDER : FactoryProvider = {
208
212
provide : MatDateSelectionModel ,
209
213
deps : [ [ new Optional ( ) , new SkipSelf ( ) , MatDateSelectionModel ] , DateAdapter ] ,
0 commit comments