2023-02-02 15:25:04 +02:00
|
|
|
///
|
2023-02-14 11:39:29 +02:00
|
|
|
/// Copyright © 2016-2023 The Thingsboard Authors
|
2023-02-02 15:25:04 +02:00
|
|
|
///
|
|
|
|
|
/// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
/// you may not use this file except in compliance with the License.
|
|
|
|
|
/// You may obtain a copy of the License at
|
|
|
|
|
///
|
|
|
|
|
/// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
///
|
|
|
|
|
/// Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
/// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
/// See the License for the specific language governing permissions and
|
|
|
|
|
/// limitations under the License.
|
|
|
|
|
///
|
|
|
|
|
|
|
|
|
|
import { Component, forwardRef, Input } from '@angular/core';
|
|
|
|
|
import { ControlValueAccessor, FormBuilder, FormGroup, NG_VALUE_ACCESSOR, Validators } from '@angular/forms';
|
|
|
|
|
import { MatChipInputEvent } from '@angular/material/chips';
|
|
|
|
|
import { COMMA, ENTER, SEMICOLON } from '@angular/cdk/keycodes';
|
2023-03-27 17:08:23 +03:00
|
|
|
import { FloatLabelType, MatFormFieldAppearance, SubscriptSizing } from '@angular/material/form-field';
|
2023-05-04 17:46:43 +03:00
|
|
|
import { coerceBoolean } from '@shared/decorators/coercion';
|
2023-02-02 15:25:04 +02:00
|
|
|
|
|
|
|
|
@Component({
|
2023-02-13 16:11:23 +02:00
|
|
|
selector: 'tb-string-items-list',
|
|
|
|
|
templateUrl: './string-items-list.component.html',
|
2023-02-02 15:25:04 +02:00
|
|
|
styleUrls: [],
|
|
|
|
|
providers: [
|
|
|
|
|
{
|
|
|
|
|
provide: NG_VALUE_ACCESSOR,
|
2023-02-13 16:11:23 +02:00
|
|
|
useExisting: forwardRef(() => StringItemsListComponent),
|
2023-02-02 15:25:04 +02:00
|
|
|
multi: true
|
|
|
|
|
}
|
|
|
|
|
]
|
|
|
|
|
})
|
2023-02-13 16:11:23 +02:00
|
|
|
export class StringItemsListComponent implements ControlValueAccessor{
|
2023-02-02 15:25:04 +02:00
|
|
|
|
2023-02-13 16:11:23 +02:00
|
|
|
stringItemsForm: FormGroup;
|
2023-02-02 15:25:04 +02:00
|
|
|
private modelValue: Array<string> | null;
|
|
|
|
|
|
|
|
|
|
readonly separatorKeysCodes: number[] = [ENTER, COMMA, SEMICOLON];
|
|
|
|
|
|
|
|
|
|
private requiredValue: boolean;
|
|
|
|
|
get required(): boolean {
|
|
|
|
|
return this.requiredValue;
|
|
|
|
|
}
|
|
|
|
|
@Input()
|
2023-02-24 11:29:36 +02:00
|
|
|
@coerceBoolean()
|
2023-02-02 15:25:04 +02:00
|
|
|
set required(value: boolean) {
|
2023-02-24 11:29:36 +02:00
|
|
|
if (this.requiredValue !== value) {
|
|
|
|
|
this.requiredValue = value;
|
2023-02-02 15:25:04 +02:00
|
|
|
this.updateValidators();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Input()
|
2023-02-24 11:29:36 +02:00
|
|
|
@coerceBoolean()
|
2023-02-02 15:25:04 +02:00
|
|
|
disabled: boolean;
|
|
|
|
|
|
2023-02-13 16:11:23 +02:00
|
|
|
@Input()
|
|
|
|
|
label: string;
|
|
|
|
|
|
|
|
|
|
@Input()
|
|
|
|
|
placeholder: string;
|
|
|
|
|
|
|
|
|
|
@Input()
|
|
|
|
|
hint: string;
|
|
|
|
|
|
|
|
|
|
@Input()
|
|
|
|
|
requiredText: string;
|
|
|
|
|
|
|
|
|
|
@Input()
|
|
|
|
|
floatLabel: FloatLabelType = 'auto';
|
|
|
|
|
|
|
|
|
|
@Input()
|
2023-03-06 11:27:07 +02:00
|
|
|
appearance: MatFormFieldAppearance = 'fill';
|
2023-02-13 16:11:23 +02:00
|
|
|
|
2023-02-24 11:29:36 +02:00
|
|
|
@Input()
|
|
|
|
|
@coerceBoolean()
|
2023-03-20 16:51:42 +02:00
|
|
|
editable = false;
|
2023-02-24 11:29:36 +02:00
|
|
|
|
2023-03-27 17:08:23 +03:00
|
|
|
@Input()
|
|
|
|
|
subscriptSizing: SubscriptSizing = 'fixed'
|
|
|
|
|
|
2023-02-02 15:25:04 +02:00
|
|
|
private propagateChange = (v: any) => { };
|
|
|
|
|
|
|
|
|
|
constructor(private fb: FormBuilder) {
|
2023-02-13 16:11:23 +02:00
|
|
|
this.stringItemsForm = this.fb.group({
|
|
|
|
|
items: [null, this.required ? [Validators.required] : []]
|
2023-02-02 15:25:04 +02:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
updateValidators() {
|
2023-02-13 16:11:23 +02:00
|
|
|
this.stringItemsForm.get('items').setValidators(this.required ? [Validators.required] : []);
|
|
|
|
|
this.stringItemsForm.get('items').updateValueAndValidity();
|
2023-02-02 15:25:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
registerOnChange(fn: any): void {
|
|
|
|
|
this.propagateChange = fn;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
registerOnTouched(fn: any): void {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
setDisabledState(isDisabled: boolean): void {
|
|
|
|
|
this.disabled = isDisabled;
|
|
|
|
|
if (isDisabled) {
|
2023-02-13 16:11:23 +02:00
|
|
|
this.stringItemsForm.disable({emitEvent: false});
|
2023-02-02 15:25:04 +02:00
|
|
|
} else {
|
2023-02-13 16:11:23 +02:00
|
|
|
this.stringItemsForm.enable({emitEvent: false});
|
2023-02-02 15:25:04 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
writeValue(value: Array<string> | null): void {
|
|
|
|
|
if (value != null && value.length > 0) {
|
|
|
|
|
this.modelValue = [...value];
|
2023-02-13 16:11:23 +02:00
|
|
|
this.stringItemsForm.get('items').setValue(value);
|
2023-02-02 15:25:04 +02:00
|
|
|
} else {
|
2023-02-13 16:11:23 +02:00
|
|
|
this.stringItemsForm.get('items').setValue(null);
|
2023-02-02 15:25:04 +02:00
|
|
|
this.modelValue = null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-13 16:11:23 +02:00
|
|
|
addItem(event: MatChipInputEvent): void {
|
|
|
|
|
let item = event.value || '';
|
2023-02-02 15:25:04 +02:00
|
|
|
const input = event.chipInput.inputElement;
|
2023-02-13 16:11:23 +02:00
|
|
|
item = item.trim();
|
|
|
|
|
if (item) {
|
|
|
|
|
if (!this.modelValue || this.modelValue.indexOf(item) === -1) {
|
2023-02-02 15:25:04 +02:00
|
|
|
if (!this.modelValue) {
|
|
|
|
|
this.modelValue = [];
|
|
|
|
|
}
|
2023-02-13 16:11:23 +02:00
|
|
|
this.modelValue.push(item);
|
|
|
|
|
this.stringItemsForm.get('items').setValue(this.modelValue);
|
2023-02-02 15:25:04 +02:00
|
|
|
}
|
|
|
|
|
this.propagateChange(this.modelValue);
|
|
|
|
|
if (input) {
|
|
|
|
|
input.value = '';
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-13 16:11:23 +02:00
|
|
|
removeItems(item: string) {
|
|
|
|
|
const index = this.modelValue.indexOf(item);
|
2023-02-02 15:25:04 +02:00
|
|
|
if (index >= 0) {
|
|
|
|
|
this.modelValue.splice(index, 1);
|
|
|
|
|
if (!this.modelValue.length) {
|
|
|
|
|
this.modelValue = null;
|
|
|
|
|
}
|
2023-02-13 16:11:23 +02:00
|
|
|
this.stringItemsForm.get('items').setValue(this.modelValue);
|
2023-02-02 15:25:04 +02:00
|
|
|
this.propagateChange(this.modelValue);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-13 16:11:23 +02:00
|
|
|
get stringItemsList(): string[] {
|
|
|
|
|
return this.stringItemsForm.get('items').value;
|
2023-02-02 15:25:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|