Skip to content
Snippets Groups Projects
dropdown.ts 1.76 KiB
Newer Older
  • Learn to ignore specific revisions
  • import { Type } from '@angular/core';
    
    import { ElementFactory } from 'common/util/element.factory';
    
      BasicStyles, InputElement, TextLabel, PositionedUIElement, PositionProperties, OptionElement,
    
      AnswerScheme, AnswerSchemeValue
    
    } from 'common/models/elements/element';
    
    import { ElementComponent } from 'common/directives/element-component.directive';
    import { DropdownComponent } from 'common/components/input-elements/dropdown.component';
    
    
    export class DropdownElement extends InputElement implements PositionedUIElement, OptionElement {
      options: TextLabel[] = [];
    
      allowUnset: boolean = false;
      position: PositionProperties;
      styling: BasicStyles;
    
    
    rhenck's avatar
    rhenck committed
      constructor(element: Partial<DropdownElement>, ...args: unknown[]) {
        super({ width: 240, height: 83, ...element }, ...args);
    
        if (element.options) this.options = [...element.options];
    
    rhenck's avatar
    rhenck committed
        if (element.allowUnset) this.allowUnset = element.allowUnset;
    
        this.position = ElementFactory.initPositionProps(element.position);
        this.styling = {
          ...ElementFactory.initStylingProps(element.styling)
        };
      }
    
    
      hasAnswerScheme(): boolean {
    
        return Boolean(this.getAnswerScheme);
    
      getAnswerScheme(): AnswerScheme {
    
        return {
          id: this.id,
          type: 'integer',
          format: '',
          multiple: false,
          nullable: this.allowUnset,
    
          values: this.getAnswerSchemeValues(),
    
      private getAnswerSchemeValues(): AnswerSchemeValue[] {
    
        return this.options
          .map((option, index) => ({ value: (index + 1).toString(), label: option.text }));
    
      getElementComponent(): Type<ElementComponent> {
    
        return DropdownComponent;
      }
    
    rhenck's avatar
    rhenck committed
      getNewOptionLabel(optionText: string): TextLabel {
        return ElementFactory.createOptionLabel(optionText) as TextLabel;