Attribute panel
authorTomas Zeman <tzeman@volny.cz>
Tue, 29 Mar 2011 13:09:55 +0200
changeset 3 61b9f48ce17d
parent 2 a1a5ebc06887
child 4 e6115c95eac2
Attribute panel
src/main/scala/net/tz/lift/snippet/Panel.scala
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/scala/net/tz/lift/snippet/Panel.scala	Tue Mar 29 13:09:55 2011 +0200
@@ -0,0 +1,57 @@
+/*
+ * 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: