2022-08-06 13:02:08 +02:00
|
|
|
import { Component, forwardRef, Input, OnInit } from '@angular/core'
|
2022-03-11 10:53:32 -08:00
|
|
|
import { NG_VALUE_ACCESSOR } from '@angular/forms'
|
2022-08-06 13:02:08 +02:00
|
|
|
import {
|
|
|
|
|
NgbDateParserFormatter,
|
|
|
|
|
NgbDateStruct,
|
|
|
|
|
} from '@ng-bootstrap/ng-bootstrap'
|
2022-03-11 10:53:32 -08:00
|
|
|
import { SettingsService } from 'src/app/services/settings.service'
|
|
|
|
|
import { AbstractInputComponent } from '../abstract-input'
|
2021-02-24 18:00:26 +01:00
|
|
|
|
|
|
|
|
@Component({
|
2022-03-11 10:53:32 -08:00
|
|
|
providers: [
|
|
|
|
|
{
|
|
|
|
|
provide: NG_VALUE_ACCESSOR,
|
|
|
|
|
useExisting: forwardRef(() => DateComponent),
|
|
|
|
|
multi: true,
|
|
|
|
|
},
|
|
|
|
|
],
|
2021-02-24 18:00:26 +01:00
|
|
|
selector: 'app-input-date',
|
|
|
|
|
templateUrl: './date.component.html',
|
2022-03-11 10:53:32 -08:00
|
|
|
styleUrls: ['./date.component.scss'],
|
2021-02-24 18:00:26 +01:00
|
|
|
})
|
2022-03-11 10:53:32 -08:00
|
|
|
export class DateComponent
|
|
|
|
|
extends AbstractInputComponent<string>
|
|
|
|
|
implements OnInit
|
|
|
|
|
{
|
2022-05-22 14:21:24 -07:00
|
|
|
constructor(
|
|
|
|
|
private settings: SettingsService,
|
|
|
|
|
private ngbDateParserFormatter: NgbDateParserFormatter
|
|
|
|
|
) {
|
2021-02-24 18:00:26 +01:00
|
|
|
super()
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-06 13:02:08 +02:00
|
|
|
@Input()
|
|
|
|
|
suggestions: Date[]
|
|
|
|
|
|
|
|
|
|
getSuggestions() {
|
|
|
|
|
if (this.suggestions == null) return []
|
|
|
|
|
|
|
|
|
|
return this.suggestions
|
|
|
|
|
.map((s) => new Date(s)) // required to call the date functions below
|
|
|
|
|
.filter(
|
|
|
|
|
(d) =>
|
|
|
|
|
this.value === null || // if value is not set, take all suggestions
|
|
|
|
|
d.toISOString().slice(0, 10) != this.value // otherwise filter out the current value
|
|
|
|
|
)
|
|
|
|
|
.map((s) =>
|
|
|
|
|
this.ngbDateParserFormatter.format({
|
|
|
|
|
year: s.getFullYear(),
|
|
|
|
|
month: s.getMonth() + 1, // month of Date is zero based
|
|
|
|
|
day: s.getDate(),
|
|
|
|
|
})
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
onSuggestionClick(dateString: string) {
|
2022-08-06 21:19:06 -07:00
|
|
|
const parsedDate = this.ngbDateParserFormatter.parse(dateString)
|
|
|
|
|
this.writeValue(`${parsedDate.year}-${parsedDate.month}-${parsedDate.day}`)
|
2022-08-06 13:02:08 +02:00
|
|
|
this.onChange(this.value)
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-24 18:00:26 +01:00
|
|
|
ngOnInit(): void {
|
|
|
|
|
super.ngOnInit()
|
|
|
|
|
this.placeholder = this.settings.getLocalizedDateInputFormat()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
placeholder: string
|
|
|
|
|
|
2022-05-22 08:59:43 -07:00
|
|
|
onPaste(event: ClipboardEvent) {
|
2022-05-22 14:21:24 -07:00
|
|
|
const clipboardData: DataTransfer =
|
2022-05-22 08:59:43 -07:00
|
|
|
event.clipboardData || window['clipboardData']
|
|
|
|
|
if (clipboardData) {
|
2022-05-22 14:21:24 -07:00
|
|
|
event.preventDefault()
|
2022-05-22 08:59:43 -07:00
|
|
|
let pastedText = clipboardData.getData('text')
|
|
|
|
|
pastedText = pastedText.replace(/[\sa-z#!$%\^&\*;:{}=\-_`~()]+/g, '')
|
2022-05-22 14:21:24 -07:00
|
|
|
const parsedDate = this.ngbDateParserFormatter.parse(pastedText)
|
|
|
|
|
const formattedDate = this.ngbDateParserFormatter.format(parsedDate)
|
|
|
|
|
this.writeValue(formattedDate)
|
|
|
|
|
this.onChange(formattedDate)
|
2022-05-22 08:59:43 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-11 00:11:59 -08:00
|
|
|
onKeyPress(event: KeyboardEvent) {
|
2022-03-11 08:11:35 -08:00
|
|
|
if ('Enter' !== event.key && !/[0-9,\.\/-]+/.test(event.key)) {
|
2022-03-11 01:03:43 -08:00
|
|
|
event.preventDefault()
|
2022-03-11 00:11:59 -08:00
|
|
|
}
|
|
|
|
|
}
|
2021-02-24 18:00:26 +01:00
|
|
|
}
|