ko-fx/src/main/java/org/netbeans/html/kofx/Knockout.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Mon, 16 Dec 2013 16:59:43 +0100
branchnetbeans
changeset 362 92fb71afdc0e
parent 358 ko-fx/src/main/java/org/apidesign/html/kofx/Knockout.java@80702021b851
child 365 5c93ad8c7a15
permissions -rw-r--r--
Moving implementation classes into org.netbeans.html namespace
jaroslav@37
     1
/**
jaroslav@358
     2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
jaroslav@37
     3
 *
jaroslav@358
     4
 * Copyright 1997-2010 Oracle and/or its affiliates. All rights reserved.
jaroslav@37
     5
 *
jaroslav@358
     6
 * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
jaroslav@358
     7
 * Other names may be trademarks of their respective owners.
jaroslav@37
     8
 *
jaroslav@358
     9
 * The contents of this file are subject to the terms of either the GNU
jaroslav@358
    10
 * General Public License Version 2 only ("GPL") or the Common
jaroslav@358
    11
 * Development and Distribution License("CDDL") (collectively, the
jaroslav@358
    12
 * "License"). You may not use this file except in compliance with the
jaroslav@358
    13
 * License. You can obtain a copy of the License at
jaroslav@358
    14
 * http://www.netbeans.org/cddl-gplv2.html
jaroslav@358
    15
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
jaroslav@358
    16
 * specific language governing permissions and limitations under the
jaroslav@358
    17
 * License.  When distributing the software, include this License Header
jaroslav@358
    18
 * Notice in each file and include the License file at
jaroslav@358
    19
 * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
jaroslav@358
    20
 * particular file as subject to the "Classpath" exception as provided
jaroslav@358
    21
 * by Oracle in the GPL Version 2 section of the License file that
jaroslav@358
    22
 * accompanied this code. If applicable, add the following below the
jaroslav@358
    23
 * License Header, with the fields enclosed by brackets [] replaced by
jaroslav@358
    24
 * your own identifying information:
jaroslav@358
    25
 * "Portions Copyrighted [year] [name of copyright owner]"
jaroslav@358
    26
 *
jaroslav@358
    27
 * Contributor(s):
jaroslav@358
    28
 *
jaroslav@358
    29
 * The Original Software is NetBeans. The Initial Developer of the Original
jaroslav@358
    30
 * Software is Oracle. Portions Copyright 2013-2013 Oracle. All Rights Reserved.
jaroslav@358
    31
 *
jaroslav@358
    32
 * If you wish your version of this file to be governed by only the CDDL
jaroslav@358
    33
 * or only the GPL Version 2, indicate your decision by adding
jaroslav@358
    34
 * "[Contributor] elects to include this software in this distribution
jaroslav@358
    35
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
jaroslav@358
    36
 * single choice of license, a recipient has the option to distribute
jaroslav@358
    37
 * your version of this file under either the CDDL, the GPL Version 2 or
jaroslav@358
    38
 * to extend the choice of license to its licensees as provided above.
jaroslav@358
    39
 * However, if you add GPL Version 2 code and therefore, elected the GPL
jaroslav@358
    40
 * Version 2 license, then the option applies only if the new code is
jaroslav@358
    41
 * made subject to such option by the copyright holder.
jaroslav@37
    42
 */
jaroslav@362
    43
package org.netbeans.html.kofx;
jaroslav@37
    44
jaroslav@131
    45
import net.java.html.js.JavaScriptBody;
jaroslav@163
    46
import net.java.html.js.JavaScriptResource;
jaroslav@37
    47
import net.java.html.json.Model;
jaroslav@37
    48
import netscape.javascript.JSObject;
jaroslav@37
    49
import org.apidesign.html.json.spi.FunctionBinding;
jaroslav@37
    50
import org.apidesign.html.json.spi.PropertyBinding;
jaroslav@37
    51
jaroslav@54
    52
/** This is an implementation package - just
jaroslav@54
    53
 * include its JAR on classpath and use official {@link Context} API
jaroslav@54
    54
 * to access the functionality.
jaroslav@54
    55
 * <p>
jaroslav@54
    56
 * Provides binding between {@link Model models} and knockout.js running
jaroslav@54
    57
 * inside a JavaFX WebView. 
jaroslav@37
    58
 *
jaroslav@37
    59
 * @author Jaroslav Tulach <jtulach@netbeans.org>
jaroslav@37
    60
 */
jaroslav@163
    61
@JavaScriptResource("knockout-2.2.1.js")
jaroslav@276
    62
final class Knockout {
jaroslav@276
    63
    static final JSObject KObject;
jaroslav@276
    64
    static {
jaroslav@276
    65
        Console.register();
jaroslav@276
    66
        KObject = (JSObject) kObj();
jaroslav@37
    67
    }
jaroslav@37
    68
jaroslav@37
    69
    static Object toArray(Object[] arr) {
jaroslav@276
    70
        return KObject.call("array", arr);
jaroslav@37
    71
    }
jaroslav@37
    72
    
jaroslav@276
    73
    @JavaScriptBody(args = { "model", "prop" }, body =
jaroslav@276
    74
          "if (model) {\n"
jaroslav@276
    75
        + "  var koProp = model[prop];\n"
jaroslav@276
    76
        + "  if (koProp && koProp['valueHasMutated']) {\n"
jaroslav@276
    77
        + "    koProp['valueHasMutated']();\n"
jaroslav@276
    78
        + "  }\n"
jaroslav@276
    79
        + "}\n"
jaroslav@276
    80
    )
jaroslav@276
    81
    public native static void valueHasMutated(JSObject model, String prop);
jaroslav@37
    82
jaroslav@276
    83
    @JavaScriptBody(args = { "bindings" }, body = "ko.applyBindings(bindings);")
jaroslav@276
    84
    native static void applyBindings(Object bindings);
jaroslav@276
    85
    
jaroslav@276
    86
    @JavaScriptBody(args = {}, body =
jaroslav@276
    87
              "  var k = {};"
jaroslav@276
    88
            + "  k.array= function() {"
jaroslav@276
    89
            + "    return Array.prototype.slice.call(arguments);"
jaroslav@276
    90
            + "  };"
jaroslav@276
    91
            + "  return k;"
jaroslav@276
    92
    )
jaroslav@276
    93
    private static native Object kObj();
jaroslav@276
    94
        
jaroslav@276
    95
        
jaroslav@276
    96
    @JavaScriptBody(
jaroslav@276
    97
        javacall = true,
jaroslav@279
    98
        args = {"model", "propNames", "propReadOnly", "propValues", "propArr", "funcNames", "funcArr"},
jaroslav@276
    99
        body
jaroslav@276
   100
        = "var ret = {};\n"
jaroslav@276
   101
        + "ret['ko-fx.model'] = model;\n"
jaroslav@279
   102
        + "function koComputed(name, readOnly, value, prop) {\n"
jaroslav@279
   103
        + "  function realGetter() {\n"
jaroslav@279
   104
        + "    try {"
jaroslav@279
   105
        + "      var v = prop.@org.apidesign.html.json.spi.PropertyBinding::getValue()();"
jaroslav@279
   106
        + "      return v;"
jaroslav@279
   107
        + "    } catch (e) {"
jaroslav@279
   108
        + "      alert(\"Cannot call getValue on \" + model + \" prop: \" + name + \" error: \" + e);"
jaroslav@279
   109
        + "    }"
jaroslav@279
   110
        + "  }\n"
jaroslav@279
   111
        + "  var activeGetter = function() { return value; };\n"
jaroslav@276
   112
        + "  var bnd = {"
jaroslav@276
   113
        + "    read: function() {"
jaroslav@279
   114
        + "      var r = activeGetter();"
jaroslav@279
   115
        + "      activeGetter = realGetter;"
jaroslav@279
   116
        + "      return r;"
jaroslav@276
   117
        + "    },"
jaroslav@276
   118
        + "    owner: ret\n"
jaroslav@276
   119
        + "  };\n"
jaroslav@276
   120
        + "  if (!readOnly) {\n"
jaroslav@276
   121
        + "    bnd.write = function(val) {\n"
jaroslav@276
   122
        + "      prop.@org.apidesign.html.json.spi.PropertyBinding::setValue(Ljava/lang/Object;)(val);\n"
jaroslav@276
   123
        + "    };"
jaroslav@276
   124
        + "  };"
jaroslav@276
   125
        + "  ret[name] = ko.computed(bnd);"
jaroslav@276
   126
        + "}\n"
jaroslav@276
   127
        + "for (var i = 0; i < propNames.length; i++) {\n"
jaroslav@279
   128
        + "  koComputed(propNames[i], propReadOnly[i], propValues[i], propArr[i]);\n"
jaroslav@276
   129
        + "}\n"
jaroslav@276
   130
        + "function koExpose(name, func) {\n"
jaroslav@276
   131
        + "  ret[name] = function(data, ev) {\n"
jaroslav@276
   132
        + "    func.@org.apidesign.html.json.spi.FunctionBinding::call(Ljava/lang/Object;Ljava/lang/Object;)(data, ev);\n"
jaroslav@276
   133
        + "  };\n"
jaroslav@276
   134
        + "}\n"
jaroslav@276
   135
        + "for (var i = 0; i < funcNames.length; i++) {\n"
jaroslav@276
   136
        + "  koExpose(funcNames[i], funcArr[i]);\n"
jaroslav@276
   137
        + "}\n"
jaroslav@276
   138
        + "return ret;\n"
jaroslav@276
   139
        )
jaroslav@276
   140
    static native JSObject wrapModel(
jaroslav@276
   141
        Object model,
jaroslav@279
   142
        String[] propNames, boolean[] propReadOnly, Object propValues, PropertyBinding[] propArr,
jaroslav@275
   143
        String[] funcNames, FunctionBinding[] funcArr
jaroslav@276
   144
    );
jaroslav@37
   145
}