diff -r 69e26359f2c8 -r cf829ec742b3 src/main/scala/net/tz/lift/snippet/Table.scala --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/scala/net/tz/lift/snippet/Table.scala Sun Apr 03 15:55:02 2011 +0200 @@ -0,0 +1,78 @@ +/* + * Copyright 2011 Tomas Zeman + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package net.tz.lift.snippet + +import net.liftweb.common._ +import net.liftweb.util.Helpers._ +import scala.xml.{Elem, NodeSeq, Text} + +object Column { + def apply[T](name: String, f: T => NodeSeq): Column[T] = + new Column[T](Text(name), f, { _ => Empty}) + def apply[T](name: String, f: T => NodeSeq, tdCss: String): Column[T] = + new Column[T](Text(name), f, { _ => Full(tdCss)}) +} + +class Column[T](name: => NodeSeq, f: T => NodeSeq, tdCss: T => Box[String]) + extends Function1[T, NodeSeq] { + + def th: Elem = {name} + def td(in: T): Elem = { + val r = {apply(in)} + tdCss(in) map { css => r % ("class" -> css) } openOr r + } + def apply(in: T): NodeSeq = f(in) +} + +object Table { + def apply[T](cols: Iterable[Column[T]], rows: Iterable[T]) = new Table(Empty, + cols, rows, Empty) + def apply[T](css: String, cols: Iterable[Column[T]], rows: Iterable[T], + foot: NodeSeq) = new Table(Full(css), cols, rows, Full(foot)) +} + +class Table[T](css: Box[String], cols: Iterable[Column[T]], + rows: => Iterable[T], foot: Box[NodeSeq]) extends + Function1[NodeSeq, NodeSeq] { + + def oddEven(i: Int) = (i % 2) match { + case 1 => "even" + case 0 => "odd" + } + + def thead: Elem = { cols.map(c => c.th) } + def tbody: Elem = { + rows.zipWithIndex.map( r => tr(r._1, r._2)) + } + def tr(in: T, idx: Int): Elem = { + cols.map(_.td(in)) + } + + def tfoot: Elem = { + foot map { f => {f} + } openOr NodeSeq.Empty + } + + def apply(ns: NodeSeq): NodeSeq = { + val t = { + List(thead, tfoot, tbody) + }
+ css map { cl => t % ("class" -> cl) } openOr t + } + +} + +// vim: set ts=2 sw=2 et: