Skip to content
Snippets Groups Projects
radio-button-group.ts 2.21 KiB
Newer Older
  • Learn to ignore specific revisions
  • import { Type } from '@angular/core';
    
      InputElement, OptionElement, PositionedUIElement, UIElement
    
    } 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 { AnswerScheme, AnswerSchemeValue } from 'common/models/elements/answer-scheme-interfaces';
    import { TextLabel } from 'common/models/elements/label-interfaces';
    import { BasicStyles, PositionProperties } from 'common/models/elements/property-group-interfaces';
    
    export class RadioButtonGroupElement extends InputElement implements PositionedUIElement, OptionElement {
      options: TextLabel[] = [];
    
      alignment: 'column' | 'row' = 'column';
      strikeOtherOptions: boolean = false;
      position: PositionProperties;
      styling: BasicStyles & {
        lineHeight: number;
      };
    
    
    rhenck's avatar
    rhenck committed
      constructor(element: Partial<RadioButtonGroupElement>) {
    
        super({ dimensions: { height: 100 }, ...element });
    
        if (element.options) this.options = [...element.options];
    
    rhenck's avatar
    rhenck committed
        if (element.alignment) this.alignment = element.alignment;
        if (element.strikeOtherOptions) this.strikeOtherOptions = element.strikeOtherOptions;
    
        this.position = UIElement.initPositionProps({ ...element.position });
    
        this.styling = {
    
    rhenck's avatar
    rhenck committed
          ...UIElement.initStylingProps({
    
            backgroundColor: 'transparent',
            lineHeight: 135,
            ...element.styling
          })
        };
      }
    
    
      hasAnswerScheme(): boolean {
    
        return Boolean(this.getAnswerScheme);
    
      getAnswerScheme(): AnswerScheme {
    
        return {
          id: this.id,
          type: 'integer',
    
          multiple: false,
    
          nullable: !this.value && this.value !== 0,
    
          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 RadioButtonGroupComponent;
      }
    
    rhenck's avatar
    rhenck committed
      getNewOptionLabel(optionText: string): TextLabel {
    
    rhenck's avatar
    rhenck committed
        return UIElement.createOptionLabel(optionText) as TextLabel;