javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/api/Element.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 22 Jan 2013 21:57:27 +0100
branchmodel
changeset 530 3ce069ec3312
parent 529 8140ba8c005b
child 592 5e13b1ac2886
permissions -rw-r--r--
Test to verify Knockout can handle String properties
     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 org.apidesign.bck2brwsr.core.JavaScriptBody;
    21 
    22 /** Represents a generic HTML element.
    23  *
    24  * @author Jaroslav Tulach <jtulach@netbeans.org>
    25  */
    26 public abstract class Element {
    27     private final String id;
    28     
    29     public Element(String id) {
    30         this.id = id;
    31     }
    32     
    33     /** Id of the element in the document.
    34      * @return the id for this element
    35      */
    36     public String getId() {
    37         return id;
    38     }
    39     
    40     abstract void dontSubclass();
    41     
    42     @JavaScriptBody(
    43         args={"el", "property", "value"},
    44         body="var e = window.document.getElementById(el.fld_id);\n"
    45            + "e[property] = value;\n"
    46     )
    47     static native void setAttribute(Element el, String property, Object value);
    48 
    49     @JavaScriptBody(
    50         args={"el", "property"},
    51         body="var e = window.document.getElementById(el.fld_id);\n"
    52            + "return e[property];\n"
    53     )
    54     static native Object getAttribute(Element el, String property);
    55     
    56     @JavaScriptBody(
    57         args={"el"},
    58         body="return window.document.getElementById(el.fld_id);"
    59     )
    60     static native Object getElementById(Element el);
    61     
    62     /** Executes given runnable when user performs a "click" on the given
    63      * element.
    64      * @param r the runnable to execute, never null
    65      */
    66     @JavaScriptBody(
    67         args={ "ev", "r" },
    68         body="var e = window.document.getElementById(this.fld_id);\n"
    69            + "e[ev.fld_id] = function() { r.run__V(); };\n"
    70     )
    71     final void on(OnEvent ev, Runnable r) {
    72     }
    73 
    74     /** Shows alert message dialog in a browser.
    75      * @param msg the message to show
    76      */
    77     @JavaScriptBody(args = "msg", body = "alert(msg);")
    78     public static native void alert(String msg);
    79 
    80     /** Generic way to query any attribute of this element.
    81      * @param property name of the attribute
    82      */
    83     public final Object getAttribute(String property) {
    84         return getAttribute(this, property);
    85     }
    86     
    87     /** Generic way to change an attribute of this element.
    88      * 
    89      * @param property name of the attribute
    90      * @param value value to associate with the attribute
    91      */
    92     public final void setAttribute(String property, Object value) {
    93         setAttribute(this, property, value);
    94     }
    95 }