Created
November 17, 2010 17:08
-
-
Save lopex/703660 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package ... | |
import _root_.net.liftweb.mapper._ | |
import _root_.net.liftweb.util._ | |
import _root_.net.liftweb.common._ | |
import _root_.net.liftweb.http._ | |
import _root_.scala.xml.{NodeSeq, Text, Node} | |
import S._ | |
import _root_.java.text.{DateFormat, SimpleDateFormat} | |
class MappedDatePicker[T<:Mapper[T]](fieldOwner: T) extends MappedDate(fieldOwner) { | |
override def defaultValue = new java.util.Date | |
override def _toForm: Box[NodeSeq] = Full(render) | |
def render = S.fmapFunc({s: List[String] => this.setFromAny(s)}){funcName => | |
val id = "entrydate" + MappedDatePicker.getNext.toString | |
val script = "$(function() {$('#" + id + "').datepicker({dateFormat:'yy/mm/dd'});});" | |
<input type='text' id={id} name={funcName} value={toString} maxlength="10" size="12" class="datepicker"/> | |
<script type="text/javascript">{script}</script> | |
} | |
override def toString = MappedDatePicker.toStringFormat.format(is) | |
override def asHtml: Node = Text(MappedDatePicker.asHtmlFormat.format(is)) | |
import java.util.Date | |
protected def max:Date = null | |
protected def min:Date = null | |
override def validations = valRange(S.?("wrong_dates")) _ :: super.validations | |
import _root_.net.liftweb.util._ | |
private def valRange(msg: => String)(value: java.util.Date): List[FieldError] = | |
if (!(min eq null) && value.before(min) || | |
!(max eq null) && value.after(max)) List(FieldError(this, Text(msg))) | |
else Nil | |
} | |
class MappedDateRangePicker[T<:Mapper[T]](fieldOwner: T, toDate: MappedDatePicker[T]) extends MappedDatePicker(fieldOwner) with ErrorMessageSupport { | |
override def toForm: Box[NodeSeq] = toForm(<div>{render} - {toDate.render}</div>) | |
override def asHtml: Node = Text(super.asHtml.text + " - " + toDate.asHtml.text) | |
override def validations = valDates(S.?("wrong_dates")) _ :: Nil // super.validations | |
private def valDates(msg: => String)(value: java.util.Date): List[FieldError] = | |
if (this.after(toDate)) List(FieldError(this, Text(S.?("wrong_dates")))) else Nil | |
} | |
object MappedDatePicker { | |
def toStringFormat = new java.text.SimpleDateFormat("yyyy/MM/dd") | |
def asHtmlFormat = DateFormat.getDateInstance(DateFormat.SHORT) | |
private var count = 0 | |
def getNext = {count += 1; count} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment