1.1 --- a/ko/bck2brwsr/src/main/java/org/apidesign/bck2brwsr/ko2brwsr/Knockout.java Tue Feb 11 10:48:24 2014 +0100
1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
1.3 @@ -1,131 +0,0 @@
1.4 -/**
1.5 - * Back 2 Browser Bytecode Translator
1.6 - * Copyright (C) 2012 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.
1.16 - *
1.17 - * You should have received a copy of the GNU General Public License
1.18 - * along with this program. Look for COPYING file in the top folder.
1.19 - * If not, see http://opensource.org/licenses/GPL-2.0.
1.20 - */
1.21 -package org.apidesign.bck2brwsr.ko2brwsr;
1.22 -
1.23 -import java.lang.reflect.Method;
1.24 -import java.util.List;
1.25 -import org.apidesign.bck2brwsr.core.ExtraJavaScript;
1.26 -import org.apidesign.bck2brwsr.core.JavaScriptBody;
1.27 -
1.28 -/** Provides binding between models and bck2brwsr VM.
1.29 - *
1.30 - * @author Jaroslav Tulach <jtulach@netbeans.org>
1.31 - */
1.32 -@ExtraJavaScript(resource = "/org/apidesign/bck2brwsr/htmlpage/knockout-2.2.1.js")
1.33 -final class Knockout {
1.34 - /** used by tests */
1.35 - static Knockout next;
1.36 - private final Object model;
1.37 -
1.38 - Knockout(Object model) {
1.39 - this.model = model == null ? this : model;
1.40 - }
1.41 -
1.42 - public static <M> Knockout applyBindings(
1.43 - Object model, String[] propsGettersAndSetters,
1.44 - String[] methodsAndSignatures
1.45 - ) {
1.46 - applyImpl(propsGettersAndSetters, model.getClass(), model, model, methodsAndSignatures);
1.47 - return new Knockout(model);
1.48 - }
1.49 - public static <M> Knockout applyBindings(
1.50 - Class<M> modelClass, M model, String[] propsGettersAndSetters,
1.51 - String[] methodsAndSignatures
1.52 - ) {
1.53 - Knockout bindings = next;
1.54 - next = null;
1.55 - if (bindings == null) {
1.56 - bindings = new Knockout(null);
1.57 - }
1.58 - applyImpl(propsGettersAndSetters, modelClass, bindings, model, methodsAndSignatures);
1.59 - applyBindings(bindings);
1.60 - return bindings;
1.61 - }
1.62 -
1.63 - public void valueHasMutated(String prop) {
1.64 - valueHasMutated(model, prop);
1.65 - }
1.66 - @JavaScriptBody(args = { "self", "prop" }, body =
1.67 - "var p = self[prop]; if (p) p.valueHasMutated();"
1.68 - )
1.69 - public static void valueHasMutated(Object self, String prop) {
1.70 - }
1.71 -
1.72 -
1.73 - @JavaScriptBody(args = { "id", "ev" }, body = "ko.utils.triggerEvent(window.document.getElementById(id), ev.substring(2));")
1.74 - public static void triggerEvent(String id, String ev) {
1.75 - }
1.76 -
1.77 - @JavaScriptBody(args = { "bindings", "model", "prop", "getter", "setter", "primitive", "array" }, body =
1.78 - "var bnd = {\n"
1.79 - + " 'read': function() {\n"
1.80 - + " var v = model[getter]();\n"
1.81 - + " if (array) v = v.koArray(); else if (v !== null) v = v.valueOf();\n"
1.82 - + " return v;\n"
1.83 - + " },\n"
1.84 - + " 'owner': bindings\n"
1.85 - + "};\n"
1.86 - + "if (setter != null) {\n"
1.87 - + " bnd['write'] = function(val) {\n"
1.88 - + " var v = val === null ? null : val.valueOf();"
1.89 - + " model[setter](v);\n"
1.90 - + " };\n"
1.91 - + "}\n"
1.92 - + "bindings[prop] = ko['computed'](bnd);"
1.93 - )
1.94 - static void bind(
1.95 - Object bindings, Object model, String prop, String getter, String setter, boolean primitive, boolean array
1.96 - ) {
1.97 - }
1.98 -
1.99 - @JavaScriptBody(args = { "bindings", "model", "prop", "sig" }, body =
1.100 - "bindings[prop] = function(data, ev) { model[sig](data, ev); };"
1.101 - )
1.102 - static void expose(
1.103 - Object bindings, Object model, String prop, String sig
1.104 - ) {
1.105 - }
1.106 -
1.107 - @JavaScriptBody(args = { "bindings" }, body = "ko.applyBindings(bindings);")
1.108 - static void applyBindings(Object bindings) {}
1.109 -
1.110 - private static void applyImpl(
1.111 - String[] propsGettersAndSetters,
1.112 - Class<?> modelClass,
1.113 - Object bindings,
1.114 - Object model,
1.115 - String[] methodsAndSignatures
1.116 - ) throws IllegalStateException, SecurityException {
1.117 - for (int i = 0; i < propsGettersAndSetters.length; i += 4) {
1.118 - try {
1.119 - Method getter = modelClass.getMethod(propsGettersAndSetters[i + 3]);
1.120 - bind(bindings, model, propsGettersAndSetters[i],
1.121 - propsGettersAndSetters[i + 1],
1.122 - propsGettersAndSetters[i + 2],
1.123 - getter.getReturnType().isPrimitive(),
1.124 - List.class.isAssignableFrom(getter.getReturnType()));
1.125 - } catch (NoSuchMethodException ex) {
1.126 - throw new IllegalStateException(ex.getMessage());
1.127 - }
1.128 - }
1.129 - for (int i = 0; i < methodsAndSignatures.length; i += 2) {
1.130 - expose(
1.131 - bindings, model, methodsAndSignatures[i], methodsAndSignatures[i + 1]);
1.132 - }
1.133 - }
1.134 -}