novo-elements
Version:
Bullhorn's NOVO Element Repository for Angular 2
187 lines (164 loc) • 6.24 kB
text/typescript
// NG2
import { Component, EventEmitter, forwardRef, Input, Output, OnInit, OnChanges, SimpleChanges } from '@angular/core';
import { NG_VALUE_ACCESSOR, ControlValueAccessor } from '@angular/forms';
// APP
import { Helpers } from './../../utils/Helpers';
// Value accessor for the component (supports ngModel)
const TIME_PICKER_VALUE_ACCESSOR = {
provide: NG_VALUE_ACCESSOR,
useExisting: forwardRef(() => NovoTimePickerElement),
multi: true
};
export class NovoTimePickerElement implements ControlValueAccessor, OnInit, OnChanges {
military: boolean = false;
inline: boolean = false;
onSelect: EventEmitter<any> = new EventEmitter();
hours: number = 12;
minutes: number = 0;
value: any = null;
meridian: string;
inBetween: boolean;
hoursClass: string;
activeHour;
minutesClass: string;
activeMinute;
MERIDIANS: Array<string> = ['am', 'pm'];
MINUTES: Array<string> = ['05', '10', '15', '20', '25', '30', '35', '40', '45', '50', '55', '00'];
HOURS: Array<string> = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12'];
model: any;
onModelChange: Function = () => {
};
onModelTouched: Function = () => {
};
ngOnInit() {
if (this.military) {
this.HOURS = ['0', ...this.HOURS, '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23'];
}
this.ngOnChanges();
}
ngOnChanges(changes?: SimpleChanges) {
if (this.model) {
this.init(this.model, false);
} else {
this.init(new Date(), false);
}
}
init(value, dispatch) {
let _value = new Date(value);
let hours: string | number = _value.getHours();
let minutes: string | number = _value.getMinutes();
if (!this.military) {
this.meridian = hours >= 12 ? 'pm' : 'am';
hours = hours % 12;
hours = hours || 12;
}
minutes = minutes < 10 ? `0${minutes}` : minutes;
this.setHours(null, hours, dispatch);
this.setMinutes(null, minutes, dispatch);
this.checkBetween(minutes);
}
checkBetween(value) {
this.inBetween = this.MINUTES.indexOf(String(value)) < 0;
}
setHours(event, hours, dispatch) {
Helpers.swallowEvent(event);
this.hours = hours;
this.hoursClass = `hour-${hours}`;
this.activeHour = hours;
if (dispatch) {
this.dispatchChange();
}
}
setMinutes(event, minutes, dispatch) {
Helpers.swallowEvent(event);
this.minutes = minutes;
this.minutesClass = `min-${minutes}`;
this.activeMinute = minutes;
this.checkBetween(minutes);
if (dispatch) {
this.dispatchChange();
}
}
setPeriod(event, period, dispatch) {
Helpers.swallowEvent(event);
this.meridian = period;
if (dispatch) {
this.dispatchChange();
}
}
dispatchChange() {
let hours = Number(this.hours);
if (!this.military) {
hours = this.meridian === 'pm' ? hours + 12 : hours;
// Special case for 12
if (this.meridian === 'pm' && hours === 24) {
hours = 12;
} else if (this.meridian === 'am' && hours === 12) {
hours = 0;
}
}
let value = new Date();
value.setHours(hours);
value.setMinutes(this.minutes);
value.setSeconds(0);
this.onSelect.next({
hours: hours,
minutes: this.minutes,
meridian: this.meridian,
date: value,
text: `${this.hours}:${this.minutes} ${this.meridian}`
});
this.onModelChange(value);
}
// ValueAccessor Functions
writeValue(model: any): void {
this.model = model;
if (Helpers.isDate(model)) {
this.init(model, false);
}
}
registerOnChange(fn: Function): void {
this.onModelChange = fn;
}
registerOnTouched(fn: Function): void {
this.onModelTouched = fn;
}
}