Snippet helpers: A, mkPath, ActionLinks
authorTomas Zeman <tzeman@volny.cz>
Tue, 29 Mar 2011 13:11:37 +0200
changeset 5 866ed30f3da3
parent 4 e6115c95eac2
child 6 9ef417a2b170
Snippet helpers: A, mkPath, ActionLinks
src/main/scala/net/tz/lift/snippet/SnippetHelpers.scala
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/scala/net/tz/lift/snippet/SnippetHelpers.scala	Tue Mar 29 13:11:37 2011 +0200
@@ -0,0 +1,74 @@
+/*
+ * 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._
+import scala.xml.{NodeSeq, Text}
+
+trait SnippetHelpers {
+  def mkPath(prefix: String, l: String*) =
+    (prefix :: l.toList) mkString ("/", "/", "")
+}
+
+class A(href: => String) extends Function1[NodeSeq, NodeSeq] {
+  def apply(in: NodeSeq): NodeSeq = <a href={href}>{in}</a>
+}
+
+object A {
+  def apply(href: String)(in: NodeSeq): NodeSeq = (new A(href))(in)
+  def apply(href: String): A = new A(href)
+  def apply(href: String, cnt: String): NodeSeq = (new A(href))(Text(cnt))
+}
+
+object ActionLinks extends DispatchSnippet {
+  def dispatch: DispatchIt = {
+    case "are" => { xhtml => if (links.is.isEmpty) NodeSeq.Empty else xhtml }
+    case _ => render _
+  }
+
+  import scala.collection.mutable.{BufferLike, ListBuffer}
+
+  private object links extends RequestVar[ListBuffer[Either[Function0[NodeSeq], Function1[NodeSeq, NodeSeq]]]](new ListBuffer())
+
+  def render(in: NodeSeq): NodeSeq = links.is.flatMap { _.fold( _(), _(in) ) ++
+    Text("  ") }
+
+  def append(l: NodeSeq) = links.is.append(Left( { () => l } ))
+  def append(l: () => NodeSeq) = links.is.append(Left(l))
+  def append(l: NodeSeq => NodeSeq) = links.is.append(Right(l))
+}
+
+object SimpleForm {
+  def apply(l: Iterable[AttrRow], submitVal: String, onSubmit: () => Any):
+    (NodeSeq => NodeSeq) = {
+    val curS = S.currentSnippet
+    def doit() = {
+      onSubmit()
+      curS foreach { S.mapSnippet(_, loop) }
+    }
+
+    def loop: (NodeSeq => NodeSeq) = {
+      Panel(l ++ List(AttrRow.submitRow(SHtml.submit(submitVal, doit))))
+    }
+
+    loop
+  }
+
+  def apply(l: Iterable[AttrRow], submitVal: String): (NodeSeq => NodeSeq) =
+    apply(l, submitVal, () => ())
+}
+
+// vim: set ts=2 sw=2 et: