Skip to content

Adds a date selection model #17363

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
Oct 11, 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
136 changes: 136 additions & 0 deletions src/material/core/datetime/date-selection-model.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/

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

export abstract class MatDateSelectionModel<D> {
protected _valueChangesSubject = new Subject<void>();
valueChanges: Observable<void> = this._valueChangesSubject.asObservable();

constructor(protected readonly adapter: DateAdapter<D>) {}

destroy() {
this._valueChangesSubject.complete();
}

abstract add(date: D | null): void;
abstract isComplete(): boolean;
abstract isSame(other: MatDateSelectionModel<D>): boolean;
abstract isValid(): boolean;
}

export interface DateRange<D> {
start: D | null;
end: D | null;
}

/**
* Concrete implementation of a MatDateSelectionModel that holds a single date.
*/
export class MatSingleDateSelectionModel<D> extends MatDateSelectionModel<D> {
private _date: D | null = null;

constructor(adapter: DateAdapter<D>, date?: D | null) {
super(adapter);
this._date = date === undefined ? null : date;
}

add(date: D | null) {
this._date = date;
this._valueChangesSubject.next();
}

compareDate(other: MatSingleDateSelectionModel<D>) {
const date = this.asDate();
const otherDate = other.asDate();
if (date != null && otherDate != null) {
return this.adapter.compareDate(date, otherDate);
}
return date === otherDate;
}

isComplete() { return this._date != null; }

isSame(other: MatDateSelectionModel<D>): boolean {
return other instanceof MatSingleDateSelectionModel &&
this.adapter.sameDate(other.asDate(), this._date);
}

isValid(): boolean {
return this._date != null && this.adapter.isDateInstance(this._date) &&
this.adapter.isValid(this._date);
}

asDate(): D | null {
return this.isValid() ? this._date : null;
}
}

/**
* Concrete implementation of a MatDateSelectionModel that holds a date range, represented by
* a start date and an end date.
*/
export class MatRangeDateSelectionModel<D> extends MatDateSelectionModel<D> {
private _start: D | null = null;
private _end: D | null = null;

constructor(adapter: DateAdapter<D>, start?: D | null, end?: D | null) {
super(adapter);
this._start = start === undefined ? null : start;
this._end = end === undefined ? null : end;
}

/**
* Adds an additional date to the range. If no date is set thus far, it will set it to the
* beginning. If the beginning is set, it will set it to the end.
* If add is called on a complete selection, it will empty the selection and set it as the start.
*/
add(date: D | null): void {
if (this._start == null) {
this._start = date;
} else if (this._end == null) {
this._end = date;
} else {
this._start = date;
this._end = null;
}

this._valueChangesSubject.next();
}

setRange(start: D | null, end: D | null) {
this._start = start;
this._end = end;
}

isComplete(): boolean {
return this._start != null && this._end != null;
}

isSame(other: MatDateSelectionModel<D>): boolean {
if (other instanceof MatRangeDateSelectionModel) {
const otherRange = other.asRange();
return this.adapter.sameDate(this._start, otherRange.start) &&
this.adapter.sameDate(this._end, otherRange.end);
}
return false;
}

isValid(): boolean {
return this._start != null && this._end != null &&
this.adapter.isValid(this._start!) && this.adapter.isValid(this._end!);
}

asRange(): DateRange<D> {
return {
start: this._start,
end: this._end,
};
}
}
1 change: 1 addition & 0 deletions src/material/core/datetime/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export * from './date-adapter';
export * from './date-formats';
export * from './native-date-adapter';
export * from './native-date-formats';
export * from './date-selection-model';


@NgModule({
Expand Down
37 changes: 37 additions & 0 deletions tools/public_api_guard/material/core.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ export declare abstract class DateAdapter<D> {
abstract today(): D;
}

export interface DateRange<D> {
end: D | null;
start: D | null;
}

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;

export declare const defaultRippleAnimationConfig: {
Expand Down Expand Up @@ -213,6 +218,18 @@ export declare type MatDateFormats = {
};
};

export declare abstract class MatDateSelectionModel<D> {
protected _valueChangesSubject: Subject<void>;
protected readonly adapter: DateAdapter<D>;
valueChanges: Observable<void>;
constructor(adapter: DateAdapter<D>);
abstract add(date: D | null): void;
destroy(): void;
abstract isComplete(): boolean;
abstract isSame(other: MatDateSelectionModel<D>): boolean;
abstract isValid(): boolean;
}

export declare const MATERIAL_SANITY_CHECKS: InjectionToken<SanityChecks>;

export declare class MatLine {
Expand Down Expand Up @@ -289,6 +306,16 @@ export declare class MatPseudoCheckboxModule {

export declare type MatPseudoCheckboxState = 'unchecked' | 'checked' | 'indeterminate';

export declare class MatRangeDateSelectionModel<D> extends MatDateSelectionModel<D> {
constructor(adapter: DateAdapter<D>, start?: D | null, end?: D | null);
add(date: D | null): void;
asRange(): DateRange<D>;
isComplete(): boolean;
isSame(other: MatDateSelectionModel<D>): boolean;
isValid(): boolean;
setRange(start: D | null, end: D | null): void;
}

export declare class MatRipple implements OnInit, OnDestroy, RippleTarget {
animation: RippleAnimationConfig;
centered: boolean;
Expand All @@ -310,6 +337,16 @@ export declare class MatRipple implements OnInit, OnDestroy, RippleTarget {
export declare class MatRippleModule {
}

export declare class MatSingleDateSelectionModel<D> extends MatDateSelectionModel<D> {
constructor(adapter: DateAdapter<D>, date?: D | null);
add(date: D | null): void;
asDate(): D | null;
compareDate(other: MatSingleDateSelectionModel<D>): number | boolean;
isComplete(): boolean;
isSame(other: MatDateSelectionModel<D>): boolean;
isValid(): boolean;
}

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;

export declare function mixinColor<T extends Constructor<HasElementRef>>(base: T, defaultColor?: ThemePalette): CanColorCtor & T;
Expand Down