ko-bck2brwsr/src/main/java/org/apidesign/html/ko2brwsr/Knockout.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Thu, 16 May 2013 13:40:26 +0200
changeset 1216 6eab20a4249f
parent 1194 3213724a4996
permissions -rw-r--r--
Verify boolena bindings
     1 /**
     2  * HTML via Java(tm) Language Bindings
     3  * Copyright (C) 2013 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     4  *
     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.
     8  *
     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.
    16  *
    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
    20  */
    21 package org.apidesign.html.ko2brwsr;
    22 
    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;
    27 
    28 /** Provides binding between models and bck2brwsr VM.
    29  *
    30  * @author Jaroslav Tulach <jtulach@netbeans.org>
    31  */
    32 @ExtraJavaScript(resource = "/org/apidesign/bck2brwsr/htmlpage/knockout-2.2.1.js")
    33 final class Knockout {
    34     /** used by tests */
    35     static Knockout next;
    36     private final Object model;
    37 
    38     Knockout(Object model) {
    39         this.model = model == null ? this : model;
    40     }
    41     
    42     public static <M> Knockout applyBindings(
    43         Object model, String[] propsGettersAndSetters,
    44         String[] methodsAndSignatures
    45     ) {
    46         applyImpl(propsGettersAndSetters, model.getClass(), model, model, methodsAndSignatures);
    47         return new Knockout(model);
    48     }
    49     public static <M> Knockout applyBindings(
    50         Class<M> modelClass, M model, String[] propsGettersAndSetters,
    51         String[] methodsAndSignatures
    52     ) {
    53         Knockout bindings = next;
    54         next = null;
    55         if (bindings == null) {
    56             bindings = new Knockout(null);
    57         }
    58         applyImpl(propsGettersAndSetters, modelClass, bindings, model, methodsAndSignatures);
    59         applyBindings(bindings);
    60         return bindings;
    61     }
    62 
    63     public void valueHasMutated(String prop) {
    64         valueHasMutated(model, prop);
    65     }
    66     @JavaScriptBody(args = { "self", "prop" }, body =
    67         "var p = self[prop]; if (p) p.valueHasMutated();"
    68     )
    69     public static void valueHasMutated(Object self, String prop) {
    70     }
    71     
    72 
    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) {
    75     }
    76     
    77     @JavaScriptBody(args = { "bindings", "model", "prop", "getter", "setter", "primitive", "array" }, body =
    78           "var bnd = {\n"
    79         + "  'read': function() {\n"
    80         + "    var v = model[getter]();\n"
    81         + "    if (array) v = v.koArray(); else if (v !== null) v = v.valueOf();\n"
    82         + "    return v;\n"
    83         + "  },\n"
    84         + "  'owner': bindings\n"
    85         + "};\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"
    90         + "  };\n"
    91         + "}\n"
    92         + "bindings[prop] = ko['computed'](bnd);"
    93     )
    94     static void bind(
    95         Object bindings, Object model, String prop, String getter, String setter, boolean primitive, boolean array
    96     ) {
    97     }
    98 
    99     @JavaScriptBody(args = { "bindings", "model", "prop", "sig" }, body = 
   100         "bindings[prop] = function(data, ev) { model[sig](data, ev); };"
   101     )
   102     static void expose(
   103         Object bindings, Object model, String prop, String sig
   104     ) {
   105     }
   106     
   107     @JavaScriptBody(args = { "bindings" }, body = "ko.applyBindings(bindings);")
   108     static void applyBindings(Object bindings) {}
   109     
   110     private static void applyImpl(
   111         String[] propsGettersAndSetters,
   112         Class<?> modelClass,
   113         Object bindings,
   114         Object model,
   115         String[] methodsAndSignatures
   116     ) throws IllegalStateException, SecurityException {
   117         for (int i = 0; i < propsGettersAndSetters.length; i += 4) {
   118             try {
   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());
   127             }
   128         }
   129         for (int i = 0; i < methodsAndSignatures.length; i += 2) {
   130             expose(
   131                 bindings, model, methodsAndSignatures[i], methodsAndSignatures[i + 1]);
   132         }
   133     }
   134 }