Skip to content
Snippets Groups Projects
text-area.ts 4.11 KiB
Newer Older
  • Learn to ignore specific revisions
  • import { Type } from '@angular/core';
    
    import {
    
    rhenck's avatar
    rhenck committed
      PositionedUIElement, TextInputElement, TextInputElementProperties, UIElementType
    
    } from 'common/models/elements/element';
    import { ElementComponent } from 'common/directives/element-component.directive';
    import { TextAreaComponent } from 'common/components/input-elements/text-area.component';
    
    import { AnswerScheme } from 'common/models/elements/answer-scheme-interfaces';
    
    rhenck's avatar
    rhenck committed
      BasicStyles, PositionProperties, PropertyGroupGenerators, PropertyGroupValidators
    
    } from 'common/models/elements/property-group-interfaces';
    
    rhenck's avatar
    rhenck committed
    import { environment } from 'common/environment';
    import { InstantiationEror } from 'common/util/errors';
    
    export class TextAreaElement extends TextInputElement implements PositionedUIElement, TextAreaProperties {
      type: UIElementType = 'text-area';
    
    rhenck's avatar
    rhenck committed
      appearance: 'fill' | 'outline' = 'outline';
      resizeEnabled: boolean = false;
      hasDynamicRowCount: boolean = false;
      rowCount: number = 3;
      expectedCharactersCount: number = 300;
      hasReturnKey: boolean = false;
      hasKeyboardIcon: boolean = false;
    
      position: PositionProperties;
      styling: BasicStyles & {
        lineHeight: number;
      };
    
    
    rhenck's avatar
    rhenck committed
      constructor(element?: TextAreaProperties) {
    
    rhenck's avatar
    rhenck committed
        if (element && isValid(element)) {
          this.appearance = element.appearance;
          this.resizeEnabled = element.resizeEnabled;
          this.rowCount = element.rowCount;
          this.hasDynamicRowCount = element.hasDynamicRowCount;
          this.expectedCharactersCount = element.expectedCharactersCount;
          this.hasReturnKey = element.hasReturnKey;
          this.hasKeyboardIcon = element.hasKeyboardIcon;
          this.position = element.position;
          this.styling = element.styling;
        } else {
          if (environment.strictInstantiation) {
            throw new InstantiationEror('Error at TextArea instantiation', element);
          }
          if (element?.appearance) this.appearance = element.appearance;
          if (element?.resizeEnabled) this.resizeEnabled = element.resizeEnabled;
          if (element?.rowCount) this.rowCount = element.rowCount;
          if (element?.hasDynamicRowCount) this.hasDynamicRowCount = element.hasDynamicRowCount;
          if (element?.expectedCharactersCount) this.expectedCharactersCount = element.expectedCharactersCount;
          if (element?.hasReturnKey) this.hasReturnKey = element.hasReturnKey;
          if (element?.hasKeyboardIcon) this.hasKeyboardIcon = element.hasKeyboardIcon;
          this.dimensions = PropertyGroupGenerators.generateDimensionProps({
            width: 230,
            height: 132,
            ...element?.dimensions
          });
          this.position = PropertyGroupGenerators.generatePositionProps(element?.position);
          this.styling = {
            ...PropertyGroupGenerators.generateBasicStyleProps(element?.styling),
            lineHeight: element?.styling?.lineHeight || 135
          };
        }
    
      hasAnswerScheme(): boolean {
    
        return Boolean(this.getAnswerScheme);
    
      getAnswerScheme(): AnswerScheme {
    
        return {
          id: this.id,
          type: 'string',
          format: '',
          multiple: false,
    
          nullable: !this.value && this.value !== '',
          values: [],
    
      getElementComponent(): Type<ElementComponent> {
    
        return TextAreaComponent;
      }
    }
    
    
    export interface TextAreaProperties extends TextInputElementProperties {
      appearance: 'fill' | 'outline';
      resizeEnabled: boolean;
      hasDynamicRowCount: boolean;
      rowCount: number;
      expectedCharactersCount: number;
      hasReturnKey: boolean;
      hasKeyboardIcon: boolean;
      position: PositionProperties;
      styling: BasicStyles & {
        lineHeight: number;
      };
    }
    
    rhenck's avatar
    rhenck committed
    
    function isValid(blueprint?: TextAreaProperties): boolean {
      if (!blueprint) return false;
      return blueprint.appearance !== undefined &&
      blueprint.resizeEnabled !== undefined &&
      blueprint.hasDynamicRowCount !== undefined &&
      blueprint.rowCount !== undefined &&
      blueprint.expectedCharactersCount !== undefined &&
      blueprint.hasReturnKey !== undefined &&
      blueprint.hasKeyboardIcon !== undefined &&
      PropertyGroupValidators.isValidPosition(blueprint.position) &&
      PropertyGroupValidators.isValidBasicStyles(blueprint.styling) &&
      blueprint.styling.lineHeight !== undefined;
    }