Skip to content
Snippets Groups Projects
math-field.ts 2.52 KiB
Newer Older
rhenck's avatar
rhenck committed
import {
  InputElement, InputElementProperties, UIElementType
rhenck's avatar
rhenck committed
} from 'common/models/elements/element';
import { Type } from '@angular/core';
import { ElementComponent } from 'common/directives/element-component.directive';
import { MathFieldComponent } from 'common/components/input-elements/math-field.component';
import { AnswerScheme } from 'common/models/elements/answer-scheme-interfaces';
rhenck's avatar
rhenck committed
import {
  BasicStyles, PositionProperties, PropertyGroupGenerators, PropertyGroupValidators
} from 'common/models/elements/property-group-interfaces';
import { environment } from 'common/environment';
import { InstantiationEror } from 'common/util/errors';
export class MathFieldElement extends InputElement implements MathFieldProperties {
  type: UIElementType = 'math-field';
rhenck's avatar
rhenck committed
  enableModeSwitch: boolean = false;
rhenck's avatar
rhenck committed
  position: PositionProperties;
  styling: BasicStyles & {
    lineHeight: number;
  };
rhenck's avatar
rhenck committed
  constructor(element?: MathFieldProperties) {
rhenck's avatar
rhenck committed
    super(element);
rhenck's avatar
rhenck committed
    if (element && isValid(element)) {
      this.enableModeSwitch = element.enableModeSwitch;
      this.position = { ...element.position };
      this.styling = { ...element.styling };
rhenck's avatar
rhenck committed
    } else {
      if (environment.strictInstantiation) {
        throw new InstantiationEror('Error at Mathfield instantiation', element);
      }
      if (element?.enableModeSwitch !== undefined) this.enableModeSwitch = element.enableModeSwitch;
      this.position = PropertyGroupGenerators.generatePositionProps(element?.position);
      this.styling = {
        ...PropertyGroupGenerators.generateBasicStyleProps(element?.styling),
        lineHeight: element?.styling?.lineHeight || 135
      };
    }
  getDuplicate(): MathFieldElement {
    return new MathFieldElement(this);
  }

rhenck's avatar
rhenck committed
  getAnswerScheme(): AnswerScheme {
    return {
      id: this.id,
      type: 'string',
      format: 'latex',
rhenck's avatar
rhenck committed
      multiple: false,
      nullable: false,
rhenck's avatar
rhenck committed
      values: [],
      valuesComplete: false
    };
  }

  getElementComponent(): Type<ElementComponent> {
    return MathFieldComponent;
  }
}

export interface MathFieldProperties extends InputElementProperties {
  enableModeSwitch: boolean;
rhenck's avatar
rhenck committed
  position: PositionProperties;
  styling: BasicStyles & {
    lineHeight: number;
  };
}
rhenck's avatar
rhenck committed

function isValid(blueprint?: MathFieldProperties): boolean {
  if (!blueprint) return false;
  return blueprint.enableModeSwitch !== undefined &&
    PropertyGroupValidators.isValidPosition(blueprint.position) &&
    PropertyGroupValidators.isValidBasicStyles(blueprint.styling) &&
    blueprint.styling.lineHeight !== undefined;
}