openaire-library/sharedComponents/input/input.component.ts

859 lines
35 KiB
TypeScript
Raw Normal View History

import {
AfterViewInit,
ChangeDetectorRef,
Component,
ElementRef,
EventEmitter,
HostListener,
Input,
OnChanges,
OnDestroy,
OnInit,
Output,
QueryList,
SimpleChanges,
ViewChild,
ViewChildren
} from "@angular/core";
import {AbstractControl, UntypedFormArray, UntypedFormControl, UntypedFormGroup, ValidatorFn} from "@angular/forms";
import {HelperFunctions} from "../../utils/HelperFunctions.class";
import {BehaviorSubject, Subscription} from "rxjs";
import {EnvProperties} from "../../utils/properties/env-properties";
import {properties} from "../../../../environments/environment";
import {ClickEvent} from "../../utils/click/click-outside-or-esc.directive";
import {LayoutService} from "../../dashboard/sharedComponents/sidebar/layout.service";
import {MobileDropdownComponent} from "../../utils/mobile-dropdown/mobile-dropdown.component";
import {MatDatepicker} from "@angular/material/datepicker";
export type InputType =
2023-06-30 13:54:25 +02:00
'text'
| 'URL'
| 'logoURL'
| 'autocomplete'
| 'autocomplete_soft'
| 'textarea'
| 'select'
| 'chips'
| 'year-range'
| 'date';
export interface Option {
icon?: string,
iconClass?: string,
value: any,
[Library | new-theme]: Redesign of project landing page and small upadates on result landing and common components. 1. addThis.component.ts: Updated social icons of addThis. 2. landing-header.component.ts: Added "uk-text-small" in subtitle. 3. metrics.component.ts: Added method "clickedMetrics()" to set "metricsClicked" | Added metricsClicked and metrics.infos conditions for loading table or iframes | Added customContainerClass and customIframeClass in <i-frame>. 4. project.component: Updated project landing page according to UI redesign. 5. project.module.ts: Import LoadingModule, IconsModule, InputModule. 6. project.service.ts: Use parsingFunctions.parseDescription. 7. projectService.module.ts: iconsService.registerIcons([link, graph]). 8. resultLanding.component.html: Small fixes in ui | Added right arrow on tooltips of actions that are links to other pages | Display of description updated as innerHtml (#7345). 9. orcid-work.component.ts: Added flex=true in <icon>. 10. input.component.ts: Added disabled?: boolean in Option interface and show disabled options as muted. 11. projectInfo.ts: Updated description to be string[] instead of string. 12. iframe.component.ts: Added @Input() customContainerClass: string = ""; and @Input() customIframeClass: string = ""; 13. search-tab.component.ts: Updated according to redesign | Update "getEntityName()" to return values from OpenaireEntities. 14. tabs.component.ts: Added "uk-height-1-1" in custom tabs | In #mytabs::before set left:0.
2022-04-28 11:13:06 +02:00
label: string,
tooltip?: string,
disabled?: boolean,
hidden?: boolean
}
export interface Placeholder {
label: string,
static?: boolean
}
export interface YearRange {
from: ControlConfiguration,
to: ControlConfiguration
}
export interface ControlConfiguration {
control: string,
placeholder: string
}
declare var UIkit;
/**
* Autocomplete soft allows values that are not listed in options list. In order to work as expected
* avoid providing options with different label and value.
*
* */
@Component({
selector: '[dashboard-input], [input]',
template: `
2023-06-30 13:54:25 +02:00
<div *ngIf="formControl" [id]="id">
<div class="input-wrapper" [class.disabled]="formControl.disabled" [class.opened]="opened"
[class.focused]="focused" [ngClass]="inputClass" [class.hint]="hint"
[class.active]="!focused && (formAsControl?.value || selectable || type === 'date' || formAsArray?.length > 0 || getLabel(formAsControl?.value) || yearRangeActive)"
2023-06-30 13:54:25 +02:00
[class.danger]="(formControl.invalid && (formControl.touched || !!searchControl?.touched)) || (!!searchControl?.invalid && !!searchControl?.touched)">
<div #inputBox class="input-box" [class.select]="selectable || type ==='date'" click-outside-or-esc
2023-06-30 13:54:25 +02:00
[class.static]="placeholderInfo?.static" (clickOutside)="click($event)">
<div *ngIf="!placeholderInfo?.static && placeholderInfo?.label" class="placeholder">
<label>{{placeholderInfo.label}} <sup *ngIf="required">*</sup></label>
</div>
<div class="uk-flex" [class.uk-flex-middle]="type !== 'textarea'"
[attr.uk-tooltip]="(tooltip && !focused && type !== 'chips' && type !== 'textarea' && (formControl.value || hint || placeholderInfo?.label))?('title: ' + (formControl.value ?getTooltip(formControl.value):(hint?hint:placeholderInfo?.label)) + '; delay: 500; pos: bottom-left'):null">
<ng-template [ngIf]="type === 'text' || type === 'URL' || type === 'logoURL'">
<input #input class="input" [attr.placeholder]="placeholderInfo?.static?placeholderInfo.label:hint"
[type]="password?'password':'text'" [formControl]="formAsControl"
[class.uk-text-truncate]="!focused">
</ng-template>
<ng-template [ngIf]="type === 'textarea'">
<textarea #textArea class="input" [attr.placeholder]="placeholderInfo?.static?placeholderInfo.label:hint"
[rows]="rows" [formControl]="formAsControl"></textarea>
2023-06-30 13:54:25 +02:00
</ng-template>
<ng-template [ngIf]="type === 'select'">
<ng-container *ngIf="placeholderInfo?.static">
<div *ngIf="!getLabel(formControl.value)"
class="input placeholder uk-width-expand uk-text-truncate" [class.uk-disabled]="formControl.disabled">{{placeholderInfo.label}}</div>
<div *ngIf="getLabel(formControl.value)"
class="input uk-width-expand uk-text-truncate" [class.uk-disabled]="formControl.disabled">{{getLabel(formControl.value)}}</div>
</ng-container>
<ng-container *ngIf="!placeholderInfo?.static">
<div *ngIf="!getLabel(formControl.value)"
class="input uk-width-expand uk-text-truncate" [class.uk-disabled]="formControl.disabled">{{noValueSelected}}</div>
<div *ngIf="getLabel(formControl.value)"
class="input uk-width-expand uk-text-truncate" [class.uk-disabled]="formControl.disabled">{{getLabel(formControl.value)}}</div>
</ng-container>
</ng-template>
<ng-template [ngIf]="type === 'autocomplete'">
<input *ngIf="focused" [attr.placeholder]="placeholderInfo?.static?placeholderInfo.label:hint"
#searchInput class="input" [formControl]="searchControl" [class.uk-text-truncate]="!focused">
<ng-container *ngIf="!focused && !selectable">
<div *ngIf="!getLabel(formControl.value)"
class="input placeholder uk-text-truncate" [class.uk-disabled]="formControl.disabled">{{placeholderInfo?.static ? placeholderInfo.label : getLabel(formAsControl.value)}}</div>
<div *ngIf="getLabel(formControl.value)"
class="input uk-text-truncate" [class.uk-disabled]="formControl.disabled">{{getLabel(formAsControl.value)}}</div>
</ng-container>
<ng-container *ngIf="!focused && selectable">
<div *ngIf="!getLabel(formControl.value)" class="input uk-text-truncate" [class.uk-disabled]="formControl.disabled">{{noValueSelected}}</div>
<div *ngIf="getLabel(formControl.value)"
class="input uk-text-truncate" [class.uk-disabled]="formControl.disabled">{{getLabel(formControl.value)}}</div>
</ng-container>
</ng-template>
<ng-template [ngIf]="type === 'autocomplete_soft'">
<input #input class="input" [attr.placeholder]="placeholderInfo?.static?placeholderInfo.label:hint"
[formControl]="formAsControl" [class.uk-text-truncate]="!focused">
</ng-template>
<ng-template [ngIf]="type === 'chips'">
<div class="uk-grid uk-grid-small uk-grid-row-collapse uk-overflow-auto uk-width-expand"
[class.uk-flex-nowrap]="noWrap" [class.uk-overflow-auto]="noWrap" uk-grid>
<div *ngFor="let chip of formAsArray.controls; let i=index" #chip
[class.uk-hidden]="!focused && i > visibleChips - 1"
class="chip">
<div class="uk-label uk-label-small uk-text-transform-none uk-flex uk-flex-middle"
[attr.uk-tooltip]="(tooltip)?('title: ' + getLabel(chip.value) + '; delay: 500; pos: bottom-left'):null">
<span class="uk-text-truncate uk-width-expand">{{getLabel(chip.value)}}</span>
<icon *ngIf="focused" (click)="remove(i, $event)"
class="uk-link-text uk-margin-small-left clickable" [flex]="true"
name="close" ratio="0.7"></icon>
</div>
</div>
<div *ngIf="searchControl && (focused || formAsArray.length === 0)" #chip
class="uk-width-expand search-input uk-flex uk-flex-column uk-flex-center">
<input #searchInput class="input" [class.search]="searchControl.value"
[attr.placeholder]="placeholderInfo?.static?placeholderInfo.label:hint"
[formControl]="searchControl" [class.uk-text-truncate]="!focused">
</div>
<div *ngIf="!focused && formAsArray.length > visibleChips"
class="uk-width-expand uk-flex uk-flex-column uk-flex-center more">
+ {{(formAsArray.length - visibleChips)}} more
</div>
</div>
</ng-template>
<ng-template [ngIf]="type === 'year-range' && yearRange && formAsGroup">
<div class="uk-width-2-5">
<input #input class="input uk-text-center uk-text-truncate" [attr.placeholder]="yearRange.from.placeholder"
maxlength="4" (click)="activeIndex = 0;$event.preventDefault()" [formControl]="getFormByName(yearRange.from.control)">
</div>
<div class="uk-width-1-5 uk-text-center">-</div>
<div class="uk-width-2-5">
<input #input class="input uk-text-center uk-text-truncate" [attr.placeholder]="yearRange.to.placeholder"
maxlength="4" (click)="activeIndex = 1;$event.preventDefault()" [formControl]="getFormByName(yearRange.to.control)">
</div>
</ng-template>
<ng-template [ngIf]="type === 'date'">
<div *ngIf="!formAsControl.getRawValue()" class="input uk-text-truncate" [class.uk-disabled]="formControl.disabled">{{selectADate}}</div>
<div *ngIf="formAsControl.getRawValue()" class="input uk-text-truncate" [class.uk-disabled]="formControl.disabled">{{formAsControl.getRawValue() | date: 'dd-MM-yyyy'}}</div>
</ng-template>
<div *ngIf="(formControl.disabled && disabledIcon) || icon || (selectable && selectArrow) || type === 'autocomplete' || searchable || type === 'date'"
2023-06-30 13:54:25 +02:00
class="uk-margin-small-left icon">
<icon *ngIf="formControl.disabled && disabledIcon" [name]="disabledIcon" [flex]="true"></icon>
<ng-template [ngIf]="formControl.enabled">
<icon *ngIf="!searchControl?.value && icon" [name]="icon" [flex]="true"></icon>
<icon *ngIf="!icon && selectable && selectArrow" [name]="selectArrow" [flex]="true"></icon>
<button *ngIf="focused && type === 'autocomplete' && (!selectable || searchControl.value)" class="uk-close uk-icon"
2023-06-30 13:54:25 +02:00
(click)="resetSearch($event)">
<icon [flex]="true" name="close"></icon>
</button>
<button *ngIf="(!focused && type === 'autocomplete' && !selectable) ||
(type !== 'autocomplete' && !searchControl?.value && !!formControl?.value && (searchable || !selectable)) ||
(type === 'date' && formAsControl?.value)"
class="uk-close uk-icon" (click)="resetValue($event);">
2023-06-30 13:54:25 +02:00
<icon [flex]="true" name="close"></icon>
</button>
</ng-template>
</div>
<!-- use action-icon class in order to apply css in your icon button-->
<ng-content select="[action]"></ng-content>
</div>
<div class="tools">
<ng-content select="[tools]"></ng-content>
</div>
</div>
</div>
<div *ngIf="!mobile && type === 'date' && opened" class="uk-dropdown" #calendarBox
2023-08-22 16:08:46 +02:00
uk-dropdown="pos: bottom-left; mode: none; flip: false ; shift: false" [attr.target]="'#' + id" [attr.boundary]="'#' + id" (click)="$event.stopPropagation()">
<mat-calendar [selected]="selectedDate" [startAt]="selectedDate" (selectedChange)="dateChanged($event)"></mat-calendar>
</div>
<mobile-dropdown *ngIf="mobile && type === 'date' && opened" (onClose)="focus(false)" #mobileDropdown>
<mat-calendar [selected]="selectedDate" [startAt]="selectedDate" (selectedChange)="dateChanged($event)"></mat-calendar>
</mobile-dropdown>
<div *ngIf="!mobile && filteredOptions && filteredOptions.length > 0 && opened" class="options uk-dropdown" #optionBox
2023-08-22 16:08:46 +02:00
uk-dropdown="mode: none; stretch: true; flip: false; shift: false" [attr.boundary]="'#' + id">
2023-06-30 13:54:25 +02:00
<ul class="uk-nav uk-dropdown-nav">
<li *ngFor="let option of filteredOptions; let i=index" [class.uk-hidden]="option.hidden"
[class.uk-active]="(formControl.value === option.value) || selectedIndex === i">
<a (click)="selectOption(option, $event)"
[class]="option.disabled ? 'uk-disabled uk-text-muted' : ''">
<span [attr.uk-tooltip]="(tooltip)?('title: ' + (option.tooltip ? option.tooltip : option.label) + '; delay: 500; pos:bottom-left'):null">{{option.label}}</span>
2023-06-30 13:54:25 +02:00
</a>
</li>
</ul>
</div>
<mobile-dropdown *ngIf="mobile && filteredOptions && filteredOptions.length > 0 && opened" (onClose)="focus(false)" #mobileDropdown>
<div *ngIf="placeholderInfo" class="uk-text-emphasis uk-text-bolder uk-text-center uk-padding-small uk-padding-remove-vertical uk-text-uppercase">
{{placeholderInfo.label}}
</div>
<div class="uk-padding uk-padding-remove-horizontal">
<ul class="uk-nav uk-nav-default">
<li *ngFor="let option of filteredOptions; let i=index" [class.uk-hidden]="option.hidden"
[class.uk-active]="formControl.value === option.value">
<a (click)="selectOption(option, $event)"
[class]="option.disabled ? 'uk-disabled uk-text-muted' : ''">
<span>{{option.label}}</span>
</a>
</li>
</ul>
</div>
</mobile-dropdown>
</div>
2023-10-12 12:10:02 +02:00
<span *ngIf="formControl?.invalid && formControl?.touched" class="uk-text-small uk-text-danger">
<span *ngIf="errors?.error">{{errors?.error}}</span>
<span *ngIf="type === 'URL' || type === 'logoURL'">Please provide a valid URL (e.g. https://example.com)</span>
</span>
2023-06-30 13:54:25 +02:00
<span class="uk-text-small uk-text-danger">
<ng-content select="[error]"></ng-content>
</span>
2023-06-30 13:54:25 +02:00
<span *ngIf="formControl?.valid" class="uk-text-small uk-text-warning uk-margin-xsmall-top">
<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>
2023-06-30 13:54:25 +02:00
<i class="uk-text-small uk-text-meta uk-margin-xsmall-top">
<ng-content select="[note]"></ng-content>
</i>
`
})
export class InputComponent implements OnInit, OnDestroy, AfterViewInit, OnChanges {
private static INPUT_COUNTER: number = 0;
/** Basic information */
@Input('formInput') formControl: AbstractControl;
@Input('type') type: InputType = 'text';
@Input() password: boolean = false;
@Input() validators: ValidatorFn[] | ValidatorFn;
@Input() disabled: boolean = false;
@Input() disabledIcon: string = 'lock';
@Input() value: any | any[];
@Output() valueChange = new EventEmitter<any | any[]>();
@Input() hint: string;
@Input() tooltip: boolean = false;
@Input() searchable: boolean = false;
/** Text */
@ViewChildren('input') input: QueryList<ElementRef>;
/** Textarea options */
@ViewChild('textArea') textArea: ElementRef;
@Input('rows') rows: number = 3;
/** Select | Autocomplete | chips available options */
@Input() selectArrow: string = 'arrow_drop_down';
@Input() selectedIndex: number = 0;
@Input() selectable: boolean = false;
2022-06-08 13:19:40 +02:00
@Input() noValueSelected: string = 'No option selected';
/** Chips && Autocomplete*/
public filteredOptions: Option[] = [];
public searchControl: UntypedFormControl;
public activeElement: BehaviorSubject<ElementRef> = new BehaviorSubject<ElementRef>(null);
/** Use modifier's class(es) to change view of your Input */
@Input() inputClass: string = 'flat';
/** Icon on the input */
@Input() icon: string = null;
/** Chip options */
@Input() addExtraChips: boolean = false;
@Input() showOptionsOnEmpty: boolean = true;
@Input() visibleChips: number = 1;
@Input() separators: string[] = [];
@Input() noWrap: boolean = false;
/** Year Range Configuration */
@Input() yearRange: YearRange;
public activeIndex: 0 | 1 | null = null;
/** Date Configuration*/
@Input() selectADate: string = 'Select a date';
public selectedDate: Date;
@Input() visibleRows: number = -1;
@Input() extendEnter: () => void = null;
@Output() focusEmitter: EventEmitter<boolean> = new EventEmitter<boolean>();
/** LogoUrl information */
public secure: boolean = true;
/** Internal basic information */
public id: string;
public placeholderInfo: Placeholder = {label: '', static: true};
public required: boolean = false;
public focused: boolean = false;
public opened: boolean = false;
public mobile: boolean = false;
public properties: EnvProperties = properties;
private initValue: any;
private optionsArray: Option[] = [];
2022-06-20 12:11:21 +02:00
private optionsBreakpoint: number = 6;
private subscriptions: any[] = [];
@ViewChild('inputBox') inputBox: ElementRef;
@ViewChild('optionBox') optionBox: ElementRef;
@ViewChild('calendarBox') calendarBox: ElementRef;
@ViewChild('mobileDropdown') mobileDropdown: MobileDropdownComponent;
@ViewChild('searchInput') searchInput: ElementRef;
@ViewChildren('chip') chips: QueryList<ElementRef>;
@ViewChild('datepicker') datepicker: MatDatepicker<any>;
2023-06-30 13:54:25 +02:00
@Input()
set placeholder(placeholder: string | Placeholder) {
if(this.type === 'year-range') {
this.placeholderInfo = null;
} else if (typeof placeholder === 'string') {
this.placeholderInfo = {label: placeholder, static: false};
} else {
if (placeholder.static && (this.type === 'autocomplete' || this.hint)) {
placeholder.static = false;
console.debug('Static placeholder is not available in this type of input and if hint is available.');
}
this.placeholderInfo = placeholder;
}
}
2023-06-30 13:54:25 +02:00
@Input()
set options(options: (Option | string | number) []) {
this.optionsArray = options.map(option => {
if(option === null) {
return {
label: this.noValueSelected,
value: ''
};
} else if (typeof option === 'string' || typeof option === 'number') {
return {
label: option.toString(),
value: option
};
} else {
return option;
}
});
if (!this.tooltip) {
this.tooltip = this.optionsArray.length > 0;
}
if (this.type === "select") {
2022-06-20 12:11:21 +02:00
if (this.optionsArray.length > this.optionsBreakpoint) {
this.type = 'autocomplete';
this.showOptionsOnEmpty = true;
this.icon = this.selectArrow;
}
this.selectable = true;
}
}
2023-06-30 13:54:25 +02:00
constructor(private elementRef: ElementRef, private cdr: ChangeDetectorRef, private layoutService: LayoutService) {
if (elementRef.nativeElement.hasAttribute('dashboard-input') && this.properties.environment === "development") {
console.warn("'dashboard-input' selector is deprecated; use 'input' instead.");
}
}
2023-06-30 13:54:25 +02:00
@HostListener('window:keydown.arrowUp', ['$event'])
arrowUp(event: KeyboardEvent) {
if (this.opened && this.optionBox) {
event.preventDefault();
if (this.selectedIndex > 0) {
this.selectedIndex--;
this.optionBox.nativeElement.scrollBy(0, -34);
}
}
}
2023-06-30 13:54:25 +02:00
@HostListener('window:keydown.arrowDown', ['$event'])
arrowDown(event: KeyboardEvent) {
if (this.opened && this.optionBox) {
event.preventDefault();
if (this.selectedIndex < (this.filteredOptions.length - 1)) {
this.selectedIndex++;
this.optionBox.nativeElement.scrollBy(0, 34);
}
}
}
2023-06-30 13:54:25 +02:00
@HostListener('window:keydown.arrowLeft', ['$event'])
arrowLeft(event: KeyboardEvent) {
if (this.type === 'chips' && this.focused) {
if (this.activeElement.getValue()) {
event.preventDefault();
let index = this.chips.toArray().indexOf(this.activeElement.getValue());
if (index > 0) {
this.activeElement.next(this.chips.get(index - 1));
return;
}
}
}
}
2023-06-30 13:54:25 +02:00
@HostListener('window:keydown.arrowRight', ['$event'])
arrowRight(event: KeyboardEvent) {
if (this.type === 'chips' && this.focused) {
if (this.activeElement.getValue()) {
event.preventDefault();
let index = this.chips.toArray().indexOf(this.activeElement.getValue());
if (index < this.chips.length - 1) {
this.activeElement.next(this.chips.get(index + 1));
return;
}
}
}
}
2023-06-30 13:54:25 +02:00
@HostListener('window:keydown.enter', ['$event'])
enter(event: KeyboardEvent) {
if (this.extendEnter) {
this.extendEnter();
}
if (this.opened && this.optionBox) {
event.preventDefault();
if (this.filteredOptions[this.selectedIndex]) {
this.selectOption(this.filteredOptions[this.selectedIndex], event);
}
this.open(false);
event.stopPropagation();
} else {
this.focus(false, event);
}
}
2023-06-30 13:54:25 +02:00
@HostListener('keydown', ['$event'])
onKeyDown(event: KeyboardEvent) {
if (this.separators.includes(event.key) || this.separators.includes(event.key.toLowerCase())) {
event.preventDefault();
this.add(event, true);
}
}
2023-06-30 13:54:25 +02:00
click(event: ClickEvent) {
this.focus(!event.clicked);
}
2023-06-30 13:54:25 +02:00
ngOnInit() {
this.subscriptions.push(this.layoutService.isMobile.subscribe(mobile => {
this.mobile = mobile;
this.cdr.detectChanges();
}));
InputComponent.INPUT_COUNTER++;
this.id = 'input-' + InputComponent.INPUT_COUNTER;
if (!this.formControl) {
if (Array.isArray(this.value)) {
this.formControl = new UntypedFormArray([]);
this.value.forEach(value => {
this.formAsArray.push(new UntypedFormControl(value, this.validators));
});
} else {
this.formControl = new UntypedFormControl(this.value, this.validators);
}
if (this.disabled) {
this.formControl.disable();
}
}
this.activeElement.subscribe(element => {
if (element) {
element.nativeElement.scrollIntoView({behavior: 'smooth'});
}
})
}
2023-06-30 13:54:25 +02:00
ngAfterViewInit() {
this.reset();
}
2023-06-30 13:54:25 +02:00
ngOnChanges(changes: SimpleChanges) {
if (this.formControl) {
if (changes.value && changes.value.currentValue !== changes.value.previousValue) {
this.formControl.setValue(this.value);
}
if (changes.validators) {
this.updateValidators();
}
if (changes.formControl || changes.validators || changes.options) {
this.reset();
}
if (changes.disabled) {
if (this.disabled) {
this.formControl.disable();
} else {
this.formControl.enable();
}
}
}
}
2023-06-30 13:54:25 +02:00
ngOnDestroy(): void {
this.unsubscribe();
}
2023-06-30 13:54:25 +02:00
getFormByName(name: string): UntypedFormControl {
if (this.formControl instanceof UntypedFormGroup) {
return <UntypedFormControl>this.formControl.get(name);
} else {
return null;
}
}
2023-06-30 13:54:25 +02:00
get formAsGroup(): UntypedFormGroup {
if (this.formControl instanceof UntypedFormGroup) {
return this.formControl;
} else {
return null;
}
}
2023-06-30 13:54:25 +02:00
get formAsControl(): UntypedFormControl {
if (this.formControl instanceof UntypedFormControl) {
return this.formControl;
} else {
return null;
}
}
2023-06-30 13:54:25 +02:00
get formAsArray(): UntypedFormArray {
if (this.formControl instanceof UntypedFormArray) {
return this.formControl;
} else {
return null;
}
}
2023-06-30 13:54:25 +02:00
get yearRangeActive(): boolean {
if(this.yearRange) {
return this.formAsGroup && (this.getFormByName(this.yearRange.from.control)?.value || this.getFormByName(this.yearRange.to.control)?.value);
}
return false;
}
2023-06-30 13:54:25 +02:00
get errors(): any {
if(this.formAsGroup) {
return (this.formAsGroup.errors
2023-06-30 13:54:25 +02:00
?this.formAsGroup.errors:(this.getFormByName(this.yearRange.from.control).errors
?this.getFormByName(this.yearRange.from.control).errors:this.getFormByName(this.yearRange.to.control).errors));
} else if(this.formAsControl) {
return this.formAsControl.errors;
} else if(this.searchControl) {
return this.searchControl.errors;
} else {
return null;
}
}
2023-06-30 13:54:25 +02:00
reset() {
this.secure = true;
this.unsubscribe();
this.initValue = HelperFunctions.copy(this.formControl.value);
if (this.type === 'logoURL') {
this.secure = (!this.initValue || this.initValue.includes('https://'));
}
if (this.optionsArray?.length > 0) {
this.filteredOptions = this.filter('');
this.cdr.detectChanges();
}
if (this.type === 'chips' || this.type === 'autocomplete') {
if (!this.searchControl) {
this.searchControl = new UntypedFormControl('', this.validators);
}
this.subscriptions.push(this.searchControl.valueChanges.subscribe(value => {
this.filteredOptions = this.filter(value);
this.cdr.detectChanges();
if (this.focused) {
this.open(true);
setTimeout(() => {
if(this.searchInput) {
this.searchInput.nativeElement.focus();
this.searchInput.nativeElement.value = value;
}
}, 0);
}
}));
}
if (this.formAsControl?.validator || this.formAsArray?.validator) {
let validator = this.formControl.validator({} as AbstractControl);
this.required = (validator && validator.required);
}
if(this.type === 'date') {
this.selectedDate = this.formAsControl.getRawValue()?new Date(this.formAsControl.getRawValue()):null;
}
this.subscriptions.push(this.formControl.valueChanges.subscribe(value => {
if (this.formControl.enabled) {
if(this.type !== 'year-range') {
value = (value === '') ? null : value;
if (this.type === 'logoURL') {
this.secure = (!value || value.includes('https://'));
}
if (this.initValue === value || (this.initValue === '' && value === null)) {
this.formControl.markAsPristine();
} else {
this.formControl.markAsDirty();
}
if (this.type === 'autocomplete_soft') {
this.filteredOptions = this.filter(value);
this.cdr.detectChanges();
if (this.focused) {
this.open(true);
}
}
if (this.type === 'date') {
this.selectedDate = value?new Date(value):null;
}
}
if ((this.value && value && this.value !== value) || (!this.value && value) || this.value && !value) {
this.valueChange.emit(this.formControl.value);
}
}
}));
if(this.formAsGroup) {
let fromControl = this.formAsGroup.get(this.yearRange.from.control);
this.subscriptions.push(fromControl.valueChanges.subscribe(value => {
let from = this.initValue[this.yearRange.from.control];
if (from === value || (from === '' && value === null)) {
fromControl.markAsPristine();
} else {
fromControl.markAsDirty();
}
if(fromControl.valid) {
if(this.activeIndex === 0 && value) {
this.activeIndex = 1;
this.input.get(this.activeIndex).nativeElement.focus();
}
}
}));
let toControl = this.formAsGroup.get(this.yearRange.to.control);
this.subscriptions.push(toControl.valueChanges.subscribe(value => {
let to = this.initValue[this.yearRange.to.control];
if (to === value || (to === '' && value === null)) {
toControl.markAsPristine();
} else {
toControl.markAsDirty();
}
}));
}
if (this.input) {
this.input.forEach(input => {
input.nativeElement.disabled = this.formControl.disabled;
});
}
}
2023-06-30 13:54:25 +02:00
unsubscribe() {
this.subscriptions.forEach(subscription => {
if (subscription instanceof Subscription) {
subscription.unsubscribe();
}
});
}
2023-06-30 13:54:25 +02:00
updateValidators() {
if (this.formAsArray) {
this.formAsArray.controls.forEach(control => {
control.setValidators(this.validators);
control.updateValueAndValidity();
})
} else {
this.formControl.setValidators(this.validators);
this.formControl.updateValueAndValidity();
}
}
2023-06-30 13:54:25 +02:00
remove(index: number, event) {
if (this.focused) {
this.formAsArray.removeAt(index);
this.formAsArray.markAsDirty();
this.focus(true);
this.searchControl.setValue('');
event.stopPropagation();
}
}
2023-06-30 13:54:25 +02:00
private filter(value: string): Option[] {
let options = this.optionsArray.filter(option => !option.hidden);
if (this.type === "chips") {
options = options.filter(option => !this.formAsArray.value.find(value => HelperFunctions.equals(option.value, value)));
}
if ((!value || value.length == 0)) {
this.selectedIndex = 0;
return (this.showOptionsOnEmpty) ? options : [];
}
const filterValue = value.toString().toLowerCase();
[Library | explore-redesign]: Fixes on FoS and SDGs suggest modal (checked existing subjects, added loading) - In FoS fixed scrolling, search and final response of selected fields. 1. composer.ts & resultLanding.component.ts: Removed "and Technology" from "Fields of Science" typename. 2. resultLanding.component.html: Bind (suggestClicked) to <fos> instead of (feedbackClicked). 3. input.component.ts: [Bug fix] Added check in "filter()" method if option.label. 4. sdg-selection.component.ts: [Bug fix] Set this.loading = false; inside subscribe. 5. fos.component.ts: In title, updated FOS to FoS. 6. sdg-fos-suggest.module.ts: Imported LoadingModule. 7. sdg-fos-suggest.component.ts: Show loading when sending email | Do not initialize <sdg-selection> and <fos-selection> before subjects have value (this way inputs of existing fos/sdgs are checked). 8. fos-selection.component.ts: a. Added @Input() inModal: boolean = false; to set activeSection, when inModal, instead of navigating with fragment. b. [Bug fix] Set this.loading = false; inside subscribe. c. Call this.setObserver(); both in ngOnInit and when keyword changes. d. Updated threshold of observer from 0.25 to 0.1 (large FoS categories were never triggered on scrolling, because less than 25% of their content was visible). e. Updated fosOptions to Map<string, boolean> to track easily checked inputs and updated getSelectedSubjects() accordingly. 9. fos-selection.component.html: In search-input of keyword no options are needed | Updated ngModel of checkbox inputs. 10. cache-interceptor.service.ts: Added check if (!properties.useLongCache) to skip cache.
2023-02-20 19:54:26 +01:00
options = options.filter(option => (option.label && option.label.toLowerCase().indexOf(filterValue) != -1));
this.selectedIndex = options.findIndex(option => option.value === this.formControl.value);
if (this.selectedIndex === -1) {
this.selectedIndex = 0;
}
return options;
}
2023-06-30 13:54:25 +02:00
add(event, addChips = false) {
if (addChips && this.searchControl.value) {
this.splitSearchControl();
} else if (!this.focused) {
this.searchControl.setValue('');
}
}
2023-06-30 13:54:25 +02:00
splitSearchControl() {
let values = [this.searchControl.value];
this.separators.forEach(separator => {
values = ([] as string[]).concat(...values.map(value => {
if (Array.isArray(value)) {
return ([] as string[]).concat(...value.map(element => element.split(separator)));
} else {
return value.split(separator);
}
}));
});
values.forEach(value => {
let control = new UntypedFormControl(value.trim(), this.validators);
if (control.valid) {
this.formAsArray.push(control);
this.formAsArray.markAsDirty();
}
});
if (this.formAsArray.dirty) {
this.activeElement.next(this.chips.last);
this.searchControl.setValue('');
}
}
2023-06-30 13:54:25 +02:00
2022-06-08 13:19:40 +02:00
getLabel(value: any): string {
let option = this.optionsArray.find(option => HelperFunctions.equals(option.value, value));
return (option) ? option.label : (value);
}
2023-06-30 13:54:25 +02:00
getTooltip(value: any): string {
let option = this.optionsArray.find(option => HelperFunctions.equals(option.value, value));
return (option) ? (option.tooltip ? option.tooltip : option.label) : (value);
}
2023-06-30 13:54:25 +02:00
focus(value: boolean, event = null) {
if(!this.activeIndex) {
this.activeIndex = 0;
}
if (this.focused) {
this.formControl.markAsTouched();
}
2023-06-30 13:54:25 +02:00
if(this.formControl.enabled) {
this.focused = value;
this.cdr.detectChanges();
if (this.focused) {
if (this.input?.length > 0) {
this.input.get(this.activeIndex).nativeElement.focus();
} else if (this.textArea) {
this.textArea.nativeElement.focus();
} else if (this.searchInput) {
this.searchInput.nativeElement.focus();
this.activeElement.next(this.chips.last);
}
if (this.selectArrow || this.datepicker) {
2023-06-30 13:54:25 +02:00
this.open(!this.opened);
} else if (this.type !== 'autocomplete' || this.showOptionsOnEmpty || !this.formControl.value) {
this.open(true);
}
} else {
this.activeIndex = null;
this.open(false);
if (this.input) {
this.input.forEach(input => {
input.nativeElement.blur();
})
} else if (this.textArea) {
this.textArea.nativeElement.blur();
} else if (this.searchInput) {
this.searchInput.nativeElement.blur();
}
if (this.searchControl) {
this.add(event, this.addExtraChips);
}
}
2023-06-30 13:54:25 +02:00
this.focusEmitter.emit(this.focused);
}
}
2023-06-30 13:54:25 +02:00
open(value: boolean) {
this.opened = value && this.formControl.enabled;
this.cdr.detectChanges();
if(this.optionBox) {
if (this.opened) {
this.selectedIndex = this.filteredOptions.findIndex(option => option.value === this.formControl.value);
if (this.selectedIndex === -1 && this.type !== 'autocomplete_soft') {
this.selectedIndex = 0;
}
UIkit.dropdown(this.optionBox.nativeElement).show();
} else {
UIkit.dropdown(this.optionBox.nativeElement).hide();
this.focused = false;
}
} else if(this.calendarBox) {
if (this.opened) {
UIkit.dropdown(this.calendarBox.nativeElement).show();
} else {
UIkit.dropdown(this.calendarBox.nativeElement).hide();
this.focused = false;
}
} else if(this.mobileDropdown) {
if(this.opened) {
this.mobileDropdown.open();
if(this.searchInput) {
this.searchInput.nativeElement.blur();
}
} else {
this.mobileDropdown.close();
}
}
}
2023-06-30 13:54:25 +02:00
resetSearch(event: any) {
event.stopPropagation();
this.searchControl.setValue('');
this.focus(true, event);
}
2023-06-30 13:54:25 +02:00
resetValue(event: any) {
event.stopPropagation();
console.log(1)
this.formControl.setValue('');
this.focus(true, event);
}
2023-06-30 13:54:25 +02:00
selectOption(option: Option, event) {
if (this.formControl.enabled) {
if (this.formAsControl) {
this.formAsControl.setValue(option.value);
} else if (this.formAsArray) {
this.formAsArray.push(new UntypedFormControl(option.value));
this.formAsArray.markAsDirty();
event.stopPropagation();
this.focus(true);
this.searchControl.setValue('');
}
}
if(this.mobileDropdown) {
this.mobileDropdown.close();
}
}
dateChanged(event: Date) {
this.focus(false);
this.formAsControl.setValue(event.getTime());
}
}