| author | Tomas Zeman <tzeman@volny.cz> |
| Fri, 10 Feb 2012 09:53:04 +0100 | |
| changeset 5 | 993582ca8d2e |
| permissions | -rw-r--r-- |
|
5
993582ca8d2e
Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff
changeset
|
1 |
/* |
|
993582ca8d2e
Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff
changeset
|
2 |
* Copyright 2011 Tomas Zeman <tzeman@volny.cz> |
|
993582ca8d2e
Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff
changeset
|
3 |
* |
|
993582ca8d2e
Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff
changeset
|
4 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
993582ca8d2e
Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff
changeset
|
5 |
* you may not use this file except in compliance with the License. |
|
993582ca8d2e
Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff
changeset
|
6 |
* You may obtain a copy of the License at |
|
993582ca8d2e
Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff
changeset
|
7 |
* |
|
993582ca8d2e
Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff
changeset
|
8 |
* http://www.apache.org/licenses/LICENSE-2.0 |
|
993582ca8d2e
Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff
changeset
|
9 |
* |
|
993582ca8d2e
Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff
changeset
|
10 |
* Unless required by applicable law or agreed to in writing, software |
|
993582ca8d2e
Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff
changeset
|
11 |
* distributed under the License is distributed on an "AS IS" BASIS, |
|
993582ca8d2e
Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff
changeset
|
12 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
993582ca8d2e
Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff
changeset
|
13 |
* See the License for the specific language governing permissions and |
|
993582ca8d2e
Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff
changeset
|
14 |
* limitations under the License. |
|
993582ca8d2e
Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff
changeset
|
15 |
*/ |
|
993582ca8d2e
Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff
changeset
|
16 |
package net.tz.lift.snippet |
|
993582ca8d2e
Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff
changeset
|
17 |
|
|
993582ca8d2e
Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff
changeset
|
18 |
import net.liftweb.common._ |
|
993582ca8d2e
Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff
changeset
|
19 |
import net.liftweb.util.Helpers._ |
|
993582ca8d2e
Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff
changeset
|
20 |
import scala.xml.{Elem, NodeSeq, Text}
|
|
993582ca8d2e
Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff
changeset
|
21 |
|
|
993582ca8d2e
Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff
changeset
|
22 |
object Column {
|
|
993582ca8d2e
Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff
changeset
|
23 |
def apply[T](name: String, f: T => NodeSeq): Column[T] = |
|
993582ca8d2e
Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff
changeset
|
24 |
new Column[T](Text(name), f, { _ => Empty})
|
|
993582ca8d2e
Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff
changeset
|
25 |
def apply[T](name: String, f: T => NodeSeq, tdCss: String): Column[T] = |
|
993582ca8d2e
Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff
changeset
|
26 |
new Column[T](Text(name), f, { _ => Full(tdCss)})
|
|
993582ca8d2e
Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff
changeset
|
27 |
} |
|
993582ca8d2e
Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff
changeset
|
28 |
|
|
993582ca8d2e
Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff
changeset
|
29 |
class Column[T](name: => NodeSeq, f: T => NodeSeq, tdCss: T => Box[String]) |
|
993582ca8d2e
Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff
changeset
|
30 |
extends Function1[T, NodeSeq] {
|
|
993582ca8d2e
Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff
changeset
|
31 |
|
|
993582ca8d2e
Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff
changeset
|
32 |
def th: Elem = <th>{name}</th>
|
|
993582ca8d2e
Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff
changeset
|
33 |
def td(in: T): Elem = {
|
|
993582ca8d2e
Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff
changeset
|
34 |
val r = <td>{apply(in)}</td>
|
|
993582ca8d2e
Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff
changeset
|
35 |
tdCss(in) map { css => r % ("class" -> css) } openOr r
|
|
993582ca8d2e
Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff
changeset
|
36 |
} |
|
993582ca8d2e
Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff
changeset
|
37 |
def apply(in: T): NodeSeq = f(in) |
|
993582ca8d2e
Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff
changeset
|
38 |
} |
|
993582ca8d2e
Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff
changeset
|
39 |
|
|
993582ca8d2e
Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff
changeset
|
40 |
object Table {
|
|
993582ca8d2e
Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff
changeset
|
41 |
def apply[T](cols: Iterable[Column[T]], rows: Iterable[T]) = new Table(Empty, |
|
993582ca8d2e
Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff
changeset
|
42 |
cols, rows, Empty) |
|
993582ca8d2e
Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff
changeset
|
43 |
def apply[T](css: String, cols: Iterable[Column[T]], rows: Iterable[T], |
|
993582ca8d2e
Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff
changeset
|
44 |
foot: NodeSeq) = new Table(Full(css), cols, rows, Full(foot)) |
|
993582ca8d2e
Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff
changeset
|
45 |
} |
|
993582ca8d2e
Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff
changeset
|
46 |
|
|
993582ca8d2e
Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff
changeset
|
47 |
class Table[T](css: Box[String], cols: Iterable[Column[T]], |
|
993582ca8d2e
Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff
changeset
|
48 |
rows: => Iterable[T], foot: Box[NodeSeq]) extends |
|
993582ca8d2e
Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff
changeset
|
49 |
Function1[NodeSeq, NodeSeq] {
|
|
993582ca8d2e
Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff
changeset
|
50 |
|
|
993582ca8d2e
Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff
changeset
|
51 |
def oddEven(i: Int) = (i % 2) match {
|
|
993582ca8d2e
Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff
changeset
|
52 |
case 1 => "even" |
|
993582ca8d2e
Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff
changeset
|
53 |
case 0 => "odd" |
|
993582ca8d2e
Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff
changeset
|
54 |
} |
|
993582ca8d2e
Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff
changeset
|
55 |
|
|
993582ca8d2e
Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff
changeset
|
56 |
def thead: Elem = <thead><tr>{ cols.map(c => c.th) }</tr></thead>
|
|
993582ca8d2e
Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff
changeset
|
57 |
def tbody: Elem = <tbody>{
|
|
993582ca8d2e
Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff
changeset
|
58 |
rows.zipWithIndex.map( r => tr(r._1, r._2)) |
|
993582ca8d2e
Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff
changeset
|
59 |
}</tbody> |
|
993582ca8d2e
Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff
changeset
|
60 |
def tr(in: T, idx: Int): Elem = <tr class={oddEven(idx)}>{
|
|
993582ca8d2e
Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff
changeset
|
61 |
cols.map(_.td(in)) |
|
993582ca8d2e
Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff
changeset
|
62 |
}</tr> |
|
993582ca8d2e
Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff
changeset
|
63 |
|
|
993582ca8d2e
Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff
changeset
|
64 |
def tfoot: Elem = <tfoot>{
|
|
993582ca8d2e
Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff
changeset
|
65 |
foot map { f => <tr><td colspan={cols.size.toString}>{f}</td></tr>
|
|
993582ca8d2e
Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff
changeset
|
66 |
} openOr NodeSeq.Empty |
|
993582ca8d2e
Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff
changeset
|
67 |
}</tfoot> |
|
993582ca8d2e
Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff
changeset
|
68 |
|
|
993582ca8d2e
Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff
changeset
|
69 |
def apply(ns: NodeSeq): NodeSeq = {
|
|
993582ca8d2e
Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff
changeset
|
70 |
val t = <table>{
|
|
993582ca8d2e
Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff
changeset
|
71 |
List(thead, tfoot, tbody) |
|
993582ca8d2e
Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff
changeset
|
72 |
}</table> |
|
993582ca8d2e
Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff
changeset
|
73 |
css map { cl => t % ("class" -> cl) } openOr t
|
|
993582ca8d2e
Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff
changeset
|
74 |
} |
|
993582ca8d2e
Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff
changeset
|
75 |
|
|
993582ca8d2e
Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff
changeset
|
76 |
} |
|
993582ca8d2e
Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff
changeset
|
77 |
|
|
993582ca8d2e
Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff
changeset
|
78 |
// vim: set ts=2 sw=2 et: |