PageRenderTime 40ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/lift/lift-framework/lift-base/lift-webkit/src/main/scala/net/liftweb/http/js/jquery/JqJsCmds.scala

http://github.com/ymasory/scala-corpus
Scala | 400 lines | 237 code | 63 blank | 100 comment | 6 complexity | f2b1714bf0863c255d17e4e6bdb2f369 MD5 | raw file
Possible License(s): Apache-2.0, GPL-3.0, AGPL-3.0, BSD-3-Clause, MIT
  1. /*
  2. * Copyright 2007-2010 WorldWide Conferencing, LLC
  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 net.liftweb {
  17. package http {
  18. package js {
  19. package jquery {
  20. import _root_.scala.xml.{NodeSeq, Group, Elem, Node, SpecialNode}
  21. import _root_.net.liftweb.util.Helpers._
  22. import _root_.net.liftweb.util.Helpers
  23. import _root_.net.liftweb.util.TimeHelpers
  24. import _root_.net.liftweb.common._
  25. import _root_.net.liftweb.util._
  26. import _root_.net.liftweb.http.js.{JsExp, JE}
  27. import JE._
  28. import JsCmds._
  29. /**
  30. * Classes mixing JQueryRight are also mixing JsMember. As JQueryRight
  31. * is deprecated clases mixing this trait with stop doing so soon and they
  32. * will mixin JsMember instead.
  33. */
  34. @deprecated
  35. trait JQueryRight {
  36. this: JsExp =>
  37. def toJsCmd: String
  38. }
  39. /**
  40. * Classes mixing JQuryLeft will soon stop doing so. Extending/Mixing JsExp will be enough
  41. */
  42. @deprecated
  43. trait JQueryLeft {
  44. this: JsExp =>
  45. }
  46. /**
  47. * A singleton that vends various different functions for WiringUI support
  48. */
  49. object JqWiringSupport {
  50. import js.JsCmds._
  51. /**
  52. * Fade out the old value and fade in the new value
  53. * using jQuery fast fade.
  54. */
  55. def fade: (String, Boolean, JsCmd) => JsCmd = {
  56. (id: String, first: Boolean, cmd: JsCmd) => {
  57. if (first) cmd
  58. else {
  59. val sel = "jQuery('#'+"+id.encJs+")"
  60. Run(sel+".fadeOut('fast', function() {"+
  61. cmd.toJsCmd+" "+sel+".fadeIn('fast');})")
  62. }
  63. }
  64. }
  65. /**
  66. * Hide the old value, set to new value and slide down.
  67. */
  68. def slideDown: (String, Boolean, JsCmd) => JsCmd = {
  69. (id: String, first: Boolean, cmd: JsCmd) => {
  70. if (first) cmd
  71. else {
  72. val sel = "jQuery('#'+"+id.encJs+")"
  73. Run(sel+".hide(); "+cmd.toJsCmd+" "+sel+".slideDown('fast')")
  74. }
  75. }
  76. }
  77. /**
  78. * Hide the old value, set to new value and slide down.
  79. */
  80. def slideUp: (String, Boolean, JsCmd) => JsCmd = {
  81. (id: String, first: Boolean, cmd: JsCmd) => {
  82. if (first) cmd
  83. else {
  84. val sel = "jQuery('#'+"+id.encJs+")"
  85. Run(sel+".hide(); "+cmd.toJsCmd+" "+sel+".slideUp('fast')")
  86. }
  87. }
  88. }
  89. }
  90. object JqJE {
  91. case object JqScrollToBottom extends JsExp with JsMember with JQueryRight with JQueryLeft {
  92. def toJsCmd = "each(function(i) {this.scrollTop=this.scrollHeight;})"
  93. }
  94. case class JqClick(exp: JsExp) extends JsExp with JsMember with JQueryLeft with JQueryRight {
  95. def toJsCmd = "click(" + exp.toJsCmd + ")"
  96. }
  97. case class JqGetAttr(key: String) extends JsExp with JsMember with JQueryRight with JQueryLeft {
  98. def toJsCmd = "attr(" + key.encJs + ")"
  99. }
  100. /**
  101. * A JQuery query
  102. */
  103. case class Jq(query: JsExp) extends JsExp with JQueryLeft {
  104. override def toJsCmd = "jQuery(" + query.toJsCmd + ")"
  105. }
  106. case object JqDoc extends JsExp with JQueryLeft {
  107. override def toJsCmd = "jQuery(document)"
  108. }
  109. case class JqKeypress(what: (Char, JsCmd)*) extends JsExp with JsMember with JQueryRight {
  110. override def toJsCmd = "keypress(function(e) {" +
  111. what.map {
  112. case (chr, cmd) =>
  113. "if (e.which == " + chr.toInt + ") {" + cmd.toJsCmd + "}"
  114. }.mkString(" else \n") +
  115. "})"
  116. }
  117. /**
  118. * A JQuery query for an element based on the id of the element
  119. */
  120. case class JqId(id: JsExp) extends JsExp with JQueryLeft {
  121. override def toJsCmd = "jQuery('#'+" + id.toJsCmd + ")"
  122. }
  123. case class JqAttr(key: String, value: JsExp) extends JsExp with JsMember with JQueryRight with JQueryLeft {
  124. def toJsCmd = "attr(" + key.encJs + ", " + value.toJsCmd + ")"
  125. }
  126. /**
  127. * Append content to a JQuery
  128. */
  129. case class JqAppend(content: NodeSeq) extends JsExp with JsMember with JQueryRight with JQueryLeft {
  130. override val toJsCmd = "append(" + fixHtml("inline", content) + ")"
  131. }
  132. /**
  133. * Remove JQuery
  134. */
  135. case class JqRemove() extends JsExp with JsMember with JQueryRight with JQueryLeft {
  136. override def toJsCmd = "remove()"
  137. }
  138. /**
  139. * AppendTo content to a JQuery
  140. */
  141. case class JqAppendTo(content: NodeSeq) extends JsExp with JsMember with JQueryRight with JQueryLeft {
  142. override val toJsCmd = "appendTo(" + fixHtml("inline", content) + ")"
  143. }
  144. /**
  145. * Prepend content to a JQuery
  146. */
  147. case class JqPrepend(content: NodeSeq) extends JsExp with JsMember with JQueryRight with JQueryLeft {
  148. override val toJsCmd = "prepend(" + fixHtml("inline", content) + ")"
  149. }
  150. /**
  151. * PrependTo content to a JQuery
  152. */
  153. case class JqPrependTo(content: NodeSeq) extends JsExp with JsMember with JQueryRight with JQueryLeft {
  154. override val toJsCmd = "prependTo(" + fixHtml("inline", content) + ")"
  155. }
  156. case class JqCss (name: JsExp, value: JsExp) extends JsExp with JsMember with JQueryRight with JQueryLeft {
  157. override def toJsCmd = "css(" + name.toJsCmd + "," + value.toJsCmd + ")"
  158. }
  159. /**
  160. * EmptyAfter will empty the node at the given uid and stick the given content behind it. Like
  161. * a cleaner innerHTML.
  162. */
  163. case class JqEmptyAfter(content: NodeSeq) extends JsExp with JsMember with JQueryRight with JQueryLeft {
  164. override val toJsCmd = "empty().after(" + fixHtml("inline", content) + ")"
  165. }
  166. object JqHtml {
  167. def apply() = new JsExp with JsMember with JQueryRight {
  168. def toJsCmd = "html()"
  169. }
  170. def apply(content: NodeSeq) = new JsExp with JsMember with JQueryRight with JQueryLeft {
  171. val toJsCmd = "html(" + fixHtml("inline", content) + ")"
  172. }
  173. }
  174. object JqText {
  175. def apply() = new JsExp with JsMember with JQueryRight {
  176. def toJsCmd = "text()"
  177. }
  178. def apply(content: String) = new JsExp with JsMember with JQueryRight with JQueryLeft {
  179. def toJsCmd = "text(" + content.encJs + ")"
  180. }
  181. }
  182. /**
  183. * Serialize input elements intoa string data. ALso works for serializing forms
  184. */
  185. case object JqSerialize extends JsExp with JsMember with JQueryRight {
  186. def toJsCmd = "serialize()"
  187. }
  188. /**
  189. * Serialize the jquery into a JSON array
  190. */
  191. case object JsonSerialize extends JsExp with JsMember with JQueryRight {
  192. def toJsCmd = "serializeArray()"
  193. }
  194. case object JqTabsSelected extends JsExp with JsMember with JQueryRight {
  195. def toJsCmd = "tabsSelected()"
  196. }
  197. object JqTabsClick {
  198. def apply(tab: JsExp): JsExp with JsMember with JQueryRight with JQueryLeft =
  199. new JsExp with JsMember with JQueryRight with JQueryLeft {
  200. def toJsCmd = "tabsClick(" + tab.toJsCmd + ")"
  201. }
  202. def apply(tab: Int): JsExp with JsMember with JQueryRight with JQueryLeft =
  203. apply(Num(tab))
  204. }
  205. object JqTabs {
  206. def apply(in: JsExp): JsExp with JsMember with JQueryRight with JQueryLeft =
  207. new JsExp with JsMember with JQueryRight with JQueryLeft {
  208. def toJsCmd = "tabs(" + in.toJsCmd + ")"
  209. }
  210. def apply(): JsExp with JsMember with JQueryRight with JQueryLeft =
  211. apply(JsRaw(""))
  212. }
  213. }
  214. object JqJsCmds {
  215. implicit def jsExpToJsCmd(in: JsExp) = in.cmd
  216. case class JqOnLoad(cmd: JsCmd) extends JsCmd {
  217. def toJsCmd = "jQuery(document).ready(function() {" + cmd.toJsCmd + "});"
  218. }
  219. /**
  220. * Append a NodeSeq to a node specified by uid using jQuery's append() method.
  221. */
  222. object AppendHtml {
  223. def apply(uid: String, content: NodeSeq): JsCmd =
  224. JqJE.JqId(JE.Str(uid)) ~> JqJE.JqAppend(content)
  225. }
  226. /**
  227. * AppendTo a NodeSeq to a node specified by uid using jQuery's appendTo() method.
  228. */
  229. object AppendToHtml {
  230. def apply(uid: String, content: NodeSeq): JsCmd =
  231. JqJE.JqId(JE.Str(uid)) ~> JqJE.JqAppendTo(content)
  232. }
  233. /**
  234. * Prepends a NodeSeq to a node specified by uid using jQuery's prepend() method.
  235. */
  236. object PrependHtml {
  237. def apply(uid: String, content: NodeSeq): JsCmd =
  238. JqJE.JqId(JE.Str(uid)) ~> JqJE.JqPrepend(content)
  239. }
  240. /**
  241. * Replaces the children of the node at { @code uid } with { @code content }
  242. */
  243. object EmptyAfter {
  244. def apply(uid: String, content: NodeSeq): JsCmd =
  245. JqJE.JqId(JE.Str(uid)) ~> JqJE.JqEmptyAfter(content)
  246. }
  247. /**
  248. * Prepends a NodeSeq to a node specified by uid using jQuery prependTo() method.
  249. */
  250. object PrependToHtml {
  251. def apply(uid: String, content: NodeSeq): JsCmd =
  252. JqJE.JqId(JE.Str(uid)) ~> JqJE.JqPrependTo(content)
  253. }
  254. case class JqSetHtml(uid: String, content: NodeSeq) extends JsCmd {
  255. /**
  256. * Eagerly evaluate
  257. */
  258. val toJsCmd =
  259. "try{jQuery(" + ("#" + uid).encJs + ").each(function(i) {this.innerHTML = " + fixHtml(uid, content) + ";});} catch (e) {}"
  260. }
  261. object Show {
  262. def apply(uid: String) = new Show(uid, Empty)
  263. def apply(uid: String, time: TimeSpan) = new Show(uid, Full(time))
  264. }
  265. class Show(val uid: String, val time: Box[TimeSpan]) extends JsCmd with HasTime {
  266. def toJsCmd = "try{jQuery(" + ("#" + uid).encJs + ").show(" + timeStr + ");} catch (e) {}"
  267. }
  268. object Hide {
  269. def apply(uid: String) = new Hide(uid, Empty)
  270. def apply(uid: String, time: TimeSpan) = new Hide(uid, Full(time))
  271. }
  272. class Hide(val uid: String, val time: Box[TimeSpan]) extends JsCmd with HasTime {
  273. def toJsCmd = "try{jQuery(" + ("#" + uid).encJs + ").hide(" + timeStr + ");} catch (e) {}"
  274. }
  275. case class DisplayMessage(where: String, msg: NodeSeq, duration: TimeSpan, fadeTime: TimeSpan) extends JsCmd {
  276. def toJsCmd = (Show(where) & JqSetHtml(where, msg) & After(duration, Hide(where, fadeTime))).toJsCmd
  277. }
  278. /**
  279. * The companion object to FadeOut that provides an alternative factory
  280. */
  281. object FadeOut {
  282. /**
  283. * Fade Out with the default duration and fadeTime provided by JsRules
  284. */
  285. def apply(id: String) = new FadeOut(id, JsRules.prefadeDuration, JsRules.fadeTime)
  286. }
  287. case class FadeOut(id: String, duration: TimeSpan, fadeTime: TimeSpan) extends JsCmd {
  288. def toJsCmd = (After(duration, JqJE.JqId(id) ~> (new JsRaw("fadeOut(" + fadeTime.millis + ")") with JsMember))).toJsCmd
  289. }
  290. /**
  291. * The companion object to FadeIn that provides an alternative factory
  292. */
  293. object FadeIn {
  294. /**
  295. * Fade In with the default duration and fadeTime provided by JsRules
  296. */
  297. def apply(id: String) = new FadeIn(id, JsRules.prefadeDuration, JsRules.fadeTime)
  298. }
  299. case class FadeIn(id: String, duration: TimeSpan, fadeTime: TimeSpan) extends JsCmd {
  300. def toJsCmd = (After(duration, JqJE.JqId(id) ~> (new JsRaw("fadeIn(" + fadeTime.millis + ")") with JsMember))).toJsCmd
  301. }
  302. object ModalDialog {
  303. def apply(html: NodeSeq) = new ModalDialog(html, Empty)
  304. def apply(html: NodeSeq, css: JsObj) = new ModalDialog(html, Full(css))
  305. }
  306. class ModalDialog(html: NodeSeq, css: Box[JsObj]) extends JsCmd {
  307. private def contentAsJsStr = {
  308. val w = new java.io.StringWriter
  309. S.htmlProperties.
  310. htmlWriter(Group(S.session.
  311. map(s =>
  312. s.fixHtml(s.processSurroundAndInclude("Modal Dialog",
  313. html))).
  314. openOr(html)),
  315. w)
  316. w.toString.encJs
  317. }
  318. val toJsCmd = "jQuery.blockUI({ message: " + contentAsJsStr +
  319. (css.map(", css: " + _.toJsCmd + " ").openOr("")) + "});"
  320. }
  321. case object Unblock extends JsCmd {
  322. def toJsCmd = "jQuery.unblockUI();"
  323. }
  324. /**
  325. * Use SetValueAndFocus from JsCmds
  326. */
  327. @deprecated
  328. case class SetValueAndFocus(id: String, value: String) extends JsCmd {
  329. def toJsCmd = "document.getElementById(" + id.encJs + ").value = " +
  330. value.encJs +
  331. "; document.getElementById(" + id.encJs + ").focus();"
  332. }
  333. }
  334. }
  335. }
  336. }
  337. }