Skip to content
Snippets Groups Projects
table.ts 3.75 KiB
Newer Older
  • Learn to ignore specific revisions
  • rhenck's avatar
    rhenck committed
    import {
      UIElement, CompoundElement, PositionedUIElement,
      UIElementProperties, UIElementType, UIElementValue
    } from 'common/models/elements/element';
    import {
    
    rhenck's avatar
    rhenck committed
      BorderStyles, PositionProperties,
    
    rhenck's avatar
    rhenck committed
      PropertyGroupGenerators, PropertyGroupValidators
    } from 'common/models/elements/property-group-interfaces';
    import { Type } from '@angular/core';
    import { ElementComponent } from 'common/directives/element-component.directive';
    import { InstantiationEror } from 'common/util/errors';
    import { environment } from 'common/environment';
    import { TableComponent } from 'common/components/compound-elements/table/table.component';
    import { ElementFactory } from 'common/util/element.factory';
    
    export class TableElement extends CompoundElement implements PositionedUIElement, TableProperties {
      type: UIElementType = 'table';
      gridColumnSizes: { value: number; unit: string }[] = [{ value: 1, unit: 'fr' }, { value: 1, unit: 'fr' }];
      gridRowSizes: { value: number; unit: string }[] = [{ value: 1, unit: 'fr' }, { value: 1, unit: 'fr' }];
      elements: PositionedUIElement[] = [];
      tableEdgesEnabled: boolean = false;
      position: PositionProperties;
    
    rhenck's avatar
    rhenck committed
      styling: { backgroundColor: string } & BorderStyles;
    
    rhenck's avatar
    rhenck committed
    
      static title: string = 'Tabelle';
    
    rhenck's avatar
    rhenck committed
      static icon: string = 'table_view';
    
    rhenck's avatar
    rhenck committed
    
      constructor(element?: TableProperties) {
        super(element);
        if (element && isValid(element)) {
          this.gridColumnSizes = element.gridColumnSizes;
          this.gridRowSizes = element.gridRowSizes;
          this.elements = element.elements
            .map(el => ElementFactory.createElement(el)) as PositionedUIElement[];
          this.tableEdgesEnabled = element.tableEdgesEnabled;
          this.position = { ...element.position };
          this.styling = { ...element.styling };
        } else {
          if (environment.strictInstantiation) {
            throw new InstantiationEror('Error at Cloze instantiation', element);
          }
          if (element?.gridColumnSizes !== undefined) this.gridColumnSizes = element.gridColumnSizes;
          if (element?.gridRowSizes !== undefined) this.gridRowSizes = element.gridRowSizes;
          this.elements = element?.elements !== undefined ?
            element.elements.map(el => ElementFactory.createElement(el)) as PositionedUIElement[] :
            [];
          if (element?.tableEdgesEnabled !== undefined) this.tableEdgesEnabled = element.tableEdgesEnabled;
          this.position = PropertyGroupGenerators.generatePositionProps(element?.position);
          this.styling = {
    
    rhenck's avatar
    rhenck committed
            backgroundColor: 'transparent',
    
    rhenck's avatar
    rhenck committed
            ...PropertyGroupGenerators.generateBorderStylingProps({
              borderWidth: 1,
              ...element?.styling
    
    rhenck's avatar
    rhenck committed
            }),
            ...element?.styling
    
    rhenck's avatar
    rhenck committed
          };
        }
      }
    
      setProperty(property: string, value: UIElementValue): void {
        // Don't preserve original array, so Component gets updated
        this[property] = value;
      }
    
      getElementComponent(): Type<ElementComponent> {
        return TableComponent;
      }
    
      getDuplicate(): UIElement {
        return new TableElement(this);
      }
    
      getChildElements(): UIElement[] {
        return this.elements;
      }
    }
    
    export interface TableProperties extends UIElementProperties {
      gridColumnSizes: { value: number; unit: string }[];
      gridRowSizes: { value: number; unit: string }[];
      elements: UIElement[];
      tableEdgesEnabled: boolean;
      position: PositionProperties;
    
    rhenck's avatar
    rhenck committed
      styling: { backgroundColor: string } & BorderStyles;
    
    rhenck's avatar
    rhenck committed
    }
    
    function isValid(blueprint?: TableProperties): boolean {
      if (!blueprint) return false;
      return blueprint.gridColumnSizes !== undefined &&
    
    rhenck's avatar
    rhenck committed
        blueprint.gridRowSizes !== undefined &&
    
    rhenck's avatar
    rhenck committed
        blueprint.elements !== undefined &&
        blueprint.tableEdgesEnabled !== undefined &&
        PropertyGroupValidators.isValidPosition(blueprint.position) &&
    
    rhenck's avatar
    rhenck committed
        blueprint.styling.backgroundColor !== undefined &&
        PropertyGroupValidators.isValidBorderStyles(blueprint.styling);