7
7
*/
8
8
9
9
import { DateAdapter } from '@angular/material/core' ;
10
+ import { FactoryProvider , Optional , SkipSelf , Injectable } from '@angular/core' ;
10
11
import { Subject , Observable } from 'rxjs' ;
11
12
12
13
export abstract class MatDateSelectionModel < D > {
@@ -23,6 +24,7 @@ export abstract class MatDateSelectionModel<D> {
23
24
abstract isComplete ( ) : boolean ;
24
25
abstract isSame ( other : MatDateSelectionModel < D > ) : boolean ;
25
26
abstract isValid ( ) : boolean ;
27
+ abstract overlaps ( range : DateRange < D > ) : boolean ;
26
28
}
27
29
28
30
export interface DateRange < D > {
@@ -33,6 +35,7 @@ export interface DateRange<D> {
33
35
/**
34
36
* Concrete implementation of a MatDateSelectionModel that holds a single date.
35
37
*/
38
+ @Injectable ( )
36
39
export class MatSingleDateSelectionModel < D > extends MatDateSelectionModel < D > {
37
40
private _date : D | null = null ;
38
41
@@ -75,12 +78,22 @@ export class MatSingleDateSelectionModel<D> extends MatDateSelectionModel<D> {
75
78
this . _date = date ;
76
79
this . _valueChangesSubject . next ( ) ;
77
80
}
81
+ /**
82
+ * Determines if the single date is within a given date range. Retuns false if either dates of
83
+ * the range is null or if the selection is undefined.
84
+ */
85
+ overlaps ( range : DateRange < D > ) : boolean {
86
+ return ! ! ( this . _date && range . start && range . end &&
87
+ this . adapter . compareDate ( range . start , this . _date ) <= 0 &&
88
+ this . adapter . compareDate ( this . _date , range . end ) <= 0 ) ;
89
+ }
78
90
}
79
91
80
92
/**
81
93
* Concrete implementation of a MatDateSelectionModel that holds a date range, represented by
82
94
* a start date and an end date.
83
95
*/
96
+ @Injectable ( )
84
97
export class MatRangeDateSelectionModel < D > extends MatDateSelectionModel < D > {
85
98
private _start : D | null = null ;
86
99
private _end : D | null = null ;
@@ -138,4 +151,39 @@ export class MatRangeDateSelectionModel<D> extends MatDateSelectionModel<D> {
138
151
end : this . _end ,
139
152
} ;
140
153
}
154
+
155
+ /**
156
+ * Returns true if the given range and the selection overlap in any way. False if otherwise, that
157
+ * includes incomplete selections or ranges.
158
+ */
159
+ overlaps ( range : DateRange < D > ) : boolean {
160
+ if ( ! ( this . _start && this . _end && range . start && range . end ) ) {
161
+ return false ;
162
+ }
163
+
164
+ return (
165
+ this . _isBetween ( range . start , this . _start , this . _end ) ||
166
+ this . _isBetween ( range . end , this . _start , this . _end ) ||
167
+ (
168
+ this . adapter . compareDate ( range . start , this . _start ) <= 0 &&
169
+ this . adapter . compareDate ( this . _end , range . end ) <= 0
170
+ )
171
+ ) ;
172
+ }
173
+
174
+ private _isBetween ( value : D , from : D , to : D ) : boolean {
175
+ return this . adapter . compareDate ( from , value ) <= 0 && this . adapter . compareDate ( value , to ) <= 0 ;
176
+ }
141
177
}
178
+
179
+ export function MAT_SINGLE_DATE_SELECTION_MODEL_FACTORY < D > ( parent :
180
+ MatSingleDateSelectionModel < D > ,
181
+ adapter : DateAdapter < D > ) {
182
+ return parent || new MatSingleDateSelectionModel ( adapter ) ;
183
+ }
184
+
185
+ export const MAT_SINGLE_DATE_SELECTION_MODEL_PROVIDER : FactoryProvider = {
186
+ provide : MatDateSelectionModel ,
187
+ deps : [ [ new Optional ( ) , new SkipSelf ( ) , MatDateSelectionModel ] , DateAdapter ] ,
188
+ useFactory : MAT_SINGLE_DATE_SELECTION_MODEL_FACTORY ,
189
+ } ;
0 commit comments