jaroslav@26
|
1 |
/**
|
jaroslav@106
|
2 |
* Back 2 Browser Bytecode Translator
|
jaroslav@106
|
3 |
* Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
|
jaroslav@26
|
4 |
*
|
jaroslav@26
|
5 |
* This program is free software: you can redistribute it and/or modify
|
jaroslav@26
|
6 |
* it under the terms of the GNU General Public License as published by
|
jaroslav@26
|
7 |
* the Free Software Foundation, version 2 of the License.
|
jaroslav@26
|
8 |
*
|
jaroslav@26
|
9 |
* This program is distributed in the hope that it will be useful,
|
jaroslav@26
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
jaroslav@26
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
jaroslav@26
|
12 |
* GNU General Public License for more details.
|
jaroslav@26
|
13 |
*
|
jaroslav@26
|
14 |
* You should have received a copy of the GNU General Public License
|
jaroslav@26
|
15 |
* along with this program. Look for COPYING file in the top folder.
|
jaroslav@26
|
16 |
* If not, see http://opensource.org/licenses/GPL-2.0.
|
jaroslav@26
|
17 |
*/
|
jaroslav@26
|
18 |
package org.apidesign.bck2brwsr.htmlpage.api;
|
jaroslav@26
|
19 |
|
jaroslav@845
|
20 |
import javafx.scene.web.WebEngine;
|
jaroslav@845
|
21 |
import netscape.javascript.JSObject;
|
jaroslav@100
|
22 |
import org.apidesign.bck2brwsr.core.JavaScriptBody;
|
jaroslav@100
|
23 |
|
jaroslav@26
|
24 |
/** Represents a generic HTML element.
|
jaroslav@26
|
25 |
*
|
jaroslav@26
|
26 |
* @author Jaroslav Tulach <jtulach@netbeans.org>
|
jaroslav@26
|
27 |
*/
|
jaroslav@978
|
28 |
public class Element {
|
jaroslav@26
|
29 |
private final String id;
|
jaroslav@26
|
30 |
|
jaroslav@26
|
31 |
public Element(String id) {
|
jaroslav@26
|
32 |
this.id = id;
|
jaroslav@26
|
33 |
}
|
jaroslav@26
|
34 |
|
jaroslav@530
|
35 |
/** Id of the element in the document.
|
jaroslav@530
|
36 |
* @return the id for this element
|
jaroslav@530
|
37 |
*/
|
jaroslav@530
|
38 |
public String getId() {
|
jaroslav@530
|
39 |
return id;
|
jaroslav@530
|
40 |
}
|
jaroslav@530
|
41 |
|
jaroslav@978
|
42 |
void dontSubclass() {
|
jaroslav@978
|
43 |
}
|
jaroslav@27
|
44 |
|
jaroslav@100
|
45 |
@JavaScriptBody(
|
jaroslav@100
|
46 |
args={"el", "property", "value"},
|
jaroslav@592
|
47 |
body="var e = window.document.getElementById(el._id());\n"
|
jaroslav@100
|
48 |
+ "e[property] = value;\n"
|
jaroslav@100
|
49 |
)
|
jaroslav@1011
|
50 |
static void setAttribute(Element el, String property, Object value) {
|
jaroslav@1011
|
51 |
JSObject js = (JSObject) web().executeScript("(function () {"
|
jaroslav@1011
|
52 |
+ " var x = {}; "
|
jaroslav@1011
|
53 |
+ " x.setAttribute = function(id, property, value) { "
|
jaroslav@1011
|
54 |
+ " var e = window.document.getElementById(id);\n"
|
jaroslav@1011
|
55 |
+ " e[property] = value;"
|
jaroslav@1011
|
56 |
+ " };"
|
jaroslav@1011
|
57 |
+ " return x;"
|
jaroslav@1011
|
58 |
+ "})()");
|
jaroslav@1011
|
59 |
js.call("setAttribute", el.id, property, value);
|
jaroslav@1011
|
60 |
}
|
jaroslav@100
|
61 |
|
jaroslav@100
|
62 |
@JavaScriptBody(
|
jaroslav@100
|
63 |
args={"el", "property"},
|
jaroslav@592
|
64 |
body="var e = window.document.getElementById(el._id());\n"
|
jaroslav@100
|
65 |
+ "return e[property];\n"
|
jaroslav@100
|
66 |
)
|
jaroslav@1011
|
67 |
static Object getAttribute(Element el, String property) {
|
jaroslav@1011
|
68 |
JSObject js = (JSObject) web().executeScript("(function () {"
|
jaroslav@1011
|
69 |
+ " var x = {}; "
|
jaroslav@1011
|
70 |
+ " x.getAttribute = function(id, property) { "
|
jaroslav@1011
|
71 |
+ " var e = window.document.getElementById(id);\n"
|
jaroslav@1011
|
72 |
+ " return e[property];"
|
jaroslav@1011
|
73 |
+ " };"
|
jaroslav@1011
|
74 |
+ " return x;"
|
jaroslav@1011
|
75 |
+ "})()");
|
jaroslav@1011
|
76 |
return js.call("getAttribute", el.id, property);
|
jaroslav@1011
|
77 |
}
|
jaroslav@28
|
78 |
|
toni@521
|
79 |
@JavaScriptBody(
|
toni@521
|
80 |
args={"el"},
|
jaroslav@592
|
81 |
body="return window.document.getElementById(el._id());"
|
toni@521
|
82 |
)
|
toni@521
|
83 |
static native Object getElementById(Element el);
|
toni@521
|
84 |
|
jaroslav@28
|
85 |
/** Executes given runnable when user performs a "click" on the given
|
jaroslav@28
|
86 |
* element.
|
jaroslav@813
|
87 |
* @param data an array of one element to fill with event parameter (if any)
|
jaroslav@28
|
88 |
* @param r the runnable to execute, never null
|
jaroslav@28
|
89 |
*/
|
jaroslav@100
|
90 |
@JavaScriptBody(
|
jaroslav@443
|
91 |
args={ "ev", "r" },
|
jaroslav@592
|
92 |
body="var e = window.document.getElementById(this._id());\n"
|
jaroslav@813
|
93 |
+ "e[ev._id()] = function(ev) {\n"
|
jaroslav@813
|
94 |
+ " var d = ev ? ev : null;\n"
|
jaroslav@813
|
95 |
+ " r.onEvent__VLjava_lang_Object_2(d);\n"
|
jaroslav@813
|
96 |
+ "};\n"
|
jaroslav@100
|
97 |
)
|
jaroslav@813
|
98 |
final void on(OnEvent ev, OnHandler r) {
|
jaroslav@845
|
99 |
WebEngine e = web();
|
jaroslav@845
|
100 |
JSObject add = (JSObject)e.executeScript(
|
jaroslav@845
|
101 |
"var x = {}; x.add = new Function('e','attr','r', "
|
jaroslav@845
|
102 |
+ " 'window.document.getElementById(e)[attr] = function(ev) { var d = ev ? ev : null; try { r.onEvent(d); } catch (x) { alert(\"OK\" + r[\"onEvent\"] + \"e\" + d + x); } };'"
|
jaroslav@845
|
103 |
+ "); x;");
|
jaroslav@845
|
104 |
add.call("add", id, ev.id, r);
|
jaroslav@845
|
105 |
}
|
jaroslav@845
|
106 |
|
jaroslav@845
|
107 |
private static WebEngine web() {
|
jaroslav@845
|
108 |
return (WebEngine) System.getProperties().get("webEngine");
|
jaroslav@514
|
109 |
}
|
jaroslav@428
|
110 |
|
jaroslav@428
|
111 |
/** Shows alert message dialog in a browser.
|
jaroslav@428
|
112 |
* @param msg the message to show
|
jaroslav@428
|
113 |
*/
|
jaroslav@428
|
114 |
@JavaScriptBody(args = "msg", body = "alert(msg);")
|
jaroslav@845
|
115 |
public static void alert(String msg) {
|
jaroslav@845
|
116 |
JSObject obj = (JSObject) web().executeScript("window");
|
jaroslav@845
|
117 |
obj.call("alert", msg);
|
jaroslav@845
|
118 |
}
|
jaroslav@436
|
119 |
|
jaroslav@436
|
120 |
/** Generic way to query any attribute of this element.
|
jaroslav@436
|
121 |
* @param property name of the attribute
|
jaroslav@436
|
122 |
*/
|
jaroslav@436
|
123 |
public final Object getAttribute(String property) {
|
jaroslav@436
|
124 |
return getAttribute(this, property);
|
jaroslav@436
|
125 |
}
|
jaroslav@428
|
126 |
|
jaroslav@436
|
127 |
/** Generic way to change an attribute of this element.
|
jaroslav@436
|
128 |
*
|
jaroslav@436
|
129 |
* @param property name of the attribute
|
jaroslav@436
|
130 |
* @param value value to associate with the attribute
|
jaroslav@436
|
131 |
*/
|
jaroslav@436
|
132 |
public final void setAttribute(String property, Object value) {
|
jaroslav@436
|
133 |
setAttribute(this, property, value);
|
jaroslav@436
|
134 |
}
|
jaroslav@26
|
135 |
}
|