/
YUIArtifacts.scala
executable file
·168 lines (145 loc) · 5.19 KB
/
YUIArtifacts.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/*
* Copyright 2007-2011 WorldWide Conferencing, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.liftweb
package http
package js
package yui
import scala.xml.{Elem, NodeSeq}
import net.liftweb.http.S
import net.liftweb.http.js.JE
import net.liftweb.http.js.JsCmds
import net.liftweb.util.Helpers
import Helpers._
import JsCmds._
import JE._
/**
* Prerequisite YUI scripts:
* yahoo.js
* dom.js
* connection.js
* event.js
*/
object YUIArtifacts extends JSArtifacts {
/**
* Toggles between current JS object and the object denominated by id
*/
def toggle(id: String) = new JsExp {
def toJsCmd = "YAHOO.lift.toggle(this, " + id.encJs + ");";
}
/**
* Hides the element denominated by id
*/
def hide(id: String) = new JsExp {
def toJsCmd = "YAHOO.util.Dom.setStyle(" + id.encJs + ", 'display', 'none');"
}
/**
* Shows the element denominated by this id
*/
def show(id: String) = new JsExp {
def toJsCmd = "YAHOO.util.Dom.setStyle(" + id.encJs + ", 'display', 'block');"
}
/**
* Shows the element denoinated by id and puts the focus on it
*/
def showAndFocus(id: String) = new JsExp {
def toJsCmd = "YAHOO.util.Dom.setStyle(" + id.encJs + ", 'display', 'block');" +
"setTimeout(function() { document.getElementById(" + id.encJs + ").focus(); }, 200);"
}
/**
* Serializes a form denominated by the id. It returns a query string
* containing the fields that are to be submitted
*/
def serialize(id: String) = new JsExp {
def toJsCmd = "YAHOO.util.Connect.setForm(" + id.encJs + ", false)"
}
/**
* Replaces the content of the node with the provided id with the markup given by content
*/
def replace(id: String, content: NodeSeq): JsCmd = new JsCmd with HtmlFixer {
override val toJsCmd = {
val (html, js) = fixHtmlAndJs("inline", content)
val ret =
"""
try {
var parent1 = document.getElementById(""" + id.encJs + """);
parent1.innerHTML = """ + html + """;
for (var i = 0; i < parent1.childNodes.length; i++) {
var node = parent1.childNodes[i];
parent1.parentNode.insertBefore(node.cloneNode(true), parent1);
}
parent1.parentNode.removeChild(parent1);
} catch (e) {
// if the node doesn't exist or something else bad happens
}
"""
if (js.isEmpty) ret else ret + " "+js.toJsCmd
}
}
/**
* Sets the inner HTML of the element denominated by the id
*/
def setHtml(uid: String, content: NodeSeq): JsCmd = new JsCmd {
val toJsCmd = fixHtmlCmdFunc(uid, content){s => "try{document.getElementById(" + uid.encJs + ").innerHTML = " + s + ";} catch (e) {}"}
}
/**
* Sets the JavScript that will be executed when document is ready
* for processing
*/
def onLoad(cmd: JsCmd): JsCmd = new JsCmd {
def toJsCmd = "YAHOO.util.Event.onDOMReady(function(){" + cmd.toJsCmd + "})"
}
/**
* Fades out the element having the provided id, by waiting
* for the given duration and fades out during fadeTime
*/
def fadeOut(id: String, duration: TimeSpan, fadeTime: TimeSpan) = Noop
/**
* Makes an Ajax request using lift's Ajax path and the request
* attributes described by data parameter
*/
def ajax(data: AjaxInfo): String = {
val url = S.encodeURL(S.contextPath + "/" + LiftRules.ajaxPath + "/")
"url = YAHOO.lift.buildURI(liftAjax.addPageName(" + url.encJs + ") , " + data.data.toJsCmd + ");" +
"YAHOO.util.Connect.asyncRequest(" + data.action.encJs + ", url, " + toJson(data) + ");"
}
/**
* Makes a Ajax comet request using lift's Comet path and the request
* attributes described by data parameter
*/
def comet(data: AjaxInfo): String = {
val url = LiftRules.calcCometPath(LiftRules.cometServer())
"url = YAHOO.lift.buildURI(" + url.toJsCmd + ", YAHOO.lift.simpleJsonToQS(" + data.data.toJsCmd + "));" +
"YAHOO.util.Connect.asyncRequest(" + data.action.encJs + ", url, " + toJson(data) + ");";
}
/**
* Trabsforms a JSON object intoits string representation
*/
def jsonStringify(in: JsExp): JsExp = new JsExp {
def toJsCmd = "YAHOO.lang.JSON.stringify(" + in.toJsCmd + ")"
}
/**
* Converts a form denominated by formId into a JSON object
*/
def formToJSON(formId: String): JsExp = new JsExp() {
def toJsCmd = "YAHOO.lift.formToJSON('" + formId + "')";
}
private def toJson(info: AjaxInfo): String =
("timeout : " + info.timeout ::
"cache : " + info.cache ::
"success : function(resp) { res = YAHOO.lift.eval(resp);" + info.successFunc.map(_ + "(res);").openOr("") + "}" ::
"failure : " + info.failFunc.openOr("function (arg) {YAHOO.log('Ajax request failed');}") ::
Nil) mkString ("{ ", ", ", " }")
}