src/main/scala/net/tz/lift/snippet/Panel.scala
changeset 3 61b9f48ce17d
child 20 22424e6508cd
equal deleted inserted replaced
2:a1a5ebc06887 3:61b9f48ce17d
       
     1 /*
       
     2  * Copyright 2011 Tomas Zeman <tzeman@volny.cz>
       
     3  *
       
     4  * Licensed under the Apache License, Version 2.0 (the "License");
       
     5  * you may not use this file except in compliance with the License.
       
     6  * You may obtain a copy of the License at
       
     7  *
       
     8  *     http://www.apache.org/licenses/LICENSE-2.0
       
     9  *
       
    10  * Unless required by applicable law or agreed to in writing, software
       
    11  * distributed under the License is distributed on an "AS IS" BASIS,
       
    12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
       
    13  * See the License for the specific language governing permissions and
       
    14  * limitations under the License.
       
    15  */
       
    16 package net.tz.lift.snippet
       
    17 
       
    18 import net.liftweb.mapper.MappedField
       
    19 import scala.xml.{NodeSeq, Text}
       
    20 
       
    21 object AttrRow {
       
    22   def apply(name: => NodeSeq, value: => NodeSeq) =  new AttrRow(name, value,
       
    23     "attr-name", "attr-value")
       
    24   def apply(f: MappedField[_, _]) = new AttrRow(Text(f.displayName), f.asHtml,
       
    25     "attr-name", "attr-value")
       
    26   def formRow(name: => NodeSeq, input: => NodeSeq) = new AttrRow(name, input,
       
    27     "form-name", "form-value")
       
    28   def submitRow(button: => NodeSeq) = new SpanRow(button)
       
    29 }
       
    30 
       
    31 class AttrRow(name: => NodeSeq, value: => NodeSeq, nameCss: String,
       
    32   valueCss: String) extends Function0[NodeSeq] {
       
    33   def tdN = <td class={nameCss}>{name}</td>
       
    34   def tdV = <td class={valueCss}>{value}</td>
       
    35   def apply(): NodeSeq = <tr>{tdN :: tdV :: Nil}</tr>
       
    36 }
       
    37 
       
    38 class SpanRow(value: => NodeSeq) extends AttrRow(NodeSeq.Empty, value, "", "")
       
    39 {
       
    40   override def apply(): NodeSeq = <tr><td colspan="2">{value}</td></tr>
       
    41 }
       
    42 
       
    43 object Panel {
       
    44   def apply(attrs: Iterable[AttrRow]) = new Panel(attrs)
       
    45   def fromFields(fields: Iterable[MappedField[_,_]]) =
       
    46     new Panel(fields.map(AttrRow(_)))
       
    47 }
       
    48 
       
    49 class Panel(attrs: => Iterable[AttrRow]) extends Function1[NodeSeq, NodeSeq]
       
    50 {
       
    51   def apply(in: NodeSeq): NodeSeq = <table>{attrs.map(_())}</table>
       
    52   def &(other: Function1[NodeSeq, NodeSeq]) = new Function1[NodeSeq, NodeSeq] {
       
    53     def apply(in: NodeSeq): NodeSeq = List(Panel.this, other) flatMap (_(in))
       
    54   }
       
    55 }
       
    56 
       
    57 // vim: set ts=2 sw=2 et: