File

src/app/shared/widgets/select-context/select-context.component.ts

Index

Properties

Properties

allow-empty
allow-empty: boolean
Type : boolean

true to add an empty option to the tree

contexts
contexts: string | any[]
Type : string | any[]

URL to load contexts. Set to null to set contexts via the "contexts" input

disabled
disabled: boolean
Type : boolean

true to disable the component

field
field: string
Type : string

Field to use as identifier when connecting to the widget. null to receive full objects

multi-selection
multi-selection: boolean
Type : boolean

true to allow multi selection

placeholder
placeholder: string
Type : string

Placeholder string to display

show-versions
show-versions: boolean
Type : boolean

true to show contexts of type CLIENTTYPE4: Versions

version-class
version-class: string
Type : string

Class to apply to contexts of type CLIENTTYPE4

import {
  WidgetComponent,
  WidgetConfiguration,
  WidgetConfigure,
  WidgetInput,
  WidgetOutput,
} from "../widget.metadata";
import { ChangeDetectionStrategy, Component } from "@angular/core";
import { WidgetConfig } from "../widget.configuration";
import { from, Observable, of, ReplaySubject, Subject } from "rxjs";
import { mapTo, startWith, switchAll, switchMap } from "rxjs/operators";
import {
  LocalStorageEntry,
  LocalStorageService,
} from "../../components/local-storage/local-storage.service";
import {
  DeletionMode,
  Scope,
} from "../../components/local-storage/local-storage-constants";

interface SelectContextConfiguration {
  /**
   * URL to load contexts. Set to `null` to set contexts via the "contexts" input
   */
  contexts: string | any[];

  /**
   * Placeholder string to display
   */
  placeholder: string;

  /**
   * Field to use as identifier when connecting to the widget. `null` to receive full objects
   */
  field: string;

  /**
   * `true` to show contexts of type `CLIENTTYPE4`: Versions
   */
  "show-versions": boolean;

  /**
   * `true` to allow multi selection
   */
  "multi-selection": boolean;

  /**
   * `true` to add an empty option to the tree
   */
  "allow-empty": boolean;

  /**
   * `true` to disable the component
   */
  disabled: boolean;

  /**
   * Class to apply to contexts of type `CLIENTTYPE4`
   */
  "version-class": string;
}

@WidgetComponent("nm-select-context")
@Component({
  selector: "nm-select-context",
  templateUrl: "./select-context.component.html",
  changeDetection: ChangeDetectionStrategy.OnPush,
})
export class SelectContextComponent {
  @WidgetConfiguration()
  public configuration: WidgetConfig<SelectContextConfiguration>;

  public _contexts: Observable<string | any[]>;

  @WidgetInput("contexts")
  public contexts = new Subject<string | any[]>();

  @WidgetInput("value")
  public value = new ReplaySubject<any>(1);

  @WidgetInput("reload")
  public reload = new Subject<any>();

  @WidgetOutput("selection")
  public selection = new ReplaySubject<any>(1);

  public localstorageClientEntry: LocalStorageEntry;

  constructor(protected localstorageService: LocalStorageService) {}

  @WidgetConfigure()
  public configureWidget(
    configuration: WidgetConfig<SelectContextConfiguration>
  ) {
    const inputs: Observable<string | any[]>[] = [];

    if (configuration.configuration.contexts) {
      inputs.push(of(configuration.configuration.contexts));
    }

    inputs.push(this.contexts);

    this._contexts = from(inputs).pipe(
      // switch input observable, as soon as one emits
      // contexts defined in the configuration will be emitted first
      switchAll(),

      // re-emit the previous value whenever reload is triggered
      switchMap((input) => this.reload.pipe(startWith(null), mapTo(input)))
    );

    this.localstorageClientEntry =
      this.localstorageService.getLocalStorageEntry(
        "data-list-client",
        Scope.USER,
        DeletionMode.RESET
      );

    if (this.localstorageClientEntry.exists()) {
      let context = parseInt(this.localstorageClientEntry.value, 10);
      this.selection.next(this.value.next(context));
    }
  }

  public onChange(value: any) {
    this.localstorageClientEntry.value = value;
    this.selection.next(value);
  }
}

results matching ""

    No results matching ""