diff --git a/README.md b/README.md index 58aebf8a..0295e65a 100644 --- a/README.md +++ b/README.md @@ -151,13 +151,14 @@ class MyContainer { } } ``` -Here is the list of APIs: -| Name | Signature | Description | -|----------------------|:---------------:|------------------------| -| open | `() => void` | Opens the date picker | -| close | `() => void` | Closes the date picker | +Here is the list of APIs: +| Name | Signature | Description | +|----------------------|:----------------------------------:|----------------------------------| +| open | `() => void` | Opens the date picker | +| close | `() => void` | Closes the date picker | +| moveCalendarTo | `(to: Moment \| String) => void` | Moves calendar to specific date | ## Inline - Day Calendar @@ -220,15 +221,6 @@ Here are the available configurations: ## Inline - Month Calendar You can use the `` component to display the calendar widget without an associated input box. - -Here is the list of APIs: - -| Name | Signature | Description | -|----------------------|:-----------------------------------------------------------------------------------:|----------------------------------| -| moveCalendarsBy | `(current: Moment, amount: number, granularity: moment.unitOfTime.Base) => void` | Moves calendar by given amount | -| moveCalendarTo | `(to: Moment \| String) => void` | Moves calendar to specific date | -| toggleCalendarMode | `(mode: day \| month) => void` | Changes clander mode day/month | - i.e. ```html @@ -273,6 +265,14 @@ Here are the available configurations: | showMultipleYearsNavigation | `boolean` | `false` | If set to `true` will show buttons to navigate by multiple years (10 by default) | | multipleYearsNavigateBy | `number` | `10` | Number of years to navigate when showMultipleYearsNavigation is `true` | +Here is the list of APIs: + +| Name | Signature | Description | +|----------------------|:-----------------------------------------------------------------------------------:|----------------------------------| +| moveCalendarsBy | `(current: Moment, amount: number, granularity: moment.unitOfTime.Base) => void` | Moves calendar by given amount | +| moveCalendarTo | `(to: Moment \| String) => void` | Moves calendar to specific date | +| toggleCalendarMode | `(mode: day \| month) => void` | Changes clander mode day/month | + ## Inline - Time Select @@ -429,6 +429,12 @@ In order to provide configurations to the date-picker you need to pass it to the The `IDatePickerDirectiveConfig` is identical to [`IDatePickerConfig`](#configuration) above except that it lacks the `showGoToCurrent` property. +Here is the list of APIs: + +| Name | Signature | Description | +|----------------------|:----------------------------------:|----------------------------------| +| moveCalendarTo | `(to: Moment \| String) => void` | Moves calendar to specific date | + ## Compatibility ### Internet Explorer 10: diff --git a/e2e/app.po.ts b/e2e/app.po.ts index 4d074e0d..5c3f58ef 100644 --- a/e2e/app.po.ts +++ b/e2e/app.po.ts @@ -43,9 +43,12 @@ export class DemoPage { showWeekNumbersRadio = $('#showWeekNumbersRadio'); hideWeekNumbersRadio = $('#hideWeekNumbersRadio'); weekNumbers = $$(`${this.popupSelector} .dp-week-number`); - deyCalendarNavHeader = $(`${this.popupSelector} .dp-nav-header`); dayCalendarNavHeaderBtn = $(`${this.popupSelector} .dp-nav-header-btn`); deyCalendarMonthNavHeader = $(`${this.popupSelector} dp-month-calendar .dp-nav-header`); + dayTimeCalendarNavHeaderBtnInline = $(`dp-day-time-calendar .dp-nav-header-btn`); + dayCalendarNavHeaderBtnInline = $(`dp-day-calendar .dp-nav-header-btn`); + monthCalendarNavHeaderInline = $(`dp-month-calendar .dp-nav-header`); + navHeader = $(`${this.popupSelector} .dp-nav-header`); dayCalendarNavMonthHeaderBtn = $(`${this.popupSelector} dp-month-calendar .dp-nav-header-btn`); calendarDisabledDays = $$(`${this.popupSelector} .dp-calendar-day[disabled]`); calendarFirstDayOfMonth = $$(`${this.popupSelector} .dp-current-month`).get(0); @@ -145,6 +148,7 @@ export class DemoPage { timeDirectiveMenu = $('#timeDirectiveMenu'); openBtn = $('#openBtn'); + moveCalendarTo = $('#moveCalendarTo'); navigateTo() { return browser.get('/'); diff --git a/e2e/move-to-date-api-e2e.spec.ts b/e2e/move-to-date-api-e2e.spec.ts new file mode 100644 index 00000000..b8e133bd --- /dev/null +++ b/e2e/move-to-date-api-e2e.spec.ts @@ -0,0 +1,36 @@ +import {DemoPage} from './app.po'; + +describe('Move to date api', () => { + + let page: DemoPage; + + beforeEach(() => { + page = new DemoPage(); + page.navigateTo(); + }); + + it('should move to date API on day', () => { + const runner = (menuItem, input, isPicker, cont) => { + menuItem.click(); + page.moveCalendarTo.click(); + + if (isPicker) { + input.click(); + } + + expect(cont.getText()).toContain('1987'); + }; + + runner(page.dayPickerMenu, page.dayPickerInput, true, page.dayCalendarNavHeaderBtn); + runner(page.dayDirectiveMenu, page.dayDirectiveInput, true, page.dayCalendarNavHeaderBtn); + runner(page.dayInlineMenu, null, false, page.dayCalendarNavHeaderBtnInline); + + runner(page.daytimePickerMenu, page.daytimePickerInput, true, page.dayCalendarNavHeaderBtn); + runner(page.daytimeDirectiveMenu, page.daytimeDirectiveInput, true, page.dayCalendarNavHeaderBtn); + runner(page.daytimeInlineMenu, null, false, page.dayTimeCalendarNavHeaderBtnInline); + + runner(page.monthPickerMenu, page.monthPickerInput, true, page.navHeader); + runner(page.monthDirectiveMenu, page.monthDirectiveInput, true, page.navHeader); + runner(page.monthInlineMenu, null, false, page.monthCalendarNavHeaderInline); + }); +}); diff --git a/src/app/date-picker/date-picker.api.ts b/src/app/date-picker/date-picker.api.ts index 9f5cecd2..654b9914 100644 --- a/src/app/date-picker/date-picker.api.ts +++ b/src/app/date-picker/date-picker.api.ts @@ -1,4 +1,7 @@ +import {SingleCalendarValue} from '../common/types/single-calendar-value'; + export interface IDpDayPickerApi { open: () => void; close: () => void; -} \ No newline at end of file + moveCalendarTo: (date: SingleCalendarValue) => void; +} diff --git a/src/app/date-picker/date-picker.component.ts b/src/app/date-picker/date-picker.component.ts index c900ebe5..e5c4a119 100644 --- a/src/app/date-picker/date-picker.component.ts +++ b/src/app/date-picker/date-picker.component.ts @@ -121,7 +121,8 @@ export class DatePickerComponent implements OnChanges, validateFn: DateValidator; api: IDpDayPickerApi = { open: this.showCalendars.bind(this), - close: this.hideCalendar.bind(this) + close: this.hideCalendar.bind(this), + moveCalendarTo: this.moveCalendarTo.bind(this) }; set selected(selected: Moment[]) { @@ -189,12 +190,12 @@ export class DatePickerComponent implements OnChanges, } } - constructor(private dayPickerService: DatePickerService, - private domHelper: DomHelper, - private elemRef: ElementRef, - private renderer: Renderer, - private utilsService: UtilsService, - public cd: ChangeDetectorRef) { + constructor(private readonly dayPickerService: DatePickerService, + private readonly domHelper: DomHelper, + private readonly elemRef: ElementRef, + private readonly renderer: Renderer, + private readonly utilsService: UtilsService, + public readonly cd: ChangeDetectorRef) { } @HostListener('click') @@ -442,6 +443,11 @@ export class DatePickerComponent implements OnChanges, } } + moveCalendarTo(date: SingleCalendarValue) { + const momentDate = this.utilsService.convertToMoment(date, this.componentConfig.format); + this.currentDateView = momentDate; + } + startGlobalListeners() { this.globalListnersUnlisteners.push( this.renderer.listen(document, 'keydown', (e: KeyboardEvent) => { diff --git a/src/app/day-time-calendar/day-time-calendar.component.ts b/src/app/day-time-calendar/day-time-calendar.component.ts index 1ff5388a..8c602453 100644 --- a/src/app/day-time-calendar/day-time-calendar.component.ts +++ b/src/app/day-time-calendar/day-time-calendar.component.ts @@ -85,6 +85,10 @@ export class DayTimeCalendarComponent implements OnInit, OnChanges, ControlValue return this._selected; } + api = { + moveCalendarTo: this.moveCalendarTo.bind(this) + }; + constructor(public dayTimeCalendarService: DayTimeCalendarService, public utilsService: UtilsService, public cd: ChangeDetectorRef) { diff --git a/src/app/demo/demo/demo.component.html b/src/app/demo/demo/demo.component.html index a6741535..5024449d 100644 --- a/src/app/demo/demo/demo.component.html +++ b/src/app/demo/demo/demo.component.html @@ -82,7 +82,7 @@

Angular Date/Time/Month Picker

Angular Date/Time/Month Picker
Angular Date/Time/Month Picker
Angular Date/Time/Month Picker
Angular Date/Time/Month Picker [config]="config" [theme]="material ? 'dp-material dp-main' : 'dp-main'"> -
-
required
-
invalid format +
+
required
+
invalid format
-
minDate invalid +
minDate invalid
-
maxDate invalid +
maxDate invalid
@@ -189,6 +191,7 @@

Angular Date/Time/Month Picker

Angular Date/Time/Month Picker
Angular Date/Time/Month Picker
Angular Date/Time/Month Picker
Angular Date/Time/Month Picker
Angular Date/Time/Month Picker
Angular Date/Time/Month Picker
Angular Date/Time/Month Picker
Angular Date/Time/Month Picker
API
+ +
+ + Move (api.moveCalendarTo('14-01-1987')): + +
+ +
+
diff --git a/src/app/demo/demo/demo.component.ts b/src/app/demo/demo/demo.component.ts index c22230fd..fe855b02 100644 --- a/src/app/demo/demo/demo.component.ts +++ b/src/app/demo/demo/demo.component.ts @@ -2,13 +2,12 @@ import debounce from '../../common/decorators/decorators'; import {IDatePickerConfig} from '../../date-picker/date-picker-config.model'; import {DatePickerComponent} from '../../date-picker/date-picker.component'; import {DatePickerDirective} from '../../date-picker/date-picker.directive'; -import {Component, HostListener, ViewChild} from '@angular/core'; +import {Component, ContentChild, HostListener, ViewChild} from '@angular/core'; import {FormControl, FormGroup, Validators} from '@angular/forms'; import * as moment from 'moment'; import {Moment} from 'moment'; import {GaService} from '../services/ga/ga.service'; import {ECalendarValue} from '../../common/types/calendar-value-enum'; -import {SingleCalendarValue} from '../../common/types/single-calendar-value'; const GLOBAL_OPTION_KEYS = [ 'theme', @@ -37,12 +36,14 @@ const DAY_PICKER_DIRECTIVE_OPTION_KEYS = [ 'closeOnSelect', 'closeOnSelectDelay', 'showGoToCurrent', + 'moveCalendarTo', ...PICKER_OPTION_KEYS ]; const DAY_PICKER_OPTION_KEYS = [ ...DAY_PICKER_DIRECTIVE_OPTION_KEYS ]; const DAY_TIME_PICKER_OPTION_KEYS = [ + 'moveCalendarTo', ...PICKER_OPTION_KEYS ]; const TIME_PICKER_OPTION_KEYS = [ @@ -60,6 +61,7 @@ const MONTH_CALENDAR_OPTION_KEYS = [ 'yearFormat', 'showGoToCurrent', 'unSelectOnClick', + 'moveCalendarTo', ...GLOBAL_OPTION_KEYS ]; const DAY_CALENDAR_OPTION_KEYS = [ @@ -77,6 +79,7 @@ const DAY_CALENDAR_OPTION_KEYS = [ 'weekdayFormat', 'showGoToCurrent', 'unSelectOnClick', + 'moveCalendarTo', ...MONTH_CALENDAR_OPTION_KEYS ]; const TIME_SELECT_SHARED_OPTION_KEYS = [ @@ -112,7 +115,7 @@ const DAY_TIME_CALENDAR_OPTION_KEYS = [ }) export class DemoComponent { showDemo: boolean = true; - @ViewChild('datePicker') datePicker: DatePickerComponent; + @ViewChild('dateComponent') dateComponent: DatePickerComponent; @ViewChild('donateForm') donateForm: any; @ViewChild('dateDirectivePicker') datePickerDirective: DatePickerDirective; demoFormat = 'DD-MM-YYYY'; @@ -259,19 +262,17 @@ export class DemoComponent { }; openCalendar() { - if (this.datePicker) { - this.datePicker.api.open(); - } - if (this.datePickerDirective) { + if (this.dateComponent) { + this.dateComponent.api.open(); + } else if (this.datePickerDirective) { this.datePickerDirective.api.open(); } } closeCalendar() { - if (this.datePicker) { - this.datePicker.api.close(); - } - if (this.datePickerDirective) { + if (this.dateComponent) { + this.dateComponent.api.close(); + } else if (this.datePickerDirective) { this.datePickerDirective.api.close(); } } @@ -366,6 +367,10 @@ export class DemoComponent { console.log(item); } + moveCalendarTo() { + this.dateComponent.api.moveCalendarTo(moment('14-01-1987', this.demoFormat)); + } + donateClicked() { this.gaService.emitEvent('donate', 'clicked'); this.donateForm.nativeElement.submit(); diff --git a/src/app/month-calendar/month-calendar.component.ts b/src/app/month-calendar/month-calendar.component.ts index 33aa5448..c3b60341 100644 --- a/src/app/month-calendar/month-calendar.component.ts +++ b/src/app/month-calendar/month-calendar.component.ts @@ -277,6 +277,7 @@ export class MonthCalendarComponent implements OnInit, OnChanges, ControlValueAc moveCalendarTo(to: SingleCalendarValue) { if (to) { this.currentDateView = this.utilsService.convertToMoment(to, this.componentConfig.format); + this.cd.markForCheck(); } }