/*
* 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 CssTr {
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))
}
}
import net.liftweb.http.TemplateFinder
import net.liftweb.util.Helpers._ // CSS transforms
/**
* A panel using template in <code>/templates-hidden/panel</code> for
* values rendering.
*/
class TplPanel(cells: List[(NodeSeq, NodeSeq)]) extends CssTr {
def apply(in: NodeSeq): NodeSeq = TemplateFinder.findAnyTemplate(
List("templates-hidden", "panel")) map { xml =>
(".row *" #> cells.map { r =>
".n *" #> r._1 &
".v *" #> r._2
})(xml)
} openOr NodeSeq.Empty
}
// vim: set ts=2 sw=2 et: