PageRenderTime 48ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/src/main/scala/code/snippet/ReminderSnips.scala

https://bitbucket.org/ta_jim/lift_userlogin_template
Scala | 167 lines | 150 code | 17 blank | 0 comment | 2 complexity | 32db57612693628120982a0c18545e99 MD5 | raw file
  1. package code.snippet
  2. import scala.xml.{ NodeSeq, Text }
  3. import net.liftweb.util._
  4. import net.liftweb.common._
  5. import java.util.Date
  6. import code.lib._
  7. import Helpers._
  8. import net.liftweb.http._
  9. import net.liftweb.http.js.JsCmd
  10. import net.liftweb.http.js.JE
  11. import net.liftweb.http.js.JsCmds.SetHtml
  12. import net.liftweb.http.js.JsCmds
  13. import code.model.Reminder
  14. import net.liftweb.mongodb.record.field.DateField
  15. import java.text.SimpleDateFormat
  16. import net.liftweb.http.js.JE.JsRaw
  17. import com.mongodb.casbah.util.DeleteOp
  18. import net.liftweb.http.PaginatorSnippet
  19. import net.liftweb.mongodb.Limit
  20. import code.model.User
  21. import net.liftweb.json.JsonAST.JObject
  22. import net.liftweb.mapper.Like
  23. import com.mongodb.casbah.commons.MongoDBObject
  24. import java.util.regex.Pattern
  25. import net.liftmodules.widgets.autocomplete.AutoComplete
  26. class Remindersnips extends SortedPaginatorSnippet[Reminder, Date] with PaginatorSnippet[Reminder] {
  27. var friend_name = ""
  28. var dob = ""
  29. var model_friend_name = ""
  30. var model_dob = ""
  31. def sortOrder = S.param("asc") match {
  32. case e: EmptyBox => 0
  33. case b: Box[String] => if (b.get.equalsIgnoreCase("true")) 1 else -1
  34. }
  35. val user = User.findCurrentUser
  36. override def count = Reminder.findAllNotes(
  37. user.userIdAsString).size
  38. override def itemsPerPage = 5
  39. override def page = Reminder.findAllNotes(user.userIdAsString,
  40. sortOrder,
  41. itemsPerPage,
  42. (curPage * itemsPerPage))
  43. def headers: List[(String, java.util.Date)] = List(("dob", Reminder.dob.is.noTime))
  44. object refreshtable extends RequestVar(rendertodoField)
  45. object refreshPagination extends RequestVar(renderPagination)
  46. def render = {
  47. "#bdayReminder" #> AutoComplete("",
  48. getAllName _,
  49. value => takeAction(value),
  50. List("minChars" -> "2")) &
  51. "#datepicker" #> SHtml.text(dob, dob = _) &
  52. "#submit" #> SHtml.ajaxSubmit("Add", () => {
  53. Reminder.createReminder(user.userIdAsString, friend_name, dob) match {
  54. case Left(notice) => S.error(notice)
  55. case Right(status) => {
  56. reloadTable &
  57. recallDatepicker &
  58. JsCmds.SetValById("datepicker", "") &
  59. JsRaw(""" $("span#bdayReminder input").val("")""").cmd
  60. }
  61. }
  62. }) &
  63. "#myTable" #> refreshtable.is &
  64. "#pagination" #> refreshPagination.is
  65. }
  66. private def takeAction(str: String) {
  67. friend_name = str
  68. }
  69. private def renderPagination = SHtml.memoize {
  70. "#pagination" #> (<div>
  71. <lift:remindersnips.paginate>
  72. <p>
  73. <nav:records></nav:records>
  74. </p>
  75. <nav:first></nav:first>
  76. |<nav:prev></nav:prev>
  77. |<nav:allpages></nav:allpages>
  78. |<nav:next></nav:next>
  79. |<nav:last></nav:last>
  80. </lift:remindersnips.paginate>
  81. </div>)
  82. }
  83. private def rendertodoField = SHtml.memoize {
  84. "#todoList1" #> page.map { item =>
  85. "#bdayReminderID" #> item.friend_name.is &
  86. "#dateID" #> Util.convertUtilDateToString(item.dob.is, "dd MMM yyyy") &
  87. ".btn_2 [id]" #> (item.id.toString + "_btn_1") &
  88. ".btn_3 [onclick]" #> SHtml.ajaxInvoke(() => {
  89. Reminder.deleteReminder(item)
  90. reloadTable &
  91. recallDatepicker
  92. }) &
  93. "#model [id]" #> (item.id.toString + "_model") &
  94. "#model_bdayReminder" #> SHtml.text(item.friend_name.is, name => {
  95. model_friend_name = name
  96. JsCmds.Noop
  97. }) &
  98. ".datepick" #> SHtml.text(Util.convertUtilDateToString(item.dob.is, "MM/dd/yyyy"), date => {
  99. model_dob = date
  100. }) &
  101. ".btn_2 [onclick]" #> showModelPopUp(item) &
  102. ".model_cancel [onclick]" #> hideModelPopUp(item.id.toString + "_model") &
  103. ".model_submit " #> SHtml.ajaxSubmit("Update", () => {
  104. Reminder.updateReminder(item, model_friend_name, model_dob) match {
  105. case Left(notice) => {
  106. S.error(notice)
  107. hideModelPopUp(item.id.toString + "_model")
  108. }
  109. case Right(status) => {
  110. S.notice("successfullt updated")
  111. hideModelPopUp(item.id.toString + "_model") &
  112. reloadTable
  113. }
  114. }
  115. })
  116. }
  117. }
  118. def getAllName(current: String, limit: Int): Seq[String] = {
  119. val pattern = Pattern.compile("^" + current, Pattern.CASE_INSENSITIVE)
  120. User.findAll(MongoDBObject("name" -> pattern)).map {
  121. _.name.is
  122. }
  123. }
  124. private def reloadTable: JsCmd = {
  125. val userId = User.findCurrentUser.userIdAsString
  126. val list = Reminder.getReminderForToday(userId)
  127. SetHtml("myTable", refreshtable.is.applyAgain) &
  128. SetHtml("pagination", refreshPagination.is.applyAgain) &
  129. SetHtml("alerts",
  130. list.map { item =>
  131. <li> Today is { item.friend_name }'s birthday</li>
  132. })
  133. }
  134. private def showModelPopUp(reminder: Reminder) = {
  135. JsRaw("""var modal= document.getElementById('""" + reminder.id.toString + """_model');
  136. var shade= document.getElementById('shade');
  137. modal.style.display= shade.style.display= 'block';""").cmd &
  138. recallDatepicker
  139. }
  140. private def hideModelPopUp(reminder: String) = {
  141. JsRaw("""var modal= document.getElementById('""" + reminder + """');
  142. var shade= document.getElementById('shade');
  143. modal.style.display=shade.style.display= 'none';""").cmd
  144. }
  145. private def recallDatepicker() = {
  146. JsRaw("""$(function() {
  147. $('.datepick').datepick({alignment:"bottomRight"});
  148. });""").cmd
  149. }
  150. }