Skip to content
Snippets Groups Projects
radio-button-group-complex.ts 3.32 KiB
Newer Older
import { Type } from '@angular/core';
import {
  InputElement, InputElementProperties, OptionElement, PositionedUIElement, UIElement, UIElementType
} from 'common/models/elements/element';
import { ElementComponent } from 'common/directives/element-component.directive';
import { RadioGroupImagesComponent } from 'common/components/input-elements/radio-group-images.component';
import { AnswerScheme, AnswerSchemeValue } from 'common/models/elements/answer-scheme-interfaces';
import { TextImageLabel } 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 RadioButtonGroupComplexElement extends InputElement
  implements PositionedUIElement, OptionElement, RadioButtonGroupComplexProperties {
  type: UIElementType = 'radio-group-images';
rhenck's avatar
rhenck committed
  options: TextImageLabel[] = [];
  itemsPerRow: number | null = null;
  position: PositionProperties;
  styling: BasicStyles;

rhenck's avatar
rhenck committed
  constructor(element?: RadioButtonGroupComplexProperties) {
rhenck's avatar
rhenck committed
    if (element && isValid(element)) {
      this.options = element.options;
      this.itemsPerRow = element.itemsPerRow;
      this.position = { ...element.position };
      this.styling = { ...element.styling };
rhenck's avatar
rhenck committed
    } else {
      if (environment.strictInstantiation) {
        throw new InstantiationEror('Error at RadioButtonGroupComplex instantiation', element);
      }
      if (element?.options) this.options = element.options;
      if (element?.itemsPerRow) this.itemsPerRow = element.itemsPerRow;
      this.dimensions = PropertyGroupGenerators.generateDimensionProps({
        height: 100,
        ...element?.dimensions
      });
      this.position = PropertyGroupGenerators.generatePositionProps({
        ...element?.position
      });
      this.styling = PropertyGroupGenerators.generateBasicStyleProps(element?.styling);
    }
  getDuplicate(): RadioButtonGroupComplexElement {
    return new RadioButtonGroupComplexElement(this);
  }

  getAnswerScheme(): AnswerScheme {
    return {
      id: this.id,
      type: 'integer',
      format: '',
      multiple: false,
      values: this.getAnswerSchemeValues(),
  private getAnswerSchemeValues(): AnswerSchemeValue[] {
    return this.options
      .map((option, index) => ({
        value: (index + 1).toString(),
        label: InputElement.stripHTML(option.text)
      }));
  getElementComponent(): Type<ElementComponent> {
    return RadioGroupImagesComponent;
  }
rhenck's avatar
rhenck committed
  getNewOptionLabel(optionText: string): TextImageLabel {
rhenck's avatar
rhenck committed
    return UIElement.createOptionLabel(optionText, true) as TextImageLabel;

export interface RadioButtonGroupComplexProperties extends InputElementProperties {
  options: TextImageLabel[];
  itemsPerRow: number | null;
  position: PositionProperties;
  styling: BasicStyles;
}
rhenck's avatar
rhenck committed

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