src/main/scala/net/tz/lift/snippet/SnippetHelpers.scala
changeset 5 866ed30f3da3
child 12 e09d28f8e4c6
equal deleted inserted replaced
4:e6115c95eac2 5:866ed30f3da3
       
     1 /*
       
     2  * Copyright 2011 Tomas Zeman <tzeman@volny.cz>
       
     3  *
       
     4  * Licensed under the Apache License, Version 2.0 (the "License");
       
     5  * you may not use this file except in compliance with the License.
       
     6  * You may obtain a copy of the License at
       
     7  *
       
     8  *     http://www.apache.org/licenses/LICENSE-2.0
       
     9  *
       
    10  * Unless required by applicable law or agreed to in writing, software
       
    11  * distributed under the License is distributed on an "AS IS" BASIS,
       
    12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
       
    13  * See the License for the specific language governing permissions and
       
    14  * limitations under the License.
       
    15  */
       
    16 package net.tz.lift.snippet
       
    17 
       
    18 import net.liftweb.http._
       
    19 import scala.xml.{NodeSeq, Text}
       
    20 
       
    21 trait SnippetHelpers {
       
    22   def mkPath(prefix: String, l: String*) =
       
    23     (prefix :: l.toList) mkString ("/", "/", "")
       
    24 }
       
    25 
       
    26 class A(href: => String) extends Function1[NodeSeq, NodeSeq] {
       
    27   def apply(in: NodeSeq): NodeSeq = <a href={href}>{in}</a>
       
    28 }
       
    29 
       
    30 object A {
       
    31   def apply(href: String)(in: NodeSeq): NodeSeq = (new A(href))(in)
       
    32   def apply(href: String): A = new A(href)
       
    33   def apply(href: String, cnt: String): NodeSeq = (new A(href))(Text(cnt))
       
    34 }
       
    35 
       
    36 object ActionLinks extends DispatchSnippet {
       
    37   def dispatch: DispatchIt = {
       
    38     case "are" => { xhtml => if (links.is.isEmpty) NodeSeq.Empty else xhtml }
       
    39     case _ => render _
       
    40   }
       
    41 
       
    42   import scala.collection.mutable.{BufferLike, ListBuffer}
       
    43 
       
    44   private object links extends RequestVar[ListBuffer[Either[Function0[NodeSeq], Function1[NodeSeq, NodeSeq]]]](new ListBuffer())
       
    45 
       
    46   def render(in: NodeSeq): NodeSeq = links.is.flatMap { _.fold( _(), _(in) ) ++
       
    47     Text("  ") }
       
    48 
       
    49   def append(l: NodeSeq) = links.is.append(Left( { () => l } ))
       
    50   def append(l: () => NodeSeq) = links.is.append(Left(l))
       
    51   def append(l: NodeSeq => NodeSeq) = links.is.append(Right(l))
       
    52 }
       
    53 
       
    54 object SimpleForm {
       
    55   def apply(l: Iterable[AttrRow], submitVal: String, onSubmit: () => Any):
       
    56     (NodeSeq => NodeSeq) = {
       
    57     val curS = S.currentSnippet
       
    58     def doit() = {
       
    59       onSubmit()
       
    60       curS foreach { S.mapSnippet(_, loop) }
       
    61     }
       
    62 
       
    63     def loop: (NodeSeq => NodeSeq) = {
       
    64       Panel(l ++ List(AttrRow.submitRow(SHtml.submit(submitVal, doit))))
       
    65     }
       
    66 
       
    67     loop
       
    68   }
       
    69 
       
    70   def apply(l: Iterable[AttrRow], submitVal: String): (NodeSeq => NodeSeq) =
       
    71     apply(l, submitVal, () => ())
       
    72 }
       
    73 
       
    74 // vim: set ts=2 sw=2 et: