File

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

Description

Control panel with form fields to call disagg service.

Handles disagg service with return period and imt.

Implements

OnInit

Metadata

Index

Properties
Methods

Constructor

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

Methods

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

On form submit.

Returns : void

Properties

DisaggTarget
Default value : DisaggTarget

Disaggregation target enum

form
Default value : this.service.formGroup

Form fields state

imlRange
Default value : computed(() => { const usage = this.service.usage(); if (usage) { return usage.response.iml.range[this.form.getRawValue().imt]; } })

IML range from usage, based on IMT value

selectSiteData
Type : Signal<SelectSiteDialogData>
Default value : computed(() => { const usage = this.service.usage(); const nshmService = this.service.nshmService(); if (usage === null) { return; } const services = environment.webServices.nshmpHazWs.services.curveServices; return { mapUrl: `${nshmService.url}${services.map}?raw=true`, sitesUrl: `${nshmService.url}${services.sites}?raw=true`, }; })

Data for select site component

Public service
Type : AppService
serviceCallInfo
Default value : this.service.serviceCallInfo

Service call info state

siteClasses
Default value : computed(() => this.service.usage()?.response.model.siteClasses)

The site classes

sourceModel
Default value : computed(() => this.service.usage()?.response?.model)

Source model info from usage

import {Component, computed, DestroyRef, OnInit, Signal} from '@angular/core';
import {takeUntilDestroyed} from '@angular/core/rxjs-interop';
import {ReactiveFormsModule} from '@angular/forms';
import {MatOption} from '@angular/material/core';
import {MatError, MatFormField, MatLabel} from '@angular/material/form-field';
import {MatInput} from '@angular/material/input';
import {MatRadioButton, MatRadioGroup} from '@angular/material/radio';
import {MatSelect} from '@angular/material/select';
import {
  DisaggTarget,
  NshmpHazardLocationFormComponent,
  NshmpHazardMaxDirectionFormComponent,
  NshmpHazardModelFormComponent,
  NshmpHazardReturnPeriodFormComponent,
  NshmpHazardSiteClassFormComponent,
} from '@ghsc/nshmp-lib-ng/hazard';
import {NshmpMapSelectSiteComponent, SelectSiteDialogData} from '@ghsc/nshmp-lib-ng/map';
import {NshmpControlPanelButtonsComponent, NshmpService} from '@ghsc/nshmp-lib-ng/nshmp';
import {environment} from 'projects/nshmp-apps/src/environments/environment';
import {combineLatest} from 'rxjs';

import {AppService} from '../../services/app.service';

/**
 * Control panel with form fields to call disagg service.
 *
 * Handles disagg service with return period and imt.
 */
@Component({
  imports: [
    NshmpHazardModelFormComponent,
    NshmpHazardLocationFormComponent,
    NshmpMapSelectSiteComponent,
    NshmpHazardSiteClassFormComponent,
    NshmpControlPanelButtonsComponent,
    NshmpHazardReturnPeriodFormComponent,
    NshmpControlPanelButtonsComponent,
    NshmpHazardMaxDirectionFormComponent,
    MatFormField,
    MatLabel,
    MatSelect,
    MatOption,
    MatRadioGroup,
    MatRadioButton,
    MatInput,
    MatError,
    ReactiveFormsModule,
  ],
  selector: 'app-control-panel',
  styleUrl: './control-panel.component.scss',
  templateUrl: './control-panel.component.html',
})
export class ControlPanelComponent implements OnInit {
  /** Disaggregation target enum */
  DisaggTarget = DisaggTarget;

  /** Form fields state */
  form = this.service.formGroup;

  /** IML range from usage, based on IMT value */
  imlRange = computed(() => {
    const usage = this.service.usage();

    if (usage) {
      return usage.response.iml.range[this.form.getRawValue().imt];
    }
  });

  /** Data for select site component */
  selectSiteData: Signal<SelectSiteDialogData> = computed(() => {
    const usage = this.service.usage();
    const nshmService = this.service.nshmService();

    if (usage === null) {
      return;
    }

    const services = environment.webServices.nshmpHazWs.services.curveServices;

    return {
      mapUrl: `${nshmService.url}${services.map}?raw=true`,
      sitesUrl: `${nshmService.url}${services.sites}?raw=true`,
    };
  });

  /** Service call info state */
  serviceCallInfo = this.service.serviceCallInfo;

  /** The site classes */
  siteClasses = computed(() => this.service.usage()?.response.model.siteClasses);

  /** Source model info from usage */
  sourceModel = computed(() => this.service.usage()?.response?.model);

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

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

    controls.disaggTarget.valueChanges.pipe(takeUntilDestroyed(this.destroyRef)).subscribe(() => {
      this.service.onDisaggTarget();
      this.service.resetState();
    });

    controls.model.valueChanges
      .pipe(takeUntilDestroyed(this.destroyRef))
      .subscribe(() => this.onModelChange());

    combineLatest([controls.latitude.valueChanges, controls.longitude.valueChanges])
      .pipe(takeUntilDestroyed(this.destroyRef))
      .subscribe(() => {
        this.service.resetState();
        const values = this.form.getRawValue();

        if (values.latitude && values.longitude) {
          this.selectSiteData().site = {
            latitude: values.latitude,
            longitude: values.longitude,
          };
        }
      });

    combineLatest([
      controls.iml.valueChanges,
      controls.imt.valueChanges,
      controls.returnPeriod.valueChanges,
      controls.vs30.valueChanges,
    ])
      .pipe(takeUntilDestroyed(this.destroyRef))
      .subscribe(() => {
        this.service.resetState();
      });
  }

  private onModelChange(): void {
    const {latitude, longitude} = this.form.getRawValue();
    const latitudeBounds = this.service.usage().response.latitude;
    const longitudeBounds = this.service.usage().response.longitude;

    if (
      !(
        latitude >= latitudeBounds.min &&
        latitude <= latitudeBounds.max &&
        longitude >= longitudeBounds.min &&
        longitude <= longitudeBounds.max
      )
    ) {
      this.form.patchValue({
        latitude: NaN,
        longitude: NaN,
      });
    }

    this.form.controls.latitude.markAsPristine();
    this.form.controls.longitude.markAsPristine();
    this.service.resetState();
  }

  /**
   * On form submit.
   */
  onSubmit(): void {
    this.service.callService();
    this.nshmpService.selectPlotControl();
  }
}
<!-- Control Panel -->
<form class="height-full overflow-auto" [formGroup]="form" (submit)="onSubmit()">
  <!-- Model -->
  <nshmp-hazard-model-form
    [modelControl]="form.controls.model"
    [models]="service.availableModels()"
  />

  @if (service.usage()) {
    <!-- Latitude -->
    <nshmp-hazard-location-form
      class="latitude-input"
      [locationControl]="form.controls.latitude"
      label="Latitude"
      [bounds]="service.usage()?.response?.latitude"
    />

    <!-- Longitude -->
    <nshmp-hazard-location-form
      class="longitude-input"
      [locationControl]="form.controls.longitude"
      label="Longitude"
      [bounds]="service.usage()?.response?.longitude"
    />
  }

  <!-- Select site -->
  <nshmp-map-select-site [data]="selectSiteData()" (siteSelect)="service.setLocation($event)" />

  <!-- Site Class -->
  <div class="grid-row grid-gap-1">
    <!-- Site classes -->
    @if (siteClasses()) {
      <nshmp-hazard-site-class-form
        [siteClassControl]="form.controls.siteClass"
        [siteClasses]="siteClasses()"
        [vs30Control]="form.controls.vs30"
        [vs30Bounds]="service.usage()?.response?.vs30"
      />
    }
  </div>

  <!-- Imt -->
  <mat-form-field class="grid-col-12 imt-select">
    <mat-label>
      Intensity Measure Type
      <span class="form-required">*</span>
    </mat-label>
    <mat-select [formControl]="form.controls.imt">
      @for (imt of sourceModel()?.imts; track imt) {
        <mat-option [value]="imt?.value">
          {{ imt?.display }}
        </mat-option>
      }
    </mat-select>
  </mat-form-field>

  <!-- Disagg target -->
  <div class="display-flex">
    <!-- Target selector -->
    <div class="disagg-target-selector">
      <mat-radio-group [formControl]="form.controls.disaggTarget">
        <mat-radio-button
          [value]="DisaggTarget.RETURN_PERIOD"
          aria-label="Select return period"
          class="return-period-button"
        />
        <mat-radio-button [value]="DisaggTarget.IML" aria-label="Select IML" class="iml-button" />
      </mat-radio-group>
    </div>

    <div class="grid-col-12">
      <!-- Return Period Controls -->
      <div class="disagg-target-return-period">
        <nshmp-hazard-return-period-form
          [returnPeriodControl]="form.controls.returnPeriod"
          [commonReturnPeriodControl]="form.controls.commonReturnPeriods"
        />
      </div>

      <!-- IML controls -->
      <div class="disagg-target-iml">
        <div class="grid-row grid-gap-1">
          <mat-form-field class="grid-col-6">
            <mat-label> Intensity Measure Level ({{ imlRange()?.units }}) </mat-label>
            <input
              type="number"
              class="iml-input"
              matInput
              [formControl]="form.controls.iml"
              [min]="imlRange()?.min"
              [max]="imlRange()?.max"
              step="any"
            />
            <mat-error> [{{ imlRange()?.min }}, {{ imlRange()?.max }}] </mat-error>
          </mat-form-field>

          <div class="center-y">
            <nshmp-hazard-max-direction-form
              label="Max Direction"
              [maxDirectionControl]="form.controls.maxDirection"
            />
          </div>
        </div>
      </div>
    </div>
  </div>

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

results matching ""

    No results matching ""