Skip to content
Snippets Groups Projects
parameter-select.component.ts 2.77 KiB
Newer Older
lotte's avatar
lotte committed
import { Component, EventEmitter, Input, OnInit, Output, Optional } from '@angular/core';
import { ProcessParameter } from '../../../processes/process-parameter.model';
import { ScriptParameter } from '../../../scripts/script-parameter.model';
import { hasNoValue } from '../../../../shared/empty.util';
lotte's avatar
lotte committed
import { ControlContainer, NgForm } from '@angular/forms';
lotte's avatar
lotte committed
import { controlContainerFactory } from '../../process-form.component';
lotte's avatar
lotte committed
/**
 * Component to select a single parameter for a process
 */
@Component({
  selector: 'ds-parameter-select',
  templateUrl: './parameter-select.component.html',
lotte's avatar
lotte committed
  styleUrls: ['./parameter-select.component.scss'],
lotte's avatar
lotte committed
  viewProviders: [{
    provide: ControlContainer,
lotte's avatar
lotte committed
    useFactory: controlContainerFactory,
lotte's avatar
lotte committed
    deps: [[new Optional(), NgForm]]
  }]
lotte's avatar
lotte committed
export class ParameterSelectComponent {
lotte's avatar
lotte committed
  @Input() index: number;

lotte's avatar
lotte committed
  /**
   * The current parameter value of the selected parameter
   */
lotte's avatar
lotte committed
  @Input() parameterValue: ProcessParameter = new ProcessParameter();
lotte's avatar
lotte committed

lotte's avatar
lotte committed
  /**
   * The available script parameters for the script
   */
  @Input() parameters: ScriptParameter[];
lotte's avatar
lotte committed

  /**
   * Whether or not this selected parameter can be removed from the list
   */
lotte's avatar
lotte committed
  @Input() removable: boolean;
lotte's avatar
lotte committed

  /**
   * Emits the parameter value when it's removed
   */
lotte's avatar
lotte committed
  @Output() removeParameter: EventEmitter<ProcessParameter> = new EventEmitter<ProcessParameter>();
lotte's avatar
lotte committed

  /**
   * Emits the updated parameter value when it changes
   */
  @Output() changeParameter: EventEmitter<ProcessParameter> = new EventEmitter<ProcessParameter>();

lotte's avatar
lotte committed
  /**
   * Returns the script parameter based on the currently selected name
   */
lotte's avatar
lotte committed
  get selectedScriptParameter(): ScriptParameter {
    return this.parameters.find((parameter: ScriptParameter) => parameter.name === this.selectedParameter);
  }

lotte's avatar
lotte committed
  /**
   * Return the currently selected parameter name
   */
  get selectedParameter(): string {
    return this.parameterValue ? this.parameterValue.name : undefined;
  }

lotte's avatar
lotte committed
  /**
   * Sets the currently selected parameter based on the provided parameter name
   * Emits the new value from the changeParameter output
   * @param value The parameter name to set
   */
  set selectedParameter(value: string) {
    this.parameterValue.name = value;
    this.selectedParameterValue = undefined;
    this.changeParameter.emit(this.parameterValue);
  }

lotte's avatar
lotte committed
  /**
   * Returns the currently selected parameter value
   */
  get selectedParameterValue(): any {
    return this.parameterValue ? this.parameterValue.value : undefined;
  }

lotte's avatar
lotte committed
  /**
   * Sets the currently selected value for the parameter
   * Emits the new value from the changeParameter output
   * @param value The parameter value to set
   */
  set selectedParameterValue(value: any) {
    this.parameterValue.value = value;
    this.changeParameter.emit(this.parameterValue);
  }
}