src/main/scala/net/tz/lift/snippet/Panel.scala
author Tomas Zeman <tzeman@volny.cz>
Mon, 02 Apr 2012 00:06:52 +0200
changeset 21 641c7190c566
parent 15 995184977e9b
child 22 94ed9cfdb123
permissions -rw-r--r--
Upgrade of sbt, lift et al.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
5
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
     1
/*
21
641c7190c566 Upgrade of sbt, lift et al.
Tomas Zeman <tzeman@volny.cz>
parents: 15
diff changeset
     2
 * Copyright 2011-2012 Tomas Zeman <tzeman@volny.cz>
5
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
     3
 *
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
     4
 * Licensed under the Apache License, Version 2.0 (the "License");
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
     5
 * you may not use this file except in compliance with the License.
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
     6
 * You may obtain a copy of the License at
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
     7
 *
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
     8
 *     http://www.apache.org/licenses/LICENSE-2.0
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
     9
 *
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    10
 * Unless required by applicable law or agreed to in writing, software
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    11
 * distributed under the License is distributed on an "AS IS" BASIS,
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    13
 * See the License for the specific language governing permissions and
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    14
 * limitations under the License.
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    15
 */
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    16
package net.tz.lift.snippet
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    17
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    18
import net.liftweb.mapper.MappedField
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    19
import scala.xml.{NodeSeq, Text}
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    20
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    21
object AttrRow {
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    22
  def apply(name: => NodeSeq, value: => NodeSeq) =  new AttrRow(name, value,
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    23
    "attr-name", "attr-value")
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    24
  def apply(f: MappedField[_, _]) = new AttrRow(Text(f.displayName), f.asHtml,
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    25
    "attr-name", "attr-value")
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    26
  def formRow(name: => NodeSeq, input: => NodeSeq) = new AttrRow(name, input,
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    27
    "form-name", "form-value")
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    28
  def submitRow(button: => NodeSeq) = new SpanRow(button)
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    29
}
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    30
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    31
class AttrRow(name: => NodeSeq, value: => NodeSeq, nameCss: String,
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    32
  valueCss: String) extends Function0[NodeSeq] {
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    33
  def tdN = <td class={nameCss}>{name}</td>
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    34
  def tdV = <td class={valueCss}>{value}</td>
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    35
  def apply(): NodeSeq = <tr>{tdN :: tdV :: Nil}</tr>
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    36
}
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    37
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    38
class SpanRow(value: => NodeSeq) extends AttrRow(NodeSeq.Empty, value, "", "")
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    39
{
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    40
  override def apply(): NodeSeq = <tr><td colspan="2">{value}</td></tr>
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    41
}
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    42
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    43
object Panel {
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    44
  def apply(attrs: Iterable[AttrRow]) = new Panel(attrs)
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    45
  def fromFields(fields: Iterable[MappedField[_,_]]) =
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    46
    new Panel(fields.map(AttrRow(_)))
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    47
}
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    48
15
995184977e9b a3e3bd30f0d19526 Contact UI
Tomas Zeman <tzeman@volny.cz>
parents: 5
diff changeset
    49
class Panel(attrs: => Iterable[AttrRow]) extends CssTr {
5
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    50
  def apply(in: NodeSeq): NodeSeq = <table>{attrs.map(_())}</table>
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    51
  def &(other: Function1[NodeSeq, NodeSeq]) = new Function1[NodeSeq, NodeSeq] {
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    52
    def apply(in: NodeSeq): NodeSeq = List(Panel.this, other) flatMap (_(in))
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    53
  }
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    54
}
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    55
21
641c7190c566 Upgrade of sbt, lift et al.
Tomas Zeman <tzeman@volny.cz>
parents: 15
diff changeset
    56
import net.liftweb.http.Templates
15
995184977e9b a3e3bd30f0d19526 Contact UI
Tomas Zeman <tzeman@volny.cz>
parents: 5
diff changeset
    57
import net.liftweb.util.Helpers._ // CSS transforms
995184977e9b a3e3bd30f0d19526 Contact UI
Tomas Zeman <tzeman@volny.cz>
parents: 5
diff changeset
    58
995184977e9b a3e3bd30f0d19526 Contact UI
Tomas Zeman <tzeman@volny.cz>
parents: 5
diff changeset
    59
/**
995184977e9b a3e3bd30f0d19526 Contact UI
Tomas Zeman <tzeman@volny.cz>
parents: 5
diff changeset
    60
 * A panel using template in <code>/templates-hidden/panel</code> for
995184977e9b a3e3bd30f0d19526 Contact UI
Tomas Zeman <tzeman@volny.cz>
parents: 5
diff changeset
    61
 * values rendering.
995184977e9b a3e3bd30f0d19526 Contact UI
Tomas Zeman <tzeman@volny.cz>
parents: 5
diff changeset
    62
 */
995184977e9b a3e3bd30f0d19526 Contact UI
Tomas Zeman <tzeman@volny.cz>
parents: 5
diff changeset
    63
class TplPanel(cells: List[(NodeSeq, NodeSeq)]) extends CssTr {
21
641c7190c566 Upgrade of sbt, lift et al.
Tomas Zeman <tzeman@volny.cz>
parents: 15
diff changeset
    64
  def apply(in: NodeSeq): NodeSeq = Templates(
15
995184977e9b a3e3bd30f0d19526 Contact UI
Tomas Zeman <tzeman@volny.cz>
parents: 5
diff changeset
    65
    List("templates-hidden", "panel")) map { xml =>
995184977e9b a3e3bd30f0d19526 Contact UI
Tomas Zeman <tzeman@volny.cz>
parents: 5
diff changeset
    66
    (".row *" #> cells.map { r =>
995184977e9b a3e3bd30f0d19526 Contact UI
Tomas Zeman <tzeman@volny.cz>
parents: 5
diff changeset
    67
      ".n *" #> r._1 &
995184977e9b a3e3bd30f0d19526 Contact UI
Tomas Zeman <tzeman@volny.cz>
parents: 5
diff changeset
    68
      ".v *" #> r._2
995184977e9b a3e3bd30f0d19526 Contact UI
Tomas Zeman <tzeman@volny.cz>
parents: 5
diff changeset
    69
    })(xml)
995184977e9b a3e3bd30f0d19526 Contact UI
Tomas Zeman <tzeman@volny.cz>
parents: 5
diff changeset
    70
  } openOr NodeSeq.Empty
995184977e9b a3e3bd30f0d19526 Contact UI
Tomas Zeman <tzeman@volny.cz>
parents: 5
diff changeset
    71
}
995184977e9b a3e3bd30f0d19526 Contact UI
Tomas Zeman <tzeman@volny.cz>
parents: 5
diff changeset
    72
5
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    73
// vim: set ts=2 sw=2 et: