import { LocalDate, Month } from 'js-joda'; import * as _ from 'lodash'; export function DOBPickerDirectiveFactory() { return new DOBPickerDirective(); } class DOBPickerDirective implements ng.IDirective { restrict = 'E'; bindToController = true; controller = DOBPickerController; controllerAs = 'ctrl'; template = require('./DOBPicker.html'); scope = { name: '@', label: '@', dateOfBirth: '=' } } export class DOBPickerController { maxYear: number; months: string[]; maxDays: number; dateOfBirth: LocalDate; selectedYear: number; selectedMonth: number; selectedDay: number; constructor() { this.maxDays = 31; this.maxYear = LocalDate.now().year(); this.months = Month.values().map(m => _.upperFirst(m.toString().toLowerCase())); // Initialise fields using initial value if (this.dateOfBirth) { this.selectedDay = this.dateOfBirth.dayOfMonth(); this.selectedMonth = this.dateOfBirth.monthValue(); this.selectedYear = this.dateOfBirth.year(); } } update() { if (this.selectedMonth && this.selectedYear) { this.maxDays = LocalDate.of(this.selectedYear, this.selectedMonth, 1).lengthOfMonth(); } else { this.maxDays = 31; } if (this.selectedDay > this.maxDays) { this.selectedDay = this.maxDays; } if (this.selectedDay && this.selectedMonth && this.selectedYear) { this.dateOfBirth = LocalDate.of(this.selectedYear, this.selectedMonth, this.selectedDay); } else { delete this.dateOfBirth; } } }