Skip to content
Snippets Groups Projects
likert.ts 2.38 KiB
Newer Older
  • Learn to ignore specific revisions
  • import { Type } from '@angular/core';
    
    import { ElementFactory } from 'common/util/element.factory';
    import {
    
      BasicStyles, CompoundElement, UIElement,
    
      PositionedUIElement, PositionProperties, UIElementValue, TextImageLabel, OptionElement, Label
    
    } from 'common/models/elements/element';
    import { LikertRowElement } from 'common/models/elements/compound-elements/likert/likert-row';
    import { ElementComponent } from 'common/directives/element-component.directive';
    import { LikertComponent } from 'common/components/compound-elements/likert/likert.component';
    
    
    export class LikertElement extends CompoundElement implements PositionedUIElement, OptionElement {
    
      rows: LikertRowElement[] = [];
    
      options: TextImageLabel[] = [];
    
      firstColumnSizeRatio: number = 5;
      position: PositionProperties;
      styling: BasicStyles & {
        lineHeight: number;
        lineColoring: boolean;
        lineColoringColor: string;
      };
    
    
    rhenck's avatar
    rhenck committed
      constructor(element: Partial<LikertElement>, ...args: unknown[]) {
    
        super({width: 250, height: 200, ...element}, ...args);
        if (element.options) this.options = [...element.options];
    
    rhenck's avatar
    rhenck committed
        if (element.firstColumnSizeRatio) this.firstColumnSizeRatio = element.firstColumnSizeRatio;
        this.rows = element.rows !== undefined ? element.rows?.map(row => new LikertRowElement(row, ...args)) : [];
    
        this.position = ElementFactory.initPositionProps(element.position);
        this.styling = {
          ...ElementFactory.initStylingProps({
            backgroundColor: 'transparent',
            lineHeight: 135,
            lineColoring: true,
            lineColoringColor: '#c9e0e0',
            ...element.styling
          })
        };
      }
    
    
      // eslint-disable-next-line class-methods-use-this
      getNewOption(optionText: string): Label {
        return { text: optionText, imgSrc: null, imgPosition: 'above' };
      }
    
      setProperty(property: string, value: UIElementValue): void {
        super.setProperty(property, value);
        if (property === 'rows') {
          this.rows = value as LikertRowElement[];
        }
        if (property === 'options') {
          this.getChildElements().forEach(childElement => childElement.setProperty('columnCount', this.options.length));
        }
        if (property === 'readOnly') {
          this.getChildElements().forEach(childElement => childElement.setProperty('readOnly', value));
        }
      }
    
    
      getElementComponent(): Type<ElementComponent> {
    
        return LikertComponent;
      }
    
      getChildElements(): UIElement[] {
        return this.rows;
      }
    }