import {Component, OnInit, forwardRef, Input, DoCheck, ElementRef} from '@angular/core'; import {ControlValueAccessor, NG_VALUE_ACCESSOR} from '@angular/forms'; import * as frLocale from 'date-fns/locale/zh_cn'; declare const $: any; @Component({ selector: 'datePicker', templateUrl: './template.html', styleUrls: ['./style.less'], providers: [{ provide: NG_VALUE_ACCESSOR, useExisting: forwardRef(() => DatePicker), multi: true }] }) export default class DatePicker implements OnInit { @Input() minYear: number; @Input() maxYear: number; @Input() minDate: string; @Input() maxDate: string; @Input() hasTime: boolean = false; private onValueChange: any; private datepicker: any; options: any; element: any; private value: string = ''; private time: any = { hour: '00', minute: '00', second: '00' }; constructor(private el: ElementRef) { this.element = $(el.nativeElement); } ngOnInit(): void { const options = { minYear: this.minYear || 2010, maxYear: this.maxYear || 2030, displayFormat: 'YYYY-MM-DD', barTitleFormat: 'YYYY-MM-DD', firstCalendarDay: 1, locale: frLocale, barTitleIfEmpty: '请选择一个日期' }; if (this.minDate) { options['minDate'] = new Date(this.minDate); options['maxDate'] = new Date(this.maxDate); } this.options = options; setTimeout(() => { this.element.find('.ngx-datepicker-input').on('click', () => { if (!this.datepicker) { this.datepicker = new Date() } }) }, 500) } remove() { this.datepicker = undefined; this.value = ''; this.time = { hour: '00', minute: '00', second: '00' }; this.onValueChange(''); } writeValue(value: any): void { if (value) { this.datepicker = new Date(value); this.change(this.datepicker); } else { this.value = ''; this.onValueChange && this.onValueChange(''); } } registerOnChange(changeFn: any): void { this.onValueChange = changeFn; } registerOnTouched(changeFn: any): void { } change(value) { if (this.onValueChange) { let date = value.getFullYear() + '-'; date += (value.getMonth() + 1 >= 10 ? value.getMonth() + 1 : '0' + (value.getMonth() + 1)) + '-'; date += value.getDate() >= 10 ? value.getDate() : '0' + value.getDate(); this.value = date; const hour = value.getHours(); const minute = value.getMinutes(); const second = value.getSeconds(); this.time = { hour: hour < 10 ? '0' + hour : hour, minute: minute < 10 ? '0' + minute : minute, second: second < 10 ? '0' + second : second, }; if (this.hasTime) { this.onValueChange(date + ' ' + this.time.hour + ':' + this.time.minute + ':' + this.time.second); } else { this.onValueChange(date); } } } hourBlur() { let hour: any = this.time.hour + ''; if (hour.length > 2) { hour = hour.substring(0, 2) } hour *= 1; if (isNaN(hour)) { this.time.hour = '00'; } else { if (hour < 10) { this.time.hour = '0' + hour; } else if (hour < 0) { this.time.hour = '00'; } else if (hour > 23) { this.time.hour = '23'; } } this.onValueChange(this.value + ' ' + this.time.hour + ':' + this.time.minute + ':' + this.time.second); } minuteBlur() { let minute: any = this.time.minute + ''; if (minute.length > 2) { minute = minute.substring(0, 2) } minute *= 1; if (isNaN(minute)) { this.time.minute = '00'; } else { if (minute < 10) { this.time.minute = '0' + minute; } else if (minute < 0) { this.time.minute = '00'; } else if (minute > 59) { this.time.minute = '59'; } } this.onValueChange(this.value + ' ' + this.time.hour + ':' + this.time.minute + ':' + this.time.second); } secondBlur() { let second: any = this.time.second + ''; if (second.length > 2) { second = second.substring(0, 2) } second *= 1; if (isNaN(second)) { this.time.second = '00'; } else { if (second < 10) { this.time.second = '0' + second; } else if (second < 0) { this.time.second = '00'; } else if (second > 59) { this.time.second = '59'; } } this.onValueChange(this.value + ' ' + this.time.hour + ':' + this.time.minute + ':' + this.time.second); } }