ko/kosample/js/src/main/java/org/apidesign/bck2brwsr/kosample/js/Dialogs.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Wed, 27 Apr 2016 06:14:49 +0200
changeset 1947 12a252145892
parent 1941 621825e167d7
permissions -rw-r--r--
Automatic testing of sample knockout based bck2brwsr application
     1 /**
     2  * Back 2 Browser Bytecode Translator
     3  * Copyright (C) 2012-2015 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.kosample.js;
    19 
    20 import net.java.html.js.JavaScriptBody;
    21 
    22 /** Use {@link JavaScriptBody} annotation on methods to
    23  * directly interact with JavaScript. See
    24  * http://bits.netbeans.org/html+java/1.2/net/java/html/js/package-summary.html
    25  * to understand how.
    26  */
    27 public final class Dialogs {
    28     private Dialogs() {
    29     }
    30     
    31     /** Shows confirmation dialog to the user.
    32      * 
    33      * @param msg the message
    34      * @param callback called back when the use accepts (can be null)
    35      */
    36     @JavaScriptBody(
    37         args = { "msg", "callback" }, 
    38         javacall = true, 
    39         body = "if (confirm(msg)) {\n"
    40              + "  callback.@java.lang.Runnable::run()();\n"
    41              + "}\n"
    42     )
    43     public static native void confirmByUser(String msg, Runnable callback);
    44     
    45     @JavaScriptBody(
    46         args = {}, body = 
    47         "var w = window,\n" +
    48         "    d = document,\n" +
    49         "    e = d.documentElement,\n" +
    50         "    g = d.getElementsByTagName('body')[0],\n" +
    51         "    x = w.innerWidth || e.clientWidth || g.clientWidth,\n" +
    52         "    y = w.innerHeight|| e.clientHeight|| g.clientHeight;\n" +
    53         "\n" +
    54         "return 'Screen size is ' + x + ' times ' + y;\n"
    55     )
    56     public static native String screenSize();
    57 
    58     @JavaScriptBody(args = { "id" }, body =
    59           "var e = window.document.getElementById(id);\n "
    60         + "var ev = window.document.createEvent('MouseEvents');\n "
    61         + "ev.initMouseEvent('click', true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);\n "
    62         + "e.dispatchEvent(ev);\n "
    63     )
    64     public static native void triggerClick(String id);
    65 }