paperless-ngx/src-ui/src/app/components/common/input/date/date.component.ts

84 lines
2.3 KiB
TypeScript
Raw Normal View History

import { Component, forwardRef, Input, OnInit } from '@angular/core'
import { NG_VALUE_ACCESSOR } from '@angular/forms'
2022-08-06 20:49:00 -07:00
import {
NgbDateAdapter,
NgbDateParserFormatter,
} from '@ng-bootstrap/ng-bootstrap'
import { SettingsService } from 'src/app/services/settings.service'
import { AbstractInputComponent } from '../abstract-input'
@Component({
providers: [
{
provide: NG_VALUE_ACCESSOR,
useExisting: forwardRef(() => DateComponent),
multi: true,
},
],
selector: 'app-input-date',
templateUrl: './date.component.html',
styleUrls: ['./date.component.scss'],
})
export class DateComponent
extends AbstractInputComponent<string>
implements OnInit
{
2022-05-22 14:21:24 -07:00
constructor(
private settings: SettingsService,
2022-08-06 20:49:00 -07:00
private ngbDateParserFormatter: NgbDateParserFormatter,
private isoDateAdapter: NgbDateAdapter<string>
2022-05-22 14:21:24 -07:00
) {
super()
}
@Input()
2022-08-07 08:37:18 -07:00
suggestions: string[]
getSuggestions() {
2022-08-07 08:37:18 -07:00
return this.suggestions == null
? []
: this.suggestions
.map((s) => this.ngbDateParserFormatter.parse(s))
.filter(
(d) =>
this.value === null || // if value is not set, take all suggestions
this.value != this.isoDateAdapter.toModel(d) // otherwise filter out current date
)
.map((s) => this.ngbDateParserFormatter.format(s))
}
onSuggestionClick(dateString: string) {
2022-08-06 21:19:06 -07:00
const parsedDate = this.ngbDateParserFormatter.parse(dateString)
2022-08-07 08:37:18 -07:00
this.writeValue(this.isoDateAdapter.toModel(parsedDate))
this.onChange(this.value)
}
ngOnInit(): void {
super.ngOnInit()
this.placeholder = this.settings.getLocalizedDateInputFormat()
}
placeholder: string
onPaste(event: ClipboardEvent) {
2022-05-22 14:21:24 -07:00
const clipboardData: DataTransfer =
event.clipboardData || window['clipboardData']
if (clipboardData) {
2022-05-22 14:21:24 -07:00
event.preventDefault()
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)
2022-08-06 20:49:00 -07:00
if (parsedDate) {
this.writeValue(this.isoDateAdapter.toModel(parsedDate))
this.onChange(this.value)
}
}
}
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()
}
}
}