Skip to content

Commit 0a0ea04

Browse files
committed
Add overlaps function to date selection model
1 parent aa1686a commit 0a0ea04

File tree

2 files changed

+60
-10
lines changed

2 files changed

+60
-10
lines changed

src/material/core/datetime/date-selection-model.ts

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
*/
88

99
import {DateAdapter} from '@angular/material/core';
10+
import {FactoryProvider, Optional, SkipSelf, Injectable} from '@angular/core';
1011
import {Subject, Observable} from 'rxjs';
1112

1213
export abstract class MatDateSelectionModel<D> {
@@ -23,6 +24,7 @@ export abstract class MatDateSelectionModel<D> {
2324
abstract isComplete(): boolean;
2425
abstract isSame(other: MatDateSelectionModel<D>): boolean;
2526
abstract isValid(): boolean;
27+
abstract overlaps(range: DateRange<D>): boolean;
2628
}
2729

2830
export interface DateRange<D> {
@@ -33,6 +35,7 @@ export interface DateRange<D> {
3335
/**
3436
* Concrete implementation of a MatDateSelectionModel that holds a single date.
3537
*/
38+
@Injectable()
3639
export class MatSingleDateSelectionModel<D> extends MatDateSelectionModel<D> {
3740
private _date: D | null = null;
3841

@@ -75,12 +78,23 @@ export class MatSingleDateSelectionModel<D> extends MatDateSelectionModel<D> {
7578
this._date = date;
7679
this._valueChangesSubject.next();
7780
}
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+
}
7891
}
7992

8093
/**
8194
* Concrete implementation of a MatDateSelectionModel that holds a date range, represented by
8295
* a start date and an end date.
8396
*/
97+
@Injectable()
8498
export class MatRangeDateSelectionModel<D> extends MatDateSelectionModel<D> {
8599
private _start: D | null = null;
86100
private _end: D | null = null;
@@ -138,4 +152,39 @@ export class MatRangeDateSelectionModel<D> extends MatDateSelectionModel<D> {
138152
end: this._end,
139153
};
140154
}
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+
}
141178
}
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+
};

tools/public_api_guard/material/core.d.ts

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,10 @@ export declare const MAT_OPTION_PARENT_COMPONENT: InjectionToken<MatOptionParent
205205

206206
export declare const MAT_RIPPLE_GLOBAL_OPTIONS: InjectionToken<RippleGlobalOptions>;
207207

208+
export declare function MAT_SINGLE_DATE_SELECTION_MODEL_FACTORY<D>(parent: MatSingleDateSelectionModel<D>, adapter: DateAdapter<D>): MatSingleDateSelectionModel<D>;
209+
210+
export declare const MAT_SINGLE_DATE_SELECTION_MODEL_PROVIDER: FactoryProvider;
211+
208212
export declare class MatCommonModule {
209213
constructor(highContrastModeDetector: HighContrastModeDetector, sanityChecks: any);
210214
static ɵinj: i0.ɵɵInjectorDef<MatCommonModule>;
@@ -233,6 +237,7 @@ export declare abstract class MatDateSelectionModel<D> {
233237
abstract isComplete(): boolean;
234238
abstract isSame(other: MatDateSelectionModel<D>): boolean;
235239
abstract isValid(): boolean;
240+
abstract overlaps(range: DateRange<D>): boolean;
236241
}
237242

238243
export declare const MATERIAL_SANITY_CHECKS: InjectionToken<SanityChecks>;
@@ -336,7 +341,10 @@ export declare class MatRangeDateSelectionModel<D> extends MatDateSelectionModel
336341
isComplete(): boolean;
337342
isSame(other: MatDateSelectionModel<D>): boolean;
338343
isValid(): boolean;
344+
overlaps(range: DateRange<D>): boolean;
339345
setRange(start: D | null, end: D | null): void;
346+
static ɵfac: i0.ɵɵFactoryDef<MatRangeDateSelectionModel<any>>;
347+
static ɵprov: i0.ɵɵInjectableDef<MatRangeDateSelectionModel<any>>;
340348
}
341349

342350
export declare class MatRipple implements OnInit, OnDestroy, RippleTarget {
@@ -372,17 +380,10 @@ export declare class MatSingleDateSelectionModel<D> extends MatDateSelectionMode
372380
isComplete(): boolean;
373381
isSame(other: MatDateSelectionModel<D>): boolean;
374382
isValid(): boolean;
383+
overlaps(range: DateRange<D>): boolean;
375384
setDate(date: D | null): void;
376-
}
377-
378-
export declare class MatSingleDateSelectionModel<D> extends MatDateSelectionModel<D> {
379-
constructor(adapter: DateAdapter<D>, date?: D | null);
380-
add(date: D | null): void;
381-
asDate(): D | null;
382-
compareDate(other: MatSingleDateSelectionModel<D>): number | boolean;
383-
isComplete(): boolean;
384-
isSame(other: MatDateSelectionModel<D>): boolean;
385-
isValid(): boolean;
385+
static ɵfac: i0.ɵɵFactoryDef<MatSingleDateSelectionModel<any>>;
386+
static ɵprov: i0.ɵɵInjectableDef<MatSingleDateSelectionModel<any>>;
386387
}
387388

388389
export declare const JAN = 0, FEB = 1, MAR = 2, APR = 3, MAY = 4, JUN = 5, JUL = 6, AUG = 7, SEP = 8, OCT = 9, NOV = 10, DEC = 11;

0 commit comments

Comments
 (0)