|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright Google LLC All Rights Reserved. |
| 4 | + * |
| 5 | + * Use of this source code is governed by an MIT-style license that can be |
| 6 | + * found in the LICENSE file at https://angular.io/license |
| 7 | + */ |
| 8 | + |
| 9 | +import {FactoryProvider, Injectable, Optional, SkipSelf, OnDestroy} from '@angular/core'; |
| 10 | +import {DateAdapter} from './date-adapter'; |
| 11 | +import {Observable, Subject} from 'rxjs'; |
| 12 | + |
| 13 | +/** A class representing a range of dates. */ |
| 14 | +export class DateRange<D> { |
| 15 | + /** |
| 16 | + * Ensures that objects with a `start` and `end` property can't be assigned to a variable that |
| 17 | + * expects a `DateRange` |
| 18 | + */ |
| 19 | + // tslint:disable-next-line:no-unused-variable |
| 20 | + private _disableStructuralEquivalency: never; |
| 21 | + |
| 22 | + constructor( |
| 23 | + /** The start date of the range. */ |
| 24 | + readonly start: D | null, |
| 25 | + /** The end date of the range. */ |
| 26 | + readonly end: D | null) {} |
| 27 | +} |
| 28 | + |
| 29 | +type ExtractDateTypeFromSelection<T> = T extends DateRange<infer D> ? D : NonNullable<T>; |
| 30 | + |
| 31 | +/** Event emitted by the date selection model when its selection changes. */ |
| 32 | +export interface DateSelectionModelChange<S> { |
| 33 | + /** New value for the selection. */ |
| 34 | + selection: S; |
| 35 | + |
| 36 | + /** Object that triggered the change. */ |
| 37 | + source: unknown; |
| 38 | +} |
| 39 | + |
| 40 | +/** A selection model containing a date selection. */ |
| 41 | +export abstract class MatDateSelectionModel<S, D = ExtractDateTypeFromSelection<S>> |
| 42 | + implements OnDestroy { |
| 43 | + private _selectionChanged = new Subject<DateSelectionModelChange<S>>(); |
| 44 | + |
| 45 | + /** Emits when the selection has changed. */ |
| 46 | + selectionChanged: Observable<DateSelectionModelChange<S>> = this._selectionChanged.asObservable(); |
| 47 | + |
| 48 | + protected constructor( |
| 49 | + /** Date adapter used when interacting with dates in the model. */ |
| 50 | + protected readonly adapter: DateAdapter<D>, |
| 51 | + /** The current selection. */ |
| 52 | + readonly selection: S) { |
| 53 | + this.selection = selection; |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Updates the current selection in the model. |
| 58 | + * @param value New selection that should be assigned. |
| 59 | + * @param source Object that triggered the selection change. |
| 60 | + */ |
| 61 | + updateSelection(value: S, source: unknown) { |
| 62 | + (this as {selection: S}).selection = value; |
| 63 | + this._selectionChanged.next({selection: value, source}); |
| 64 | + } |
| 65 | + |
| 66 | + ngOnDestroy() { |
| 67 | + this._selectionChanged.complete(); |
| 68 | + } |
| 69 | + |
| 70 | + /** Adds a date to the current selection. */ |
| 71 | + abstract add(date: D | null): void; |
| 72 | + |
| 73 | + /** Checks whether the current selection is complete. */ |
| 74 | + abstract isComplete(): boolean; |
| 75 | + |
| 76 | + /** Checks whether the current selection is identical to the passed-in selection. */ |
| 77 | + abstract isSame(other: S): boolean; |
| 78 | + |
| 79 | + /** Checks whether the current selection is valid. */ |
| 80 | + abstract isValid(): boolean; |
| 81 | + |
| 82 | + /** Checks whether the current selection overlaps with the given range. */ |
| 83 | + abstract overlaps(range: DateRange<D>): boolean; |
| 84 | +} |
| 85 | + |
| 86 | +/** A selection model that contains a single date. */ |
| 87 | +@Injectable() |
| 88 | +export class MatSingleDateSelectionModel<D> extends MatDateSelectionModel<D | null, D> { |
| 89 | + constructor(adapter: DateAdapter<D>) { |
| 90 | + super(adapter, null); |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Adds a date to the current selection. In the case of a single date selection, the added date |
| 95 | + * simply overwrites the previous selection |
| 96 | + */ |
| 97 | + add(date: D | null) { |
| 98 | + super.updateSelection(date, this); |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * Checks whether the current selection is complete. In the case of a single date selection, this |
| 103 | + * is true if the current selection is not null. |
| 104 | + */ |
| 105 | + isComplete() { return this.selection != null; } |
| 106 | + |
| 107 | + /** Checks whether the current selection is identical to the passed-in selection. */ |
| 108 | + isSame(other: D): boolean { |
| 109 | + return this.adapter.sameDate(other, this.selection); |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * Checks whether the current selection is valid. In the case of a single date selection, this |
| 114 | + * means that the current selection is not null and is a valid date. |
| 115 | + */ |
| 116 | + isValid(): boolean { |
| 117 | + return this.selection != null && this.adapter.isDateInstance(this.selection) && |
| 118 | + this.adapter.isValid(this.selection); |
| 119 | + } |
| 120 | + |
| 121 | + /** Checks whether the current selection overlaps with the given range. */ |
| 122 | + overlaps(range: DateRange<D>): boolean { |
| 123 | + return !!(this.selection && range.start && range.end && |
| 124 | + this.adapter.compareDate(range.start, this.selection) <= 0 && |
| 125 | + this.adapter.compareDate(this.selection, range.end) <= 0); |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +/** A selection model that contains a date range. */ |
| 130 | +@Injectable() |
| 131 | +export class MatRangeDateSelectionModel<D> extends MatDateSelectionModel<DateRange<D>, D> { |
| 132 | + constructor(adapter: DateAdapter<D>) { |
| 133 | + super(adapter, new DateRange<D>(null, null)); |
| 134 | + } |
| 135 | + |
| 136 | + /** |
| 137 | + * Adds a date to the current selection. In the case of a date range selection, the added date |
| 138 | + * fills in the next `null` value in the range. If both the start and the end already have a date, |
| 139 | + * the selection is reset so that the given date is the new `start` and the `end` is null. |
| 140 | + */ |
| 141 | + add(date: D | null): void { |
| 142 | + let {start, end} = this.selection; |
| 143 | + |
| 144 | + if (start == null) { |
| 145 | + start = date; |
| 146 | + } else if (end == null) { |
| 147 | + end = date; |
| 148 | + } else { |
| 149 | + start = date; |
| 150 | + end = null; |
| 151 | + } |
| 152 | + |
| 153 | + super.updateSelection(new DateRange<D>(start, end), this); |
| 154 | + } |
| 155 | + |
| 156 | + /** |
| 157 | + * Checks whether the current selection is complete. In the case of a date range selection, this |
| 158 | + * is true if the current selection has a non-null `start` and `end`. |
| 159 | + */ |
| 160 | + isComplete(): boolean { |
| 161 | + return this.selection.start != null && this.selection.end != null; |
| 162 | + } |
| 163 | + |
| 164 | + /** Checks whether the current selection is identical to the passed-in selection. */ |
| 165 | + isSame(other: DateRange<D>): boolean { |
| 166 | + return this.adapter.sameDate(this.selection.start, other.start) && |
| 167 | + this.adapter.sameDate(this.selection.end, other.end); |
| 168 | + } |
| 169 | + |
| 170 | + /** |
| 171 | + * Checks whether the current selection is valid. In the case of a date range selection, this |
| 172 | + * means that the current selection has a `start` and `end` that are both non-null and valid |
| 173 | + * dates. |
| 174 | + */ |
| 175 | + isValid(): boolean { |
| 176 | + return this.selection.start != null && this.selection.end != null && |
| 177 | + this.adapter.isValid(this.selection.start!) && this.adapter.isValid(this.selection.end!); |
| 178 | + } |
| 179 | + |
| 180 | + /** |
| 181 | + * Returns true if the given range and the selection overlap in any way. False if otherwise, that |
| 182 | + * includes incomplete selections or ranges. |
| 183 | + */ |
| 184 | + overlaps(range: DateRange<D>): boolean { |
| 185 | + if (!(this.selection.start && this.selection.end && range.start && range.end)) { |
| 186 | + return false; |
| 187 | + } |
| 188 | + |
| 189 | + return ( |
| 190 | + this._isBetween(range.start, this.selection.start, this.selection.end) || |
| 191 | + this._isBetween(range.end, this.selection.start, this.selection.end) || |
| 192 | + ( |
| 193 | + this.adapter.compareDate(range.start, this.selection.start) <= 0 && |
| 194 | + this.adapter.compareDate(this.selection.end, range.end) <= 0 |
| 195 | + ) |
| 196 | + ); |
| 197 | + } |
| 198 | + |
| 199 | + private _isBetween(value: D, from: D, to: D): boolean { |
| 200 | + return this.adapter.compareDate(from, value) <= 0 && this.adapter.compareDate(value, to) <= 0; |
| 201 | + } |
| 202 | +} |
| 203 | + |
| 204 | +/** @docs-private */ |
| 205 | +export function MAT_SINGLE_DATE_SELECTION_MODEL_FACTORY( |
| 206 | + parent: MatSingleDateSelectionModel<unknown>, adapter: DateAdapter<unknown>) { |
| 207 | + return parent || new MatSingleDateSelectionModel(adapter); |
| 208 | +} |
| 209 | + |
| 210 | +/** Used to provide a single selection model to a component. */ |
| 211 | +export const MAT_SINGLE_DATE_SELECTION_MODEL_PROVIDER: FactoryProvider = { |
| 212 | + provide: MatDateSelectionModel, |
| 213 | + deps: [[new Optional(), new SkipSelf(), MatDateSelectionModel], DateAdapter], |
| 214 | + useFactory: MAT_SINGLE_DATE_SELECTION_MODEL_FACTORY, |
| 215 | +}; |
0 commit comments