2021-02-12 12:31:12 +01:00
|
|
|
import {
|
2022-02-08 10:52:11 +01:00
|
|
|
AfterViewInit,
|
2021-03-22 16:10:18 +01:00
|
|
|
Component,
|
|
|
|
ElementRef,
|
|
|
|
EventEmitter,
|
|
|
|
HostListener,
|
2021-02-12 12:31:12 +01:00
|
|
|
Input,
|
|
|
|
OnChanges,
|
|
|
|
OnDestroy,
|
|
|
|
OnInit,
|
|
|
|
Output,
|
|
|
|
SimpleChanges,
|
|
|
|
ViewChild
|
|
|
|
} from "@angular/core";
|
2021-04-23 18:25:52 +02:00
|
|
|
import {AbstractControl, FormArray, FormControl, ValidatorFn} from "@angular/forms";
|
2020-11-12 18:57:32 +01:00
|
|
|
import {HelperFunctions} from "../../utils/HelperFunctions.class";
|
2021-01-28 17:33:16 +01:00
|
|
|
import {Observable, of, Subscription} from "rxjs";
|
2020-10-19 11:06:23 +02:00
|
|
|
import {MatSelect} from "@angular/material/select";
|
2021-01-28 17:33:16 +01:00
|
|
|
import {MatAutocompleteSelectedEvent} from "@angular/material/autocomplete";
|
|
|
|
import {map, startWith} from "rxjs/operators";
|
2021-03-22 16:10:18 +01:00
|
|
|
import {MatChipInputEvent} from "@angular/material/chips";
|
2019-12-23 14:59:56 +01:00
|
|
|
|
2019-12-23 16:17:35 +01:00
|
|
|
|
|
|
|
export interface Option {
|
|
|
|
icon?: string,
|
|
|
|
iconClass?: string,
|
|
|
|
value: any,
|
|
|
|
label: string
|
|
|
|
}
|
|
|
|
|
2019-12-23 14:59:56 +01:00
|
|
|
@Component({
|
|
|
|
selector: '[dashboard-input]',
|
|
|
|
template: `
|
2021-01-28 17:33:16 +01:00
|
|
|
<div *ngIf="label && type != 'checkbox'"
|
|
|
|
class="uk-text-bold uk-form-label uk-margin-small-bottom">{{label + (required ? ' *' : '')}}</div>
|
2021-02-19 19:01:03 +01:00
|
|
|
<div *ngIf="hint" class="uk-margin-bottom uk-form-hint">{{hint}}</div>
|
2021-02-12 12:31:12 +01:00
|
|
|
<div class="uk-grid uk-flex" [ngClass]="'uk-flex-' + flex" [class.uk-grid-small]="gridSmall" uk-grid>
|
2020-10-19 11:06:23 +02:00
|
|
|
<ng-content></ng-content>
|
2021-01-28 17:33:16 +01:00
|
|
|
<div [class.uk-hidden]="hideControl" class="uk-width-expand uk-position-relative"
|
|
|
|
[class.uk-flex-first]="!extraLeft">
|
2020-12-23 17:06:31 +01:00
|
|
|
<ng-template [ngIf]="icon && formControl.enabled">
|
2021-09-07 10:13:10 +02:00
|
|
|
<span class="uk-text-muted" [class.top]="type === 'textarea'" [ngClass]="iconLeft?('left'):'right'">
|
2020-11-01 16:41:02 +01:00
|
|
|
<icon [name]="icon"></icon>
|
|
|
|
</span>
|
|
|
|
</ng-template>
|
2020-12-23 17:06:31 +01:00
|
|
|
<ng-template [ngIf]="formControl.disabled">
|
2021-09-07 10:13:10 +02:00
|
|
|
<span class="uk-text-muted left" [class.top]="type === 'textarea'">
|
2020-12-23 17:06:31 +01:00
|
|
|
<icon [name]="'lock'"></icon>
|
|
|
|
</span>
|
|
|
|
</ng-template>
|
2021-03-03 19:04:53 +01:00
|
|
|
<ng-template [ngIf]="type === 'text' || type === 'URL' || type === 'logoURL'">
|
2021-02-12 12:31:12 +01:00
|
|
|
<div [ngClass]="inputClass"
|
|
|
|
[class.uk-form-danger]="formControl.invalid && formControl.touched"
|
2021-04-09 15:05:06 +02:00
|
|
|
[attr.uk-tooltip]="formControl.disabled?'title: This field is not editable; pos: bottom-left':null">
|
2021-04-09 15:01:07 +02:00
|
|
|
<input #input class="uk-input" [placeholder]="placeholder" [formControl]="formControl">
|
2021-02-12 12:31:12 +01:00
|
|
|
</div>
|
2020-10-19 11:06:23 +02:00
|
|
|
</ng-template>
|
|
|
|
<ng-template [ngIf]="type === 'textarea'">
|
2021-02-17 19:44:02 +01:00
|
|
|
<div [ngClass]="inputClass" class="uk-padding-remove-right"
|
2021-02-12 12:31:12 +01:00
|
|
|
[class.uk-form-danger]="formControl.invalid && formControl.touched"
|
2021-04-09 15:05:06 +02:00
|
|
|
[attr.uk-tooltip]="formControl.disabled?'title: This field is not editable; pos: bottom-left':null">
|
2021-02-19 19:01:03 +01:00
|
|
|
<textarea class="uk-textarea"
|
2021-02-12 12:31:12 +01:00
|
|
|
[rows]="rows" [placeholder]="placeholder"
|
|
|
|
[formControl]="formControl">
|
|
|
|
</textarea>
|
|
|
|
<div class="tools" [class.focused]="focused">
|
|
|
|
<ng-content select="[options]"></ng-content>
|
|
|
|
</div>
|
|
|
|
</div>
|
2020-10-19 11:06:23 +02:00
|
|
|
</ng-template>
|
|
|
|
<ng-template [ngIf]="type === 'select'">
|
2021-02-12 12:31:12 +01:00
|
|
|
<div [ngClass]="inputClass"
|
2021-01-28 17:33:16 +01:00
|
|
|
[attr.uk-tooltip]="formControl.disabled?'title: This field is not editable; pos: bottom-left':null"
|
|
|
|
[class.clickable]="formControl.enabled"
|
|
|
|
[class.uk-form-danger]="formControl.invalid && formControl.touched" (click)="openSelect()">
|
2020-10-19 11:06:23 +02:00
|
|
|
<mat-form-field class="uk-width-1-1">
|
2021-01-28 17:33:16 +01:00
|
|
|
<mat-select #select [required]="required" [value]="null"
|
|
|
|
(openedChange)="stopPropagation()" [formControl]="formControl"
|
|
|
|
[disableOptionCentering]="true">
|
2020-10-19 18:47:23 +02:00
|
|
|
<mat-option *ngIf="placeholder" class="uk-hidden" [value]="''">{{placeholder}}</mat-option>
|
2022-01-11 16:20:05 +01:00
|
|
|
<mat-option *ngFor="let option of options" [value]="option.value" [attr.uk-tooltip]="(tooltip) ? option.label : null">
|
2020-10-19 11:06:23 +02:00
|
|
|
{{option.label}}
|
|
|
|
</mat-option>
|
|
|
|
</mat-select>
|
|
|
|
</mat-form-field>
|
|
|
|
</div>
|
|
|
|
</ng-template>
|
2021-03-19 15:23:47 +01:00
|
|
|
<ng-template [ngIf]="type === 'autocomplete'">
|
|
|
|
<div [ngClass]="inputClass"
|
|
|
|
[attr.uk-tooltip]="formControl.disabled?'title: This field is not editable; pos: bottom-left':null"
|
|
|
|
[class.clickable]="formControl.enabled"
|
|
|
|
[class.uk-form-danger]="formControl.invalid && formControl.touched" (click)="openSelect()">
|
|
|
|
<mat-form-field class="uk-width-1-1">
|
|
|
|
<mat-chip-list #chipList>
|
2021-03-22 16:10:18 +01:00
|
|
|
<mat-chip *ngIf="formControl.value" [selectable]="false" [removable]="removable"
|
2021-03-19 15:23:47 +01:00
|
|
|
[attr.uk-tooltip]="getLabel(formControl.value)">
|
2021-03-22 16:10:18 +01:00
|
|
|
<span class="uk-flex uk-flex-middle uk-width-1-1">
|
2021-03-19 15:23:47 +01:00
|
|
|
<span class="uk-width-expand uk-text-truncate" [class.uk-text-small]="smallChip">{{getLabel(formControl.value)}}</span>
|
|
|
|
<icon name="remove_circle" class="mat-chip-remove" [flex]="true" [ratio]="smallChip?0.8:1"
|
|
|
|
(click)="resetSearch($event)"></icon>
|
|
|
|
</span>
|
|
|
|
</mat-chip>
|
|
|
|
<div [class.uk-hidden]="formControl.value" class="uk-width-expand uk-position-relative chip-input">
|
2021-04-23 18:25:52 +02:00
|
|
|
<input #searchInput [formControl]="searchControl" [matAutocomplete]="auto"
|
2021-03-19 15:23:47 +01:00
|
|
|
[matChipInputFor]="chipList" [matAutocompleteConnectedTo]="origin">
|
2021-03-22 16:10:18 +01:00
|
|
|
<div *ngIf="placeholder && !searchInput.value" class="placeholder uk-width-1-1"
|
2021-03-19 15:23:47 +01:00
|
|
|
(click)="searchInput.focus()">{{placeholder}}</div>
|
|
|
|
</div>
|
|
|
|
<div class="uk-width-1-1 uk-invisible" matAutocompleteOrigin #origin="matAutocompleteOrigin"></div>
|
|
|
|
</mat-chip-list>
|
|
|
|
<mat-autocomplete #auto="matAutocomplete" (optionSelected)="formControl.setValue($event.option.value)" [class]="panelClass" [panelWidth]="panelWidth">
|
2021-05-25 14:29:07 +02:00
|
|
|
<mat-option *ngFor="let option of filteredOptions | async" [value]="option.value" [attr.uk-tooltip]="option.label">
|
2021-03-19 15:23:47 +01:00
|
|
|
{{option.label}}
|
|
|
|
</mat-option>
|
|
|
|
</mat-autocomplete>
|
|
|
|
</mat-form-field>
|
|
|
|
</div>
|
|
|
|
</ng-template>
|
2021-01-28 17:33:16 +01:00
|
|
|
<ng-template [ngIf]="type === 'chips'">
|
2021-02-12 12:31:12 +01:00
|
|
|
<div [ngClass]="inputClass"
|
2021-01-28 17:33:16 +01:00
|
|
|
[attr.uk-tooltip]="formControl.disabled?'title: This field is not editable; pos: bottom-left':null"
|
|
|
|
[class.clickable]="formControl.enabled"
|
2021-04-23 18:25:52 +02:00
|
|
|
[class.uk-form-danger]="formControl.invalid && searchControl.invalid && searchControl.touched" (click)="openSelect()">
|
2021-01-28 17:33:16 +01:00
|
|
|
<mat-form-field class="uk-width-1-1">
|
2021-02-17 19:44:02 +01:00
|
|
|
<mat-chip-list #chipList>
|
|
|
|
<mat-chip *ngFor="let chip of formAsArray.controls; let i=index" [selectable]="false"
|
|
|
|
[removable]="removable" [attr.uk-tooltip]="getLabel(chip.value)">
|
2021-03-22 16:10:18 +01:00
|
|
|
<span class="uk-flex uk-flex-middle uk-width-1-1">
|
2021-03-04 11:17:30 +01:00
|
|
|
<span class="uk-width-expand uk-text-truncate" [class.uk-text-small]="smallChip">{{getLabel(chip.value)}}</span>
|
|
|
|
<icon name="remove_circle" class="mat-chip-remove" [flex]="true" [ratio]="smallChip?0.8:1" (click)="removed(i)"></icon>
|
|
|
|
</span>
|
2021-01-28 17:33:16 +01:00
|
|
|
</mat-chip>
|
2021-02-19 19:01:03 +01:00
|
|
|
<div class="uk-width-expand uk-position-relative chip-input">
|
2021-04-23 18:25:52 +02:00
|
|
|
<input #searchInput style="width: calc(100% - 8px) !important;" [formControl]="searchControl" [matAutocomplete]="auto"
|
2021-02-24 17:53:31 +01:00
|
|
|
[matChipInputFor]="chipList" [matAutocompleteConnectedTo]="origin"
|
2021-02-12 12:31:12 +01:00
|
|
|
[matChipInputAddOnBlur]="addExtraChips && searchControl.value"
|
|
|
|
(matChipInputTokenEnd)="add($event)">
|
2021-01-28 17:33:16 +01:00
|
|
|
<div *ngIf="placeholder && !searchControl.value" class="placeholder uk-width-1-1"
|
|
|
|
(click)="searchInput.focus()">{{placeholder}}</div>
|
|
|
|
</div>
|
2021-02-24 17:53:31 +01:00
|
|
|
<div class="uk-width-1-1 uk-invisible" matAutocompleteOrigin #origin="matAutocompleteOrigin"></div>
|
2021-01-28 17:33:16 +01:00
|
|
|
</mat-chip-list>
|
2021-02-17 19:44:02 +01:00
|
|
|
<mat-autocomplete #auto="matAutocomplete" (optionSelected)="selected($event)" [class]="panelClass" [panelWidth]="panelWidth">
|
2021-05-25 14:29:07 +02:00
|
|
|
<mat-option *ngFor="let option of filteredOptions | async" [value]="option.value" [attr.uk-tooltip]="option.label">
|
2021-01-28 17:33:16 +01:00
|
|
|
{{option.label}}
|
|
|
|
</mat-option>
|
|
|
|
</mat-autocomplete>
|
|
|
|
</mat-form-field>
|
|
|
|
</div>
|
|
|
|
</ng-template>
|
2021-03-03 19:04:53 +01:00
|
|
|
<span *ngIf="formControl.invalid && formControl.touched" class="uk-text-danger input-message">
|
|
|
|
<span *ngIf="formControl.errors.error">{{formControl.errors.error}}</span>
|
|
|
|
<span *ngIf="type === 'URL' || type === 'logoURL'">Please provide a valid URL (e.g. https://example.com)</span>
|
2021-03-04 15:30:31 +01:00
|
|
|
</span>
|
|
|
|
<span class="uk-text-danger input-message">
|
2021-03-03 19:04:53 +01:00
|
|
|
<ng-content select="[error]"></ng-content>
|
|
|
|
</span>
|
|
|
|
<span *ngIf="formControl.valid" class="uk-text-warning input-message">
|
|
|
|
<ng-content select="[warning]"></ng-content>
|
|
|
|
<span *ngIf="!secure">
|
|
|
|
<span class="uk-text-bold">Note:</span> Prefer urls like "<span class="uk-text-bold">https://</span>example.com/my-secure-image.png"
|
|
|
|
instead of "<span class="uk-text-bold">http://</span>example.com/my-image.png".
|
|
|
|
<span class="uk-text-bold">Browsers may not load non secure content.</span>
|
|
|
|
</span>
|
|
|
|
</span>
|
|
|
|
<span class="input-message">
|
|
|
|
<ng-content select="[note]"></ng-content>
|
|
|
|
</span>
|
2020-10-19 11:06:23 +02:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<mat-checkbox *ngIf="type === 'checkbox'" [formControl]="formControl">{{label}}</mat-checkbox>
|
2020-11-01 16:41:02 +01:00
|
|
|
`,
|
|
|
|
styleUrls: ['input.component.css']
|
2019-12-23 14:59:56 +01:00
|
|
|
})
|
2022-02-08 10:52:11 +01:00
|
|
|
export class InputComponent implements OnDestroy, AfterViewInit, OnChanges {
|
2021-02-12 12:31:12 +01:00
|
|
|
/** Basic information */
|
2019-12-23 14:59:56 +01:00
|
|
|
@Input('formInput') formControl: AbstractControl;
|
2021-03-18 11:29:54 +01:00
|
|
|
@Input('type') type: 'text' | 'URL' | 'logoURL' | 'autocomplete' | 'textarea' | 'select' | 'checkbox' | 'chips' = 'text';
|
2019-12-23 14:59:56 +01:00
|
|
|
@Input('label') label: string;
|
2021-04-09 15:01:07 +02:00
|
|
|
/** Text */
|
|
|
|
@ViewChild('input') input: ElementRef;
|
2021-03-03 19:04:53 +01:00
|
|
|
/** Textarea options */
|
2019-12-23 14:59:56 +01:00
|
|
|
@Input('rows') rows: number = 3;
|
2021-02-12 12:31:12 +01:00
|
|
|
/** Select | chips available options */
|
2021-04-23 18:25:52 +02:00
|
|
|
@Input('options') options: Option[] = [];
|
2020-10-19 11:06:23 +02:00
|
|
|
@Input('hint') hint = null;
|
|
|
|
@Input('placeholder') placeholder = '';
|
2021-02-12 12:31:12 +01:00
|
|
|
@Input() inputClass: string = 'input-box';
|
|
|
|
/** Extra element Right or Left of the input */
|
2020-10-19 11:06:23 +02:00
|
|
|
@Input() extraLeft: boolean = true;
|
2021-01-21 16:15:53 +01:00
|
|
|
@Input() gridSmall: boolean = false;
|
2020-10-19 17:05:16 +02:00
|
|
|
@Input() hideControl: boolean = false;
|
2021-02-12 12:31:12 +01:00
|
|
|
@Input() flex: 'middle' | 'top' | 'bottom' = 'middle';
|
|
|
|
/** Icon Right or Left on the input */
|
2020-11-01 16:41:02 +01:00
|
|
|
@Input() icon: string = null;
|
|
|
|
@Input() iconLeft: boolean = false;
|
2021-02-12 12:31:12 +01:00
|
|
|
/** Chip options */
|
2021-01-28 17:33:16 +01:00
|
|
|
@Input() removable: boolean = true;
|
2021-02-12 12:31:12 +01:00
|
|
|
@Input() addExtraChips: boolean = false;
|
2021-02-17 19:44:02 +01:00
|
|
|
@Input() smallChip: boolean = false;
|
|
|
|
@Input() panelWidth: number = 300;
|
|
|
|
@Input() panelClass: string = null;
|
2021-02-24 17:53:31 +01:00
|
|
|
@Input() showOptionsOnEmpty: boolean = true;
|
2022-01-11 12:15:37 +01:00
|
|
|
@Input() validators: ValidatorFn[] | ValidatorFn;
|
2021-02-12 12:31:12 +01:00
|
|
|
@Output() focusEmitter: EventEmitter<boolean> = new EventEmitter<boolean>();
|
2021-03-03 19:04:53 +01:00
|
|
|
/** LogoUrl information */
|
|
|
|
public secure: boolean = true;
|
2021-02-12 12:31:12 +01:00
|
|
|
/** Internal basic information */
|
2020-06-05 16:48:12 +02:00
|
|
|
public required: boolean = false;
|
2019-12-23 14:59:56 +01:00
|
|
|
private initValue: any;
|
2021-03-19 15:23:47 +01:00
|
|
|
/** Chips && Autocomplete*/
|
2021-02-12 12:31:12 +01:00
|
|
|
public filteredOptions: Observable<Option[]>;
|
|
|
|
public searchControl: FormControl;
|
2020-10-19 11:06:23 +02:00
|
|
|
private subscriptions: any[] = [];
|
2022-01-11 16:20:05 +01:00
|
|
|
@Input() tooltip: boolean = true;
|
2021-02-12 12:31:12 +01:00
|
|
|
@ViewChild('select') select: MatSelect;
|
|
|
|
@ViewChild('searchInput') searchInput: ElementRef;
|
|
|
|
focused: boolean = false;
|
2020-10-19 11:06:23 +02:00
|
|
|
|
2021-02-12 12:31:12 +01:00
|
|
|
constructor(private elementRef: ElementRef) {
|
|
|
|
}
|
|
|
|
|
|
|
|
@HostListener('document:click', ['$event'])
|
|
|
|
clickOut(event) {
|
|
|
|
this.focused = !!this.elementRef.nativeElement.contains(event.target);
|
|
|
|
this.focusEmitter.emit(this.focused);
|
2019-12-23 14:59:56 +01:00
|
|
|
}
|
2020-10-19 11:06:23 +02:00
|
|
|
|
2022-02-08 10:52:11 +01:00
|
|
|
ngAfterViewInit() {
|
2020-10-19 11:06:23 +02:00
|
|
|
this.reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
ngOnChanges(changes: SimpleChanges) {
|
2022-01-11 12:15:37 +01:00
|
|
|
if (changes.formControl || changes.validators) {
|
2020-10-19 11:06:23 +02:00
|
|
|
this.reset();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-28 17:33:16 +01:00
|
|
|
get formAsArray(): FormArray {
|
2021-02-12 12:31:12 +01:00
|
|
|
return (<FormArray>this.formControl);
|
2021-01-28 17:33:16 +01:00
|
|
|
}
|
|
|
|
|
2020-10-19 11:06:23 +02:00
|
|
|
reset() {
|
2021-03-03 19:04:53 +01:00
|
|
|
this.secure = true;
|
2020-10-19 11:06:23 +02:00
|
|
|
this.unsubscribe();
|
2019-12-23 14:59:56 +01:00
|
|
|
this.initValue = HelperFunctions.copy(this.formControl.value);
|
2021-03-03 19:04:53 +01:00
|
|
|
if(this.type === 'logoURL') {
|
|
|
|
this.secure = (!this.initValue || this.initValue.includes('https://'));
|
|
|
|
}
|
2021-03-22 16:10:18 +01:00
|
|
|
if (this.type === 'chips' || this.type === 'autocomplete') {
|
2021-04-23 18:25:52 +02:00
|
|
|
if(this.options) {
|
2021-03-22 16:10:18 +01:00
|
|
|
this.filteredOptions = of(this.options);
|
2021-04-23 18:25:52 +02:00
|
|
|
this.searchControl = new FormControl('', this.validators);
|
2021-03-22 16:10:18 +01:00
|
|
|
this.subscriptions.push(this.searchControl.valueChanges.subscribe(value => {
|
|
|
|
setTimeout(() => {
|
|
|
|
this.searchInput.nativeElement.focus();
|
|
|
|
this.searchInput.nativeElement.value = value;
|
|
|
|
},0);
|
|
|
|
}));
|
|
|
|
this.filteredOptions = this.searchControl.valueChanges.pipe(startWith(''),
|
|
|
|
map(option => this.filter(option)));
|
|
|
|
}
|
2021-01-28 17:33:16 +01:00
|
|
|
}
|
2020-10-19 11:06:23 +02:00
|
|
|
if (this.formControl && this.formControl.validator) {
|
2020-06-05 16:48:12 +02:00
|
|
|
let validator = this.formControl.validator({} as AbstractControl);
|
|
|
|
this.required = (validator && validator.required);
|
|
|
|
}
|
2020-10-19 11:06:23 +02:00
|
|
|
this.subscriptions.push(this.formControl.valueChanges.subscribe(value => {
|
2021-02-12 12:31:12 +01:00
|
|
|
value = (value === '') ? null : value;
|
2021-03-03 19:04:53 +01:00
|
|
|
if(this.type === 'logoURL') {
|
|
|
|
this.secure = (!value || value.includes('https://'));
|
|
|
|
}
|
2021-02-12 12:31:12 +01:00
|
|
|
if (this.initValue === value || (this.initValue === '' && value === null)) {
|
2019-12-23 14:59:56 +01:00
|
|
|
this.formControl.markAsPristine();
|
|
|
|
}
|
2021-03-22 16:10:18 +01:00
|
|
|
if(this.searchControl) {
|
|
|
|
this.searchControl.setValue(null);
|
|
|
|
}
|
2020-10-19 11:06:23 +02:00
|
|
|
}));
|
|
|
|
if (!this.formControl.value) {
|
2021-04-22 09:55:54 +02:00
|
|
|
this.formControl.setValue((this.type === "checkbox")?false:'');
|
2020-10-19 11:06:23 +02:00
|
|
|
}
|
2022-02-08 10:52:11 +01:00
|
|
|
if(this.input) {
|
|
|
|
this.input.nativeElement.disabled = this.formControl.disabled;
|
|
|
|
}
|
2020-10-19 11:06:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
unsubscribe() {
|
|
|
|
this.subscriptions.forEach(subscription => {
|
2021-02-12 12:31:12 +01:00
|
|
|
if (subscription instanceof Subscription) {
|
2020-10-19 11:06:23 +02:00
|
|
|
subscription.unsubscribe();
|
|
|
|
}
|
2019-12-23 14:59:56 +01:00
|
|
|
});
|
|
|
|
}
|
2020-10-19 11:06:23 +02:00
|
|
|
|
|
|
|
openSelect() {
|
2021-02-12 12:31:12 +01:00
|
|
|
if (this.select) {
|
2020-10-19 11:06:23 +02:00
|
|
|
this.select.open();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-23 14:59:56 +01:00
|
|
|
ngOnDestroy(): void {
|
2020-10-19 11:06:23 +02:00
|
|
|
this.unsubscribe();
|
2019-12-23 14:59:56 +01:00
|
|
|
}
|
2020-10-19 11:06:23 +02:00
|
|
|
|
2019-12-23 14:59:56 +01:00
|
|
|
stopPropagation() {
|
2020-10-19 11:06:23 +02:00
|
|
|
event.stopPropagation();
|
2019-12-23 14:59:56 +01:00
|
|
|
}
|
2021-01-28 17:33:16 +01:00
|
|
|
|
|
|
|
removed(index: number) {
|
|
|
|
this.formAsArray.removeAt(index);
|
|
|
|
this.formAsArray.markAsDirty();
|
2021-02-12 12:31:12 +01:00
|
|
|
this.searchControl.setValue('');
|
|
|
|
this.stopPropagation();
|
2021-01-28 17:33:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
selected(event: MatAutocompleteSelectedEvent): void {
|
|
|
|
this.formAsArray.push(new FormControl(event.option.value));
|
|
|
|
this.formAsArray.markAsDirty();
|
|
|
|
this.searchControl.setValue('');
|
2021-02-12 12:31:12 +01:00
|
|
|
this.stopPropagation();
|
2021-01-28 17:33:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private filter(value: string): Option[] {
|
2021-03-19 15:23:47 +01:00
|
|
|
let options = this.options;
|
|
|
|
if(this.type === "chips") {
|
2021-04-22 10:41:22 +02:00
|
|
|
options = options.filter(option => !this.formAsArray.value.find(value => HelperFunctions.equals(option.value, value)));
|
2021-03-19 15:23:47 +01:00
|
|
|
}
|
|
|
|
if ((!value || value.length == 0)) {
|
|
|
|
return (this.showOptionsOnEmpty)?options:[];
|
2021-01-28 17:33:16 +01:00
|
|
|
}
|
|
|
|
const filterValue = value.toString().toLowerCase();
|
|
|
|
return options.filter(option => option.label.toLowerCase().indexOf(filterValue) != -1);
|
|
|
|
}
|
2021-02-12 12:31:12 +01:00
|
|
|
|
|
|
|
add(event: MatChipInputEvent) {
|
2021-04-23 18:25:52 +02:00
|
|
|
if (this.addExtraChips && event.value && this.searchControl.valid) {
|
2021-02-12 12:31:12 +01:00
|
|
|
this.stopPropagation();
|
2021-04-23 18:25:52 +02:00
|
|
|
this.formAsArray.push(new FormControl(event.value, this.validators));
|
2021-02-12 12:31:12 +01:00
|
|
|
this.formAsArray.markAsDirty();
|
|
|
|
this.searchControl.setValue('');
|
|
|
|
this.searchInput.nativeElement.value = '';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
getLabel(value: any) {
|
2021-04-21 17:28:35 +02:00
|
|
|
let option = this.options.find(option => HelperFunctions.equals(option.value, value));
|
2021-02-12 12:31:12 +01:00
|
|
|
return (option) ? option.label : value;
|
|
|
|
}
|
2021-03-19 15:23:47 +01:00
|
|
|
|
2021-03-22 16:10:18 +01:00
|
|
|
resetSearch(event: any) {
|
2021-03-19 15:23:47 +01:00
|
|
|
event.stopPropagation();
|
2021-03-22 16:10:18 +01:00
|
|
|
this.searchControl.setValue('');
|
2022-01-11 12:15:37 +01:00
|
|
|
this.formControl.markAsDirty(); // TODO check - should it also become dirty on addition?
|
2021-03-19 15:23:47 +01:00
|
|
|
this.formControl.setValue(null);
|
|
|
|
}
|
2019-12-23 14:59:56 +01:00
|
|
|
}
|