|
1 /* |
|
2 * Copyright 2012 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 fis.cl.model |
|
17 |
|
18 import net.tz.lift.model.FieldLabel |
|
19 import net.liftweb.common._ |
|
20 import net.liftweb.http.SHtml |
|
21 import net.liftweb.record.{Field, MetaRecord, Record} |
|
22 import net.liftweb.record.field._ |
|
23 import net.liftweb.util.Helpers._ |
|
24 import scala.xml.{Elem, NodeSeq, Text} |
|
25 |
|
26 class CodeListItemField[T <: Record[T]](own: T, val cl: CodeList, |
|
27 val dispF: CodeListItem => String, val htmlF: CodeListItem => NodeSeq) |
|
28 extends LongField(own) with FieldLabel { |
|
29 |
|
30 def this(o: T, id: Symbol) = this(o, CodeList(id), _.linkName, _.toXHtml) |
|
31 |
|
32 def item: Box[CodeListItem] = cl(get) |
|
33 |
|
34 override def defaultValueBox: Box[Long] = cl.dflt.map { _.id } |
|
35 |
|
36 override def asHtml = item map { htmlF } openOr NodeSeq.Empty |
|
37 |
|
38 protected def buildDisplayList: List[(Long, String)] = |
|
39 cl.items.toList.map { i => (i.id, dispF(i)) } |
|
40 |
|
41 private def elem = SHtml.selectObj[Long](buildDisplayList, Full(get), |
|
42 set(_)) % ("tabindex" -> tabIndex.toString) |
|
43 |
|
44 override def toForm: Box[NodeSeq] = uniqueFieldId match { |
|
45 case Full(id) => Full(elem % ("id" -> id)) |
|
46 case _ => Full(elem) |
|
47 } |
|
48 } |
|
49 |
|
50 class OptionalCodeListItemField[T <: Record[T]](own: T, val cl: CodeList, |
|
51 val dispF: CodeListItem => String, val htmlF: CodeListItem => NodeSeq) |
|
52 extends OptionalLongField(own) with FieldLabel { |
|
53 |
|
54 def this(o: T, id: Symbol) = this(o, CodeList(id), _.linkName, _.toXHtml) |
|
55 |
|
56 def item: Box[CodeListItem] = get flatMap { cl(_) } |
|
57 |
|
58 override def defaultValueBox: Box[Long] = cl.dflt.map { _.id } |
|
59 |
|
60 override def asHtml = item map { htmlF } openOr NodeSeq.Empty |
|
61 |
|
62 protected def buildDisplayList: List[(Box[Long], String)] = |
|
63 (Empty, "---") :: |
|
64 cl.items.toList.map { i => (Full(i.id), dispF(i)) } |
|
65 |
|
66 private def elem = SHtml.selectObj[Box[Long]](buildDisplayList, Full(get), |
|
67 set(_)) % ("tabindex" -> tabIndex.toString) |
|
68 |
|
69 override def toForm: Box[NodeSeq] = uniqueFieldId match { |
|
70 case Full(id) => Full(elem % ("id" -> id)) |
|
71 case _ => Full(elem) |
|
72 } |
|
73 } |
|
74 |
|
75 // vim: set ts=2 sw=2 et: |