Merging in default branch before release 0.6 is created. Code compiles.
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.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;
32 /** Provides binding between models and
34 * @author Jaroslav Tulach <jtulach@netbeans.org>
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());
41 private final Object model;
44 BufferedReader r = new BufferedReader(new InputStreamReader(Knockout.class.getResourceAsStream("knockout-2.2.1.js")));
45 StringBuilder sb = new StringBuilder();
48 String l = r.readLine();
52 sb.append(l).append('\n');
53 } catch (IOException ex) {
54 throw new IllegalStateException(ex);
57 web().executeScript(sb.toString());
58 Object ko = web().executeScript("ko");
59 assert ko != null : "Knockout library successfully defined 'ko'";
63 Knockout(Object model) {
64 this.model = model == null ? this : model;
67 public static <M> Knockout applyBindings(
68 Object model, String[] propsGettersAndSetters,
69 String[] methodsAndSignatures
71 applyImpl(propsGettersAndSetters, model.getClass(), model, model, methodsAndSignatures);
72 return new Knockout(model);
74 public static <M> Knockout applyBindings(
75 Class<M> modelClass, M model, String[] propsGettersAndSetters,
76 String[] methodsAndSignatures
78 Object bindings = next;
80 if (bindings == null) {
81 bindings = web().executeScript("new Object()");
83 applyImpl(propsGettersAndSetters, modelClass, bindings, model, methodsAndSignatures);
84 applyBindings(bindings);
85 return new Knockout(bindings);
88 public void valueHasMutated(String prop) {
89 LOG.log(Level.FINE, "property mutated: {0}", prop);
90 JSObject koProp = (JSObject) ((JSObject)model).getMember(prop);
91 koProp.call("valueHasMutated");
95 @JavaScriptBody(args = { "id", "ev" }, body = "ko.utils.triggerEvent(window.document.getElementById(id), ev.substring(2));")
96 public static void triggerEvent(String id, String ev) {
99 @JavaScriptBody(args = { "bindings", "model", "prop", "getter", "setter", "primitive", "array" }, body =
101 + " 'read': function() {\n"
102 + " var v = model[getter]();\n"
103 + " if (array) v = v.koArray();\n"
106 + " 'owner': bindings\n"
108 + "if (setter != null) {\n"
109 + " bnd['write'] = function(val) {\n"
110 + " model[setter](primitive ? new Number(val) : val);\n"
113 + "bindings[prop] = ko['computed'](bnd);"
115 private static void bind(
116 Object bindings, Object model, String prop, String getter, String setter, boolean primitive, boolean array
119 JSObject bnd = (JSObject) e.executeScript("var x = {}; x.bnd = "
120 + "new Function('ko', 'bindings', 'model', 'prop', 'getter', 'setter', 'primitive', '"
122 + " read: function() {"
124 + " return model[getter]();"
126 + " alert(\"Cannot call \" + getter + \" on \" + model + \" error: \" + e);"
131 + "if (setter != null) {"
132 + " bnd.write = function(val) {"
133 + " model[setter](primitive ? new Number(val) : val);"
136 + "bindings[prop] = ko.computed(bnd);'"
139 Object ko = e.executeScript("ko");
140 bnd.call("bnd", ko, bindings, model, prop, strip(getter), strip(setter), primitive);
141 LOG.log(Level.FINE, "binding defined for {0}: {1}", new Object[]{prop, ((JSObject)bindings).getMember(prop)});
144 private static String strip(String mangled) {
145 if (mangled == null) {
148 int under = mangled.indexOf("__");
149 return mangled.substring(0, under);
151 @JavaScriptBody(args = { "bindings", "model", "prop", "sig" }, body =
152 "bindings[prop] = function(data, ev) { model[sig](data, ev); };"
154 private static void expose(
155 Object bindings, Object model, String prop, String sig
159 @JavaScriptBody(args = { "bindings" }, body = "ko.applyBindings(bindings);")
160 private static void applyBindings(Object bindings) {
161 JSObject ko = (JSObject) web().executeScript("ko");
162 ko.call("applyBindings", bindings);
165 private static WebEngine web() {
166 return (WebEngine) System.getProperties().get("webEngine");
170 private static void applyImpl(
171 String[] propsGettersAndSetters,
175 String[] methodsAndSignatures
176 ) throws IllegalStateException, SecurityException {
177 for (int i = 0; i < propsGettersAndSetters.length; i += 4) {
179 Method getter = modelClass.getMethod(propsGettersAndSetters[i + 3]);
180 bind(bindings, model, propsGettersAndSetters[i],
181 propsGettersAndSetters[i + 1],
182 propsGettersAndSetters[i + 2],
183 getter.getReturnType().isPrimitive(),
184 List.class.isAssignableFrom(getter.getReturnType()));
185 } catch (NoSuchMethodException ex) {
186 throw new IllegalStateException(ex.getMessage());
189 for (int i = 0; i < methodsAndSignatures.length; i += 2) {
191 bindings, model, methodsAndSignatures[i], methodsAndSignatures[i + 1]);