src/main/scala/net/tz/lift/snippet/SnippetHelpers.scala
author Tomas Zeman <tzeman@volny.cz>
Tue, 12 Apr 2011 19:03:43 +0200
changeset 7 6e3d323f9ec5
parent 2 cf829ec742b3
child 22 6b1a7f3429ca
permissions -rw-r--r--
d6a28603feb67d4d CDR table columns

/*
 * 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: