javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/Knockout.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Thu, 18 Apr 2013 23:09:30 +0200
branchfx
changeset 1016 6dc2c6c752df
parent 1015 d7cff2cba6e5
permissions -rw-r--r--
Can execute 'dual' tests: bck2brwsr can use regular launcher, javaquery.api can use FX Web View one
     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.List;
    25 import java.util.logging.Level;
    26 import java.util.logging.Logger;
    27 import javafx.scene.web.WebEngine;
    28 import netscape.javascript.JSObject;
    29 import org.apidesign.bck2brwsr.core.ExtraJavaScript;
    30 import org.apidesign.bck2brwsr.core.JavaScriptBody;
    31 
    32 /** Provides binding between models and 
    33  *
    34  * @author Jaroslav Tulach <jtulach@netbeans.org>
    35  */
    36 @ExtraJavaScript(resource = "/org/apidesign/bck2brwsr/htmlpage/knockout-2.2.1.js")
    37 public class Knockout {
    38     private static final Logger LOG = Logger.getLogger(Knockout.class.getName());
    39     /** used by tests */
    40     static Knockout next;
    41     private final Object model;
    42 
    43     Knockout(Object model) {
    44         this.model = model == null ? this : model;
    45     }
    46     
    47     public Object koData() {
    48         return model;
    49     }
    50 
    51     static Object toArray(Object[] arr) {
    52         return InvokeJS.KObject.call("array", arr);
    53     }
    54     
    55     public static <M> Knockout applyBindings(
    56         Object model, String[] propsGettersAndSetters,
    57         String[] methodsAndSignatures
    58     ) {
    59         Object bindings = InvokeJS.KObject.call("create", model);
    60         applyImpl(propsGettersAndSetters, model.getClass(), bindings, model, methodsAndSignatures);
    61         return new Knockout(bindings);
    62     }
    63     public static <M> Knockout applyBindings(
    64         Class<M> modelClass, M model, String[] propsGettersAndSetters,
    65         String[] methodsAndSignatures
    66     ) {
    67         Object bindings = next;
    68         next = null;
    69         if (bindings == null) {
    70             bindings = InvokeJS.KObject.call("create", model);
    71         }
    72         applyImpl(propsGettersAndSetters, modelClass, bindings, model, methodsAndSignatures);
    73         applyBindings(bindings);
    74         return new Knockout(bindings);
    75     }
    76 
    77     public void valueHasMutated(String prop) {
    78         LOG.log(Level.FINE, "property mutated: {0}", prop);
    79         try {
    80             JSObject koProp = (JSObject) ((JSObject) model).getMember(prop);
    81             koProp.call("valueHasMutated");
    82         } catch (Throwable t) {
    83             LOG.log(Level.WARNING, "valueHasMutated failed for {0}", model);
    84         }
    85     }
    86     
    87 
    88     @JavaScriptBody(args = { "id", "ev" }, body = "ko.utils.triggerEvent(window.document.getElementById(id), ev.substring(2));")
    89     public static void triggerEvent(String id, String ev) {
    90         JSObject js = (JSObject) web().executeScript("(function () {"
    91             + "  var x = {}; "
    92             + "  x.trigger= function(id, ev) { "
    93             + "    ko.utils.triggerEvent(window.document.getElementById(id), ev.substring(2));;\n"
    94             + "  };"
    95             + "  return x;"
    96             + "})()");
    97         js.call("trigger", id, ev);
    98     }
    99     
   100     @JavaScriptBody(args = { "bindings", "model", "prop", "getter", "setter", "primitive", "array" }, body =
   101           "var bnd = {\n"
   102         + "  'read': function() {\n"
   103         + "    var v = model[getter]();\n"
   104         + "    if (array) v = v.koArray();\n"
   105         + "    return v;\n"
   106         + "  },\n"
   107         + "  'owner': bindings\n"
   108         + "};\n"
   109         + "if (setter != null) {\n"
   110         + "  bnd['write'] = function(val) {\n"
   111         + "    model[setter](primitive ? new Number(val) : val);\n"
   112         + "  };\n"
   113         + "}\n"
   114         + "bindings[prop] = ko['computed'](bnd);"
   115     )
   116     private static void bind(
   117         Object bindings, Object model, String prop, String getter, String setter, boolean primitive, boolean array
   118     ) {
   119         WebEngine e = web();
   120         if (e == null) {
   121             return;
   122         }
   123         JSObject bnd = (JSObject) e.executeScript("var x = {}; x.bnd = "
   124         + "new Function('ko', 'bindings', 'model', 'prop', 'getter', 'setter', 'primitive', 'array', '"
   125         + "var bnd = {"
   126         + "  read: function() {"
   127         + "    try {"
   128         + "      var v = model[getter]();"
   129 //        + "      console.log(\" getter value \" + v + \" for property \" + prop);"
   130         + "      try { v = v.koData(); } catch (ignore) {"
   131 //        + "        console.log(\"Cannot convert to koData: \" + ignore);"
   132         + "      };"
   133 //        + "      console.log(\" getter ret value \" + v);"
   134 //        + "      for (var pn in v) {"
   135 //        + "         console.log(\"  prop: \" + pn + \" + in + \" + v + \" = \" + v[pn]);"
   136 //        + "         if (typeof v[pn] == \"function\") console.log(\"  its function value:\" + v[pn]());"
   137 //        + "      }"
   138 //        + "      console.log(\" all props printed for \" + (typeof v));"
   139         + "      return v;"
   140         + "    } catch (e) {"
   141         + "      alert(\"Cannot call \" + getter + \" on \" + model + \" error: \" + e);"
   142         + "    }"
   143         + "  },"
   144         + "  owner: bindings,"
   145         + "  deferEvaluation: true"
   146         + "};"
   147         + "if (setter != null) {"
   148         + "  bnd.write = function(val) {"
   149         + "    model[setter](primitive ? new Number(val) : val);"
   150         + "  };"
   151         + "};"
   152         + "bindings[prop] = ko.computed(bnd);'"
   153         + "); x;");
   154 
   155         Object ko = e.executeScript("ko");
   156         try {
   157             KOProperty kop = new KOProperty(model, strip(getter), strip(setter));
   158             bnd.call("bnd", ko, bindings, kop, prop, "get", "set", primitive, array);
   159             
   160             ((JSObject)bindings).setMember("koModel", model);
   161             LOG.log(Level.FINE, "binding defined for {0}: {1}", new Object[]{prop, ((JSObject)bindings).getMember(prop)});
   162         } catch (Throwable ex) {
   163             LOG.log(Level.WARNING, "binding failed for {0} on {1}", new Object[]{prop, bindings});
   164         }
   165     }
   166     
   167     private static String strip(String mangled) {
   168         if (mangled == null) {
   169             return null;
   170         }
   171         int under = mangled.indexOf("__");
   172         return mangled.substring(0, under);
   173     }
   174     @JavaScriptBody(args = { "bindings", "model", "prop", "sig" }, body = 
   175         "bindings[prop] = function(data, ev) { model[sig](data, ev); };"
   176     )
   177     private static void expose(
   178         Object bindings, Object model, String prop, String sig
   179     ) {
   180         WebEngine e = web();
   181         if (e == null) {
   182             return;
   183         }
   184         try {
   185             KOFunction f = new KOFunction(model, strip(sig));
   186             InvokeJS.KObject.call("expose", bindings, f, prop, "call");
   187         } catch (Throwable ex) {
   188             LOG.log(Level.SEVERE, "Cannot define binding for " + prop + " in model " + model, ex);
   189         }
   190     }
   191     
   192     @JavaScriptBody(args = { "bindings" }, body = "ko.applyBindings(bindings);")
   193     private static void applyBindings(Object bindings) {
   194         if (web() != null) {
   195             JSObject ko = (JSObject) web().executeScript("ko");
   196             ko.call("applyBindings", bindings);
   197         }
   198     }
   199     
   200     private static WebEngine web() {
   201         return (WebEngine) System.getProperties().get("webEngine");
   202     }
   203     
   204     
   205     private static void applyImpl(
   206         String[] propsGettersAndSetters,
   207         Class<?> modelClass,
   208         Object bindings,
   209         Object model,
   210         String[] methodsAndSignatures
   211     ) throws IllegalStateException, SecurityException {
   212         for (int i = 0; i < propsGettersAndSetters.length; i += 4) {
   213             try {
   214                 Method getter = modelClass.getMethod(propsGettersAndSetters[i + 3]);
   215                 bind(bindings, model, propsGettersAndSetters[i],
   216                     propsGettersAndSetters[i + 1],
   217                     propsGettersAndSetters[i + 2],
   218                     getter.getReturnType().isPrimitive(),
   219                     List.class.isAssignableFrom(getter.getReturnType()));
   220             } catch (NoSuchMethodException ex) {
   221                 throw new IllegalStateException(ex.getMessage());
   222             }
   223         }
   224         for (int i = 0; i < methodsAndSignatures.length; i += 2) {
   225             expose(
   226                 bindings, model, methodsAndSignatures[i], methodsAndSignatures[i + 1]);
   227         }
   228     }
   229     
   230     private static final class InvokeJS {
   231         static final JSObject KObject;
   232 
   233         static {
   234             BufferedReader r = new BufferedReader(new InputStreamReader(Knockout.class.getResourceAsStream("knockout-2.2.1.js")));
   235             StringBuilder sb = new StringBuilder();
   236             for (;;) {
   237                 try {
   238                     String l = r.readLine();
   239                     if (l == null) {
   240                         break;
   241                     }
   242                     sb.append(l).append('\n');
   243                 } catch (IOException ex) {
   244                     throw new IllegalStateException(ex);
   245                 }
   246             }
   247             web().executeScript(sb.toString());
   248             Object ko = web().executeScript("ko");
   249             assert ko != null : "Knockout library successfully defined 'ko'";
   250 
   251             Console.register(web());
   252             KObject = (JSObject) web().executeScript(
   253                 "(function(scope) {"
   254                 + "  var kCnt = 0; "
   255                 + "  scope.KObject = {};"
   256                 + "  scope.KObject.create= function(value) {"
   257                 + "    var cnt = ++kCnt;"
   258                 + "    var ret = {};"
   259                 + "    ret.toString = function() { return 'KObject' + cnt + ' value: ' + value + ' props: ' + Object.keys(this); };"
   260                 + "    return ret;"
   261                 + "  };"
   262                 + "  scope.KObject.array= function() {"
   263                 + "    return Array.prototype.slice.call(arguments);"
   264                 + "  };"
   265                 + "  scope.KObject.expose = function(bindings, model, prop, sig) {"
   266                 + "    bindings[prop] = function(data, ev) {"
   267                 //            + "         console.log(\"  callback on prop: \" + prop);"
   268                 + "      model[sig](data, ev);"
   269                 + "    };"
   270                 + "  };"
   271                 + "})(window); window.KObject");
   272         }
   273         
   274     }
   275 }