2021-02-12 12:31:12 +01:00
|
|
|
import {
|
|
|
|
Component, ElementRef,
|
|
|
|
EventEmitter, HostListener,
|
|
|
|
Input,
|
|
|
|
OnChanges,
|
|
|
|
OnDestroy,
|
|
|
|
OnInit,
|
|
|
|
Output,
|
|
|
|
SimpleChanges,
|
|
|
|
ViewChild
|
|
|
|
} from "@angular/core";
|
2021-01-28 17:33:16 +01:00
|
|
|
import {AbstractControl, FormArray, FormControl} 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-02-12 12:31:12 +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">
|
2020-11-01 16:41:02 +01:00
|
|
|
<span class="uk-text-muted" [ngClass]="iconLeft?('left'):'right'">
|
|
|
|
<icon [name]="icon"></icon>
|
|
|
|
</span>
|
|
|
|
</ng-template>
|
2020-12-23 17:06:31 +01:00
|
|
|
<ng-template [ngIf]="formControl.disabled">
|
|
|
|
<span class="uk-text-muted left">
|
|
|
|
<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"
|
|
|
|
[attr.uk-tooltip]="formControl.disabled?'title: This field is not editable; pos: bottom-left':''">
|
2021-02-19 19:01:03 +01:00
|
|
|
<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"
|
|
|
|
[attr.uk-tooltip]="formControl.disabled?'title: This field is not editable; pos: bottom-left':''">
|
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>
|
2020-10-19 11:06:23 +02:00
|
|
|
<mat-option *ngFor="let option of options" [value]="option.value">
|
|
|
|
{{option.label}}
|
|
|
|
</mat-option>
|
|
|
|
</mat-select>
|
|
|
|
</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"
|
|
|
|
[class.uk-form-danger]="formControl.invalid && formControl.touched" (click)="openSelect()">
|
|
|
|
<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-04 11:17:30 +01:00
|
|
|
<span class="uk-flex uk-flex-middle">
|
|
|
|
<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-02-12 12:31:12 +01:00
|
|
|
<input #searchInput class="uk-width-1-1" [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-01-28 17:33:16 +01:00
|
|
|
<mat-option *ngFor="let option of filteredOptions | async" [value]="option.value">
|
|
|
|
{{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
|
|
|
})
|
2020-10-19 11:06:23 +02:00
|
|
|
export class InputComponent implements OnInit, OnDestroy, 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-03 19:04:53 +01:00
|
|
|
@Input('type') type: 'text' | 'URL' | 'logoURL' | 'textarea' | 'select' | 'checkbox' | 'chips' = 'text';
|
2019-12-23 14:59:56 +01:00
|
|
|
@Input('label') label: string;
|
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 */
|
2019-12-23 14:59:56 +01: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
|
|
|
/** Custom messages */
|
2020-11-11 10:19:12 +01:00
|
|
|
@Input() warning: string = null;
|
2021-01-21 16:15:53 +01:00
|
|
|
@Input() note: string = null;
|
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;
|
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-02-12 12:31:12 +01:00
|
|
|
/** Chips */
|
|
|
|
public filteredOptions: Observable<Option[]>;
|
|
|
|
public searchControl: FormControl;
|
2020-10-19 11:06:23 +02:00
|
|
|
private subscriptions: any[] = [];
|
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
|
|
|
|
2019-12-23 14:59:56 +01:00
|
|
|
ngOnInit(): void {
|
2020-10-19 11:06:23 +02:00
|
|
|
this.reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
ngOnChanges(changes: SimpleChanges) {
|
2021-02-12 12:31:12 +01:00
|
|
|
if (changes.formControl) {
|
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-02-12 12:31:12 +01:00
|
|
|
if (this.options && this.type === 'chips') {
|
2021-01-28 17:33:16 +01:00
|
|
|
this.filteredOptions = of(this.options);
|
|
|
|
this.searchControl = new FormControl('');
|
|
|
|
this.filteredOptions = this.searchControl.valueChanges.pipe(startWith(''),
|
|
|
|
map(option => this.filter(option)));
|
|
|
|
}
|
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();
|
|
|
|
}
|
2020-10-19 11:06:23 +02:00
|
|
|
}));
|
|
|
|
if (!this.formControl.value) {
|
|
|
|
this.formControl.setValue('');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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.searchInput.nativeElement.focus();
|
|
|
|
this.searchInput.nativeElement.value = '';
|
|
|
|
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.searchInput.nativeElement.focus();
|
2021-01-28 17:33:16 +01:00
|
|
|
this.searchInput.nativeElement.value = '';
|
2021-02-12 12:31:12 +01:00
|
|
|
this.stopPropagation();
|
2021-01-28 17:33:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private filter(value: string): Option[] {
|
2021-02-12 12:31:12 +01:00
|
|
|
let options = this.options.filter(option => !this.formAsArray.value.find(value => option.value === value));
|
2021-02-24 17:53:31 +01:00
|
|
|
if ((!value || value.length == 0) && !this.showOptionsOnEmpty) {
|
2021-02-18 13:28:56 +01:00
|
|
|
return [];
|
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) {
|
|
|
|
if (this.addExtraChips && event.value) {
|
|
|
|
this.stopPropagation();
|
|
|
|
this.formAsArray.push(new FormControl(event.value));
|
|
|
|
this.formAsArray.markAsDirty();
|
|
|
|
this.searchControl.setValue('');
|
|
|
|
this.searchInput.nativeElement.value = '';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
getLabel(value: any) {
|
|
|
|
let option = this.options.find(option => option.value === value);
|
|
|
|
return (option) ? option.label : value;
|
|
|
|
}
|
2019-12-23 14:59:56 +01:00
|
|
|
}
|