File

src/components/day/calendarDayView.component.ts

Description

Shows all events on a given day. Example usage:

<mwl-calendar-day-view
[viewDate]="viewDate"
[events]="events">
</mwl-calendar-day-view>

Metadata

selector mwl-calendar-day-view
template <div class="cal-day-view" #dayViewContainer>
  <mwl-calendar-all-day-event
    *ngFor="let event of view.allDayEvents"
    [event]="event"
    (eventClicked)="eventClicked.emit({event: event})">
  </mwl-calendar-all-day-event>
  <div class="cal-hour-rows">
    <div class="cal-events">
      <mwl-calendar-day-view-event
        *ngFor="let dayEvent of view?.events"
        [dayEvent]="dayEvent"
        [hourSegments]="hourSegments"
        [tooltipPlacement]="tooltipPlacement"
        [eventSnapSize]="eventSnapSize"
        [dayViewContainer]="dayViewContainer"
        (eventClicked)="eventClicked.emit({event: dayEvent.event})"
        (eventResized)="eventTimesChanged.emit($event)">
      </mwl-calendar-day-view-event>
    </div>
    <div class="cal-hour" *ngFor="let hour of hours" [style.minWidth.px]="view?.width + 70">
      <mwl-calendar-day-view-hour-segment
        *ngFor="let segment of hour.segments"
        [segment]="segment"
        [locale]="locale"
        (click)="hourSegmentClicked.emit({date: segment.date})">
      </mwl-calendar-day-view-hour-segment>
    </div>
  </div>
</div>

Inputs

dayEndHour

The day end hours in 24 hour time. Must be 0-23

Type: number

Default value: 23

dayEndMinute

The day end minutes. Must be 0-59

Type: number

Default value: 59

dayStartHour

The day start hours in 24 hour time. Must be 0-23

Type: number

Default value: 0

dayStartMinute

The day start minutes. Must be 0-59

Type: number

Default value: 0

events

An array of events to display on view

Type: any[]

eventSnapSize

The grid size to snap resizing and dragging of events to

Type: number

Default value: 30

eventWidth

The width in pixels of each event on the view

Type: number

Default value: 150

hourSegmentModifier

A function that will be called before each hour segment is called. The first argument will contain the hour segment.
If you add the cssClass property to the segment it will add that class to the hour segment in the template

Type: Function

hourSegments

The number of segments in an hour. Must be <= 6

Type: number

Default value: 2

locale

The locale used to format dates

Type: string

refresh

An observable that when emitted on will re-render the current view

Type: any

tooltipPlacement

The placement of the event tooltip

Type: string

Default value: top

viewDate

The current view date

Type: Date

Outputs

eventClicked

Called when an event title is clicked

$event type: any
eventTimesChanged

Called when an event is resized or dragged and dropped

$event type: any
hourSegmentClicked

Called when an hour segment is clicked

$event type: any
import {
  Component,
  Input,
  OnChanges,
  Output,
  EventEmitter,
  ChangeDetectorRef,
  LOCALE_ID,
  Inject,
  OnInit,
  OnDestroy
} from '@angular/core';
import { getDayView, getDayViewHourGrid, CalendarEvent, DayView, DayViewHour } from 'calendar-utils';
import { Subject } from 'rxjs/Subject';
import { Subscription } from 'rxjs/Subscription';
import { CalendarEventTimesChangedEvent } from '../../interfaces/calendarEventTimesChangedEvent.interface';

/**
 * @hidden
 */
const SEGMENT_HEIGHT: number = 30;

/**
 * Shows all events on a given day. Example usage:
 *
 * ```
 * &lt;mwl-calendar-day-view
 *  [viewDate]="viewDate"
 *  [events]="events"&gt;
 * &lt;/mwl-calendar-day-view&gt;
 * ```
 */
@Component({
  selector: 'mwl-calendar-day-view',
  template: `
    <div class="cal-day-view" #dayViewContainer>
      <mwl-calendar-all-day-event
        *ngFor="let event of view.allDayEvents"
        [event]="event"
        (eventClicked)="eventClicked.emit({event: event})">
      </mwl-calendar-all-day-event>
      <div class="cal-hour-rows">
        <div class="cal-events">
          <mwl-calendar-day-view-event
            *ngFor="let dayEvent of view?.events"
            [dayEvent]="dayEvent"
            [hourSegments]="hourSegments"
            [tooltipPlacement]="tooltipPlacement"
            [eventSnapSize]="eventSnapSize"
            [dayViewContainer]="dayViewContainer"
            (eventClicked)="eventClicked.emit({event: dayEvent.event})"
            (eventResized)="eventTimesChanged.emit($event)">
          </mwl-calendar-day-view-event>
        </div>
        <div class="cal-hour" *ngFor="let hour of hours" [style.minWidth.px]="view?.width + 70">
          <mwl-calendar-day-view-hour-segment
            *ngFor="let segment of hour.segments"
            [segment]="segment"
            [locale]="locale"
            (click)="hourSegmentClicked.emit({date: segment.date})">
          </mwl-calendar-day-view-hour-segment>
        </div>
      </div>
    </div>
  `
})
export class CalendarDayViewComponent implements OnChanges, OnInit, OnDestroy {

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

  /**
   * An array of events to display on view
   */
  @Input() events: CalendarEvent[] = [];

  /**
   * The number of segments in an hour. Must be <= 6
   */
  @Input() hourSegments: number = 2;

  /**
   * The day start hours in 24 hour time. Must be 0-23
   */
  @Input() dayStartHour: number = 0;

  /**
   * The day start minutes. Must be 0-59
   */
  @Input() dayStartMinute: number = 0;

  /**
   * The day end hours in 24 hour time. Must be 0-23
   */
  @Input() dayEndHour: number = 23;

  /**
   * The day end minutes. Must be 0-59
   */
  @Input() dayEndMinute: number = 59;

  /**
   * The width in pixels of each event on the view
   */
  @Input() eventWidth: number = 150;

  /**
   * An observable that when emitted on will re-render the current view
   */
  @Input() refresh: Subject<any>;

  /**
   * The locale used to format dates
   */
  @Input() locale: string;

  /**
   * A function that will be called before each hour segment is called. The first argument will contain the hour segment.
   * If you add the `cssClass` property to the segment it will add that class to the hour segment in the template
   */
  @Input() hourSegmentModifier: Function;

  /**
   * The grid size to snap resizing and dragging of events to
   */
  @Input() eventSnapSize: number = 30;

  /**
   * The placement of the event tooltip
   */
  @Input() tooltipPlacement: string = 'top';

  /**
   * Called when an event title is clicked
   */
  @Output() eventClicked: EventEmitter<{event: CalendarEvent}> = new EventEmitter<{event: CalendarEvent}>();

  /**
   * Called when an hour segment is clicked
   */
  @Output() hourSegmentClicked: EventEmitter<{date: Date}> = new EventEmitter<{date: Date}>();

  /**
   * Called when an event is resized or dragged and dropped
   */
  @Output() eventTimesChanged: EventEmitter<CalendarEventTimesChangedEvent> = new EventEmitter<CalendarEventTimesChangedEvent>();

  /**
   * @hidden
   */
  hours: DayViewHour[] = [];

  /**
   * @hidden
   */
  view: DayView;

  /**
   * @hidden
   */
  width: number = 0;

  /**
   * @hidden
   */
  refreshSubscription: Subscription;

  /**
   * @hidden
   */
  constructor(private cdr: ChangeDetectorRef, @Inject(LOCALE_ID) locale: string) {
    this.locale = locale;
  }

  /**
   * @hidden
   */
  ngOnInit(): void {
    if (this.refresh) {
      this.refreshSubscription = this.refresh.subscribe(() => {
        this.refreshAll();
        this.cdr.markForCheck();
      });
    }
  }

  /**
   * @hidden
   */
  ngOnDestroy(): void {
    if (this.refreshSubscription) {
      this.refreshSubscription.unsubscribe();
    }
  }

  /**
   * @hidden
   */
  ngOnChanges(changes: any): void {

    if (
      changes.viewDate ||
      changes.dayStartHour ||
      changes.dayStartMinute ||
      changes.dayEndHour ||
      changes.dayEndMinute
    ) {
      this.refreshHourGrid();
    }

    if (
      changes.viewDate ||
      changes.events ||
      changes.dayStartHour ||
      changes.dayStartMinute ||
      changes.dayEndHour ||
      changes.dayEndMinute ||
      changes.eventWidth
    ) {
      this.refreshView();
    }

  }

  private refreshHourGrid(): void {
    this.hours = getDayViewHourGrid({
      viewDate: this.viewDate,
      hourSegments: this.hourSegments,
      dayStart: {
        hour: this.dayStartHour,
        minute: this.dayStartMinute
      },
      dayEnd: {
        hour: this.dayEndHour,
        minute: this.dayEndMinute
      }
    });
    if (this.hourSegmentModifier) {
      this.hours.forEach(hour => {
        hour.segments.forEach(segment => this.hourSegmentModifier(segment));
      });
    }
  }

  private refreshView(): void {
    this.view = getDayView({
      events: this.events,
      viewDate: this.viewDate,
      hourSegments: this.hourSegments,
      dayStart: {
        hour: this.dayStartHour,
        minute: this.dayStartMinute
      },
      dayEnd: {
        hour: this.dayEndHour,
        minute: this.dayEndMinute
      },
      eventWidth: this.eventWidth,
      segmentHeight: SEGMENT_HEIGHT
    });
  }

  private refreshAll(): void {
    this.refreshHourGrid();
    this.refreshView();
  }

}

results matching ""

    No results matching ""