Skip to content

Commit

Permalink
Closes lift#1138 Made SetHtml and Replace use jQuery.html() and jQuer…
Browse files Browse the repository at this point in the history
…y.replaceWith() if JqueryArtifacts is chosen as JSArtifact.
  • Loading branch information
andreak committed Nov 2, 2011
1 parent 28c0dea commit 0b8ce63
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 27 deletions.
Expand Up @@ -54,10 +54,15 @@ trait JSArtifacts {
*/
def serialize(id: String): JsExp

/**
* Replaces the content of the node with the provided id with the markup given by content
*/
def replace(id: String, content: NodeSeq): JsCmd

/**
* Sets the inner HTML of the element denominated by the id
*/
def setHtml(id: String, xml: NodeSeq): JsCmd
def setHtml(id: String, content: NodeSeq): JsCmd

/**
* Sets the JavScript that willbe executed when document is ready
Expand Down
23 changes: 2 additions & 21 deletions web/webkit/src/main/scala/net/liftweb/http/js/JsCommands.scala
Expand Up @@ -684,27 +684,8 @@ object JsCmds {
* @param id - the id of the node that will be replaced
* @param node - the new node
*/
case class Replace(id: String, content: NodeSeq) extends JsCmd with HtmlFixer {
override val toJsCmd = {
val (html, js) = fixHtmlAndJs("inline", content)

var 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

}
case class Replace(id: String, content: NodeSeq) extends JsCmd {
val toJsCmd = LiftRules.jsArtifacts.replace(id, Helpers.stripHead(content)).toJsCmd
}

/**
Expand Down
Expand Up @@ -49,6 +49,29 @@ object ExtCoreArtifacts extends JSArtifacts {
def toJsCmd = "Ext.Ajax.serializeForm(" + id.encJs + ")"
}

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

}
}

def setHtml(id: String, xml: NodeSeq): JsCmd = new JsCmd {
def toJsCmd = fixHtmlCmdFunc(id, xml){s => "try { Ext.fly(" + id.encJs + ").dom.innerHTML = " + s + "; } catch (e) {}"}
}
Expand Down
Expand Up @@ -51,7 +51,9 @@ trait JQueryArtifacts extends JSArtifacts {
def toJsCmd = "serialize()"
}

def setHtml(id: String, xml: NodeSeq): JsCmd = JqJsCmds.JqSetHtml(id, xml)
def replace(id: String, content: NodeSeq): JsCmd = JqJsCmds.JqReplace(id, content)

def setHtml(id: String, content: NodeSeq): JsCmd = JqJsCmds.JqSetHtml(id, content)

def onLoad(cmd: JsCmd): JsCmd = JqJsCmds.JqOnLoad(cmd)

Expand Down
Expand Up @@ -251,14 +251,17 @@ object JqJE {
"empty().after(" + fixHtmlFunc("inline", content){str => str} + ")"
}

case class JqReplace(content: NodeSeq) extends JsExp with JsMember {
override val toJsCmd = fixHtmlCmdFunc("inline", content){"replaceWith(" + _ + ")"}
}

object JqHtml {
def apply(): JsExp with JsMember with JQueryRight = new JsExp with JsMember with JQueryRight {
def toJsCmd = "html()"
}

def apply(content: NodeSeq): JsExp with JsMember with JQueryRight with JQueryLeft = new JsExp with JsMember with JQueryRight with JQueryLeft {
val toJsCmd =
"html(" + fixHtmlFunc("inline", content){str => str} + ")"
val toJsCmd = fixHtmlCmdFunc("inline", content){"html(" + _ + ")"}
}
}

Expand Down Expand Up @@ -359,13 +362,15 @@ object JqJsCmds {
JqJE.JqId(JE.Str(uid)) ~> JqJE.JqPrependTo(content)
}

case class JqReplace(uid: String, content: NodeSeq) extends JsCmd {
val toJsCmd = (JqJE.JqId(JE.Str(uid)) ~> JqJE.JqReplace(content)).cmd.toJsCmd
}

case class JqSetHtml(uid: String, content: NodeSeq) extends JsCmd {
/**
* Eagerly evaluate
*/
val toJsCmd =
fixHtmlCmdFunc(uid, content){"try{jQuery(" + ("#" + uid).encJs + ").each(function(i) {this.innerHTML = " + _ + ";});} catch (e) {}"}
val toJsCmd = (JqJE.JqId(JE.Str(uid)) ~> JqJE.JqHtml(content)).cmd.toJsCmd
}

object Show {
Expand Down
Expand Up @@ -58,6 +58,29 @@ object YUIArtifacts extends JSArtifacts {
def toJsCmd = "YAHOO.util.Connect.setForm(" + id.encJs + ", false)"
}

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

}
}

def setHtml(uid: String, content: NodeSeq): JsCmd = new JsCmd {
val toJsCmd = fixHtmlCmdFunc(uid, content){s => "try{document.getElementById(" + uid.encJs + ").innerHTML = " + s + ";} catch (e) {}"}
}
Expand Down

0 comments on commit 0b8ce63

Please sign in to comment.