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