# HG changeset patch # User Jaroslav Tulach # Date 1366297135 -7200 # Node ID 9cc253aa94054497e40ef651bf641a6d0b80527c # Parent 66ccab5a353079934b7e4d42afe522988e0d29f0 Getting bunch of the knockout tests work in FX Web View diff -r 66ccab5a3530 -r 9cc253aa9405 javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/ConvertTypes.java --- a/javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/ConvertTypes.java Thu Apr 18 09:06:18 2013 +0200 +++ b/javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/ConvertTypes.java Thu Apr 18 16:58:55 2013 +0200 @@ -22,6 +22,7 @@ import java.io.InputStreamReader; import java.io.PushbackInputStream; import java.io.Reader; +import java.net.MalformedURLException; import java.net.URL; import java.util.Iterator; import java.util.concurrent.Executor; @@ -29,6 +30,7 @@ import java.util.logging.Level; import java.util.logging.Logger; import javafx.application.Platform; +import javafx.scene.web.WebEngine; import netscape.javascript.JSObject; import org.apidesign.bck2brwsr.core.JavaScriptBody; import org.json.JSONArray; @@ -106,6 +108,18 @@ } } + private static String findBaseURL() { + WebEngine eng = (WebEngine) System.getProperties().get("webEngine"); + return (String)eng.executeScript( + "var h;" + + "if (!!window && !!window.location && !!window.location.href)\n" + + " h = window.location.href;\n" + + "else " + + " h = null;" + + "h\n" + ); + } + public static String createJSONP(Object[] jsonResult, Runnable whenDone) { return "json" + Integer.toHexString(whenDone.hashCode()); } @@ -123,12 +137,21 @@ private final Object[] jsonResult; private final Runnable whenDone; private final String jsonp; + private final URL base; LoadJSON(String url, Object[] jsonResult, Runnable whenDone, String jsonp) { this.url = url; this.jsonResult = jsonResult; this.whenDone = whenDone; this.jsonp = jsonp; + URL b; + try { + b = new URL(findBaseURL()); + } catch (MalformedURLException ex) { + LOG.log(Level.SEVERE, "Can't find base url for " + url, ex); + b = null; + } + this.base = b; } @Override @@ -138,7 +161,7 @@ return; } try { - URL u = new URL(url.replace(" ", "%20")); + URL u = new URL(base, url.replace(" ", "%20")); InputStream is = u.openStream(); if (jsonp != null) { PushbackInputStream pis = new PushbackInputStream(is, 1); diff -r 66ccab5a3530 -r 9cc253aa9405 javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/PageProcessor.java --- a/javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/PageProcessor.java Thu Apr 18 09:06:18 2013 +0200 +++ b/javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/PageProcessor.java Thu Apr 18 16:58:55 2013 +0200 @@ -200,10 +200,10 @@ w.append(body.toString()); w.append(" private static Class<" + inPckName(e) + "> modelFor() { return null; }\n"); w.append(" public ").append(className).append("() {\n"); - w.append(" intKnckt();\n"); w.append(" };\n"); - w.append(" private void intKnckt() {\n"); - w.append(" ko = org.apidesign.bck2brwsr.htmlpage.Knockout.applyBindings(this, "); + w.append(" private org.apidesign.bck2brwsr.htmlpage.Knockout intKnckt() {\n"); + w.append(" if (ko != null) return ko;\n"); + w.append(" return ko = org.apidesign.bck2brwsr.htmlpage.Knockout.applyBindings(this, "); writeStringArray(propsGetSet, w); w.append(", "); writeStringArray(functions, w); @@ -276,12 +276,11 @@ } cnt++; } - w.append(" intKnckt();\n"); w.append(" };\n"); writeToString(props, w); writeClone(className, props, w); w.append(" public Object koData() {\n"); - w.append(" return ko.koData();\n"); + w.append(" return intKnckt().koData();\n"); w.append(" }\n"); w.append("}\n"); } finally { diff -r 66ccab5a3530 -r 9cc253aa9405 javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/api/Element.java --- a/javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/api/Element.java Thu Apr 18 09:06:18 2013 +0200 +++ b/javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/api/Element.java Thu Apr 18 16:58:55 2013 +0200 @@ -47,14 +47,34 @@ body="var e = window.document.getElementById(el._id());\n" + "e[property] = value;\n" ) - static native void setAttribute(Element el, String property, Object value); + static void setAttribute(Element el, String property, Object value) { + JSObject js = (JSObject) web().executeScript("(function () {" + + " var x = {}; " + + " x.setAttribute = function(id, property, value) { " + + " var e = window.document.getElementById(id);\n" + + " e[property] = value;" + + " };" + + " return x;" + + "})()"); + js.call("setAttribute", el.id, property, value); + } @JavaScriptBody( args={"el", "property"}, body="var e = window.document.getElementById(el._id());\n" + "return e[property];\n" ) - static native Object getAttribute(Element el, String property); + static Object getAttribute(Element el, String property) { + JSObject js = (JSObject) web().executeScript("(function () {" + + " var x = {}; " + + " x.getAttribute = function(id, property) { " + + " var e = window.document.getElementById(id);\n" + + " return e[property];" + + " };" + + " return x;" + + "})()"); + return js.call("getAttribute", el.id, property); + } @JavaScriptBody( args={"el"}, diff -r 66ccab5a3530 -r 9cc253aa9405 javaquery/api/src/test/java/org/apidesign/bck2brwsr/htmlpage/KnockoutTest.java --- a/javaquery/api/src/test/java/org/apidesign/bck2brwsr/htmlpage/KnockoutTest.java Thu Apr 18 09:06:18 2013 +0200 +++ b/javaquery/api/src/test/java/org/apidesign/bck2brwsr/htmlpage/KnockoutTest.java Thu Apr 18 16:58:55 2013 +0200 @@ -18,6 +18,8 @@ package org.apidesign.bck2brwsr.htmlpage; import java.util.List; +import javafx.scene.web.WebEngine; +import netscape.javascript.JSObject; import org.apidesign.bck2brwsr.core.JavaScriptBody; import org.apidesign.bck2brwsr.htmlpage.api.ComputedProperty; import org.apidesign.bck2brwsr.htmlpage.api.OnEvent; @@ -39,7 +41,7 @@ @Property(name="name", type=String.class), @Property(name="results", type=String.class, array = true), @Property(name="callbackCount", type=int.class), - @Property(name="people", type=PersonImpl.class, array = true) + @Property(name="people", type=Person.class, array = true) }) public class KnockoutTest { @@ -234,25 +236,53 @@ return VMTest.create(KnockoutTest.class); } - @JavaScriptBody(args = { "id" }, body = + private static final String COUNT_CHILDREN = "var e = window.document.getElementById(id);\n " + "if (typeof e === 'undefined') return -2;\n " - + "return e.children.length;\n " - ) - private static native int countChildren(String id); + + "return e.children.length;\n"; + @JavaScriptBody(args = { "id" }, body = COUNT_CHILDREN) + private static int countChildren(String id) { + return ((Number)js().call("countChildren", id)).intValue(); + } - @JavaScriptBody(args = { "id", "pos" }, body = + private static final String TRIGGER_CHILD_CLICK = "var e = window.document.getElementById(id);\n " + "var ev = window.document.createEvent('MouseEvents');\n " + "ev.initMouseEvent('click', true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);\n " - + "e.children[pos].dispatchEvent(ev);\n " - ) - private static native void triggerChildClick(String id, int pos); + + "e.children[pos].dispatchEvent(ev);\n "; + @JavaScriptBody(args = { "id", "pos" }, body = TRIGGER_CHILD_CLICK) + private static void triggerChildClick(String id, int pos) { + js().call("triggerChildClick", id, pos); + } - @JavaScriptBody(args = { "id", "pos" }, body = + private static final String CHILD_TEXT = "var e = window.document.getElementById(id);\n " + "var t = e.children[pos].innerHTML;\n " - + "return t ? t : null;" - ) - private static native String childText(String id, int pos); + + "return t ? t : null;"; + @JavaScriptBody(args = { "id", "pos" }, body = CHILD_TEXT) + private static String childText(String id, int pos) { + return (String) js().call("childText", id, pos); + } + + + private static JSObject js; + private static JSObject js() { + if (js == null) { + WebEngine eng = (WebEngine)System.getProperties().get("webEngine"); + if (eng == null) { + js = null; + } else { + js = (JSObject) eng.executeScript("(function () {" + + "var obj = {};" + + "" + + "obj.countChildren = function(id) { " + COUNT_CHILDREN + "};" + + "obj.triggerChildClick = function(id, pos) { " + TRIGGER_CHILD_CLICK + "};" + + "obj.childText = function(id, pos) { " + CHILD_TEXT + "};" + + "return obj;" + + "})();"); + } + } + + return js; + } } diff -r 66ccab5a3530 -r 9cc253aa9405 rt/launcher/src/main/java/org/apidesign/bck2brwsr/launcher/impl/Console.java --- a/rt/launcher/src/main/java/org/apidesign/bck2brwsr/launcher/impl/Console.java Thu Apr 18 09:06:18 2013 +0200 +++ b/rt/launcher/src/main/java/org/apidesign/bck2brwsr/launcher/impl/Console.java Thu Apr 18 16:58:55 2013 +0200 @@ -99,7 +99,7 @@ + "var status = window.document.createElement('a');\n" + "status.innerHTML = 'running';" + "details.onclick = function() { li.appendChild(p); li.removeChild(details); status.innerHTML = 'Run Again'; status.href = '#'; };\n" - + "status.onclick = function() { c.again__V_3Ljava_lang_Object_2(arr); }\n" + + "status.onclick = function() { c.again(arr); }\n" + "var pre = window.document.createElement('textarea');\n" + "pre.cols = 100;" + "pre.rows = 10;"