Skip to content
Snippets Groups Projects
section.ts 2.62 KiB
Newer Older
rhenck's avatar
rhenck committed
import {
  PositionedElement,
rhenck's avatar
rhenck committed
  UIElement
} from './uI-element';
rhenck's avatar
rhenck committed
import * as ElementFactory from '../util/element.factory';

export class Section {
  [index: string]: string | number | boolean | UIElement[] | ((...args: any) => any);
  elements: PositionedElement[] = [];
rhenck's avatar
rhenck committed
  height: number = 400;
  backgroundColor: string = 'white';
  dynamicPositioning: boolean = false;
  autoColumnSize: boolean = true;
  autoRowSize: boolean = true;
rhenck's avatar
rhenck committed
  gridColumnSizes: string = '1fr 1fr';
  gridRowSizes: string = '1fr';

  constructor(serializedSection?: Section) {
    Object.assign(this, serializedSection);
    this.elements = [];
    if (serializedSection) {
rhenck's avatar
rhenck committed
      serializedSection?.elements.forEach((element: UIElement) => {
        this.elements.push(ElementFactory.createElement(element) as PositionedElement);
  addElement(element: PositionedElement): void {
rhenck's avatar
rhenck committed
    this.elements.push(element);
rhenck's avatar
rhenck committed
  }

  deleteElements(elements: UIElement[]): void {
    this.elements = this.elements.filter(element => !elements.includes(element));
  }

  updateProperty(property: string, value: string | number | boolean): void {
    if (property === 'dynamicPositioning') {
      this.setDynamicPositioning(value as boolean);
    } else {
      this[property] = value;
    }
  }

  private setDynamicPositioning(value: boolean): void {
    this.dynamicPositioning = value;
    this.elements.forEach((element: PositionedElement) => {
      element.positionProps.dynamicPositioning = value;
  static alignElements(elements: PositionedElement[], alignmentDirection: 'left' | 'right' | 'top' | 'bottom'): void {
rhenck's avatar
rhenck committed
    let newValue: number;
    switch (alignmentDirection) {
      case 'left':
        newValue = Math.min(...elements.map(element => element.positionProps.xPosition));
rhenck's avatar
rhenck committed
        elements.forEach((element: UIElement) => {
          element.xPosition = newValue;
        });
        break;
      case 'right':
        newValue = Math.max(...elements.map(element => element.positionProps.xPosition + element.width));
rhenck's avatar
rhenck committed
        elements.forEach((element: UIElement) => {
          element.xPosition = newValue - element.width;
        });
        break;
      case 'top':
        newValue = Math.min(...elements.map(element => element.positionProps.yPosition));
rhenck's avatar
rhenck committed
        elements.forEach((element: UIElement) => {
          element.yPosition = newValue;
        });
        break;
      case 'bottom':
        newValue = Math.max(...elements.map(element => element.positionProps.yPosition + element.height));
rhenck's avatar
rhenck committed
        elements.forEach((element: UIElement) => {
          element.yPosition = newValue - element.height;
        });
        break;
      // no default
    }
  }
}