Skip to content

Commit 969eb01

Browse files
authored
B 269 (#280)
* fixes #269 * reverting demo page * fixing lint * fixing e2e * fixing bugs
1 parent 4a376e2 commit 969eb01

File tree

8 files changed

+125
-31
lines changed

8 files changed

+125
-31
lines changed

CHANGELOG.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ All notable changes to this project will be documented in this file.
55
# [2.6.1] (???)
66

77
### Bug Fixed
8-
- `disableKeypress` doesn't work on the directive ([1c00e48](https://github.com/vlio20/angular-datepicker/commit/1c00e48)) closes [#267](https://github.com/vlio20/angular-datepicker/issues/267)
9-
8+
- Set `min` property dynamically ([???](https://github.com/vlio20/angular-datepicker/commit/???)) closes [#269](https://github.com/vlio20/angular-datepicker/issues/269)
9+
- `disableKeypress` does not work on the directive ([1c00e48](https://github.com/vlio20/angular-datepicker/commit/1c00e48)) closes [#267](https://github.com/vlio20/angular-datepicker/issues/267)
1010
- Go to current button not centered ([9b6dc99](https://github.com/vlio20/angular-datepicker/commit/9b6dc99)) closes [#275](https://github.com/vlio20/angular-datepicker/issues/275) - PR by [@KevinJannis](https://github.com/KevinJannis)
1111

1212
<a name="2.6.0"></a>

protractor.conf.js

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ exports.config = {
1212
],
1313
capabilities: {
1414
browserName: 'chrome',
15-
shardTestFiles: headless,
16-
maxInstances: headless ? 3 : 1,
1715
chromeOptions: {
1816
args: headless ? [
1917
'--headless',
@@ -30,11 +28,6 @@ exports.config = {
3028
print: function () {
3129
}
3230
},
33-
// beforeLaunch: function () {
34-
// require('ts-node').register({
35-
// project: 'e2e/tsconfig.e2e.json'
36-
// });
37-
// },
3831
onPrepare() {
3932
jasmine.getEnv().addReporter(
4033
new SpecReporter({spec: {displayStacktrace: true}}));

src/app/common/services/utils/utils.service.ts

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import {ECalendarValue} from '../../types/calendar-value-enum';
22
import {SingleCalendarValue} from '../../types/single-calendar-value';
3-
import {Injectable} from '@angular/core';
3+
import {Injectable, SimpleChange} from '@angular/core';
44
import * as moment from 'moment';
55
import {Moment, unitOfTime} from 'moment';
66
import {CalendarValue} from '../../types/calendar-value';
77
import {IDate} from '../../models/date.model';
88
import {CalendarMode} from '../../types/calendar-mode';
99
import {DateValidator} from '../../types/validator.type';
10+
import {ICalendarInternal} from '../../models/calendar.model';
1011

1112
export interface DateLimits {
1213
minDate?: SingleCalendarValue;
@@ -38,7 +39,7 @@ export class UtilsService {
3839
} else if (typeof date === 'string') {
3940
return moment(date, format);
4041
} else {
41-
return date;
42+
return date.clone();
4243
}
4344
}
4445

@@ -99,9 +100,9 @@ export class UtilsService {
99100
case (ECalendarValue.StringArr):
100101
return (<string[]>value).map(v => v ? moment(v, format, true) : null).filter(Boolean);
101102
case (ECalendarValue.Moment):
102-
return [<Moment>value];
103+
return value ? [(<Moment>value).clone()] : [];
103104
case (ECalendarValue.MomentArr):
104-
return <Moment[]>[].concat(value);
105+
return (<Moment[]>value || []).map(v => v.clone());
105106
default:
106107
return [];
107108
}
@@ -117,9 +118,9 @@ export class UtilsService {
117118
case (ECalendarValue.StringArr):
118119
return value.filter(Boolean).map(v => v.format(format));
119120
case (ECalendarValue.Moment):
120-
return value[0];
121+
return value[0] ? value[0].clone() : value[0];
121122
case (ECalendarValue.MomentArr):
122-
return value;
123+
return value ? value.map(v => v.clone()) : value;
123124
default:
124125
return value;
125126
}
@@ -285,4 +286,22 @@ export class UtilsService {
285286
}
286287
});
287288
}
289+
290+
shouldResetCurrentView<T extends ICalendarInternal>(prevConf: T, currentConf: T): boolean {
291+
if (prevConf && currentConf) {
292+
if (!prevConf.min && currentConf.min) {
293+
return true;
294+
} else if (prevConf.min && currentConf.min && !prevConf.min.isSame(currentConf.min, 'd')) {
295+
return true;
296+
} else if (!prevConf.max && currentConf.max) {
297+
return true;
298+
} else if (prevConf.max && currentConf.max && !prevConf.max.isSame(currentConf.max, 'd')) {
299+
return true;
300+
}
301+
302+
return false;
303+
}
304+
305+
return false;
306+
}
288307
}

src/app/date-picker/date-picker.component.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
*ngSwitchCase="'day'"
1818
[config]="dayCalendarConfig"
1919
[ngModel]="_selected"
20-
[displayDate]="currentDateView"
20+
[displayDate]="displayDate"
2121
(onSelect)="dateSelected($event, 'day')"
2222
[theme]="theme">
2323
</dp-day-calendar>
@@ -26,7 +26,7 @@
2626
*ngSwitchCase="'month'"
2727
[config]="dayCalendarConfig"
2828
[ngModel]="_selected"
29-
[displayDate]="currentDateView"
29+
[displayDate]="displayDate"
3030
(onSelect)="dateSelected($event, 'month')"
3131
[theme]="theme">
3232
</dp-month-calendar>
@@ -42,7 +42,7 @@
4242
<dp-day-time-calendar #daytimeCalendar
4343
*ngSwitchCase="'daytime'"
4444
[config]="dayTimeCalendarConfig"
45-
[displayDate]="currentDateView"
45+
[displayDate]="displayDate"
4646
[ngModel]="_selected && _selected[0]"
4747
(onChange)="dateSelected($event, 'second', true)"
4848
[theme]="theme">

src/app/date-picker/date-picker.component.ts

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@ import {
2929
OnChanges,
3030
OnDestroy,
3131
OnInit,
32-
Output,
32+
Output, QueryList,
3333
Renderer,
3434
SimpleChanges,
35-
ViewChild,
35+
ViewChild, ViewChildren,
3636
ViewEncapsulation
3737
} from '@angular/core';
3838
import {
@@ -46,6 +46,8 @@ import {
4646
import * as moment from 'moment';
4747
import {Moment, unitOfTime} from 'moment';
4848
import {DateValidator} from '../common/types/validator.type';
49+
import {MonthCalendarComponent} from '../month-calendar/month-calendar.component';
50+
import {DayTimeCalendarComponent} from '../day-time-calendar/day-time-calendar.component';
4951

5052
@Component({
5153
selector: 'dp-date-picker',
@@ -93,6 +95,8 @@ export class DatePickerComponent implements OnChanges,
9395

9496
@ViewChild('container') calendarContainer: ElementRef;
9597
@ViewChild('dayCalendar') dayCalendarRef: DayCalendarComponent;
98+
@ViewChild('monthCalendar') monthCalendarRef: MonthCalendarComponent;
99+
@ViewChild('daytimeCalendar') dayTimeCalendarRef: DayTimeCalendarComponent;
96100
@ViewChild('timeSelect') timeSelectRef: TimeSelectComponent;
97101

98102
componentConfig: IDatePickerConfigInternal;
@@ -105,7 +109,7 @@ export class DatePickerComponent implements OnChanges,
105109
inputValue: CalendarValue;
106110
inputValueType: ECalendarValue;
107111
isFocusedTrigger: boolean = false;
108-
currentDateView: Moment;
112+
_currentDateView: Moment;
109113
inputElementValue: string;
110114
calendarWrapper: HTMLElement;
111115
appendToElement: HTMLElement;
@@ -164,6 +168,26 @@ export class DatePickerComponent implements OnChanges,
164168
this._areCalendarsShown = value;
165169
}
166170

171+
get currentDateView(): Moment {
172+
return this._currentDateView;
173+
}
174+
175+
set currentDateView(date: Moment) {
176+
this._currentDateView = date;
177+
178+
if (this.dayCalendarRef) {
179+
this.dayCalendarRef.moveCalendarTo(date);
180+
}
181+
182+
if (this.monthCalendarRef) {
183+
this.monthCalendarRef.moveCalendarTo(date);
184+
}
185+
186+
if (this.dayTimeCalendarRef) {
187+
this.dayTimeCalendarRef.moveCalendarTo(date);
188+
}
189+
}
190+
167191
constructor(private dayPickerService: DatePickerService,
168192
private domHelper: DomHelper,
169193
private elemRef: ElementRef,
@@ -267,6 +291,7 @@ export class DatePickerComponent implements OnChanges,
267291
ngOnChanges(changes: SimpleChanges) {
268292
if (this.isInitialized) {
269293
const {minDate, maxDate, minTime, maxTime} = changes;
294+
270295
this.init();
271296

272297
if (minDate || maxDate || minTime || maxTime) {
@@ -396,10 +421,6 @@ export class DatePickerComponent implements OnChanges,
396421
this.mode !== 'time';
397422
}
398423

399-
moveToCurrent() {
400-
this.currentDateView = moment();
401-
}
402-
403424
dateSelected(date: IDate, granularity: unitOfTime.Base, ignoreClose?: boolean) {
404425
this.selected = this.utilsService
405426
.updateSelected(this.componentConfig.allowMultiSelect, this.selected, date, granularity);

src/app/day-calendar/day-calendar.component.ts

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
OnChanges,
1111
OnInit,
1212
Output,
13+
SimpleChange,
1314
SimpleChanges,
1415
ViewEncapsulation
1516
} from '@angular/core';
@@ -82,7 +83,8 @@ export class DayCalendarComponent implements OnInit, OnChanges, ControlValueAcce
8283

8384
api = {
8485
moveCalendarsBy: this.moveCalendarsBy.bind(this),
85-
toggleCalendar: this.toggleCalendar.bind(this)
86+
toggleCalendar: this.toggleCalendar.bind(this),
87+
moveCalendarTo: this.moveCalendarTo.bind(this)
8688
};
8789

8890
set selected(selected: Moment[]) {
@@ -123,7 +125,12 @@ export class DayCalendarComponent implements OnInit, OnChanges, ControlValueAcce
123125
this.currentDateView = this.displayDate
124126
? this.utilsService.convertToMoment(this.displayDate, this.componentConfig.format).clone()
125127
: this.utilsService
126-
.getDefaultDisplayDate(this.currentDateView, this.selected, this.componentConfig.allowMultiSelect, this.componentConfig.min);
128+
.getDefaultDisplayDate(
129+
this.currentDateView,
130+
this.selected,
131+
this.componentConfig.allowMultiSelect,
132+
this.componentConfig.min
133+
);
127134
this.weekdays = this.dayCalendarService
128135
.generateWeekdays(this.componentConfig.firstDayOfWeek);
129136
this.inputValueType = this.utilsService.getInputType(this.inputValue, this.componentConfig.allowMultiSelect);
@@ -133,7 +140,9 @@ export class DayCalendarComponent implements OnInit, OnChanges, ControlValueAcce
133140

134141
ngOnChanges(changes: SimpleChanges) {
135142
if (this.isInited) {
136-
const {minDate, maxDate} = changes;
143+
const {minDate, maxDate, config} = changes;
144+
145+
this.handleConfigChange(config);
137146
this.init();
138147

139148
if (minDate || maxDate) {
@@ -254,6 +263,12 @@ export class DayCalendarComponent implements OnInit, OnChanges, ControlValueAcce
254263
this.currentDateView = current.clone().add(amount, granularity);
255264
}
256265

266+
moveCalendarTo(to: SingleCalendarValue) {
267+
if (to) {
268+
this.currentDateView = this.utilsService.convertToMoment(to, this.componentConfig.format);
269+
}
270+
}
271+
257272
shouldShowCurrent(): boolean {
258273
return this.utilsService.shouldShowCurrent(
259274
this.componentConfig.showGoToCurrent,
@@ -266,4 +281,15 @@ export class DayCalendarComponent implements OnInit, OnChanges, ControlValueAcce
266281
goToCurrent() {
267282
this.currentDateView = moment();
268283
}
284+
285+
handleConfigChange(config: SimpleChange) {
286+
if (config) {
287+
const prevConf: IDayCalendarConfigInternal = this.dayCalendarService.getConfig(config.previousValue);
288+
const currentConf: IDayCalendarConfigInternal = this.dayCalendarService.getConfig(config.currentValue);
289+
290+
if (this.utilsService.shouldResetCurrentView(prevConf, currentConf)) {
291+
this._currentDateView = null;
292+
}
293+
}
294+
}
269295
}

src/app/day-time-calendar/day-time-calendar.component.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import {
88
Input,
99
OnChanges,
1010
OnInit,
11-
Output,
12-
SimpleChanges,
11+
Output, SimpleChange,
12+
SimpleChanges, ViewChild,
1313
ViewEncapsulation
1414
} from '@angular/core';
1515
import {
@@ -29,6 +29,7 @@ import {TimeSelectService} from '../time-select/time-select.service';
2929
import {IDayTimeCalendarConfig} from './day-time-calendar-config.model';
3030
import {DayTimeCalendarService} from './day-time-calendar.service';
3131
import {DateValidator} from '../common/types/validator.type';
32+
import {DayCalendarComponent} from '../day-calendar/day-calendar.component';
3233

3334
@Component({
3435
selector: 'dp-day-time-calendar',
@@ -61,6 +62,8 @@ export class DayTimeCalendarComponent implements OnInit, OnChanges, ControlValue
6162

6263
@Output() onChange: EventEmitter<IDate> = new EventEmitter();
6364

65+
@ViewChild('dayCalendar') dayCalendarRef: DayCalendarComponent;
66+
6467
isInited: boolean = false;
6568
componentConfig: IDayTimeCalendarConfig;
6669
_selected: Moment;
@@ -165,4 +168,10 @@ export class DayTimeCalendarComponent implements OnInit, OnChanges, ControlValue
165168
emitChange() {
166169
this.onChange.emit({date: this.selected, selected: false});
167170
}
171+
172+
moveCalendarTo(to: SingleCalendarValue) {
173+
if (to) {
174+
this.dayCalendarRef.moveCalendarTo(to);
175+
}
176+
}
168177
}

src/app/month-calendar/month-calendar.component.ts

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
OnChanges,
99
OnInit,
1010
Output,
11+
SimpleChange,
1112
SimpleChanges,
1213
ViewEncapsulation
1314
} from '@angular/core';
@@ -27,6 +28,7 @@ import {
2728
import {CalendarValue} from '../common/types/calendar-value';
2829
import {UtilsService} from '../common/services/utils/utils.service';
2930
import {DateValidator} from '../common/types/validator.type';
31+
import {SingleCalendarValue} from '../common/types/single-calendar-value';
3032

3133
@Component({
3234
selector: 'dp-month-calendar',
@@ -72,6 +74,11 @@ export class MonthCalendarComponent implements OnInit, OnChanges, ControlValueAc
7274
showSecondaryLeftNav: boolean;
7375
showSecondaryRightNav: boolean;
7476

77+
api = {
78+
toggleCalendar: this.toggleCalendar.bind(this),
79+
moveCalendarTo: this.moveCalendarTo.bind(this)
80+
};
81+
7582
set selected(selected: Moment[]) {
7683
this._selected = selected;
7784
this.onChangeCallback(this.processOnChangeCallback(selected));
@@ -108,7 +115,9 @@ export class MonthCalendarComponent implements OnInit, OnChanges, ControlValueAc
108115

109116
ngOnChanges(changes: SimpleChanges) {
110117
if (this.isInited) {
111-
const {minDate, maxDate} = changes;
118+
const {minDate, maxDate, config} = changes;
119+
120+
this.handleConfigChange(config);
112121
this.init();
113122

114123
if (minDate || maxDate) {
@@ -252,4 +261,21 @@ export class MonthCalendarComponent implements OnInit, OnChanges, ControlValueAc
252261
goToCurrent() {
253262
this.currentDateView = moment();
254263
}
264+
265+
moveCalendarTo(to: SingleCalendarValue) {
266+
if (to) {
267+
this.currentDateView = this.utilsService.convertToMoment(to, this.componentConfig.format);
268+
}
269+
}
270+
271+
handleConfigChange(config: SimpleChange) {
272+
if (config) {
273+
const prevConf: IMonthCalendarConfigInternal = this.monthCalendarService.getConfig(config.previousValue);
274+
const currentConf: IMonthCalendarConfigInternal = this.monthCalendarService.getConfig(config.currentValue);
275+
276+
if (this.utilsService.shouldResetCurrentView(prevConf, currentConf)) {
277+
this._currentDateView = null;
278+
}
279+
}
280+
}
255281
}

0 commit comments

Comments
 (0)