@WidgetComponent

nm-form-widget-input

File

src/app/shared/widgets/search-app-search-widget/search-app-search-input/search-app-search-widget-input.component.ts

Implements

ControlValueAccessor OnChanges

Metadata

providers { : , : (() => ), : true, }
selector nm-form-widget-input
styleUrls search-app-search-widget-input.component.scss
templateUrl ./search-app-search-widget-input.component.html

Index

Widget inputs
Widget outputs
Properties
Methods
Inputs
Outputs

Constructor

constructor(cd: ChangeDetectorRef)
Parameters :
Name Type Optional
cd ChangeDetectorRef no

Inputs

autofocus
field

Type: FormWidgetField

locale
lookups
placeholder

Outputs

enterPressed $event type: EventEmitter
event $event type: EventEmitter

Methods

Public dateChanged
dateChanged()
Returns : void
dateInputKeydown
dateInputKeydown(event: )
Parameters :
Name Optional
event no
Returns : boolean
Public externalValueChanged
externalValueChanged(newValue: string)
Parameters :
Name Type Optional
newValue string no
Returns : void
getIconForSearchMode
getIconForSearchMode(value: , key: )
Parameters :
Name Optional
value no
key no
Returns : "alpha-s-box-outline" | "alpha-p-box-outline" | "alpha-k-box-outline" | "alpha-c-box-outline"
getLookups
getLookups(key: )
Parameters :
Name Optional
key no
Returns : any[]
isSelectedMenu
isSelectedMenu(value: )
Parameters :
Name Optional
value no
Returns : boolean
ngOnChanges
ngOnChanges(changes: SimpleChanges)
Parameters :
Name Type Optional
changes SimpleChanges no
Returns : void
onEnterPressed
onEnterPressed(event: )
Parameters :
Name Optional
event no
Returns : void
registerOnChange
registerOnChange(fn: )
Parameters :
Name Optional
fn no
Returns : void
registerOnTouched
registerOnTouched(fn: any)
Parameters :
Name Type Optional
fn any no
Returns : void
resetDateValue
resetDateValue(event: )
Parameters :
Name Optional
event no
Returns : boolean
resetValue
resetValue(event: )
Parameters :
Name Optional
event no
Returns : boolean
setDisabledState
setDisabledState(isDisabled: boolean)
Parameters :
Name Type Optional
isDisabled boolean no
Returns : void
setValue
setValue(value: )
Parameters :
Name Optional
value no
Returns : void
Public valueChanged
valueChanged(event?: any)
Parameters :
Name Type Optional
event any yes
Returns : void
writeValue
writeValue(obj: any)
Parameters :
Name Type Optional
obj any no
Returns : void

Properties

Public categoryValue
categoryValue:
Public dateValue
dateValue:
Public displayIcon
displayIcon:
Public filterCtrl
filterCtrl: FormControl
Type : FormControl
Default value : new FormControl()
Public option
option:
propagateChange
propagateChange:
Default value : (_: any) => {}
Public value
value:
import {
  ChangeDetectorRef,
  Component,
  EventEmitter,
  forwardRef,
  Input,
  OnChanges,
  Output,
  SimpleChanges,
} from "@angular/core";
import { FormEventPayload } from "../search-app-search-widget.component";
import { FormWidgetField } from "../../search/advanced/search-advanced.component";
import {
  ControlValueAccessor,
  FormControl,
  NG_VALUE_ACCESSOR,
} from "@angular/forms";
import { deepCopy, toDate } from "../../../components/util/util.service";

@Component({
  selector: "nm-form-widget-input",
  templateUrl: "./search-app-search-widget-input.component.html",
  styleUrls: ["./search-app-search-widget-input.component.scss"],
  providers: [
    {
      provide: NG_VALUE_ACCESSOR,
      useExisting: forwardRef(() => SearchAppSearchWidgetInputComponent),
      multi: true,
    },
  ],
})
export class SearchAppSearchWidgetInputComponent
  implements ControlValueAccessor, OnChanges {
  @Input() public field: FormWidgetField;

  @Input() public lookups;
  @Input() public locale;
  @Input() public placeholder;
  @Input() public autofocus;
  @Output() public event = new EventEmitter<FormEventPayload>();
  @Output() public enterPressed = new EventEmitter();

  public value;
  public dateValue;
  // Value for initializing the category
  public categoryValue;
  public displayIcon;
  public option;

  public filterCtrl: FormControl = new FormControl();

  public externalValueChanged(newValue: string) {
    this.value = deepCopy(newValue);
    this.valueChanged();
  }

  public valueChanged(event?: any) {
    if (this.field.eventField) {
      const payload = event ? event : this.value;
      this.event.next({ field: this.field.identifier, payload });
    } else {
      if (event !== undefined) {
        this.value = event;
      }
      if (this.value) {
        this.displayIcon = this.value.icon;
      } else {
        this.displayIcon = null;
      }

      this.propagateChange(this.value);
    }
  }

  constructor(private cd: ChangeDetectorRef) {}

  public dateChanged() {
    if (this.dateValue) {
      this.value = String(this.dateValue);
    } else {
      this.value = null;
    }
    this.propagateChange(this.value);
  }

  propagateChange = (_: any) => {};

  registerOnChange(fn) {
    this.propagateChange = fn;
  }

  registerOnTouched(fn: any): void {}

  writeValue(obj: any): void {
    this.value = obj;

    if (this.field.type === "DATE") {
      if (this.value) {
        this.dateValue = toDate(this.value);
      } else {
        this.dateValue = null;
      }
    }
    if (!obj) {
      this.categoryValue = {};
      this.categoryValue = null;
    }
    if (this.field.type === "category" && !obj) {
      this.categoryValue = null;
    }
    //    this.cd.detectChanges();
  }

  getLookups(key): any[] {
    if (!this.locale) {
      return [];
    }

    const lookups = this.lookups?.[this.locale]?.[key];
    if (lookups) {
      if (!this.filterCtrl.value) {
        return lookups;
      }

      return lookups.filter(
        (entry) =>
          entry.label
            .toLowerCase()
            .indexOf(this.filterCtrl.value.toLowerCase()) !== -1
      );
    } else {
      return [];
    }
  }

  setValue(value) {
    this.value = value;
    this.valueChanged();
  }

  isSelectedMenu(value) {
    return this.value === value;
  }

  resetDateValue(event) {
    this.dateValue = null;
    this.dateChanged();
    event.preventDefault();
    event.stopPropagation();
    return false;
  }

  resetValue(event) {
    this.value = null;
    event.preventDefault();
    event.stopPropagation();
    this.propagateChange(null);
    return false;
  }

  ngOnChanges(changes: SimpleChanges): void {
    if (changes.lookups && this.value) {
      if (this.field.type === "lookup") {
        // this.value = this.value.slice(0);
      } else if (this.field.type === "multi-lookup") {
        // this.value = [].concat(this.value);
      }
    }
    // this.cd.markForCheck()
  }

  setDisabledState(isDisabled: boolean): void {}

  dateInputKeydown(event) {
    event.preventDefault();
    return false;
  }

  onEnterPressed(event) {
    event.preventDefault();
    this.enterPressed.emit();
  }

  getIconForSearchMode(value, key) {
    if (value === "SEARCH_MODE_STANDARD") {
      return "alpha-s-box-outline";
    } else if (value === "SEARCH_MODE_PROFESSIONAL") {
      return "alpha-p-box-outline";
    } else if (value === "SEARCH_MODE_COMFORT") {
      if (this.locale && this.locale.startsWith("de")) {
        return "alpha-k-box-outline";
      } else {
        return "alpha-c-box-outline";
      }
    }
    return "alpha-s-box-outline";
  }
}
<div [ngSwitch]="field.type" class="form-input-row">
  <ng-container *ngSwitchCase="'PLAIN_STRING'">
    <mat-form-field [ngClass]="{ virgin: !value }">
      <input
        autocomplete="off"
        matInput
        [placeholder]="placeholder"
        [(ngModel)]="value"
        (ngModelChange)="valueChanged()"
        (keydown.enter)="onEnterPressed($event)"
        [nmAutofocus]="autofocus"
      />
      <button
        mat-icon-button
        matSuffix
        aria-label="Clear"
        color="primary"
        *ngIf="value"
        (click)="resetValue($event)"
        tabIndex="-1"
      >
        <mat-icon color="primary" class="fade-in">close</mat-icon>
      </button>
    </mat-form-field>
  </ng-container>
  <ng-container *ngSwitchCase="'DATE'">
    <mat-form-field
      class="nm-date-time-input"
      (click)="datepicker.open()"
      [ngClass]="{ virgin: !dateValue }"
    >
      <input
        matInput
        autocomplete="off"
        [placeholder]="placeholder"
        [owlDateTime]="datepicker"
        [(ngModel)]="dateValue"
        (ngModelChange)="dateChanged()"
        (keydown)="dateInputKeydown($event)"
      />
      <button
        mat-icon-button
        matSuffix
        aria-label="Clear"
        color="primary"
        *ngIf="value"
        (click)="resetDateValue($event)"
      >
        <mat-icon color="primary" class="fade-in">close</mat-icon>
      </button>
      <button
        mat-button
        matSuffix
        mat-icon-button
        aria-label="date_range"
        [owlDateTimeTrigger]="datepicker"
      >
        <mat-icon>event</mat-icon>
      </button>
    </mat-form-field>
    <owl-date-time pickerType="calendar" #datepicker></owl-date-time>
  </ng-container>

  <mat-form-field
    *ngSwitchCase="'LOOKUP'"
    class="{{ value ? 'with-clear-button' : '' }}"
    [ngClass]="{ virgin: !value }"
  >
    <mat-select
      [placeholder]="placeholder"
      [(value)]="value"
      (valueChange)="valueChanged()"
      (keydown.backspace)="resetDateValue($event)"
    >
      <mat-option>
        <ngx-mat-select-search
          [formControl]="filterCtrl"
          placeholderLabel="{{ 'placeholder.search' | translate }}"
          noEntriesFoundLabel="{{ 'select.no.options' | translate }}"
          (keydown.escape)="selectSearch.matSelect.focus()"
          #selectSearch
        ></ngx-mat-select-search>
      </mat-option>
      <mat-option *ngIf="!field.default"></mat-option>
      <mat-option
        *ngFor="let value of getLookups(field.lookupSource)"
        [value]="value.value"
        >{{ value.label }}</mat-option
      >
    </mat-select>
    <button
      mat-icon-button
      matSuffix
      aria-label="Clear"
      color="primary"
      *ngIf="value"
      (click)="resetDateValue($event)"
      tabIndex="-1"
    >
      <mat-icon color="primary" class="fade-in">close</mat-icon>
    </button>
  </mat-form-field>

  <mat-form-field *ngSwitchCase="'MENU'" class="nm-formfiled-menu">
    <mat-select
      *ngIf="value"
      [placeholder]="placeholder"
      [(value)]="value"
      (valueChange)="valueChanged()"
    >
      <mat-select-trigger>
        <mat-icon
          [svgIcon]="getIconForSearchMode(value, field.lookupSource)"
        ></mat-icon>
      </mat-select-trigger>
      <mat-option *ngIf="!field.default"></mat-option>
      <mat-option
        *ngFor="let value of getLookups(field.lookupSource)"
        [value]="value.value"
      >
        <mat-icon
          [svgIcon]="getIconForSearchMode(value.value, field.lookupSource)"
        ></mat-icon>
        {{ value.label }}</mat-option
      >
    </mat-select>
  </mat-form-field>

  <mat-form-field *ngSwitchCase="'MULTI_LOOKUP'" [ngClass]="{ virgin: !value }">
    <mat-select
      [placeholder]="placeholder"
      multiple
      [(value)]="value"
      (valueChange)="valueChanged()"
      (keydown.backspace)="resetValue($event)"
    >
      <mat-select-trigger *ngIf="true">
        <nm-chip-list-trigger
          [lookupOptions]="getLookups(field.lookupSource)"
          [selection]="value"
          (selectionChanged)="externalValueChanged($event)"
        ></nm-chip-list-trigger>
      </mat-select-trigger>
      <mat-option>
        <ngx-mat-select-search
          [formControl]="filterCtrl"
          placeholderLabel="{{ 'placeholder.search' | translate }}"
          noEntriesFoundLabel="{{ 'select.no.options' | translate }}"
          (keydown.escape)="selectSearch.matSelect.focus()"
          #selectSearch
        ></ngx-mat-select-search>
      </mat-option>
      <mat-option
        *ngFor="let value of getLookups(field.lookupSource)"
        [value]="value.value"
        >{{ value.label }}</mat-option
      >
    </mat-select>
    <button
      mat-icon-button
      matSuffix
      aria-label="Clear"
      color="primary"
      *ngIf="value"
      (click)="resetValue($event)"
    >
      <mat-icon color="primary" class="fade-in">close</mat-icon>
    </button>
  </mat-form-field>

  <nm-category-select-control
    *ngSwitchCase="'CATEGORY'"
    [placeholder]="placeholder"
    [value]="value"
    (change)="valueChanged($event)"
    localStorageRecovery="false"
  ></nm-category-select-control>

  <div *ngSwitchDefault>
    {{ field | json }}
  </div>
</div>
Legend
Html element
Component
Html element with directive

results matching ""

    No results matching ""