jaroslav@37
|
1 |
/**
|
jaroslav@358
|
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
|
jaroslav@37
|
3 |
*
|
jaroslav@551
|
4 |
* Copyright 2013-2014 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@551
|
30 |
* Software is Oracle. Portions Copyright 2013-2014 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@442
|
43 |
package org.netbeans.html.ko4j;
|
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 org.apidesign.html.json.spi.FunctionBinding;
|
jaroslav@37
|
49 |
import org.apidesign.html.json.spi.PropertyBinding;
|
jaroslav@37
|
50 |
|
jaroslav@54
|
51 |
/** This is an implementation package - just
|
jaroslav@54
|
52 |
* include its JAR on classpath and use official {@link Context} API
|
jaroslav@54
|
53 |
* to access the functionality.
|
jaroslav@54
|
54 |
* <p>
|
jaroslav@54
|
55 |
* Provides binding between {@link Model models} and knockout.js running
|
jaroslav@54
|
56 |
* inside a JavaFX WebView.
|
jaroslav@37
|
57 |
*
|
jaroslav@37
|
58 |
* @author Jaroslav Tulach <jtulach@netbeans.org>
|
jaroslav@37
|
59 |
*/
|
jaroslav@163
|
60 |
@JavaScriptResource("knockout-2.2.1.js")
|
jaroslav@276
|
61 |
final class Knockout {
|
jaroslav@276
|
62 |
static {
|
jaroslav@430
|
63 |
loadKnockout();
|
jaroslav@37
|
64 |
}
|
jaroslav@37
|
65 |
|
jaroslav@430
|
66 |
@JavaScriptBody(args = { }, body = "")
|
jaroslav@430
|
67 |
private static native void loadKnockout();
|
jaroslav@430
|
68 |
|
jaroslav@276
|
69 |
@JavaScriptBody(args = { "model", "prop" }, body =
|
jaroslav@276
|
70 |
"if (model) {\n"
|
jaroslav@276
|
71 |
+ " var koProp = model[prop];\n"
|
jaroslav@276
|
72 |
+ " if (koProp && koProp['valueHasMutated']) {\n"
|
jaroslav@276
|
73 |
+ " koProp['valueHasMutated']();\n"
|
jaroslav@276
|
74 |
+ " }\n"
|
jaroslav@276
|
75 |
+ "}\n"
|
jaroslav@276
|
76 |
)
|
jaroslav@430
|
77 |
public native static void valueHasMutated(Object model, String prop);
|
jaroslav@37
|
78 |
|
jaroslav@437
|
79 |
@JavaScriptBody(args = { "bindings" }, body = "ko.applyBindings(bindings);\n")
|
jaroslav@276
|
80 |
native static void applyBindings(Object bindings);
|
jaroslav@276
|
81 |
|
jaroslav@276
|
82 |
@JavaScriptBody(
|
jaroslav@276
|
83 |
javacall = true,
|
jaroslav@279
|
84 |
args = {"model", "propNames", "propReadOnly", "propValues", "propArr", "funcNames", "funcArr"},
|
jaroslav@276
|
85 |
body
|
jaroslav@276
|
86 |
= "var ret = {};\n"
|
jaroslav@276
|
87 |
+ "ret['ko-fx.model'] = model;\n"
|
jaroslav@279
|
88 |
+ "function koComputed(name, readOnly, value, prop) {\n"
|
jaroslav@279
|
89 |
+ " function realGetter() {\n"
|
jaroslav@437
|
90 |
+ " try {\n"
|
jaroslav@437
|
91 |
+ " var v = prop.@org.apidesign.html.json.spi.PropertyBinding::getValue()();\n"
|
jaroslav@437
|
92 |
+ " return v;\n"
|
jaroslav@437
|
93 |
+ " } catch (e) {\n"
|
jaroslav@437
|
94 |
+ " alert(\"Cannot call getValue on \" + model + \" prop: \" + name + \" error: \" + e);\n"
|
jaroslav@437
|
95 |
+ " }\n"
|
jaroslav@279
|
96 |
+ " }\n"
|
jaroslav@279
|
97 |
+ " var activeGetter = function() { return value; };\n"
|
jaroslav@437
|
98 |
+ " var bnd = {\n"
|
jaroslav@437
|
99 |
+ " read: function() {\n"
|
jaroslav@437
|
100 |
+ " var r = activeGetter();\n"
|
jaroslav@437
|
101 |
+ " activeGetter = realGetter;\n"
|
jaroslav@438
|
102 |
+ " return r == null ? null : r.valueOf();\n"
|
jaroslav@437
|
103 |
+ " },\n"
|
jaroslav@276
|
104 |
+ " owner: ret\n"
|
jaroslav@276
|
105 |
+ " };\n"
|
jaroslav@276
|
106 |
+ " if (!readOnly) {\n"
|
jaroslav@276
|
107 |
+ " bnd.write = function(val) {\n"
|
jaroslav@276
|
108 |
+ " prop.@org.apidesign.html.json.spi.PropertyBinding::setValue(Ljava/lang/Object;)(val);\n"
|
jaroslav@437
|
109 |
+ " };\n"
|
jaroslav@437
|
110 |
+ " };\n"
|
jaroslav@437
|
111 |
+ " ret[name] = ko.computed(bnd);\n"
|
jaroslav@276
|
112 |
+ "}\n"
|
jaroslav@276
|
113 |
+ "for (var i = 0; i < propNames.length; i++) {\n"
|
jaroslav@279
|
114 |
+ " koComputed(propNames[i], propReadOnly[i], propValues[i], propArr[i]);\n"
|
jaroslav@276
|
115 |
+ "}\n"
|
jaroslav@276
|
116 |
+ "function koExpose(name, func) {\n"
|
jaroslav@276
|
117 |
+ " ret[name] = function(data, ev) {\n"
|
jaroslav@276
|
118 |
+ " func.@org.apidesign.html.json.spi.FunctionBinding::call(Ljava/lang/Object;Ljava/lang/Object;)(data, ev);\n"
|
jaroslav@276
|
119 |
+ " };\n"
|
jaroslav@276
|
120 |
+ "}\n"
|
jaroslav@276
|
121 |
+ "for (var i = 0; i < funcNames.length; i++) {\n"
|
jaroslav@276
|
122 |
+ " koExpose(funcNames[i], funcArr[i]);\n"
|
jaroslav@276
|
123 |
+ "}\n"
|
jaroslav@276
|
124 |
+ "return ret;\n"
|
jaroslav@276
|
125 |
)
|
jaroslav@430
|
126 |
static native Object wrapModel(
|
jaroslav@276
|
127 |
Object model,
|
jaroslav@279
|
128 |
String[] propNames, boolean[] propReadOnly, Object propValues, PropertyBinding[] propArr,
|
jaroslav@275
|
129 |
String[] funcNames, FunctionBinding[] funcArr
|
jaroslav@276
|
130 |
);
|
jaroslav@430
|
131 |
|
jaroslav@430
|
132 |
@JavaScriptBody(args = { "o" }, body = "return o['ko-fx.model'] ? o['ko-fx.model'] : o;")
|
jaroslav@430
|
133 |
static native Object toModel(Object wrapper);
|
jaroslav@37
|
134 |
}
|