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