Skip to content

Commit a41213e

Browse files
committed
refactor(datepicker): clean up date selection model (#18943)
- Removes some methods from the date selection model that were put in place under the assumption that they'd be used, but we never ended up using them. - Moves the selection model out of `core` and into `datepicker`. I decided to do it, because it seems very tightly coupled to the datepicker and I don't see us needing it in other components in the future.
1 parent 5dc649b commit a41213e

17 files changed

+84
-156
lines changed

src/material/core/datetime/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ export * from './date-adapter';
1717
export * from './date-formats';
1818
export * from './native-date-adapter';
1919
export * from './native-date-formats';
20-
export * from './date-selection-model';
2120

2221

2322
@NgModule({

src/material/datepicker/calendar.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,6 @@ import {
2929
DateAdapter,
3030
MAT_DATE_FORMATS,
3131
MatDateFormats,
32-
MatDateSelectionModel,
33-
MAT_SINGLE_DATE_SELECTION_MODEL_PROVIDER,
34-
DateRange,
3532
} from '@angular/material/core';
3633
import {Subject, Subscription} from 'rxjs';
3734
import {MatCalendarCellCssClasses} from './calendar-body';
@@ -45,6 +42,11 @@ import {
4542
yearsPerPage
4643
} from './multi-year-view';
4744
import {MatYearView} from './year-view';
45+
import {
46+
MAT_SINGLE_DATE_SELECTION_MODEL_PROVIDER,
47+
DateRange,
48+
MatDateSelectionModel,
49+
} from './date-selection-model';
4850

4951
/**
5052
* Possible views for the calendar.

src/material/datepicker/date-range-input-parts.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,10 @@ import {
3636
DateAdapter,
3737
MatDateFormats,
3838
ErrorStateMatcher,
39-
DateRange,
4039
} from '@angular/material/core';
4140
import {BooleanInput} from '@angular/cdk/coercion';
4241
import {MatDatepickerInputBase, DateFilterFn} from './datepicker-input-base';
42+
import {DateRange} from './date-selection-model';
4343

4444
/** Parent component that should be wrapped around `MatStartDate` and `MatEndDate`. */
4545
export interface MatDateRangeInputParent<D> {

src/material/datepicker/date-range-input.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,7 @@ import {
2020
ElementRef,
2121
} from '@angular/core';
2222
import {MatFormFieldControl, MatFormField} from '@angular/material/form-field';
23-
import {
24-
DateRange,
25-
ThemePalette,
26-
DateAdapter,
27-
MatDateSelectionModel,
28-
} from '@angular/material/core';
23+
import {ThemePalette, DateAdapter} from '@angular/material/core';
2924
import {NgControl, ControlContainer} from '@angular/forms';
3025
import {Subject, merge} from 'rxjs';
3126
import {coerceBooleanProperty, BooleanInput} from '@angular/cdk/coercion';
@@ -39,6 +34,7 @@ import {MatDatepickerControl} from './datepicker-base';
3934
import {createMissingDateImplError} from './datepicker-errors';
4035
import {DateFilterFn} from './datepicker-input-base';
4136
import {MatDateRangePicker} from './date-range-picker';
37+
import {DateRange, MatDateSelectionModel} from './date-selection-model';
4238

4339
let nextUniqueId = 0;
4440

src/material/datepicker/date-range-picker.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
*/
88

99
import {ChangeDetectionStrategy, Component, ViewEncapsulation} from '@angular/core';
10-
import {MAT_RANGE_DATE_SELECTION_MODEL_PROVIDER, DateRange} from '@angular/material/core';
1110
import {MatDatepickerBase, MatDatepickerContent} from './datepicker-base';
1211
import {MatDateRangeInput} from './date-range-input';
12+
import {MAT_RANGE_DATE_SELECTION_MODEL_PROVIDER, DateRange} from './date-selection-model';
1313

1414
// TODO(mmalerba): We use a component instead of a directive here so the user can use implicit
1515
// template reference variables (e.g. #d vs #d="matDateRangePicker"). We can change this to a

src/material/core/datetime/date-selection-model.ts renamed to src/material/datepicker/date-selection-model.ts

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

99
import {FactoryProvider, Injectable, Optional, SkipSelf, OnDestroy} from '@angular/core';
10-
import {DateAdapter} from './date-adapter';
10+
import {DateAdapter} from '@angular/material/core';
1111
import {Observable, Subject} from 'rxjs';
1212

1313
/** A class representing a range of dates. */
@@ -76,15 +76,6 @@ export abstract class MatDateSelectionModel<S, D = ExtractDateTypeFromSelection<
7676

7777
/** Checks whether the current selection is complete. */
7878
abstract isComplete(): boolean;
79-
80-
/** Checks whether the current selection is identical to the passed-in selection. */
81-
abstract isSame(other: S): boolean;
82-
83-
/** Checks whether the current selection is valid. */
84-
abstract isValid(): boolean;
85-
86-
/** Checks whether the current selection overlaps with the given range. */
87-
abstract overlaps(range: DateRange<D>): boolean;
8879
}
8980

9081
/** A selection model that contains a single date. */
@@ -106,27 +97,8 @@ export class MatSingleDateSelectionModel<D> extends MatDateSelectionModel<D | nu
10697
* Checks whether the current selection is complete. In the case of a single date selection, this
10798
* is true if the current selection is not null.
10899
*/
109-
isComplete() { return this.selection != null; }
110-
111-
/** Checks whether the current selection is identical to the passed-in selection. */
112-
isSame(other: D): boolean {
113-
return this.adapter.sameDate(other, this.selection);
114-
}
115-
116-
/**
117-
* Checks whether the current selection is valid. In the case of a single date selection, this
118-
* means that the current selection is not null and is a valid date.
119-
*/
120-
isValid(): boolean {
121-
return this.selection != null && this.adapter.isDateInstance(this.selection) &&
122-
this.adapter.isValid(this.selection);
123-
}
124-
125-
/** Checks whether the current selection overlaps with the given range. */
126-
overlaps(range: DateRange<D>): boolean {
127-
return !!(this.selection && range.start && range.end &&
128-
this.adapter.compareDate(range.start, this.selection) <= 0 &&
129-
this.adapter.compareDate(this.selection, range.end) <= 0);
100+
isComplete() {
101+
return this.selection != null;
130102
}
131103
}
132104

@@ -164,45 +136,6 @@ export class MatRangeDateSelectionModel<D> extends MatDateSelectionModel<DateRan
164136
isComplete(): boolean {
165137
return this.selection.start != null && this.selection.end != null;
166138
}
167-
168-
/** Checks whether the current selection is identical to the passed-in selection. */
169-
isSame(other: DateRange<D>): boolean {
170-
return this.adapter.sameDate(this.selection.start, other.start) &&
171-
this.adapter.sameDate(this.selection.end, other.end);
172-
}
173-
174-
/**
175-
* Checks whether the current selection is valid. In the case of a date range selection, this
176-
* means that the current selection has a `start` and `end` that are both non-null and valid
177-
* dates.
178-
*/
179-
isValid(): boolean {
180-
return this.selection.start != null && this.selection.end != null &&
181-
this.adapter.isValid(this.selection.start!) && this.adapter.isValid(this.selection.end!);
182-
}
183-
184-
/**
185-
* Returns true if the given range and the selection overlap in any way. False if otherwise, that
186-
* includes incomplete selections or ranges.
187-
*/
188-
overlaps(range: DateRange<D>): boolean {
189-
if (!(this.selection.start && this.selection.end && range.start && range.end)) {
190-
return false;
191-
}
192-
193-
return (
194-
this._isBetween(range.start, this.selection.start, this.selection.end) ||
195-
this._isBetween(range.end, this.selection.start, this.selection.end) ||
196-
(
197-
this.adapter.compareDate(range.start, this.selection.start) <= 0 &&
198-
this.adapter.compareDate(this.selection.end, range.end) <= 0
199-
)
200-
);
201-
}
202-
203-
private _isBetween(value: D, from: D, to: D): boolean {
204-
return this.adapter.compareDate(from, value) <= 0 && this.adapter.compareDate(value, to) <= 0;
205-
}
206139
}
207140

208141
/** @docs-private */

src/material/datepicker/datepicker-base.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,6 @@ import {
4444
DateAdapter,
4545
mixinColor,
4646
ThemePalette,
47-
MatDateSelectionModel,
48-
ExtractDateTypeFromSelection,
4947
} from '@angular/material/core';
5048
import {MatDialog, MatDialogRef} from '@angular/material/dialog';
5149
import {merge, Subject, Observable} from 'rxjs';
@@ -55,6 +53,7 @@ import {matDatepickerAnimations} from './datepicker-animations';
5553
import {createMissingDateImplError} from './datepicker-errors';
5654
import {MatCalendarCellCssClasses} from './calendar-body';
5755
import {DateFilterFn} from './datepicker-input-base';
56+
import {ExtractDateTypeFromSelection, MatDateSelectionModel} from './date-selection-model';
5857

5958
/** Used to generate a unique ID for each datepicker instance. */
6059
let datepickerUid = 0;

src/material/datepicker/datepicker-input-base.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,10 @@ import {
3030
DateAdapter,
3131
MAT_DATE_FORMATS,
3232
MatDateFormats,
33-
MatDateSelectionModel,
34-
ExtractDateTypeFromSelection,
3533
} from '@angular/material/core';
3634
import {Subscription} from 'rxjs';
3735
import {createMissingDateImplError} from './datepicker-errors';
36+
import {ExtractDateTypeFromSelection, MatDateSelectionModel} from './date-selection-model';
3837

3938
/**
4039
* An event used for datepicker input and change events. We don't always have access to a native

src/material/datepicker/datepicker.spec.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import {
1616
MAT_DATE_LOCALE,
1717
MatNativeDateModule,
1818
NativeDateModule,
19-
MatDateSelectionModel,
2019
} from '@angular/material/core';
2120
import {MatFormField, MatFormFieldModule} from '@angular/material/form-field';
2221
import {DEC, JAN, JUL, JUN, SEP} from '@angular/material/testing';
@@ -29,7 +28,12 @@ import {MatInputModule} from '../input/index';
2928
import {MatDatepicker} from './datepicker';
3029
import {MatDatepickerInput} from './datepicker-input';
3130
import {MatDatepickerToggle} from './datepicker-toggle';
32-
import {MAT_DATEPICKER_SCROLL_STRATEGY, MatDatepickerIntl, MatDatepickerModule} from './index';
31+
import {
32+
MAT_DATEPICKER_SCROLL_STRATEGY,
33+
MatDatepickerIntl,
34+
MatDatepickerModule,
35+
MatDateSelectionModel,
36+
} from './index';
3337

3438
describe('MatDatepicker', () => {
3539
const SUPPORTS_INTL = typeof Intl != 'undefined';

src/material/datepicker/datepicker.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
*/
88

99
import {ChangeDetectionStrategy, Component, ViewEncapsulation} from '@angular/core';
10-
import {MAT_SINGLE_DATE_SELECTION_MODEL_PROVIDER} from '@angular/material/core';
1110
import {MatDatepickerBase} from './datepicker-base';
1211
import {MatDatepickerInput} from './datepicker-input';
12+
import {MAT_SINGLE_DATE_SELECTION_MODEL_PROVIDER} from './date-selection-model';
1313

1414
// TODO(mmalerba): We use a component instead of a directive here so the user can use implicit
1515
// template reference variables (e.g. #d vs #d="matDatepicker"). We can change this to a directive

src/material/datepicker/month-view.spec.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,12 @@ import {
2121
} from '@angular/cdk/testing/private';
2222
import {Component} from '@angular/core';
2323
import {async, ComponentFixture, TestBed} from '@angular/core/testing';
24-
import {MatNativeDateModule, DateRange} from '@angular/material/core';
24+
import {MatNativeDateModule} from '@angular/material/core';
2525
import {DEC, FEB, JAN, MAR, NOV} from '@angular/material/testing';
2626
import {By} from '@angular/platform-browser';
2727
import {MatCalendarBody} from './calendar-body';
2828
import {MatMonthView} from './month-view';
29+
import {DateRange} from './date-selection-model';
2930

3031
describe('MatMonthView', () => {
3132
let dir: {value: Direction};

src/material/datepicker/month-view.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,13 @@ import {
3333
ViewChild,
3434
OnDestroy,
3535
} from '@angular/core';
36-
import {DateAdapter, MAT_DATE_FORMATS, MatDateFormats, DateRange} from '@angular/material/core';
36+
import {DateAdapter, MAT_DATE_FORMATS, MatDateFormats} from '@angular/material/core';
3737
import {Directionality} from '@angular/cdk/bidi';
3838
import {MatCalendarBody, MatCalendarCell, MatCalendarCellCssClasses} from './calendar-body';
3939
import {createMissingDateImplError} from './datepicker-errors';
4040
import {Subscription} from 'rxjs';
4141
import {startWith} from 'rxjs/operators';
42+
import {DateRange} from './date-selection-model';
4243

4344

4445
const DAYS_PER_WEEK = 7;

src/material/datepicker/multi-year-view.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,13 @@ import {
3131
ViewEncapsulation,
3232
OnDestroy,
3333
} from '@angular/core';
34-
import {DateAdapter, DateRange} from '@angular/material/core';
34+
import {DateAdapter} from '@angular/material/core';
3535
import {Directionality} from '@angular/cdk/bidi';
3636
import {MatCalendarBody, MatCalendarCell} from './calendar-body';
3737
import {createMissingDateImplError} from './datepicker-errors';
3838
import {Subscription} from 'rxjs';
3939
import {startWith} from 'rxjs/operators';
40+
import {DateRange} from './date-selection-model';
4041

4142
export const yearsPerPage = 24;
4243

src/material/datepicker/public-api.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,6 @@ export * from './month-view';
2929
export * from './year-view';
3030
export * from './date-range-input';
3131
export * from './date-range-picker';
32+
export * from './date-selection-model';
3233
export {MatStartDate, MatEndDate} from './date-range-input-parts';
3334
export {MatMultiYearView, yearsPerPage, yearsPerRow} from './multi-year-view';

src/material/datepicker/year-view.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,13 @@ import {
3232
ViewEncapsulation,
3333
OnDestroy,
3434
} from '@angular/core';
35-
import {DateAdapter, MAT_DATE_FORMATS, MatDateFormats, DateRange} from '@angular/material/core';
35+
import {DateAdapter, MAT_DATE_FORMATS, MatDateFormats} from '@angular/material/core';
3636
import {Directionality} from '@angular/cdk/bidi';
3737
import {MatCalendarBody, MatCalendarCell} from './calendar-body';
3838
import {createMissingDateImplError} from './datepicker-errors';
3939
import {Subscription} from 'rxjs';
4040
import {startWith} from 'rxjs/operators';
41+
import {DateRange} from './date-selection-model';
4142

4243
/**
4344
* An internal component used to display a single year in the datepicker.

0 commit comments

Comments
 (0)