Skip to content
Snippets Groups Projects
radio-button-group.ts 3.67 KiB
Newer Older
import { Type } from '@angular/core';
  InputElement, InputElementProperties, OptionElement, PositionedUIElement, UIElement, UIElementType
} from 'common/models/elements/element';
import { ElementComponent } from 'common/directives/element-component.directive';
import { RadioButtonGroupComponent } from 'common/components/input-elements/radio-button-group.component';
import { VariableInfo, VariableValue } from '@iqb/responses';
import { TextLabel } from 'common/models/elements/label-interfaces';
rhenck's avatar
rhenck committed
import {
  BasicStyles, PositionProperties, PropertyGroupGenerators, PropertyGroupValidators
} from 'common/models/elements/property-group-interfaces';
import { environment } from 'common/environment';
import { InstantiationEror } from 'common/util/errors';
export class RadioButtonGroupElement extends InputElement
  implements PositionedUIElement, OptionElement, RadioButtonGroupProperties {
  type: UIElementType = 'radio';
  options: TextLabel[] = [];
  alignment: 'column' | 'row' = 'column';
  strikeOtherOptions: boolean = false;
  position: PositionProperties;
  styling: BasicStyles & {
    lineHeight: number;
  };

rhenck's avatar
rhenck committed
  constructor(element?: RadioButtonGroupProperties) {
rhenck's avatar
rhenck committed
    if (element && isValid(element)) {
      this.options = element.options;
      this.alignment = element.alignment;
      this.strikeOtherOptions = element.strikeOtherOptions;
      this.position = { ...element.position };
      this.styling = { ...element.styling };
rhenck's avatar
rhenck committed
    } else {
      if (environment.strictInstantiation) {
        throw new InstantiationEror('Error at RadioButtonGroupElement instantiation', element);
      }
      if (element?.options) this.options = element.options;
      if (element?.alignment) this.alignment = element.alignment;
      if (element?.strikeOtherOptions) this.strikeOtherOptions = element.strikeOtherOptions;
      this.dimensions = PropertyGroupGenerators.generateDimensionProps({
        height: 100,
        ...element?.dimensions
      });
      this.position = PropertyGroupGenerators.generatePositionProps(element?.position);
      this.styling = {
        ...PropertyGroupGenerators.generateBasicStyleProps(element?.styling),
        lineHeight: element?.styling?.lineHeight || 135
      };
    }
  getDuplicate(): RadioButtonGroupElement {
    return new RadioButtonGroupElement(this);
  }

  getVariableInfos(): VariableInfo[] {
    return [{
      id: this.id,
      type: 'integer',
      multiple: false,
      values: this.getVariableInfoValues(),
      valuePositionLabels: [],
      page: '',
      valuesComplete: true
  private getVariableInfoValues(): VariableValue[] {
    return this.options
      .map((option, index) => ({
        value: (index + 1).toString(),
        label: InputElement.stripHTML(option.text)
      }));
  getElementComponent(): Type<ElementComponent> {
    return RadioButtonGroupComponent;
  }
rhenck's avatar
rhenck committed
  getNewOptionLabel(optionText: string): TextLabel {
rhenck's avatar
rhenck committed
    return UIElement.createOptionLabel(optionText) as TextLabel;

export interface RadioButtonGroupProperties extends InputElementProperties {
  options: TextLabel[];
  alignment: 'column' | 'row';
  strikeOtherOptions: boolean;
  position: PositionProperties;
  styling: BasicStyles & {
    lineHeight: number;
  };
}
rhenck's avatar
rhenck committed

function isValid(blueprint?: RadioButtonGroupProperties): boolean {
  if (!blueprint) return false;
  return blueprint.options !== undefined &&
    blueprint.alignment !== undefined &&
    blueprint.strikeOtherOptions !== undefined &&
    PropertyGroupValidators.isValidPosition(blueprint.position) &&
    PropertyGroupValidators.isValidBasicStyles(blueprint.styling) &&
    blueprint.styling.lineHeight !== undefined;
}