Added OptionalFieldDisplay, DataTable, Loc helpers
authorTomas Zeman <tzeman@volny.cz>
Wed, 17 Aug 2011 08:48:43 +0200
changeset 20 22424e6508cd
parent 19 5492a4e4e60d
child 21 9026ebb197e2
Added OptionalFieldDisplay, DataTable, Loc helpers
project/build/LiftProject.scala
src/main/scala/net/tz/lift/model/OptionalFieldDisplay.scala
src/main/scala/net/tz/lift/snippet/DataTable.scala
src/main/scala/net/tz/lift/snippet/Loc.scala
src/main/scala/net/tz/lift/snippet/Panel.scala
src/main/scala/net/tz/lift/snippet/SnippetHelpers.scala
--- a/project/build/LiftProject.scala	Wed Aug 17 08:47:55 2011 +0200
+++ b/project/build/LiftProject.scala	Wed Aug 17 08:48:43 2011 +0200
@@ -22,6 +22,7 @@
   // lift
   val lift_mapper = "net.liftweb" %% "lift-mapper" % liftVer
   val lift_widgets = "net.liftweb" %% "lift-widgets" % liftVer
+  val lift_record = "net.liftweb" %% "lift-record" % liftVer
 
   // logging
   val logback = "ch.qos.logback" % "logback-classic" % "0.9.28" % "runtime"
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/scala/net/tz/lift/model/OptionalFieldDisplay.scala	Wed Aug 17 08:48:43 2011 +0200
@@ -0,0 +1,28 @@
+/*
+ * 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.model
+
+import net.liftweb.record.OptionalTypedField
+import scala.xml.Text
+
+/**
+ * Nice display of optional fields, ie. w/o Some(...)
+ */
+trait OptionalFieldDisplay { self: OptionalTypedField[_] =>
+  override def asHtml = Text(get map { _.toString } getOrElse "")
+}
+
+// vim: set ts=2 sw=2 et:
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/scala/net/tz/lift/snippet/DataTable.scala	Wed Aug 17 08:48:43 2011 +0200
@@ -0,0 +1,34 @@
+/*
+ * 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.http.TemplateFinder
+import net.liftweb.util.Helpers._ // CSS transforms
+import scala.xml.NodeSeq
+
+/**
+ * Template driven table, compatible w/ datatables.net.
+ * Template location: <code>/templates-hidden/datatable</code>.
+ */
+class DataTable(cols: List[String], cells: List[List[NodeSeq]]) extends CssTr {
+  def apply(in: NodeSeq): NodeSeq = TemplateFinder.findAnyTemplate(
+    List("templates-hidden", "datatable")) map { xml =>
+    (".field-name *" #> cols &
+    ".row *" #> cells.map { vals => "td *" #> vals })(xml)
+  } openOr NodeSeq.Empty
+}
+
+// vim: set ts=2 sw=2 et:
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/scala/net/tz/lift/snippet/Loc.scala	Wed Aug 17 08:48:43 2011 +0200
@@ -0,0 +1,36 @@
+/*
+ * 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.http.TemplateFinder
+import net.liftweb.sitemap.Loc._
+import scala.xml.NodeSeq
+
+/**
+ * Helper object for custom Loc template based on template path, eg.
+ * <code>item/view</code>.
+ */
+object LocTpl {
+
+  def apply(path: String): Template = Template(() =>
+    TemplateFinder.findAnyTemplate(path split "/" toList) openOr NodeSeq.Empty)
+
+  def apply[T](f: T => String): ValueTemplate[T] = ValueTemplate[T](
+    _.flatMap { v => TemplateFinder.findAnyTemplate(f(v) split "/" toList)
+    } openOr NodeSeq.Empty)
+}
+
+// vim: set ts=2 sw=2 et:
--- a/src/main/scala/net/tz/lift/snippet/Panel.scala	Wed Aug 17 08:47:55 2011 +0200
+++ b/src/main/scala/net/tz/lift/snippet/Panel.scala	Wed Aug 17 08:48:43 2011 +0200
@@ -46,12 +46,28 @@
     new Panel(fields.map(AttrRow(_)))
 }
 
-class Panel(attrs: => Iterable[AttrRow]) extends Function1[NodeSeq, NodeSeq]
-{
+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:
--- a/src/main/scala/net/tz/lift/snippet/SnippetHelpers.scala	Wed Aug 17 08:47:55 2011 +0200
+++ b/src/main/scala/net/tz/lift/snippet/SnippetHelpers.scala	Wed Aug 17 08:48:43 2011 +0200
@@ -19,10 +19,15 @@
 import scala.xml.{NodeSeq, Text}
 
 trait SnippetHelpers {
+
+  type CssTr = (NodeSeq => NodeSeq)
+
   def mkPath(prefix: String, l: String*) =
     (prefix :: l.toList) mkString ("/", "/", "")
 }
 
+trait CssTr extends Function1[NodeSeq, NodeSeq]
+
 class A(href: => String) extends Function1[NodeSeq, NodeSeq] {
   def apply(in: NodeSeq): NodeSeq = <a href={href}>{in}</a>
 }