# HG changeset patch # User Tomas Zeman # Date 1313563723 -7200 # Node ID 22424e6508cdf296397ef259f9615f184782b3cd # Parent 5492a4e4e60d0141a077886ec6fce1cf83b26e85 Added OptionalFieldDisplay, DataTable, Loc helpers diff -r 5492a4e4e60d -r 22424e6508cd project/build/LiftProject.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" diff -r 5492a4e4e60d -r 22424e6508cd src/main/scala/net/tz/lift/model/OptionalFieldDisplay.scala --- /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 + * + * 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: diff -r 5492a4e4e60d -r 22424e6508cd src/main/scala/net/tz/lift/snippet/DataTable.scala --- /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 + * + * 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: /templates-hidden/datatable. + */ +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: diff -r 5492a4e4e60d -r 22424e6508cd src/main/scala/net/tz/lift/snippet/Loc.scala --- /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 + * + * 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. + * item/view. + */ +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: diff -r 5492a4e4e60d -r 22424e6508cd src/main/scala/net/tz/lift/snippet/Panel.scala --- 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 = {attrs.map(_())}
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 /templates-hidden/panel 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: diff -r 5492a4e4e60d -r 22424e6508cd src/main/scala/net/tz/lift/snippet/SnippetHelpers.scala --- 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 = {in} }