src/main/scala/net/tz/lift/snippet/Panel.scala
author Tomas Zeman <tzeman@volny.cz>
Wed, 30 Nov 2011 16:59:10 +0100
changeset 22 6b1a7f3429ca
parent 2 cf829ec742b3
permissions -rw-r--r--
ca1737768512bd36 Search by dynamic IP address, IP assignment history list

/*
 * Copyright 2011 Tomas Zeman <tzeman@volny.cz>
 *
 * 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.mapper.MappedField
import scala.xml.{NodeSeq, Text}

object AttrRow {
  def apply(name: => NodeSeq, value: => NodeSeq) =  new AttrRow(name, value,
    "attr-name", "attr-value")
  def apply(f: MappedField[_, _]) = new AttrRow(Text(f.displayName), f.asHtml,
    "attr-name", "attr-value")
  def formRow(name: => NodeSeq, input: => NodeSeq) = new AttrRow(name, input,
    "form-name", "form-value")
  def submitRow(button: => NodeSeq) = new SpanRow(button)
}

class AttrRow(name: => NodeSeq, value: => NodeSeq, nameCss: String,
  valueCss: String) extends Function0[NodeSeq] {
  def tdN = <td class={nameCss}>{name}</td>
  def tdV = <td class={valueCss}>{value}</td>
  def apply(): NodeSeq = <tr>{tdN :: tdV :: Nil}</tr>
}

class SpanRow(value: => NodeSeq) extends AttrRow(NodeSeq.Empty, value, "", "")
{
  override def apply(): NodeSeq = <tr><td colspan="2">{value}</td></tr>
}

object Panel {
  def apply(attrs: Iterable[AttrRow]) = new Panel(attrs)
  def fromFields(fields: Iterable[MappedField[_,_]]) =
    new Panel(fields.map(AttrRow(_)))
}

class Panel(attrs: => Iterable[AttrRow]) extends Function1[NodeSeq, NodeSeq]
{
  def apply(in: NodeSeq): NodeSeq = <table>{attrs.map(_())}</table>
  def &(other: Function1[NodeSeq, NodeSeq]) = new Function1[NodeSeq, NodeSeq] {
    def apply(in: NodeSeq): NodeSeq = List(Panel.this, other) flatMap (_(in))
  }
}

// vim: set ts=2 sw=2 et: