Skip to content
Snippets Groups Projects
page.ts 3.5 KiB
Newer Older
  • Learn to ignore specific revisions
  • import { Section } from 'common/models/section';
    
    import { UIElement } from 'common/models/elements/element';
    
    import { VariableInfo } from '@iqb/responses';
    
    rhenck's avatar
    rhenck committed
    import { environment } from 'common/environment';
    
    import { InstantiationEror } from 'common/util/errors';
    
    import { DropListElement } from 'common/models/elements/input-elements/drop-list';
    
    
    export class Page {
    
    rhenck's avatar
    rhenck committed
      [index: string]: unknown;
    
      sections: Section[] = [];
    
      hasMaxWidth: boolean = true;
      maxWidth: number = 750;
    
      margin: number = 30;
      backgroundColor: string = '#ffffff';
      alwaysVisible: boolean = false;
      alwaysVisiblePagePosition: 'left' | 'right' | 'top' | 'bottom' = 'left';
      alwaysVisibleAspectRatio: number = 50;
    
    
    rhenck's avatar
    rhenck committed
      constructor(page?: PageProperties) {
        if (page && isValid(page)) {
          this.hasMaxWidth = page.hasMaxWidth;
          this.maxWidth = page.maxWidth;
          this.margin = page.margin;
          this.backgroundColor = page.backgroundColor;
          this.alwaysVisible = page.alwaysVisible;
          this.alwaysVisiblePagePosition = page.alwaysVisiblePagePosition;
          this.alwaysVisibleAspectRatio = page.alwaysVisibleAspectRatio;
          this.sections = page.sections.map(section => new Section(section));
        } else {
          if (environment.strictInstantiation) {
    
            throw new InstantiationEror('Error at Page instantiation');
    
    rhenck's avatar
    rhenck committed
          }
          if (page?.hasMaxWidth !== undefined) this.hasMaxWidth = page.hasMaxWidth;
          if (page?.maxWidth !== undefined) this.maxWidth = page.maxWidth;
          if (page?.margin !== undefined) this.margin = page.margin;
          if (page?.backgroundColor !== undefined) this.backgroundColor = page.backgroundColor;
          if (page?.alwaysVisible !== undefined) this.alwaysVisible = page.alwaysVisible;
          if (page?.alwaysVisiblePagePosition !== undefined) this.alwaysVisiblePagePosition = page.alwaysVisiblePagePosition;
          if (page?.alwaysVisibleAspectRatio !== undefined) this.alwaysVisibleAspectRatio = page.alwaysVisibleAspectRatio;
          this.sections = page?.sections.map(section => new Section(section)) || [new Section()];
        }
    
    
      getAllElements(elementType?: string): UIElement[] {
        return this.sections.map(section => section.getAllElements(elementType)).flat();
      }
    
      getVariableInfos(dropLists: DropListElement[]): VariableInfo[] {
        return this.sections.map(section => section.getVariableInfos(dropLists)).flat();
    
    rhenck's avatar
    rhenck committed
    
      addSection(section?: Section, sectionIndex?: number): void {
        if (sectionIndex !== undefined) {
          this.sections.splice(sectionIndex, 0, section || new Section());
        } else {
          this.sections.push(section || new Section());
        }
      }
    
    
    rhenck's avatar
    rhenck committed
      replaceSection(sectionIndex: number, section: Section): void {
        this.sections.splice(sectionIndex, 1, section);
      }
    
    
    rhenck's avatar
    rhenck committed
      deleteSection(sectionIndex: number): Section {
        return this.sections.splice(sectionIndex, 1)[0];
    
    rhenck's avatar
    rhenck committed
      }
    
    
    export interface PageProperties {
      sections: Section[];
      hasMaxWidth: boolean;
      maxWidth: number;
      margin: number;
      backgroundColor: string;
      alwaysVisible: boolean;
      alwaysVisiblePagePosition: 'left' | 'right' | 'top' | 'bottom';
      alwaysVisibleAspectRatio: number;
    }
    
    rhenck's avatar
    rhenck committed
    
    function isValid(blueprint?: PageProperties): boolean {
      if (!blueprint) return false;
      return blueprint.sections !== undefined &&
        blueprint.hasMaxWidth !== undefined &&
        blueprint.maxWidth !== undefined &&
        blueprint.margin !== undefined &&
        blueprint.backgroundColor !== undefined &&
        blueprint.alwaysVisible !== undefined &&
        blueprint.alwaysVisiblePagePosition !== undefined &&
        blueprint.alwaysVisibleAspectRatio !== undefined;
    }