jaroslav@1189
|
1 |
/**
|
jaroslav@1189
|
2 |
* HTML via Java(tm) Language Bindings
|
jaroslav@1189
|
3 |
* Copyright (C) 2013 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
|
jaroslav@1189
|
4 |
*
|
jaroslav@1189
|
5 |
* This program is free software: you can redistribute it and/or modify
|
jaroslav@1189
|
6 |
* it under the terms of the GNU General Public License as published by
|
jaroslav@1189
|
7 |
* the Free Software Foundation, version 2 of the License.
|
jaroslav@1189
|
8 |
*
|
jaroslav@1189
|
9 |
* This program is distributed in the hope that it will be useful,
|
jaroslav@1189
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
jaroslav@1189
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
jaroslav@1189
|
12 |
* GNU General Public License for more details. apidesign.org
|
jaroslav@1189
|
13 |
* designates this particular file as subject to the
|
jaroslav@1189
|
14 |
* "Classpath" exception as provided by apidesign.org
|
jaroslav@1189
|
15 |
* in the License file that accompanied this code.
|
jaroslav@1189
|
16 |
*
|
jaroslav@1189
|
17 |
* You should have received a copy of the GNU General Public License
|
jaroslav@1189
|
18 |
* along with this program. Look for COPYING file in the top folder.
|
jaroslav@1189
|
19 |
* If not, see http://wiki.apidesign.org/wiki/GPLwithClassPathException
|
jaroslav@1189
|
20 |
*/
|
jaroslav@1189
|
21 |
package org.apidesign.html.ko2brwsr;
|
jaroslav@1189
|
22 |
|
jaroslav@1189
|
23 |
import java.lang.reflect.Method;
|
jaroslav@1189
|
24 |
import java.util.List;
|
jaroslav@1189
|
25 |
import org.apidesign.bck2brwsr.core.ExtraJavaScript;
|
jaroslav@1189
|
26 |
import org.apidesign.bck2brwsr.core.JavaScriptBody;
|
jaroslav@1189
|
27 |
|
jaroslav@1194
|
28 |
/** Provides binding between models and bck2brwsr VM.
|
jaroslav@1189
|
29 |
*
|
jaroslav@1189
|
30 |
* @author Jaroslav Tulach <jtulach@netbeans.org>
|
jaroslav@1189
|
31 |
*/
|
jaroslav@1189
|
32 |
@ExtraJavaScript(resource = "/org/apidesign/bck2brwsr/htmlpage/knockout-2.2.1.js")
|
jaroslav@1194
|
33 |
final class Knockout {
|
jaroslav@1189
|
34 |
/** used by tests */
|
jaroslav@1189
|
35 |
static Knockout next;
|
jaroslav@1189
|
36 |
private final Object model;
|
jaroslav@1189
|
37 |
|
jaroslav@1189
|
38 |
Knockout(Object model) {
|
jaroslav@1189
|
39 |
this.model = model == null ? this : model;
|
jaroslav@1189
|
40 |
}
|
jaroslav@1189
|
41 |
|
jaroslav@1189
|
42 |
public static <M> Knockout applyBindings(
|
jaroslav@1189
|
43 |
Object model, String[] propsGettersAndSetters,
|
jaroslav@1189
|
44 |
String[] methodsAndSignatures
|
jaroslav@1189
|
45 |
) {
|
jaroslav@1189
|
46 |
applyImpl(propsGettersAndSetters, model.getClass(), model, model, methodsAndSignatures);
|
jaroslav@1189
|
47 |
return new Knockout(model);
|
jaroslav@1189
|
48 |
}
|
jaroslav@1189
|
49 |
public static <M> Knockout applyBindings(
|
jaroslav@1189
|
50 |
Class<M> modelClass, M model, String[] propsGettersAndSetters,
|
jaroslav@1189
|
51 |
String[] methodsAndSignatures
|
jaroslav@1189
|
52 |
) {
|
jaroslav@1189
|
53 |
Knockout bindings = next;
|
jaroslav@1189
|
54 |
next = null;
|
jaroslav@1189
|
55 |
if (bindings == null) {
|
jaroslav@1189
|
56 |
bindings = new Knockout(null);
|
jaroslav@1189
|
57 |
}
|
jaroslav@1189
|
58 |
applyImpl(propsGettersAndSetters, modelClass, bindings, model, methodsAndSignatures);
|
jaroslav@1189
|
59 |
applyBindings(bindings);
|
jaroslav@1189
|
60 |
return bindings;
|
jaroslav@1189
|
61 |
}
|
jaroslav@1189
|
62 |
|
jaroslav@1189
|
63 |
public void valueHasMutated(String prop) {
|
jaroslav@1189
|
64 |
valueHasMutated(model, prop);
|
jaroslav@1189
|
65 |
}
|
jaroslav@1189
|
66 |
@JavaScriptBody(args = { "self", "prop" }, body =
|
jaroslav@1189
|
67 |
"var p = self[prop]; if (p) p.valueHasMutated();"
|
jaroslav@1189
|
68 |
)
|
jaroslav@1189
|
69 |
public static void valueHasMutated(Object self, String prop) {
|
jaroslav@1189
|
70 |
}
|
jaroslav@1189
|
71 |
|
jaroslav@1189
|
72 |
|
jaroslav@1189
|
73 |
@JavaScriptBody(args = { "id", "ev" }, body = "ko.utils.triggerEvent(window.document.getElementById(id), ev.substring(2));")
|
jaroslav@1189
|
74 |
public static void triggerEvent(String id, String ev) {
|
jaroslav@1189
|
75 |
}
|
jaroslav@1189
|
76 |
|
jaroslav@1189
|
77 |
@JavaScriptBody(args = { "bindings", "model", "prop", "getter", "setter", "primitive", "array" }, body =
|
jaroslav@1189
|
78 |
"var bnd = {\n"
|
jaroslav@1189
|
79 |
+ " 'read': function() {\n"
|
jaroslav@1189
|
80 |
+ " var v = model[getter]();\n"
|
jaroslav@1216
|
81 |
+ " if (array) v = v.koArray(); else if (v !== null) v = v.valueOf();\n"
|
jaroslav@1189
|
82 |
+ " return v;\n"
|
jaroslav@1189
|
83 |
+ " },\n"
|
jaroslav@1189
|
84 |
+ " 'owner': bindings\n"
|
jaroslav@1189
|
85 |
+ "};\n"
|
jaroslav@1189
|
86 |
+ "if (setter != null) {\n"
|
jaroslav@1189
|
87 |
+ " bnd['write'] = function(val) {\n"
|
jaroslav@1216
|
88 |
+ " var v = val === null ? null : val.valueOf();"
|
jaroslav@1216
|
89 |
+ " model[setter](v);\n"
|
jaroslav@1189
|
90 |
+ " };\n"
|
jaroslav@1189
|
91 |
+ "}\n"
|
jaroslav@1189
|
92 |
+ "bindings[prop] = ko['computed'](bnd);"
|
jaroslav@1189
|
93 |
)
|
jaroslav@1189
|
94 |
static void bind(
|
jaroslav@1189
|
95 |
Object bindings, Object model, String prop, String getter, String setter, boolean primitive, boolean array
|
jaroslav@1189
|
96 |
) {
|
jaroslav@1189
|
97 |
}
|
jaroslav@1189
|
98 |
|
jaroslav@1189
|
99 |
@JavaScriptBody(args = { "bindings", "model", "prop", "sig" }, body =
|
jaroslav@1189
|
100 |
"bindings[prop] = function(data, ev) { model[sig](data, ev); };"
|
jaroslav@1189
|
101 |
)
|
jaroslav@1189
|
102 |
static void expose(
|
jaroslav@1189
|
103 |
Object bindings, Object model, String prop, String sig
|
jaroslav@1189
|
104 |
) {
|
jaroslav@1189
|
105 |
}
|
jaroslav@1189
|
106 |
|
jaroslav@1189
|
107 |
@JavaScriptBody(args = { "bindings" }, body = "ko.applyBindings(bindings);")
|
jaroslav@1189
|
108 |
static void applyBindings(Object bindings) {}
|
jaroslav@1189
|
109 |
|
jaroslav@1189
|
110 |
private static void applyImpl(
|
jaroslav@1189
|
111 |
String[] propsGettersAndSetters,
|
jaroslav@1189
|
112 |
Class<?> modelClass,
|
jaroslav@1189
|
113 |
Object bindings,
|
jaroslav@1189
|
114 |
Object model,
|
jaroslav@1189
|
115 |
String[] methodsAndSignatures
|
jaroslav@1189
|
116 |
) throws IllegalStateException, SecurityException {
|
jaroslav@1189
|
117 |
for (int i = 0; i < propsGettersAndSetters.length; i += 4) {
|
jaroslav@1189
|
118 |
try {
|
jaroslav@1189
|
119 |
Method getter = modelClass.getMethod(propsGettersAndSetters[i + 3]);
|
jaroslav@1189
|
120 |
bind(bindings, model, propsGettersAndSetters[i],
|
jaroslav@1189
|
121 |
propsGettersAndSetters[i + 1],
|
jaroslav@1189
|
122 |
propsGettersAndSetters[i + 2],
|
jaroslav@1189
|
123 |
getter.getReturnType().isPrimitive(),
|
jaroslav@1189
|
124 |
List.class.isAssignableFrom(getter.getReturnType()));
|
jaroslav@1189
|
125 |
} catch (NoSuchMethodException ex) {
|
jaroslav@1189
|
126 |
throw new IllegalStateException(ex.getMessage());
|
jaroslav@1189
|
127 |
}
|
jaroslav@1189
|
128 |
}
|
jaroslav@1189
|
129 |
for (int i = 0; i < methodsAndSignatures.length; i += 2) {
|
jaroslav@1189
|
130 |
expose(
|
jaroslav@1189
|
131 |
bindings, model, methodsAndSignatures[i], methodsAndSignatures[i + 1]);
|
jaroslav@1189
|
132 |
}
|
jaroslav@1189
|
133 |
}
|
jaroslav@1189
|
134 |
}
|