Skip to content
Snippets Groups Projects
geometry.ts 4.35 KiB
Newer Older
  • Learn to ignore specific revisions
  • import { Type } from '@angular/core';
    import {
    
      PositionedUIElement, UIElement, UIElementProperties, UIElementType
    
    } from 'common/models/elements/element';
    import { ElementComponent } from 'common/directives/element-component.directive';
    import { GeometryComponent } from 'common/components/geometry/geometry.component';
    
    rhenck's avatar
    rhenck committed
    import {
      PositionProperties,
      PropertyGroupGenerators, PropertyGroupValidators
    } from 'common/models/elements/property-group-interfaces';
    
    import { AnswerScheme } from 'common/models/elements/answer-scheme-interfaces';
    
    rhenck's avatar
    rhenck committed
    import { environment } from 'common/environment';
    import { InstantiationEror } from 'common/util/errors';
    
    export class GeometryElement extends UIElement implements PositionedUIElement, GeometryProperties {
      type: UIElementType = 'geometry';
    
    rhenck's avatar
    rhenck committed
      appDefinition: string = '';
    
      trackedVariables: string[] = [];
    
    rhenck's avatar
    rhenck committed
      showResetIcon: boolean = true;
      enableUndoRedo: boolean = true;
    
      showToolbar: boolean = true;
    
    rhenck's avatar
    rhenck committed
      enableShiftDragZoom: boolean = true;
      showZoomButtons: boolean = true;
      showFullscreenButton: boolean = true;
      customToolbar: string = '';
    
      position: PositionProperties;
    
    
    rhenck's avatar
    rhenck committed
      constructor(element?: GeometryProperties) {
    
        super(element);
    
    rhenck's avatar
    rhenck committed
        if (element && isValid(element)) {
          this.appDefinition = element.appDefinition;
    
          this.trackedVariables = [...element.trackedVariables];
    
    rhenck's avatar
    rhenck committed
          this.showResetIcon = element.showResetIcon;
          this.enableUndoRedo = element.enableUndoRedo;
          this.showToolbar = element.showToolbar;
          this.enableShiftDragZoom = element.enableShiftDragZoom;
          this.showZoomButtons = element.showZoomButtons;
          this.showFullscreenButton = element.showFullscreenButton;
          this.customToolbar = element.customToolbar;
    
          this.position = { ...element.position };
    
    rhenck's avatar
    rhenck committed
        } else {
          if (environment.strictInstantiation) {
            throw new InstantiationEror('Error at Geometry instantiation', element);
          }
          if (element?.appDefinition !== undefined) this.appDefinition = element.appDefinition;
    
          if (element?.trackedVariables !== undefined) this.trackedVariables = [...element.trackedVariables];
    
    rhenck's avatar
    rhenck committed
          if (element?.showResetIcon !== undefined) this.showResetIcon = element.showResetIcon;
          if (element?.enableUndoRedo !== undefined) this.enableUndoRedo = element.enableUndoRedo;
          if (element?.showToolbar !== undefined) this.showToolbar = element.showToolbar;
          if (element?.enableShiftDragZoom !== undefined) this.enableShiftDragZoom = element.enableShiftDragZoom;
          if (element?.showZoomButtons !== undefined) this.showZoomButtons = element.showZoomButtons;
          if (element?.showFullscreenButton !== undefined) this.showFullscreenButton = element.showFullscreenButton;
          if (element?.customToolbar !== undefined) this.customToolbar = element.customToolbar;
          this.dimensions = PropertyGroupGenerators.generateDimensionProps({
            width: 600,
            height: 400,
            ...element?.dimensions
          });
          this.position = PropertyGroupGenerators.generatePositionProps(element?.position);
        }
    
      hasAnswerScheme(): boolean {
        return Boolean(this.getAnswerScheme);
      }
    
      getAnswerScheme(): AnswerScheme {
        return {
          id: this.id,
          type: 'string',
          format: 'ggb-file',
          multiple: false,
          nullable: false,
          values: [],
          valuesComplete: false
        };
      }
    
    
      getElementComponent(): Type<ElementComponent> {
        return GeometryComponent;
      }
    
    
      getDuplicate(): GeometryElement {
        return new GeometryElement(this);
      }
    
    
    export interface GeometryProperties extends UIElementProperties {
      appDefinition: string;
    
      trackedVariables: string[];
    
      showResetIcon: boolean;
      enableUndoRedo: boolean;
      showToolbar: boolean;
      enableShiftDragZoom: boolean;
      showZoomButtons: boolean;
      showFullscreenButton: boolean;
      customToolbar: string;
      position: PositionProperties;
    }
    
    rhenck's avatar
    rhenck committed
    
    function isValid(blueprint?: GeometryProperties): boolean {
      if (!blueprint) return false;
      return blueprint.appDefinition !== undefined &&
    
        blueprint.trackedVariables !== undefined &&
    
    rhenck's avatar
    rhenck committed
        blueprint.showResetIcon !== undefined &&
        blueprint.enableUndoRedo !== undefined &&
        blueprint.showToolbar !== undefined &&
        blueprint.enableShiftDragZoom !== undefined &&
        blueprint.showZoomButtons !== undefined &&
        blueprint.showFullscreenButton !== undefined &&
        blueprint.customToolbar !== undefined &&
        PropertyGroupValidators.isValidPosition(blueprint.position);
    }