All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.wa9nnn.wa9nnnutil.tableui.Table.scala Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (C) @year  Dick Lieber, WA9NNN
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see .
 */

package com.wa9nnn.wa9nnnutil.tableui


/**
 * Something that can be rendered as an html  using the play Whirl template /app/views/renderTable.scala.html
 *
 * @param headers  zero or more rows of column headers [[Cell]]s that makeup the 
 * @param rows     in the 
 * @param cssClass class of table element.
 */
case class Table(headers: Seq[Seq[Cell]], rows: Seq[Row], id: Option[String] = None, cssClass: Seq[String] = Seq("headeredTable")) {

  lazy val cols: Int = headers
    .lastOption // are there headers
    .map(_.length) // how long is the last one.
    .getOrElse(0) // 0 if not header
  val columnHeaders: Seq[Seq[Cell]] = headers.map(hw => hw.map(Cell(_)))

  def withId(id: String): Table = copy(id = Option(id))

  def withCssClass(css: String): Table = copy(cssClass = cssClass.appended(css))


  def append(newRows: Seq[Row]): Table =
    copy(rows =
      rows.appendedAll(newRows)
    )

  def append(tableSection: TableSection): Table =
    copy(rows =
      rows.appendedAll(tableSection.rows)
    )

  //  /**
  //   * Add rows to this table with a header
  //   * This may not correctly set the colspan of the section headers corredtly it the lat row o the header '
  //   * doesn't match the length of [[Cell]]s in the new [[Rows]]s.
  //   *
  //   * @param sectionName full with 
   */
  def apply(header: Header, rows: Seq[RowSectionOrKV]): Table = {
    val rowBuilder = Seq.newBuilder[Row]
    rows.foreach {
      case r: Row =>
        rowBuilder += r

      case (name: String, value: Any) =>
        rowBuilder += Row(Seq(Cell(name), Cell(value)))

      case t: TableSection =>
        rowBuilder ++= t.rows
    }

    Table(header.rows, rowBuilder.result())
  }

  type RowSectionOrKV = Row | TableSection | (String, Any)
}