{"version":3,"file":"Table.min.mjs","sources":["../../../src/shapes/Table.ts"],"sourcesContent":["//*PMW* class addded for tables\nimport { Group, type GroupProps } from './Group';\nimport { classRegistry } from '../ClassRegistry';\nimport type { FabricObject, IText } from 'fabric';\n\nexport interface TableProps extends GroupProps {\n  rows: number;\n  columns: number;\n  layoutType: string;\n  alternateBackgroundColor1: string;\n  alternateBackgroundColor2: string;\n  highlightedRowsBackgroundColor: string;\n  highlightedRows: Array<number>;\n  tableArray: Array<Array<IText>>;\n  ySpacing: number;\n  xSpacing: number;\n  fontSize: number;\n  hasButton: boolean;\n}\n\nexport class Table extends Group {\n  /**\n   * Type of an object\n   * @type String\n   * @default\n   */\n  static type = 'table';\n\n  /**\n   * Number of table rows\n   */\n  rows = 0;\n  /**\n   * Number of table columns\n   */\n  columns = 0;\n  /**\n   * Layout style\n   */\n  layoutType = '';\n  /**\n   * Background color 1 for alternate table background\n   */\n  alternateBackgroundColor1 = null;\n  /**\n   * Background color 2 for alternate table background\n   */\n  alternateBackgroundColor2 = null;\n  /**\n   * Background color for highlighted rows\n   */\n  highlightedRowsBackgroundColor = null;\n  /**\n   * Array containing indices of highlighted rows\n   */\n  highlightedRows: Array<number> = [];\n  /**\n   * 2D array containing table data\n   */\n  tableArray: Array<Array<IText>> = [[]];\n  /**\n   * Spacing Between rows of table\n   */\n  ySpacing = 0;\n  /**\n   * Spacing Between column of table\n   */\n  xSpacing = 0;\n\n  fontSize = 0;\n  /**\n   * Property used for showing the 'edit content' button\n   */\n  hasButton = true;\n\n  constructor(objects: FabricObject[] = [], options: Partial<TableProps> = {}) {\n    super(objects,options);\n  }\n\n\n  render(ctx: CanvasRenderingContext2D) {\n    this._transformDone = true;\n    super.render(ctx);\n    ctx.save();\n    this.transform(ctx);\n    this.renderTableBorders(ctx);\n    ctx.restore();\n    this._transformDone = false;\n  }\n\n\n  /**\n   * Draws the table/schedule border\n   * @param {CanvasRenderingContext2D} ctx context to draw on\n   */\n  renderTableBorders(ctx: CanvasRenderingContext2D) {\n    if (!this.stroke || this.strokeWidth === 0) {\n      return;\n    }\n    ctx.save();\n    this._setStrokeStyles(ctx, this);\n    ctx.strokeRect(\n      -(this.width / 2),\n      -(this.height / 2),\n      this.width,\n      this.height\n    );\n\n    // if custom table layout them draw rows and column border too\n    if (this.isTableLayout()) {\n      this.drawColumnBorders(ctx);\n      this.drawRowBorders(ctx);\n    }\n    ctx.restore();\n  }\n\n  public isTable(): this is Table{\n    return true;\n  }\n\n  /**\n   * This function is responsible for rendering the background of table.\n   * It loops over all the rows in the table and draws the appropriate color rectangle for each row.\n   * If more then one consecutive rows have background of same color then it draws a one big rectangle of that color.\n   * @param {CanvasRenderingContext2D} ctx context to render on\n   */\n  _renderBackground(ctx: CanvasRenderingContext2D) {\n    if (\n      (this.highlightedRows.length == 0 &&\n        !(this.alternateBackgroundColor1 && this.alternateBackgroundColor2)) ||\n      !this.isTableLayout()\n    ) {\n      super._renderBackground(ctx);\n      return;\n    }\n\n    const backgroundData = this.getTableBackGroundData();\n    ctx.save();\n    const objects = this.getObjects();\n    let top = null;\n    let height = null;\n    let renderBackground = false;\n\n    for (let i = 0; i < backgroundData.length; i++) {\n      renderBackground = false;\n      if (backgroundData[i] != 'none') {\n        if (top == null) {\n          if (i == 0) {\n            top = -this.height / 2;\n          } else {\n            top = objects[i].top - this.ySpacing / 2;\n          }\n        }\n\n        if (backgroundData[i] != backgroundData[i + 1]) {\n          // set height of rectangle to render\n          height =\n            Math.abs(top - objects[i].top) +\n            this.getHeightOfRow(i) +\n            this.ySpacing / 2;\n          renderBackground = true;\n\n          switch (backgroundData[i]) {\n            case 'highlight':\n              // @ts-ignore\n              ctx.fillStyle = this.highlightedRowsBackgroundColor;\n              break;\n            case 'color':\n              ctx.fillStyle = this.backgroundColor;\n              break;\n            case 'alternate1':\n              // @ts-ignore\n              ctx.fillStyle = this.alternateBackgroundColor1;\n              break;\n            case 'alternate2':\n              // @ts-ignore\n              ctx.fillStyle = this.alternateBackgroundColor2;\n              break;\n          }\n        } else {\n          renderBackground = false;\n        }\n\n        if (renderBackground) {\n          ctx.fillRect(-this.width / 2, top, this.width, height ?? 0);\n          top = null;\n          height = null;\n        }\n      }\n    }\n    ctx.restore();\n  }\n\n  /**\n   * Returns an array containing string values corresponding to rows background color.\n   * 'highlight' for selected rows\n   * 'color' for when colored background is selected by user\n   * 'alternate1' for even rows when alternate background is selected\n   * 'alternate2' for odd rows when alternate background is selected\n   * 'none' for transparent background\n   * @returns {Array}\n   */\n  getTableBackGroundData() {\n    const data = [];\n    for (let i = 0; i < this.rows; i++) {\n      if (this.highlightedRows.indexOf(i) != -1) {\n        data.push('highlight');\n      } else if (this.backgroundColor != null) {\n        data.push('color');\n      } else if (\n        this.alternateBackgroundColor1 &&\n        this.alternateBackgroundColor2\n      ) {\n        if (i % 2 == 0) {\n          data.push('alternate1');\n        } else {\n          data.push('alternate2');\n        }\n      } else {\n        data.push('none');\n      }\n    }\n    return data;\n  }\n\n  /**\n   * Returns the height of an item in a given row with max height,\n   * this value is basically the minimum space in y-axis needed by this row in a table.\n   * @param {Number} row\n   * @returns {Number}\n   */\n  getHeightOfRow(row: number) {\n    let height = 0,\n      h;\n    for (let i = 0; i < this.columns; i++) {\n      h = this.tableArray[i][row].calcTextHeight();\n      if (h > height) {\n        height = h;\n      }\n    }\n    return height;\n  }\n\n  /**\n   * Returns the width of an item in a given column with max width,\n   * this value is basically the minimum space in x-axis needed by this column in a table.\n   * @param {Number} column column index\n   * @returns {Number} minimum width required by this column\n   */\n  getWidthOfColumn(column: number) {\n    let width = 0,\n      w;\n    for (let i = 0; i < this.rows; i++) {\n      w = this.tableArray[column][i].calcTextWidth();\n      if (w > width) {\n        width = w;\n      }\n    }\n    return width;\n  }\n\n  /**\n   * renders border for table columns\n   * @param {CanvasRenderingContext2D} ctx context to render on\n   */\n  drawColumnBorders(ctx: CanvasRenderingContext2D) {\n    const objects = this.getObjects() as Array<IText>;\n    let x = this.rows,\n      maxWidth,\n      w,\n      itemIndex;\n    for (let i = 2; i <= this.columns; i++) {\n      maxWidth = 0;\n      while (objects[x] && objects[x].column == i) {\n        w = objects[x].width;\n        if (w > maxWidth) {\n          maxWidth = w;\n          itemIndex = x;\n        }\n        x++;\n      }\n      if(itemIndex) {\n        ctx.beginPath();\n        ctx.moveTo(\n          objects[itemIndex].left - this.xSpacing / 2,\n          -(this.height / 2)\n        );\n        ctx.lineTo(\n          objects[itemIndex].left - this.xSpacing / 2,\n          -(this.height / 2) + this.height\n        );\n        ctx.stroke();\n      }\n    }\n  }\n\n  /**\n   * renders border for table rows\n   * @param {CanvasRenderingContext2D} ctx context to render on\n   */\n  drawRowBorders(ctx: CanvasRenderingContext2D) {\n    const objects = this.getObjects();\n    for (let i = 1; i < this.rows; i++) {\n      const startX = -this.width / 2,\n        startY = objects[i].top - this.ySpacing / 2,\n        endX = startX + this.width,\n        endY = startY;\n      ctx.beginPath();\n      ctx.moveTo(startX, startY);\n      ctx.lineTo(endX, endY);\n      ctx.stroke();\n    }\n  }\n\n  /**\n   * Returns true if design is simple table structure('custom-table' or 'layout-1'), false otherwise\n   * @returns {boolean}\n   */\n  isTableLayout() {\n    return this.layoutType == 'layout-1' || this.layoutType == 'custom-table';\n  }\n}\n\nclassRegistry.setClass(Table);\nclassRegistry.setClass(Table, 'table');\n"],"names":["Table","Group","constructor","super","arguments","length","undefined","_defineProperty","this","render","ctx","_transformDone","save","transform","renderTableBorders","restore","stroke","strokeWidth","_setStrokeStyles","strokeRect","width","height","isTableLayout","drawColumnBorders","drawRowBorders","isTable","_renderBackground","highlightedRows","alternateBackgroundColor1","alternateBackgroundColor2","backgroundData","getTableBackGroundData","objects","getObjects","top","renderBackground","i","ySpacing","Math","abs","getHeightOfRow","fillStyle","highlightedRowsBackgroundColor","backgroundColor","fillRect","data","rows","indexOf","push","row","h","columns","tableArray","calcTextHeight","getWidthOfColumn","column","w","calcTextWidth","maxWidth","itemIndex","x","beginPath","moveTo","left","xSpacing","lineTo","startX","startY","endX","endY","layoutType","class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