Moving Context into its own separate module, renaming to BrwsrCtx and making it flexible
2 * HTML via Java(tm) Language Bindings
3 * Copyright (C) 2013 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. apidesign.org
13 * designates this particular file as subject to the
14 * "Classpath" exception as provided by apidesign.org
15 * in the License file that accompanied this code.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. Look for COPYING file in the top folder.
19 * If not, see http://wiki.apidesign.org/wiki/GPLwithClassPathException
21 package org.apidesign.html.ko2brwsr;
23 import java.lang.reflect.Method;
24 import java.util.List;
25 import org.apidesign.bck2brwsr.core.ExtraJavaScript;
26 import org.apidesign.bck2brwsr.core.JavaScriptBody;
28 /** Provides binding between models and bck2brwsr VM.
30 * @author Jaroslav Tulach <jtulach@netbeans.org>
32 @ExtraJavaScript(resource = "/org/apidesign/bck2brwsr/htmlpage/knockout-2.2.1.js")
33 final class Knockout {
36 private final Object model;
38 Knockout(Object model) {
39 this.model = model == null ? this : model;
42 public static <M> Knockout applyBindings(
43 Object model, String[] propsGettersAndSetters,
44 String[] methodsAndSignatures
46 applyImpl(propsGettersAndSetters, model.getClass(), model, model, methodsAndSignatures);
47 return new Knockout(model);
49 public static <M> Knockout applyBindings(
50 Class<M> modelClass, M model, String[] propsGettersAndSetters,
51 String[] methodsAndSignatures
53 Knockout bindings = next;
55 if (bindings == null) {
56 bindings = new Knockout(null);
58 applyImpl(propsGettersAndSetters, modelClass, bindings, model, methodsAndSignatures);
59 applyBindings(bindings);
63 public void valueHasMutated(String prop) {
64 valueHasMutated(model, prop);
66 @JavaScriptBody(args = { "self", "prop" }, body =
67 "var p = self[prop]; if (p) p.valueHasMutated();"
69 public static void valueHasMutated(Object self, String prop) {
73 @JavaScriptBody(args = { "id", "ev" }, body = "ko.utils.triggerEvent(window.document.getElementById(id), ev.substring(2));")
74 public static void triggerEvent(String id, String ev) {
77 @JavaScriptBody(args = { "bindings", "model", "prop", "getter", "setter", "primitive", "array" }, body =
79 + " 'read': function() {\n"
80 + " var v = model[getter]();\n"
81 + " if (array) v = v.koArray(); else if (v !== null) v = v.valueOf();\n"
84 + " 'owner': bindings\n"
86 + "if (setter != null) {\n"
87 + " bnd['write'] = function(val) {\n"
88 + " var v = val === null ? null : val.valueOf();"
89 + " model[setter](v);\n"
92 + "bindings[prop] = ko['computed'](bnd);"
95 Object bindings, Object model, String prop, String getter, String setter, boolean primitive, boolean array
99 @JavaScriptBody(args = { "bindings", "model", "prop", "sig" }, body =
100 "bindings[prop] = function(data, ev) { model[sig](data, ev); };"
103 Object bindings, Object model, String prop, String sig
107 @JavaScriptBody(args = { "bindings" }, body = "ko.applyBindings(bindings);")
108 static void applyBindings(Object bindings) {}
110 private static void applyImpl(
111 String[] propsGettersAndSetters,
115 String[] methodsAndSignatures
116 ) throws IllegalStateException, SecurityException {
117 for (int i = 0; i < propsGettersAndSetters.length; i += 4) {
119 Method getter = modelClass.getMethod(propsGettersAndSetters[i + 3]);
120 bind(bindings, model, propsGettersAndSetters[i],
121 propsGettersAndSetters[i + 1],
122 propsGettersAndSetters[i + 2],
123 getter.getReturnType().isPrimitive(),
124 List.class.isAssignableFrom(getter.getReturnType()));
125 } catch (NoSuchMethodException ex) {
126 throw new IllegalStateException(ex.getMessage());
129 for (int i = 0; i < methodsAndSignatures.length; i += 2) {
131 bindings, model, methodsAndSignatures[i], methodsAndSignatures[i + 1]);