2 * Back 2 Browser Bytecode Translator
3 * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
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.
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.
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.
18 package org.apidesign.bck2brwsr.htmlpage;
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;
31 /** Provides binding between models and
33 * @author Jaroslav Tulach <jtulach@netbeans.org>
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());
42 BufferedReader r = new BufferedReader(new InputStreamReader(Knockout.class.getResourceAsStream("knockout-2.2.1.js")));
43 StringBuilder sb = new StringBuilder();
46 String l = r.readLine();
50 sb.append(l).append('\n');
51 } catch (IOException ex) {
52 throw new IllegalStateException(ex);
55 web().executeScript(sb.toString());
56 Object ko = web().executeScript("ko");
57 assert ko != null : "Knockout library successfully defined 'ko'";
61 private final Object bindings;
63 Knockout(Object bindings) {
64 this.bindings = bindings;
67 public static <M> Knockout applyBindings(
68 Class<M> modelClass, M model, String[] propsGettersAndSetters
70 Object bindings = next;
72 if (bindings == null) {
73 bindings = web().executeScript("new Object()");
75 for (int i = 0; i < propsGettersAndSetters.length; i += 4) {
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()
83 } catch (NoSuchMethodException ex) {
84 throw new IllegalStateException(ex.getMessage());
87 applyBindings(bindings);
88 return new Knockout(bindings);
91 @JavaScriptBody(args = { "prop" }, body =
92 "this[prop].valueHasMutated();"
94 public void valueHasMutated(String prop) {
95 LOG.log(Level.INFO, "property mutated: {0}", prop);
96 JSObject koProp = (JSObject) ((JSObject)bindings).getMember(prop);
97 koProp.call("valueHasMutated");
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) {
105 @JavaScriptBody(args = { "bindings", "model", "prop", "getter", "setter", "primitive" }, body =
107 + " read: function() {\n"
108 + " var v = model[getter]();\n"
111 + " owner: bindings\n"
113 + "if (setter != null) {\n"
114 + " bnd.write = function(val) {\n"
115 + " model[setter](primitive ? new Number(val) : val);\n"
118 + "bindings[prop] = ko.computed(bnd);"
120 private static void bind(
121 Object bindings, Object model, String prop, String getter, String setter, boolean primitive
124 JSObject bnd = (JSObject) e.executeScript("var x = {}; x.bnd = "
125 + "new Function('ko', 'bindings', 'model', 'prop', 'getter', 'setter', 'primitive', '"
127 + " read: function() {"
129 + " return model[getter]();"
131 + " alert(\"Cannot call \" + getter + \" on \" + model + \" error: \" + e);"
136 + "if (setter != null) {"
137 + " bnd.write = function(val) {"
138 + " model[setter](primitive ? new Number(val) : val);"
141 + "bindings[prop] = ko.computed(bnd);'"
144 Object ko = e.executeScript("ko");
145 bnd.call("bnd", ko, bindings, model, prop, strip(getter), strip(setter), primitive);
146 LOG.log(Level.INFO, "binding defined for {0}: {1}", new Object[]{prop, ((JSObject)bindings).getMember(prop)});
149 private static String strip(String mangled) {
150 if (mangled == null) {
153 int under = mangled.indexOf("__");
154 return mangled.substring(0, under);
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);
163 private static WebEngine web() {
164 return (WebEngine) System.getProperties().get("webEngine");