Skip to content
Snippets Groups Projects
text-area.ts 4.33 KiB
Newer Older
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 { VariableInfo } from '@iqb/responses';
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 TextAreaProperties {
  type: UIElementType = 'text-area';
rhenck's avatar
rhenck committed
  appearance: 'fill' | 'outline' = 'outline';
  resizeEnabled: boolean = false;
  hasDynamicRowCount: boolean = false;
  hasAutoHeight: boolean = false;
rhenck's avatar
rhenck committed
  rowCount: number = 3;
  expectedCharactersCount: number = 300;
  hasReturnKey: boolean = false;
  hasKeyboardIcon: boolean = false;
  position: PositionProperties;
  styling: BasicStyles & {
    lineHeight: number;
  };

  static title: string = 'Eingabebereich';
  static icon: string = 'edit_note';

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.hasAutoHeight = element.hasAutoHeight;
rhenck's avatar
rhenck committed
      this.expectedCharactersCount = element.expectedCharactersCount;
      this.hasReturnKey = element.hasReturnKey;
      this.hasKeyboardIcon = element.hasKeyboardIcon;
      this.position = { ...element.position };
      this.styling = { ...element.styling };
rhenck's avatar
rhenck committed
    } 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
      };
    }
  getVariableInfos(): VariableInfo[] {
    return [{
      id: this.id,
      type: 'string',
      format: '',
      multiple: false,
      nullable: false,
      valuePositionLabels: [],
      page: '',
  getElementComponent(): Type<ElementComponent> {
    return TextAreaComponent;
  }

  getDuplicate(): TextAreaElement {
    return new TextAreaElement(this);
  }

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.hasAutoHeight !== undefined &&
rhenck's avatar
rhenck committed
  blueprint.rowCount !== undefined &&
  blueprint.expectedCharactersCount !== undefined &&
  blueprint.hasReturnKey !== undefined &&
  blueprint.hasKeyboardIcon !== undefined &&
  PropertyGroupValidators.isValidPosition(blueprint.position) &&
  PropertyGroupValidators.isValidBasicStyles(blueprint.styling) &&
  blueprint.styling.lineHeight !== undefined;
}