1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/ko/bck2brwsr/src/main/java/org/apidesign/html/ko2brwsr/Knockout.java Mon Jun 24 17:50:44 2013 +0200
1.3 @@ -0,0 +1,134 @@
1.4 +/**
1.5 + * HTML via Java(tm) Language Bindings
1.6 + * Copyright (C) 2013 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
1.7 + *
1.8 + * This program is free software: you can redistribute it and/or modify
1.9 + * it under the terms of the GNU General Public License as published by
1.10 + * the Free Software Foundation, version 2 of the License.
1.11 + *
1.12 + * This program is distributed in the hope that it will be useful,
1.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1.15 + * GNU General Public License for more details. apidesign.org
1.16 + * designates this particular file as subject to the
1.17 + * "Classpath" exception as provided by apidesign.org
1.18 + * in the License file that accompanied this code.
1.19 + *
1.20 + * You should have received a copy of the GNU General Public License
1.21 + * along with this program. Look for COPYING file in the top folder.
1.22 + * If not, see http://wiki.apidesign.org/wiki/GPLwithClassPathException
1.23 + */
1.24 +package org.apidesign.html.ko2brwsr;
1.25 +
1.26 +import java.lang.reflect.Method;
1.27 +import java.util.List;
1.28 +import org.apidesign.bck2brwsr.core.ExtraJavaScript;
1.29 +import org.apidesign.bck2brwsr.core.JavaScriptBody;
1.30 +
1.31 +/** Provides binding between models and bck2brwsr VM.
1.32 + *
1.33 + * @author Jaroslav Tulach <jtulach@netbeans.org>
1.34 + */
1.35 +@ExtraJavaScript(resource = "/org/apidesign/bck2brwsr/htmlpage/knockout-2.2.1.js")
1.36 +final class Knockout {
1.37 + /** used by tests */
1.38 + static Knockout next;
1.39 + private final Object model;
1.40 +
1.41 + Knockout(Object model) {
1.42 + this.model = model == null ? this : model;
1.43 + }
1.44 +
1.45 + public static <M> Knockout applyBindings(
1.46 + Object model, String[] propsGettersAndSetters,
1.47 + String[] methodsAndSignatures
1.48 + ) {
1.49 + applyImpl(propsGettersAndSetters, model.getClass(), model, model, methodsAndSignatures);
1.50 + return new Knockout(model);
1.51 + }
1.52 + public static <M> Knockout applyBindings(
1.53 + Class<M> modelClass, M model, String[] propsGettersAndSetters,
1.54 + String[] methodsAndSignatures
1.55 + ) {
1.56 + Knockout bindings = next;
1.57 + next = null;
1.58 + if (bindings == null) {
1.59 + bindings = new Knockout(null);
1.60 + }
1.61 + applyImpl(propsGettersAndSetters, modelClass, bindings, model, methodsAndSignatures);
1.62 + applyBindings(bindings);
1.63 + return bindings;
1.64 + }
1.65 +
1.66 + public void valueHasMutated(String prop) {
1.67 + valueHasMutated(model, prop);
1.68 + }
1.69 + @JavaScriptBody(args = { "self", "prop" }, body =
1.70 + "var p = self[prop]; if (p) p.valueHasMutated();"
1.71 + )
1.72 + public static void valueHasMutated(Object self, String prop) {
1.73 + }
1.74 +
1.75 +
1.76 + @JavaScriptBody(args = { "id", "ev" }, body = "ko.utils.triggerEvent(window.document.getElementById(id), ev.substring(2));")
1.77 + public static void triggerEvent(String id, String ev) {
1.78 + }
1.79 +
1.80 + @JavaScriptBody(args = { "bindings", "model", "prop", "getter", "setter", "primitive", "array" }, body =
1.81 + "var bnd = {\n"
1.82 + + " 'read': function() {\n"
1.83 + + " var v = model[getter]();\n"
1.84 + + " if (array) v = v.koArray(); else if (v !== null) v = v.valueOf();\n"
1.85 + + " return v;\n"
1.86 + + " },\n"
1.87 + + " 'owner': bindings\n"
1.88 + + "};\n"
1.89 + + "if (setter != null) {\n"
1.90 + + " bnd['write'] = function(val) {\n"
1.91 + + " var v = val === null ? null : val.valueOf();"
1.92 + + " model[setter](v);\n"
1.93 + + " };\n"
1.94 + + "}\n"
1.95 + + "bindings[prop] = ko['computed'](bnd);"
1.96 + )
1.97 + static void bind(
1.98 + Object bindings, Object model, String prop, String getter, String setter, boolean primitive, boolean array
1.99 + ) {
1.100 + }
1.101 +
1.102 + @JavaScriptBody(args = { "bindings", "model", "prop", "sig" }, body =
1.103 + "bindings[prop] = function(data, ev) { model[sig](data, ev); };"
1.104 + )
1.105 + static void expose(
1.106 + Object bindings, Object model, String prop, String sig
1.107 + ) {
1.108 + }
1.109 +
1.110 + @JavaScriptBody(args = { "bindings" }, body = "ko.applyBindings(bindings);")
1.111 + static void applyBindings(Object bindings) {}
1.112 +
1.113 + private static void applyImpl(
1.114 + String[] propsGettersAndSetters,
1.115 + Class<?> modelClass,
1.116 + Object bindings,
1.117 + Object model,
1.118 + String[] methodsAndSignatures
1.119 + ) throws IllegalStateException, SecurityException {
1.120 + for (int i = 0; i < propsGettersAndSetters.length; i += 4) {
1.121 + try {
1.122 + Method getter = modelClass.getMethod(propsGettersAndSetters[i + 3]);
1.123 + bind(bindings, model, propsGettersAndSetters[i],
1.124 + propsGettersAndSetters[i + 1],
1.125 + propsGettersAndSetters[i + 2],
1.126 + getter.getReturnType().isPrimitive(),
1.127 + List.class.isAssignableFrom(getter.getReturnType()));
1.128 + } catch (NoSuchMethodException ex) {
1.129 + throw new IllegalStateException(ex.getMessage());
1.130 + }
1.131 + }
1.132 + for (int i = 0; i < methodsAndSignatures.length; i += 2) {
1.133 + expose(
1.134 + bindings, model, methodsAndSignatures[i], methodsAndSignatures[i + 1]);
1.135 + }
1.136 + }
1.137 +}