Skip to content

Make the selection model injectable and add an overlaps method #17766

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 21, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions src/material/core/datetime/date-selection-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/

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

export abstract class MatDateSelectionModel<D> {
Expand All @@ -23,6 +24,7 @@ export abstract class MatDateSelectionModel<D> {
abstract isComplete(): boolean;
abstract isSame(other: MatDateSelectionModel<D>): boolean;
abstract isValid(): boolean;
abstract overlaps(range: DateRange<D>): boolean;
}

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

Expand Down Expand Up @@ -75,12 +78,23 @@ export class MatSingleDateSelectionModel<D> extends MatDateSelectionModel<D> {
this._date = date;
this._valueChangesSubject.next();
}

/**
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: newline before

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.

* Determines if the single date is within a given date range. Retuns false if either dates of
* the range is null or if the selection is undefined.
*/
overlaps(range: DateRange<D>): boolean {
return !!(this._date && range.start && range.end &&
this.adapter.compareDate(range.start, this._date) <= 0 &&
this.adapter.compareDate(this._date, range.end) <= 0);
}
}

/**
* Concrete implementation of a MatDateSelectionModel that holds a date range, represented by
* a start date and an end date.
*/
@Injectable()
export class MatRangeDateSelectionModel<D> extends MatDateSelectionModel<D> {
private _start: D | null = null;
private _end: D | null = null;
Expand Down Expand Up @@ -138,4 +152,39 @@ export class MatRangeDateSelectionModel<D> extends MatDateSelectionModel<D> {
end: this._end,
};
}

/**
* Returns true if the given range and the selection overlap in any way. False if otherwise, that
* includes incomplete selections or ranges.
*/
overlaps(range: DateRange<D>): boolean {
if (!(this._start && this._end && range.start && range.end)) {
return false;
}

return (
this._isBetween(range.start, this._start, this._end) ||
this._isBetween(range.end, this._start, this._end) ||
(
this.adapter.compareDate(range.start, this._start) <= 0 &&
this.adapter.compareDate(this._end, range.end) <= 0
)
);
}

private _isBetween(value: D, from: D, to: D): boolean {
return this.adapter.compareDate(from, value) <= 0 && this.adapter.compareDate(value, to) <= 0;
}
}

export function MAT_SINGLE_DATE_SELECTION_MODEL_FACTORY<D>(parent:
MatSingleDateSelectionModel<D>,
adapter: DateAdapter<D>) {
return parent || new MatSingleDateSelectionModel(adapter);
}

export const MAT_SINGLE_DATE_SELECTION_MODEL_PROVIDER: FactoryProvider = {
provide: MatDateSelectionModel,
deps: [[new Optional(), new SkipSelf(), MatDateSelectionModel], DateAdapter],
useFactory: MAT_SINGLE_DATE_SELECTION_MODEL_FACTORY,
};
11 changes: 11 additions & 0 deletions tools/public_api_guard/material/core.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,10 @@ export declare const MAT_OPTION_PARENT_COMPONENT: InjectionToken<MatOptionParent

export declare const MAT_RIPPLE_GLOBAL_OPTIONS: InjectionToken<RippleGlobalOptions>;

export declare function MAT_SINGLE_DATE_SELECTION_MODEL_FACTORY<D>(parent: MatSingleDateSelectionModel<D>, adapter: DateAdapter<D>): MatSingleDateSelectionModel<D>;

export declare const MAT_SINGLE_DATE_SELECTION_MODEL_PROVIDER: FactoryProvider;

export declare class MatCommonModule {
constructor(highContrastModeDetector: HighContrastModeDetector, sanityChecks: any);
static ɵinj: i0.ɵɵInjectorDef<MatCommonModule>;
Expand Down Expand Up @@ -233,6 +237,7 @@ export declare abstract class MatDateSelectionModel<D> {
abstract isComplete(): boolean;
abstract isSame(other: MatDateSelectionModel<D>): boolean;
abstract isValid(): boolean;
abstract overlaps(range: DateRange<D>): boolean;
}

export declare const MATERIAL_SANITY_CHECKS: InjectionToken<SanityChecks>;
Expand Down Expand Up @@ -336,7 +341,10 @@ export declare class MatRangeDateSelectionModel<D> extends MatDateSelectionModel
isComplete(): boolean;
isSame(other: MatDateSelectionModel<D>): boolean;
isValid(): boolean;
overlaps(range: DateRange<D>): boolean;
setRange(start: D | null, end: D | null): void;
static ɵfac: i0.ɵɵFactoryDef<MatRangeDateSelectionModel<any>>;
static ɵprov: i0.ɵɵInjectableDef<MatRangeDateSelectionModel<any>>;
}

export declare class MatRipple implements OnInit, OnDestroy, RippleTarget {
Expand Down Expand Up @@ -372,7 +380,10 @@ export declare class MatSingleDateSelectionModel<D> extends MatDateSelectionMode
isComplete(): boolean;
isSame(other: MatDateSelectionModel<D>): boolean;
isValid(): boolean;
overlaps(range: DateRange<D>): boolean;
setDate(date: D | null): void;
static ɵfac: i0.ɵɵFactoryDef<MatSingleDateSelectionModel<any>>;
static ɵprov: i0.ɵɵInjectableDef<MatSingleDateSelectionModel<any>>;
}

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;
Expand Down