Permalink
Browse files

Closes #1034. Closes #1032. Screen and Wizard honor FormParam entrie…

…s and it's easier to add FormParams to MappedField
  • Loading branch information...
dpp committed Aug 25, 2011
1 parent f254f08 commit d98b45180631e626dbf2e9ff6190f1cab04d15f3
@@ -195,6 +195,11 @@ trait BaseMappedField extends SelectableField with Bindable with MixableMappedFi
def asJs: List[(String, JsExp)] = List((name, asJsExp))
+ /**
+ * What form elements are we going to add to this field?
+ */
+ def formElemAttrs: scala.Seq[SHtml.ElemAttr] = Nil
+
def renderJs_? = true
/**
@@ -451,7 +456,7 @@ trait MappedField[FieldType <: Any,OwnerType <: Mapper[OwnerType]] extends Typed
case other => other
}
- _toForm.map(_.flatMap(mf) )
+ _toForm.map(_.flatMap(mf) ).map(SHtml.ElemAttr.applyToAllElems(_, formElemAttrs))
}
/**
@@ -23,6 +23,7 @@ import net.liftweb.json.JsonAST.{JNothing, JNull, JString, JValue}
import net.liftweb.util._
import scala.reflect.Manifest
import scala.xml._
+import http.SHtml
/** Base trait of record fields, with functionality common to any type of field owned by any type of record */
trait BaseField extends FieldIdentifier with util.BaseField {
@@ -92,6 +93,11 @@ trait BaseField extends FieldIdentifier with util.BaseField {
/** Encode the field value into a JValue */
def asJValue: JValue
+ /**
+ * What form elements are we going to add to this field?
+ */
+ def formElemAttrs: Seq[SHtml.ElemAttr] = Nil
+
/**
* Set the name of this field
*/
@@ -188,6 +188,11 @@ trait AbstractScreen extends Factory {
override def helpAsHtml: Box[NodeSeq] = Empty
+ /**
+ * What form elements are we going to add to this field?
+ */
+ def formElemAttrs: Seq[SHtml.ElemAttr] = Nil
+
/**
* Is the field editable
*/
@@ -198,7 +203,8 @@ trait AbstractScreen extends Factory {
AbstractScreen.this.vendForm(manifest) or otherFuncVendors(manifest) or
LiftRules.vendForm(manifest)
- func.map(f => f(is, set _)).filter(x => editable_?)
+ func.map(f => f(is, set _)).filter(x => editable_?).
+ map(ns => SHtml.ElemAttr.applyToAllElems(ns, formElemAttrs))
}
protected def otherFuncVendors(what: Manifest[ValueType]): Box[(ValueType, ValueType => Any) => NodeSeq] = Empty
@@ -277,6 +283,11 @@ trait AbstractScreen extends Factory {
override def default = FieldBuilder.this.default
+ /**
+ * What form elements are we going to add to this field?
+ */
+ override lazy val formElemAttrs: Seq[SHtml.ElemAttr] = grabParams(stuff)
+
override implicit def manifest: Manifest[ValueType] = FieldBuilder.this.manifest
override def helpAsHtml = help
@@ -387,6 +398,11 @@ trait AbstractScreen extends Factory {
*/
override def show_? = underlying.show_?
+ /**
+ * What form elements are we going to add to this field?
+ */
+ override lazy val formElemAttrs: Seq[SHtml.ElemAttr] = grabParams(stuff)
+
/**
* Given the current context, should this field be displayed
*/
@@ -464,6 +480,11 @@ trait AbstractScreen extends Factory {
*/
override def show_? = underlying.map(_.show_?) openOr false
+ /**
+ * What form elements are we going to add to this field?
+ */
+ override lazy val formElemAttrs: Seq[SHtml.ElemAttr] = grabParams(stuff)
+
/**
* Given the current context, should this field be displayed
*/
@@ -629,6 +650,11 @@ trait AbstractScreen extends Factory {
override def default: T = defaultValue
+ /**
+ * What form elements are we going to add to this field?
+ */
+ override lazy val formElemAttrs: Seq[SHtml.ElemAttr] = grabParams(stuff)
+
override val setFilter = stuff.flatMap {
case AFilter(f) => List(f)
case _ => Nil
@@ -653,6 +679,11 @@ trait AbstractScreen extends Factory {
override def default: T = defaultValue
+ /**
+ * What form elements are we going to add to this field?
+ */
+ override lazy val formElemAttrs: Seq[SHtml.ElemAttr] = grabParams(stuff)
+
override val setFilter = stuff.flatMap {
case AFilter(f) => List(f)
case _ => Nil
@@ -96,6 +96,16 @@ trait SHtml {
implicit def strSeqToElemAttr(in: Seq[(String, String)]):
Seq[ElemAttr] = in.map(a => a: ElemAttr)
+
+ def applyToAllElems(in: Seq[Node], elemAttrs: Seq[ElemAttr]): Seq[Node] = in map {
+ case Group(ns) => Group(applyToAllElems(ns, elemAttrs))
+ case e: Elem => val updated = elemAttrs.foldLeft(e)((e, f) => f(e))
+
+ new Elem(updated.prefix, updated.label,
+ updated.attributes, updated.scope,
+ applyToAllElems(updated.child, elemAttrs) :_*)
+ case n => n
+ }
}
private class ApplicableElem(in: Elem) {

0 comments on commit d98b451

Please sign in to comment.