@WidgetComponent

nm-select-context

File

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

Metadata

changeDetection ChangeDetectionStrategy.OnPush
selector nm-select-context
templateUrl ./select-context.component.html

Index

Widget inputs
Widget outputs
Properties
Methods

Constructor

constructor(localstorageService: LocalStorageService)
Parameters :
Name Type Optional
localstorageService LocalStorageService no

Methods

Public configureWidget
configureWidget(configuration: WidgetConfig)
Decorators : WidgetConfigure
Parameters :
Name Type Optional
configuration WidgetConfig<SelectContextConfiguration> no
Returns : void
Public onChange
onChange(value: any)
Parameters :
Name Type Optional
value any no
Returns : void

Properties

Public _contexts
_contexts: Observable<string | []>
Type : Observable<string | []>
Public configuration
configuration: WidgetConfig<SelectContextConfiguration>
Type : WidgetConfig<SelectContextConfiguration>
Decorators : WidgetConfiguration
Public contexts
contexts:
Default value : new Subject<string | any[]>()
Decorators : WidgetInput
Public localstorageClientEntry
localstorageClientEntry: LocalStorageEntry
Type : LocalStorageEntry
Public reload
reload:
Default value : new Subject<any>()
Decorators : WidgetInput
Public selection
selection:
Default value : new ReplaySubject<any>(1)
Decorators : WidgetOutput
Public value
value:
Default value : new ReplaySubject<any>(1)
Decorators : WidgetInput
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);
  }
}
<nm-context-selector
  [field]="configuration.configuration.field"
  [placeholder]="configuration.configuration.placeholder | translate"
  [disabled]="configuration.configuration.disabled"
  [show-versions]="configuration.configuration['show-versions']"
  [multi-selection]="configuration.configuration['multi-selection']"
  [allow-empty]="configuration.configuration['allow-empty']"
  [version-class]="configuration.configuration['version-class']"
  [name]="'context-selector'"
  [clearable]="false"
  [contexts]="_contexts | async"
  [ngModel]="value | async"
  (ngModelChange)="onChange($event)"
>
</nm-context-selector>
Legend
Html element
Component
Html element with directive

results matching ""

    No results matching ""