Skip to content

Commit

Permalink
feat: add helper directives for changing the current view
Browse files Browse the repository at this point in the history
See the documentation or the kitchen sink demo to see usage

Closes #124
  • Loading branch information
Matt Lewis committed Jan 28, 2017
1 parent e469810 commit df398cb
Show file tree
Hide file tree
Showing 11 changed files with 372 additions and 83 deletions.
74 changes: 32 additions & 42 deletions demos/demo-modules/demo-utils/calendar-header.component.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,29 @@
import { Component, Input, Output, EventEmitter } from '@angular/core';
import {
subDays,
addDays,
addWeeks,
subWeeks,
addMonths,
subMonths
} from 'date-fns';

@Component({
selector: 'mwl-calendar-header',
template: `
<div class="row text-center">
<div class="col-md-4">
<div class="btn-group">
<div class="btn btn-primary" (click)="decrement()">
<div
class="btn btn-primary"
mwlCalendarPreviousView
[view]="view"
[(viewDate)]="viewDate">
Previous
</div>
<div class="btn btn-secondary" (click)="today()">
<div
class="btn btn-secondary"
mwlCalendarToday
[(viewDate)]="viewDate">
Today
</div>
<div class="btn btn-primary" (click)="increment()">
<div
class="btn btn-primary"
mwlCalendarNextView
[view]="view"
[(viewDate)]="viewDate">
Next
</div>
</div>
Expand All @@ -30,9 +33,24 @@ import {
</div>
<div class="col-md-4">
<div class="btn-group">
<div class="btn btn-primary" (click)="viewChange.emit('month')" [class.active]="view === 'month'">Month</div>
<div class="btn btn-primary" (click)="viewChange.emit('week')" [class.active]="view === 'week'">Week</div>
<div class="btn btn-primary" (click)="viewChange.emit('day')" [class.active]="view === 'day'">Day</div>
<div
class="btn btn-primary"
(click)="viewChange.emit('month')"
[class.active]="view === 'month'">
Month
</div>
<div
class="btn btn-primary"
(click)="viewChange.emit('week')"
[class.active]="view === 'week'">
Week
</div>
<div
class="btn btn-primary"
(click)="viewChange.emit('day')"
[class.active]="view === 'day'">
Day
</div>
</div>
</div>
</div>
Expand All @@ -51,32 +69,4 @@ export class CalendarHeaderComponent {

@Output() viewDateChange: EventEmitter<Date> = new EventEmitter();

increment(): void {

const addFn: any = {
day: addDays,
week: addWeeks,
month: addMonths
}[this.view];

this.viewDateChange.emit(addFn(this.viewDate, 1));

}

decrement(): void {

const subFn: any = {
day: subDays,
week: subWeeks,
month: subMonths
}[this.view];

this.viewDateChange.emit(subFn(this.viewDate, 1));

}

today(): void {
this.viewDateChange.emit(new Date());
}

}
32 changes: 0 additions & 32 deletions demos/demo-modules/kitchen-sink/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@ import {
endOfMonth,
isSameDay,
isSameMonth,
addWeeks,
subWeeks,
addMonths,
subMonths,
addHours
} from 'date-fns';
import { Subject } from 'rxjs/Subject';
Expand Down Expand Up @@ -103,34 +99,6 @@ export class DemoComponent {

constructor(private modal: NgbModal) {}

increment(): void {

const addFn: any = {
day: addDays,
week: addWeeks,
month: addMonths
}[this.view];

this.viewDate = addFn(this.viewDate, 1);

}

decrement(): void {

const subFn: any = {
day: subDays,
week: subWeeks,
month: subMonths
}[this.view];

this.viewDate = subFn(this.viewDate, 1);

}

today(): void {
this.viewDate = new Date();
}

dayClicked({date, events}: {date: Date, events: CalendarEvent[]}): void {

if (isSameMonth(date, this.viewDate)) {
Expand Down
38 changes: 32 additions & 6 deletions demos/demo-modules/kitchen-sink/template.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,24 @@ <h5 class="modal-title">Event action occurred</h5>
<div class="row text-center">
<div class="col-md-4">
<div class="btn-group">
<div class="btn btn-primary" (click)="decrement()">
<div
class="btn btn-primary"
mwlCalendarPreviousView
[view]="view"
[(viewDate)]="viewDate">
Previous
</div>
<div class="btn btn-secondary" (click)="today()">
<div
class="btn btn-secondary"
mwlCalendarToday
[(viewDate)]="viewDate">
Today
</div>
<div class="btn btn-primary" (click)="increment()">
<div
class="btn btn-primary"
mwlCalendarNextView
[view]="view"
[(viewDate)]="viewDate">
Next
</div>
</div>
Expand All @@ -41,9 +52,24 @@ <h3>{{ viewDate | calendarDate:(view + 'ViewTitle'):'en' }}</h3>
</div>
<div class="col-md-4">
<div class="btn-group">
<div class="btn btn-primary" (click)="view = 'month'" [class.active]="view === 'month'">Month</div>
<div class="btn btn-primary" (click)="view = 'week'" [class.active]="view === 'week'">Week</div>
<div class="btn btn-primary" (click)="view = 'day'" [class.active]="view === 'day'">Day</div>
<div
class="btn btn-primary"
(click)="view = 'month'"
[class.active]="view === 'month'">
Month
</div>
<div
class="btn btn-primary"
(click)="view = 'week'"
[class.active]="view === 'week'">
Week
</div>
<div
class="btn btn-primary"
(click)="view = 'day'"
[class.active]="view === 'day'">
Day
</div>
</div>
</div>
</div>
Expand Down
15 changes: 13 additions & 2 deletions src/calendar.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ import { CalendarAllDayEventComponent } from './components/day/calendarAllDayEve
import { CalendarDayViewHourSegmentComponent } from './components/day/calendarDayViewHourSegment.component';
import { CalendarDayViewEventComponent } from './components/day/calendarDayViewEvent.component';
import { CalendarTooltipWindowComponent, CalendarTooltipDirective } from './directives/calendarTooltip.directive';
import { CalendarPreviousViewDirective } from './directives/calendarPreviousView.directive';
import { CalendarNextViewDirective } from './directives/calendarNextView.directive';
import { CalendarTodayDirective } from './directives/calendarToday.directive';
import { CalendarDatePipe } from './pipes/calendarDate.pipe';
import { CalendarEventTitlePipe } from './pipes/calendarEventTitle.pipe';
import { CalendarEventTitleFormatter } from './providers/calendarEventTitle.provider';
Expand Down Expand Up @@ -51,6 +54,9 @@ import { CalendarDateFormatter } from './providers/calendarDateFormatter.provide
CalendarDayViewEventComponent,
CalendarTooltipWindowComponent,
CalendarTooltipDirective,
CalendarPreviousViewDirective,
CalendarNextViewDirective,
CalendarTodayDirective,
CalendarDatePipe,
CalendarEventTitlePipe
],
Expand All @@ -63,9 +69,14 @@ import { CalendarDateFormatter } from './providers/calendarDateFormatter.provide
CalendarDayViewComponent,
CalendarWeekViewComponent,
CalendarMonthViewComponent,
CalendarDatePipe
CalendarDatePipe,
CalendarPreviousViewDirective,
CalendarNextViewDirective,
CalendarTodayDirective
],
entryComponents: [CalendarTooltipWindowComponent]
entryComponents: [
CalendarTooltipWindowComponent
]
})
export class CalendarModule {

Expand Down
54 changes: 54 additions & 0 deletions src/directives/calendarNextView.directive.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { Directive, HostListener, Input, Output, EventEmitter } from '@angular/core';
import addDays from 'date-fns/add_days';
import addWeeks from 'date-fns/add_weeks';
import addMonths from 'date-fns/add_months';

/**
* Change the view date to the next view. For example:
*
* ```
* &lt;button
* mwlCalendarNextView
* [(viewDate)]="viewDate"
* [view]="view"&gt;
* Next
* &lt;/button&gt;
* ```
*/
@Directive({
selector: '[mwlCalendarNextView]'
})
export class CalendarNextViewDirective {

/**
* The current view
*/
@Input() view: string;

/**
* The current view date
*/
@Input() viewDate: Date;

/**
* Called when the view date is changed
*/
@Output() viewDateChange: EventEmitter<Date> = new EventEmitter();

/**
* @hidden
*/
@HostListener('click')
onClick(): void {

const addFn: any = {
day: addDays,
week: addWeeks,
month: addMonths
}[this.view];

this.viewDateChange.emit(addFn(this.viewDate, 1));

}

}
54 changes: 54 additions & 0 deletions src/directives/calendarPreviousView.directive.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { Directive, HostListener, Input, Output, EventEmitter } from '@angular/core';
import subDays from 'date-fns/sub_days';
import subWeeks from 'date-fns/sub_weeks';
import subMonths from 'date-fns/sub_months';

/**
* Change the view date to the previous view. For example:
*
* ```
* &lt;button
* mwlCalendarPreviousView
* [(viewDate)]="viewDate"
* [view]="view"&gt;
* Previous
* &lt;/button&gt;
* ```
*/
@Directive({
selector: '[mwlCalendarPreviousView]'
})
export class CalendarPreviousViewDirective {

/**
* The current view
*/
@Input() view: string;

/**
* The current view date
*/
@Input() viewDate: Date;

/**
* Called when the view date is changed
*/
@Output() viewDateChange: EventEmitter<Date> = new EventEmitter();

/**
* @hidden
*/
@HostListener('click')
onClick(): void {

const subFn: any = {
day: subDays,
week: subWeeks,
month: subMonths
}[this.view];

this.viewDateChange.emit(subFn(this.viewDate, 1));

}

}
38 changes: 38 additions & 0 deletions src/directives/calendarToday.directive.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { Directive, HostListener, Input, Output, EventEmitter } from '@angular/core';
import startOfToday from 'date-fns/start_of_today';

/**
* Change the view date to the current day. For example:
*
* ```
* &lt;button
* mwlCalendarToday
* [(viewDate)]="viewDate"&gt;
* Today
* &lt;/button&gt;
* ```
*/
@Directive({
selector: '[mwlCalendarToday]'
})
export class CalendarTodayDirective {

/**
* The current view date
*/
@Input() viewDate: Date;

/**
* Called when the view date is changed
*/
@Output() viewDateChange: EventEmitter<Date> = new EventEmitter();

/**
* @hidden
*/
@HostListener('click')
onClick(): void {
this.viewDateChange.emit(startOfToday());
}

}
Loading

0 comments on commit df398cb

Please sign in to comment.