1.1 --- a/ko-fx/src/main/java/org/apidesign/html/kofx/Knockout.java Mon Dec 16 15:48:09 2013 +0100
1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
1.3 @@ -1,145 +0,0 @@
1.4 -/**
1.5 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
1.6 - *
1.7 - * Copyright 1997-2010 Oracle and/or its affiliates. All rights reserved.
1.8 - *
1.9 - * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
1.10 - * Other names may be trademarks of their respective owners.
1.11 - *
1.12 - * The contents of this file are subject to the terms of either the GNU
1.13 - * General Public License Version 2 only ("GPL") or the Common
1.14 - * Development and Distribution License("CDDL") (collectively, the
1.15 - * "License"). You may not use this file except in compliance with the
1.16 - * License. You can obtain a copy of the License at
1.17 - * http://www.netbeans.org/cddl-gplv2.html
1.18 - * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
1.19 - * specific language governing permissions and limitations under the
1.20 - * License. When distributing the software, include this License Header
1.21 - * Notice in each file and include the License file at
1.22 - * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this
1.23 - * particular file as subject to the "Classpath" exception as provided
1.24 - * by Oracle in the GPL Version 2 section of the License file that
1.25 - * accompanied this code. If applicable, add the following below the
1.26 - * License Header, with the fields enclosed by brackets [] replaced by
1.27 - * your own identifying information:
1.28 - * "Portions Copyrighted [year] [name of copyright owner]"
1.29 - *
1.30 - * Contributor(s):
1.31 - *
1.32 - * The Original Software is NetBeans. The Initial Developer of the Original
1.33 - * Software is Oracle. Portions Copyright 2013-2013 Oracle. All Rights Reserved.
1.34 - *
1.35 - * If you wish your version of this file to be governed by only the CDDL
1.36 - * or only the GPL Version 2, indicate your decision by adding
1.37 - * "[Contributor] elects to include this software in this distribution
1.38 - * under the [CDDL or GPL Version 2] license." If you do not indicate a
1.39 - * single choice of license, a recipient has the option to distribute
1.40 - * your version of this file under either the CDDL, the GPL Version 2 or
1.41 - * to extend the choice of license to its licensees as provided above.
1.42 - * However, if you add GPL Version 2 code and therefore, elected the GPL
1.43 - * Version 2 license, then the option applies only if the new code is
1.44 - * made subject to such option by the copyright holder.
1.45 - */
1.46 -package org.apidesign.html.kofx;
1.47 -
1.48 -import net.java.html.js.JavaScriptBody;
1.49 -import net.java.html.js.JavaScriptResource;
1.50 -import net.java.html.json.Model;
1.51 -import netscape.javascript.JSObject;
1.52 -import org.apidesign.html.json.spi.FunctionBinding;
1.53 -import org.apidesign.html.json.spi.PropertyBinding;
1.54 -
1.55 -/** This is an implementation package - just
1.56 - * include its JAR on classpath and use official {@link Context} API
1.57 - * to access the functionality.
1.58 - * <p>
1.59 - * Provides binding between {@link Model models} and knockout.js running
1.60 - * inside a JavaFX WebView.
1.61 - *
1.62 - * @author Jaroslav Tulach <jtulach@netbeans.org>
1.63 - */
1.64 -@JavaScriptResource("knockout-2.2.1.js")
1.65 -final class Knockout {
1.66 - static final JSObject KObject;
1.67 - static {
1.68 - Console.register();
1.69 - KObject = (JSObject) kObj();
1.70 - }
1.71 -
1.72 - static Object toArray(Object[] arr) {
1.73 - return KObject.call("array", arr);
1.74 - }
1.75 -
1.76 - @JavaScriptBody(args = { "model", "prop" }, body =
1.77 - "if (model) {\n"
1.78 - + " var koProp = model[prop];\n"
1.79 - + " if (koProp && koProp['valueHasMutated']) {\n"
1.80 - + " koProp['valueHasMutated']();\n"
1.81 - + " }\n"
1.82 - + "}\n"
1.83 - )
1.84 - public native static void valueHasMutated(JSObject model, String prop);
1.85 -
1.86 - @JavaScriptBody(args = { "bindings" }, body = "ko.applyBindings(bindings);")
1.87 - native static void applyBindings(Object bindings);
1.88 -
1.89 - @JavaScriptBody(args = {}, body =
1.90 - " var k = {};"
1.91 - + " k.array= function() {"
1.92 - + " return Array.prototype.slice.call(arguments);"
1.93 - + " };"
1.94 - + " return k;"
1.95 - )
1.96 - private static native Object kObj();
1.97 -
1.98 -
1.99 - @JavaScriptBody(
1.100 - javacall = true,
1.101 - args = {"model", "propNames", "propReadOnly", "propValues", "propArr", "funcNames", "funcArr"},
1.102 - body
1.103 - = "var ret = {};\n"
1.104 - + "ret['ko-fx.model'] = model;\n"
1.105 - + "function koComputed(name, readOnly, value, prop) {\n"
1.106 - + " function realGetter() {\n"
1.107 - + " try {"
1.108 - + " var v = prop.@org.apidesign.html.json.spi.PropertyBinding::getValue()();"
1.109 - + " return v;"
1.110 - + " } catch (e) {"
1.111 - + " alert(\"Cannot call getValue on \" + model + \" prop: \" + name + \" error: \" + e);"
1.112 - + " }"
1.113 - + " }\n"
1.114 - + " var activeGetter = function() { return value; };\n"
1.115 - + " var bnd = {"
1.116 - + " read: function() {"
1.117 - + " var r = activeGetter();"
1.118 - + " activeGetter = realGetter;"
1.119 - + " return r;"
1.120 - + " },"
1.121 - + " owner: ret\n"
1.122 - + " };\n"
1.123 - + " if (!readOnly) {\n"
1.124 - + " bnd.write = function(val) {\n"
1.125 - + " prop.@org.apidesign.html.json.spi.PropertyBinding::setValue(Ljava/lang/Object;)(val);\n"
1.126 - + " };"
1.127 - + " };"
1.128 - + " ret[name] = ko.computed(bnd);"
1.129 - + "}\n"
1.130 - + "for (var i = 0; i < propNames.length; i++) {\n"
1.131 - + " koComputed(propNames[i], propReadOnly[i], propValues[i], propArr[i]);\n"
1.132 - + "}\n"
1.133 - + "function koExpose(name, func) {\n"
1.134 - + " ret[name] = function(data, ev) {\n"
1.135 - + " func.@org.apidesign.html.json.spi.FunctionBinding::call(Ljava/lang/Object;Ljava/lang/Object;)(data, ev);\n"
1.136 - + " };\n"
1.137 - + "}\n"
1.138 - + "for (var i = 0; i < funcNames.length; i++) {\n"
1.139 - + " koExpose(funcNames[i], funcArr[i]);\n"
1.140 - + "}\n"
1.141 - + "return ret;\n"
1.142 - )
1.143 - static native JSObject wrapModel(
1.144 - Object model,
1.145 - String[] propNames, boolean[] propReadOnly, Object propValues, PropertyBinding[] propArr,
1.146 - String[] funcNames, FunctionBinding[] funcArr
1.147 - );
1.148 -}