import { Component, Input, Output, EventEmitter, ExistingProvider, forwardRef, OnChanges } from '@angular/core'; import { RdLib } from '../../base/rdLib'; import { InputBase } from "./inputBase"; const provider: ExistingProvider = { provide: InputBase, useExisting: forwardRef(() => InputTimeSelector) } export enum TimeSelectorTypes { Day, Month, Year } export enum TimeDirectionTypes { Prev, Next } export enum Months { January = 1, February, March, April, May, June, July, August, September, October, November, December } @Component({ selector: 'rd-time-selector', template: ` {{selectedDay}} {{translateEn(Months[selectedMonth])}} {{selectedYear}} `, providers: [provider] }) export class InputTimeSelector extends InputBase implements OnChanges { @Input('rd-model') selectedDate: any; @Output('rd-change') changeEvent: EventEmitter = new EventEmitter(); @Output("rd-modelChange") modelChange: EventEmitter = new EventEmitter(); @Input('rd-type') type: TimeSelectorTypes = TimeSelectorTypes.Month; selectedDay: number = 1; selectedYear: number; selectedMonth: number; Months = Months; TimeDirectionTypes = TimeDirectionTypes; TimeSelectorTypes = TimeSelectorTypes; translateEn = RdLib.localization.translateEn; ngOnChanges() { if (this.selectedDate) { var modelHasDate = true; if (!RdLib.typeOperations.longToString(this.selectedDate, null)) { this.error("Invalid Date"); return; } } else { modelHasDate = false; this.selectedDate = RdLib.typeOperations.dateToLong(new Date(), this.type == TimeSelectorTypes.Month ? 'month' : 'day'); } this.setTimeDetails(); if (modelHasDate) this.setDate(); else this.changes(this.setDate()); } navigateYear(direction) { if (direction == TimeDirectionTypes.Prev) this.selectedYear--; else this.selectedYear++; this.changes(this.setDate()); } navigateMonth(direction) { if (direction == TimeDirectionTypes.Prev) this.selectedMonth--; else this.selectedMonth++; this.changes(this.setDate()); } navigateDay(direction) { if (direction == TimeDirectionTypes.Prev) this.selectedDay--; else this.selectedDay++; this.changes(this.setDate()); } private changes(selectedDate) { this.modelChange.emit(selectedDate); this.changeEvent.emit(selectedDate); } setDate() { this.selectedDate = RdLib.typeOperations.dateToLong(new Date(this.selectedYear, (this.selectedMonth - 1), this.type == TimeSelectorTypes.Month ? 1 : this.selectedDay)); this.setTimeDetails(); return this.selectedDate; } setTimeDetails() { this.selectedYear = RdLib.typeOperations.longToDate(this.selectedDate).getFullYear(); this.selectedMonth = RdLib.typeOperations.longToDate(this.selectedDate).getMonth() + 1; this.selectedDay = RdLib.typeOperations.longToDate(this.selectedDate).getDate(); } }