File

components/control-panel/control-panel.component.ts

Description

Control panel form fields to set values to call MFD service.

Implements

OnInit OnDestroy

Metadata

Index

Properties
Methods

Constructor

constructor(service: AppService, nshmpService: NshmpService)
Parameters :
Name Type Optional
service AppService No
nshmpService NshmpService No

Methods

ngOnDestroy
ngOnDestroy()
Returns : void
ngOnInit
ngOnInit()
Returns : void
Private onModelChange
onModelChange()
Returns : void
Private onSourceChange
onSourceChange()
Returns : void
onSubmit
onSubmit()

On form submit.

Returns : void
Private toSourceTrees
toSourceTrees(source: MfdSource, trees: SettingGroup[])

Returns the tree info list associated with a source and trees.

Parameters :
Name Type Optional Description
source MfdSource No

The MfdSource

trees SettingGroup[] No

The setting groups

Returns : TreeInfo[]
Private toSourceTypes
toSourceTypes(trees: SettingGroup[])

Convert setting group to source types.

Parameters :
Name Type Optional Description
trees SettingGroup[] No

The settings groups

Returns : SettingsGroup[]
toSourceValue
toSourceValue(sourceType: SourceType, tectonicSettings: TectonicSettings)
Parameters :
Name Type Optional
sourceType SourceType No
tectonicSettings TectonicSettings No
Returns : string

Properties

formGroup
Default value : this.service.formGroup

Form field state

Public service
Type : AppService
settingsGroup
Default value : computed(() => { const usage = this.service.usage(); if (usage) { return this.toSourceTypes(usage.response.trees); } else { return []; } })

Settings group state

settingsToString
Default value : tectonicSettingsToString

Function to convert tectonic settings to string

sourceTrees$
Default value : this.formGroup.controls.source.valueChanges.pipe( map(source => this.toSourceTrees(source, this.service.usage().response.trees), ), )

Source trees state

sourceTypeToString
Default value : sourceTypeToCapitalCase

Function to convert source type to string

Private subs
Type : Subscription[]
Default value : []
import {AsyncPipe} from '@angular/common';
import {Component, computed, OnDestroy, OnInit} from '@angular/core';
import {ReactiveFormsModule} from '@angular/forms';
import {MatOptgroup, MatOption} from '@angular/material/core';
import {MatFormField, MatLabel} from '@angular/material/form-field';
import {MatSelect} from '@angular/material/select';
import {MatSlideToggle} from '@angular/material/slide-toggle';
import {NshmpLibNgHazardModelFormComponent} from '@ghsc/nshmp-lib-ng/hazard';
import {
  NshmpLibNgControlPanelButtonsComponent,
  NshmpService,
} from '@ghsc/nshmp-lib-ng/nshmp';
import {
  SettingGroup,
  SourceType,
  sourceTypeToCapitalCase,
  TectonicSettings,
  tectonicSettingsToString,
  TreeInfo,
} from '@ghsc/nshmp-utils-ts/libs/nshmp-lib/model';
import {map, Subscription} from 'rxjs';

import {MfdSource} from '../../models/control-form.model';
import {AppService} from '../../services/app.service';

/**
 * Earthquake settings group
 */
interface SettingsGroup {
  /** Tectonic settings */
  settings: TectonicSettings;
  /** Earthquake source types */
  types: SourceType[];
}

/**
 * Control panel form fields to set values to call MFD service.
 */
@Component({
  imports: [
    NshmpLibNgHazardModelFormComponent,
    NshmpLibNgControlPanelButtonsComponent,
    MatFormField,
    MatLabel,
    MatSelect,
    MatOptgroup,
    MatOption,
    MatSlideToggle,
    AsyncPipe,
    ReactiveFormsModule,
  ],
  selector: 'app-control-panel',
  styleUrl: './control-panel.component.scss',
  templateUrl: './control-panel.component.html',
})
export class ControlPanelComponent implements OnInit, OnDestroy {
  /** Function to convert tectonic settings to string */
  settingsToString = tectonicSettingsToString;
  /** Function to convert source type to string */
  sourceTypeToString = sourceTypeToCapitalCase;

  /** Form field state */
  formGroup = this.service.formGroup;

  /** Settings group state */
  settingsGroup = computed(() => {
    const usage = this.service.usage();

    if (usage) {
      return this.toSourceTypes(usage.response.trees);
    } else {
      return [];
    }
  });

  /** Source trees state */
  sourceTrees$ = this.formGroup.controls.source.valueChanges.pipe(
    map(source =>
      this.toSourceTrees(source, this.service.usage().response.trees),
    ),
  );

  private subs: Subscription[] = [];

  constructor(
    public service: AppService,
    private nshmpService: NshmpService,
  ) {}

  ngOnInit(): void {
    const controls = this.formGroup.controls;

    this.subs.push(
      controls.model.valueChanges.subscribe(() => this.onModelChange()),
    );

    this.subs.push(
      controls.sourceAsString.valueChanges.subscribe(sourceAsString => {
        controls.source.setValue(JSON.parse(sourceAsString) as MfdSource);
        this.onSourceChange();
      }),
    );

    this.subs.push(
      controls.sourceTree.valueChanges.subscribe(() =>
        this.service.resetState(),
      ),
    );

    this.subs.push(
      controls.cumulative.valueChanges.subscribe(() =>
        this.service.createPlots(),
      ),
    );

    this.subs.push(
      controls.weightedMfds.valueChanges.subscribe(() =>
        this.service.createPlots(),
      ),
    );
  }

  ngOnDestroy(): void {
    this.subs.forEach(sub => sub.unsubscribe());
  }

  /**
   * On form submit.
   */
  onSubmit(): void {
    this.service.callService();
    this.nshmpService.selectPlotControl();
  }

  private onModelChange(): void {
    const trees = this.service.usage().response.trees;
    const defaultSource = this.service.defaultSource(trees);
    const defaultTreeInfo = this.service.defaultSourceTree(
      trees,
      defaultSource,
    );

    this.formGroup.patchValue({
      source: defaultSource,
      sourceAsString: JSON.stringify(defaultSource),
      sourceTree: defaultTreeInfo.id,
    });

    this.service.resetState();
  }

  private onSourceChange(): void {
    const trees = this.service.usage().response.trees;
    const defaultTreeInfo = this.service.defaultSourceTree(
      trees,
      this.formGroup.getRawValue().source,
    );

    this.formGroup.patchValue({
      sourceTree: defaultTreeInfo.id,
    });
    this.service.resetState();
  }

  toSourceValue(
    sourceType: SourceType,
    tectonicSettings: TectonicSettings,
  ): string {
    return JSON.stringify({
      sourceType,
      tectonicSettings,
    });
  }

  /**
   * Returns the tree info list associated with a source and trees.
   *
   * @param source The MfdSource
   * @param trees The setting groups
   */
  private toSourceTrees(source: MfdSource, trees: SettingGroup[]): TreeInfo[] {
    if (
      source === null ||
      source === undefined ||
      trees === null ||
      trees === undefined
    ) {
      return [];
    }

    const settingsTree = trees.find(
      tree => tree.setting === source.tectonicSettings,
    );
    const tree = settingsTree?.data?.find(
      data => data.type === source.sourceType,
    );
    const data = tree?.data.sort((a, b) => a.name.localeCompare(b.name));

    return data ?? [];
  }

  /**
   *  Convert setting group to source types.
   *
   * @param trees The settings groups
   */
  private toSourceTypes(trees: SettingGroup[]): SettingsGroup[] {
    if (trees === null || trees === undefined) {
      return [];
    }

    return trees.map(tree => {
      const types = [...tree.data].map(data => data.type);

      return {
        settings: tree.setting,
        types: types.sort((a, b) => a.localeCompare(b)),
      };
    });
  }
}
<form
  class="height-full overflow-auto"
  [formGroup]="formGroup"
  (submit)="onSubmit()"
>
  <!-- Model -->
  <nshmp-lib-ng-hazard-model-form
    [modelControl]="formGroup.controls.model"
    [models]="service.availableModels()"
  />

  <!-- Source Type Control -->
  <mat-form-field class="grid-col-12 source-select">
    <mat-label>Source Type</mat-label>

    <mat-select [formControl]="formGroup.controls.sourceAsString">
      @for (group of settingsGroup(); track group) {
        <mat-optgroup [label]="settingsToString(group.settings)">
          @for (type of group.types; track type) {
            <mat-option [value]="toSourceValue(type, group.settings)">
              {{ settingsToString(group.settings) }} -
              {{ sourceTypeToString(type) }}
            </mat-option>
          }
        </mat-optgroup>
      }
    </mat-select>
  </mat-form-field>

  <!-- Source Tree Control -->
  <mat-form-field class="grid-col-12 source-tree-select">
    <mat-label>Source Tree</mat-label>

    <mat-select [formControl]="formGroup.controls.sourceTree">
      @if (formGroup.value?.source === null) {
        <mat-option [value]="-1"> ---- Select Source Type ---- </mat-option>
      }
      @for (sourceTree of sourceTrees$ | async; track sourceTree) {
        <mat-option [value]="sourceTree.id">
          {{ sourceTree.name }}
        </mat-option>
      }
    </mat-select>
  </mat-form-field>

  <!-- Plot options -->
  <div>
    <mat-label>Plot Options</mat-label>
    <div class="plot-options">
      <!-- Cumulative -->
      <div class="grid-row">
        <mat-slide-toggle
          color="primary"
          [formControl]="formGroup.controls.cumulative"
        >
          Cumulative
        </mat-slide-toggle>
      </div>

      <!-- Weighted MFDs -->
      <div class="grid-row">
        <mat-slide-toggle
          color="primary"
          [formControl]="formGroup.controls.weightedMfds"
        >
          Weighted MFDs
        </mat-slide-toggle>
      </div>
    </div>
  </div>

  <!-- Form Buttons -->
  <nshmp-lib-ng-control-panel-buttons
    [plotDisabled]="formGroup.invalid"
    [serviceCallInfo]="service.serviceCallInfo()"
    [resetDisabled]="formGroup.pristine"
    (resetClick)="service.resetControlPanel()"
  />
</form>
Legend
Html element
Component
Html element with directive

results matching ""

    No results matching ""