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