Skip to content
Snippets Groups Projects
unit.ts 1.98 KiB
Newer Older
  • Learn to ignore specific revisions
  • import { Page } from 'common/models/page';
    
    import { UIElement } from 'common/models/elements/element';
    
    import { VariableInfo } from '@iqb/responses';
    
    import { StateVariable } from 'common/models/state-variable';
    
    rhenck's avatar
    rhenck committed
    import { environment } from 'common/environment';
    import { VersionManager } from 'common/services/version-manager';
    
    import { InstantiationEror } from 'common/util/errors';
    
    rhenck's avatar
    rhenck committed
    import { ArrayUtils } from 'common/util/array';
    
    import { DropListElement } from 'common/models/elements/input-elements/drop-list';
    
    export class Unit implements UnitProperties {
    
      type = 'aspect-unit-definition';
      version: string;
    
    rhenck's avatar
    rhenck committed
      stateVariables: StateVariable[] = [];
    
    rhenck's avatar
    rhenck committed
      constructor(unit?: UnitProperties) {
        if (unit && isValid(unit)) {
          this.version = unit.version;
          this.stateVariables = unit.stateVariables;
          this.pages = unit.pages.map(page => new Page(page));
        } else {
          if (environment.strictInstantiation) {
    
            throw new InstantiationEror('Error at unit instantiation');
    
    rhenck's avatar
    rhenck committed
          }
          this.version = VersionManager.getCurrentVersion();
          if (unit?.stateVariables !== undefined) this.stateVariables = unit.stateVariables;
          this.pages = unit?.pages.map(page => new Page(page)) || [new Page()];
        }
    
      }
    
      getAllElements(elementType?: string): UIElement[] {
        return this.pages.map(page => page.getAllElements(elementType)).flat();
    
      getVariableInfos(): VariableInfo[] {
    
        const dropLists: DropListElement[] = [
          ...this.getAllElements('drop-list') as DropListElement[]
    
        return this.pages.map(page => page.getVariableInfos(dropLists)).flat();
    
    rhenck's avatar
    rhenck committed
    function isValid(blueprint?: UnitProperties): boolean {
      if (!blueprint) return false;
    
      return blueprint.version === VersionManager.getCurrentVersion() &&
    
    rhenck's avatar
    rhenck committed
        blueprint.stateVariables !== undefined &&
        blueprint.type !== undefined &&
        blueprint.pages !== undefined;
    }
    
    
    export interface UnitProperties {
      type: string;
      version: string;
    
    rhenck's avatar
    rhenck committed
      stateVariables: StateVariable[];