/* * Deepkit Framework * Copyright (C) 2021 Deepkit UG, Marc J. Schmidt * * This program is free software: you can redistribute it and/or modify * it under the terms of the MIT License. * * You should have received a copy of the MIT License along with this program. */ import { formatDate } from '@angular/common'; import { AfterViewInit, ChangeDetectionStrategy, Component, ElementRef, LOCALE_ID, booleanAttribute, computed, inject, input, output, viewChild, } from '@angular/core'; import { FormsModule } from '@angular/forms'; import { ValueAccessorBase, ngValueAccessor } from '../../core/form'; import { DuiDocument } from '../document'; import { IconComponent } from '../icon/icon.component'; const dateTimeTypes: string[] = ['time', 'date', 'datetime', 'datetime-local']; @Component({ selector: 'dui-input', template: ` @if (icon(); as icon) { } @if (type() === 'textarea') { } @else { @if (type() !== 'textarea') { } } @if (clearer() && !disabled()) { } `, styleUrls: ['./input.component.scss'], host: { '[class.is-textarea]': 'type() === "textarea"', '[class.light-focus]': 'lightFocus()', '[class.focus-outline]': '!lightFocus()', '[class.semi-transparent]': 'semiTransparent()', '[class.no-controls]': 'noControls()', '[class.has-clearer]': 'clearer()', '[class.filled]': 'value()', '[class.round]': 'round()', '[class.textured]': 'textured()', '[class.has-icon]': 'icon()', '[class.focused]': 'isFocused()', }, providers: [ngValueAccessor(InputComponent)], changeDetection: ChangeDetectionStrategy.Eager, imports: [IconComponent, FormsModule], }) export class InputComponent extends ValueAccessorBase implements AfterViewInit { type = input('text'); step = input(1); placeholder = input(''); name = input(''); icon = input(''); min = input(); max = input(); maxLength = input(); minLength = input(); iconSize = input(17); /** * Focuses this element once created (AfterViewInit). */ autoFocus = input(false, { alias: 'auto-focus', transform: booleanAttribute }); /** * Uses a more decent focus border. */ lightFocus = input(false, { transform: booleanAttribute }); /** * Disables input controls (like for type=number the arrow buttons) */ noControls = input(false, { transform: booleanAttribute }); /** * Appears a little bit transparent. Perfect for blurry background. */ semiTransparent = input(false, { transform: booleanAttribute }); esc = output(); enter = output(); keydown = output(); keyup = output(); blur = output(); focus = output(); input = viewChild('input', { read: ElementRef }); textured = input(false, { transform: booleanAttribute }); readonly = input(false, { transform: booleanAttribute }); protected duiDocument = inject(DuiDocument); protected locale = inject(LOCALE_ID); isFocused = computed(() => this.duiDocument.activeElement() === this.input()?.nativeElement); round = input(false, { transform: booleanAttribute }); clearer = input(false, { transform: booleanAttribute }); protected normalizeValue = computed(() => { if (dateTimeTypes.includes(this.type())) { if (this.value() instanceof Date) { if (this.type() === 'date') return formatDate(this.value(), `yyyy-MM-dd`, this.locale); return formatDate(this.value(), 'yyyy-MM-ddTHH:mm:ss.SSS', this.locale); } else if ('string' === typeof this.value()) { return formatDate(new Date(this.value()), 'yyyy-MM-ddTHH:mm:ss.SSS', this.locale); } } return this.value(); }); constructor() { super(); this.value.set(''); } clear() { this.setValue(''); } setValue(value: any | undefined) { if (this.type() === 'file' && !value && this.input) { //we need to manually reset the field, since writing to it via ngModel is not supported. const input = this.input(); if (input) input.nativeElement.value = ''; } value = value === undefined || value === null ? '' : value; if (this.type() === 'file') return; if (this.type() === 'text') { } else if (this.type() === 'number') { if (value && 'number' !== typeof value) { value = parseFloat(value); } } else if (dateTimeTypes.includes(this.type())) { if ('string' === typeof value) { value = new Date(value); } } super.setValue(value); } protected onKeyDown(event: KeyboardEvent) { this.touch(); this.keydown.emit(event); } protected onKeyUp(event: KeyboardEvent) { if (event.key.toLowerCase() === 'enter' && this.type() !== 'textarea') { this.enter.emit(event); } if (event.key.toLowerCase() === 'esc' || event.key.toLowerCase() === 'escape') { this.esc.emit(event); } this.keyup.emit(event); } focusInput() { setTimeout(() => { const input = this.input(); if (input) input.nativeElement.focus(); }); } ngAfterViewInit() { if (this.autoFocus() && this.input()) { this.focusInput(); } } protected async handleFileInput(event: any) { const files = event.target.files; this.touch(); const readFile = (file: File): Promise => { return new Promise((resolve, reject) => { const reader = new FileReader(); reader.onload = () => { if (reader.result) { if (reader.result instanceof ArrayBuffer) { resolve(reader.result); } else { resolve(undefined); } } }; reader.onerror = error => { console.log('Error: ', error); reject(); }; reader.readAsArrayBuffer(file); }); }; if (files) { if (files.length > 1) { const value: any[] = []; for (let i = 0; i < files.length; i++) { const file = files.item(i); if (file) { value.push(await readFile(file)); } } this.setValue(value); } else if (files.length === 1) { this.setValue(await readFile(files.item(0))); } } } }