javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/Knockout.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 19 Mar 2013 11:51:08 +0100
branchfx
changeset 852 671fc517fe17
parent 851 c285a78302af
child 969 df08556c5c7c
permissions -rw-r--r--
Disable logging by default
     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;
    19 
    20 import java.io.BufferedReader;
    21 import java.io.IOException;
    22 import java.io.InputStreamReader;
    23 import java.lang.reflect.Method;
    24 import java.util.logging.Level;
    25 import java.util.logging.Logger;
    26 import javafx.scene.web.WebEngine;
    27 import netscape.javascript.JSObject;
    28 import org.apidesign.bck2brwsr.core.ExtraJavaScript;
    29 import org.apidesign.bck2brwsr.core.JavaScriptBody;
    30 
    31 /** Provides binding between models and 
    32  *
    33  * @author Jaroslav Tulach <jtulach@netbeans.org>
    34  */
    35 @ExtraJavaScript(resource = "/org/apidesign/bck2brwsr/htmlpage/knockout-2.2.1.js")
    36 public class Knockout {
    37     private static final Logger LOG = Logger.getLogger(Knockout.class.getName());
    38     /** used by tests */
    39     static Knockout next;
    40     
    41     static {
    42         BufferedReader r = new BufferedReader(new InputStreamReader(Knockout.class.getResourceAsStream("knockout-2.2.1.js")));
    43         StringBuilder sb = new StringBuilder();
    44         for (;;) {
    45             try {
    46                 String l = r.readLine();
    47                 if (l == null) {
    48                     break;
    49                 }
    50                 sb.append(l).append('\n');
    51             } catch (IOException ex) {
    52                 throw new IllegalStateException(ex);
    53             }
    54         }
    55         web().executeScript(sb.toString());
    56         Object ko = web().executeScript("ko");
    57         assert ko != null : "Knockout library successfully defined 'ko'";
    58     }
    59     
    60     
    61     private final Object bindings;
    62     
    63     Knockout(Object bindings) {
    64         this.bindings = bindings;
    65     }
    66     
    67     public static <M> Knockout applyBindings(
    68         Class<M> modelClass, M model, String[] propsGettersAndSetters
    69     ) {
    70         Object bindings = next;
    71         next = null;
    72         if (bindings == null) {
    73             bindings = web().executeScript("new Object()");
    74         }
    75         for (int i = 0; i < propsGettersAndSetters.length; i += 4) {
    76             try {
    77                 Method getter = modelClass.getMethod(propsGettersAndSetters[i + 3]);
    78                 bind(bindings, model, propsGettersAndSetters[i],
    79                     propsGettersAndSetters[i + 1],
    80                     propsGettersAndSetters[i + 2],
    81                     getter.getReturnType().isPrimitive()
    82                 );
    83             } catch (NoSuchMethodException ex) {
    84                 throw new IllegalStateException(ex.getMessage());
    85             }
    86         }
    87         applyBindings(bindings);
    88         return new Knockout(bindings);
    89     }
    90 
    91     @JavaScriptBody(args = { "prop" }, body =
    92         "this[prop].valueHasMutated();"
    93     )
    94     public void valueHasMutated(String prop) {
    95         LOG.log(Level.FINE, "property mutated: {0}", prop);
    96         JSObject koProp = (JSObject) ((JSObject)bindings).getMember(prop);
    97         koProp.call("valueHasMutated");
    98     }
    99     
   100 
   101     @JavaScriptBody(args = { "id", "ev" }, body = "ko.utils.triggerEvent(window.document.getElementById(id), ev.substring(2));")
   102     public static void triggerEvent(String id, String ev) {
   103     }
   104     
   105     @JavaScriptBody(args = { "bindings", "model", "prop", "getter", "setter", "primitive" }, body =
   106           "var bnd = {\n"
   107         + "  read: function() {\n"
   108         + "    var v = model[getter]();\n"
   109         + "    return v;\n"
   110         + "  },\n"
   111         + "  owner: bindings\n"
   112         + "};\n"
   113         + "if (setter != null) {\n"
   114         + "  bnd.write = function(val) {\n"
   115         + "    model[setter](primitive ? new Number(val) : val);\n"
   116         + "  };\n"
   117         + "}\n"
   118         + "bindings[prop] = ko.computed(bnd);"
   119     )
   120     private static void bind(
   121         Object bindings, Object model, String prop, String getter, String setter, boolean primitive
   122     ) {
   123         WebEngine e = web();
   124         JSObject bnd = (JSObject) e.executeScript("var x = {}; x.bnd = "
   125         + "new Function('ko', 'bindings', 'model', 'prop', 'getter', 'setter', 'primitive', '"
   126         + "var bnd = {"
   127         + "  read: function() {"
   128         + "    try {"
   129         + "      return model[getter]();"
   130         + "    } catch (e) {"
   131         + "      alert(\"Cannot call \" + getter + \" on \" + model + \" error: \" + e);"
   132         + "    }"
   133         + "  },"
   134         + "  owner: bindings"
   135         + "};"
   136         + "if (setter != null) {"
   137         + "  bnd.write = function(val) {"
   138         + "    model[setter](primitive ? new Number(val) : val);"
   139         + "  };"
   140         + "};"
   141         + "bindings[prop] = ko.computed(bnd);'"
   142         + "); x;");
   143         
   144         Object ko = e.executeScript("ko");
   145         bnd.call("bnd", ko, bindings, model, prop, strip(getter), strip(setter), primitive);
   146         LOG.log(Level.FINE, "binding defined for {0}: {1}", new Object[]{prop, ((JSObject)bindings).getMember(prop)});
   147     }
   148     
   149     private static String strip(String mangled) {
   150         if (mangled == null) {
   151             return null;
   152         }
   153         int under = mangled.indexOf("__");
   154         return mangled.substring(0, under);
   155     }
   156     
   157     @JavaScriptBody(args = { "bindings" }, body = "ko.applyBindings(bindings);")
   158     private static void applyBindings(Object bindings) {
   159         JSObject ko = (JSObject) web().executeScript("ko");
   160         ko.call("applyBindings", bindings);
   161     }
   162     
   163     private static WebEngine web() {
   164         return (WebEngine) System.getProperties().get("webEngine");
   165     }
   166     
   167 }