#258088: Non-mutable values aren't wrapped into ko.observable, but rather stored as primitive values
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4 * Copyright 2013-2014 Oracle and/or its affiliates. All rights reserved.
6 * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
7 * Other names may be trademarks of their respective owners.
9 * The contents of this file are subject to the terms of either the GNU
10 * General Public License Version 2 only ("GPL") or the Common
11 * Development and Distribution License("CDDL") (collectively, the
12 * "License"). You may not use this file except in compliance with the
13 * License. You can obtain a copy of the License at
14 * http://www.netbeans.org/cddl-gplv2.html
15 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
16 * specific language governing permissions and limitations under the
17 * License. When distributing the software, include this License Header
18 * Notice in each file and include the License file at
19 * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this
20 * particular file as subject to the "Classpath" exception as provided
21 * by Oracle in the GPL Version 2 section of the License file that
22 * accompanied this code. If applicable, add the following below the
23 * License Header, with the fields enclosed by brackets [] replaced by
24 * your own identifying information:
25 * "Portions Copyrighted [year] [name of copyright owner]"
29 * The Original Software is NetBeans. The Initial Developer of the Original
30 * Software is Oracle. Portions Copyright 2013-2014 Oracle. All Rights Reserved.
32 * If you wish your version of this file to be governed by only the CDDL
33 * or only the GPL Version 2, indicate your decision by adding
34 * "[Contributor] elects to include this software in this distribution
35 * under the [CDDL or GPL Version 2] license." If you do not indicate a
36 * single choice of license, a recipient has the option to distribute
37 * your version of this file under either the CDDL, the GPL Version 2 or
38 * to extend the choice of license to its licensees as provided above.
39 * However, if you add GPL Version 2 code and therefore, elected the GPL
40 * Version 2 license, then the option applies only if the new code is
41 * made subject to such option by the copyright holder.
43 package org.netbeans.html.ko4j;
45 import java.lang.ref.ReferenceQueue;
46 import java.lang.ref.WeakReference;
47 import java.util.Collections;
48 import java.util.HashSet;
50 import net.java.html.js.JavaScriptBody;
51 import net.java.html.js.JavaScriptResource;
52 import net.java.html.json.Model;
53 import org.netbeans.html.json.spi.FunctionBinding;
54 import org.netbeans.html.json.spi.PropertyBinding;
56 /** This is an implementation package - just
57 * include its JAR on classpath and use official {@link Context} API
58 * to access the functionality.
60 * Provides binding between {@link Model models} and knockout.js running
61 * inside a JavaFX WebView.
63 * @author Jaroslav Tulach
65 @JavaScriptResource("knockout-3.4.0.js")
66 final class Knockout extends WeakReference<Object> {
67 private static final ReferenceQueue<Object> QUEUE = new ReferenceQueue();
68 private static final Set<Knockout> active = Collections.synchronizedSet(new HashSet<Knockout>());
70 @JavaScriptBody(args = {"object", "property"}, body =
72 "if (property === null) ret = object;\n" +
73 "else if (object === null) ret = null;\n" +
74 "else ret = object[property];\n" +
75 "return ret ? ko.utils.unwrapObservable(ret) : null;"
77 static Object getProperty(Object object, String property) {
81 private PropertyBinding[] props;
82 private FunctionBinding[] funcs;
84 private Object strong;
86 public Knockout(Object model, Object js, PropertyBinding[] props, FunctionBinding[] funcs) {
89 this.props = new PropertyBinding[props.length];
90 for (int i = 0; i < props.length; i++) {
91 this.props[i] = props[i].weak();
93 this.funcs = new FunctionBinding[funcs.length];
94 for (int i = 0; i < funcs.length; i++) {
95 this.funcs[i] = funcs[i].weak();
100 static void cleanUp() {
102 Knockout ko = (Knockout)QUEUE.poll();
118 final Object getValue(int index) {
119 return props[index].getValue();
122 final void setValue(int index, Object v) {
123 if (v instanceof Knockout) {
124 v = ((Knockout)v).get();
126 props[index].setValue(v);
129 final void call(int index, Object data, Object ev) {
130 funcs[index].call(data, ev);
133 @JavaScriptBody(args = { "model", "prop", "oldValue", "newValue" },
137 + " var koProp = model[prop];\n"
138 + " if (koProp && koProp['valueHasMutated']) {\n"
139 + " if ((oldValue !== null || newValue !== null)) {\n"
140 + " koProp['valueHasMutated'](newValue);\n"
141 + " } else if (koProp['valueHasMutated']) {\n"
142 + " koProp['valueHasMutated']();\n"
147 public native static void valueHasMutated(
148 Object model, String prop, Object oldValue, Object newValue
151 @JavaScriptBody(args = { "id", "bindings" }, body =
152 "var d = window['document'];\n" +
153 "var e = id ? d['getElementById'](id) : d['body'];\n" +
154 "ko['cleanNode'](e);\n" +
155 "ko['applyBindings'](bindings, e);\n" +
156 "return bindings['ko4j'];\n"
158 native static Object applyBindings(String id, Object bindings);
160 @JavaScriptBody(args = { "cnt" }, body =
161 "var arr = new Array(cnt);\n" +
162 "for (var i = 0; i < cnt; i++) arr[i] = new Object();\n" +
165 native static Object[] allocJS(int cnt);
171 args = { "ret", "copyFrom", "propNames", "propReadOnly", "propConstant", "propValues", "funcNames" },
173 "Object.defineProperty(ret, 'ko4j', { value : this });\n"
174 + "function koComputed(index, name, readOnly, value) {\n"
175 + " var orig = copyFrom ? copyFrom[name] : null;\n"
176 + " if (!ko['isObservable'](orig)) {\n"
178 + " var trigger = ko['observable']()['extend']({'notify':'always'});\n"
180 + " var trigger = orig;\n"
182 + " function realGetter() {\n"
183 + " var self = ret['ko4j'];\n"
185 + " var v = self ? self.@org.netbeans.html.ko4j.Knockout::getValue(I)(index) : null;\n"
188 + " alert(\"Cannot call getValue on \" + self + \" prop: \" + name + \" error: \" + e);\n"
191 + " var activeGetter = orig ? orig : function() { return value; };\n"
193 + " 'read': function() {\n"
196 + " var r = orig();\n"
198 + " var r = activeGetter();\n"
199 + " activeGetter = realGetter;\n"
201 + " if (r) try { var br = r.valueOf(); } catch (err) {}\n"
202 + " return br === undefined ? r: br;\n"
206 + " if (!readOnly) {\n"
207 + " function write(val) {\n"
208 + " if (orig) orig(val);\n"
209 + " var self = ret['ko4j'];\n"
210 + " if (!self) return;\n"
211 + " var model = val ? val['ko4j'] : null;\n"
212 + " self.@org.netbeans.html.ko4j.Knockout::setValue(ILjava/lang/Object;)(index, model ? model : val);\n"
214 + " bnd['write'] = write;\n"
216 + " write(orig());\n"
217 + " orig.subscribe(write);\n"
220 + " var cmpt = ko['computed'](bnd);\n"
221 + " cmpt['valueHasMutated'] = function(val) {\n"
222 + " if (arguments.length === 1) activeGetter = function() { return val; };\n"
225 + " ret[name] = cmpt;\n"
227 + "for (var i = 0; i < propNames.length; i++) {\n"
228 + " if (propConstant[i]) {\n"
229 + " ret[propNames[i]] = propValues[i];\n"
231 + " koComputed(i, propNames[i], propReadOnly[i], propValues[i]);\n"
234 + "function koExpose(index, name) {\n"
235 + " ret[name] = function(data, ev) {\n"
236 + " var self = ret['ko4j'];\n"
237 + " if (!self) return;\n"
238 + " self.@org.netbeans.html.ko4j.Knockout::call(ILjava/lang/Object;Ljava/lang/Object;)(index, data, ev);\n"
241 + "for (var i = 0; i < funcNames.length; i++) {\n"
242 + " koExpose(i, funcNames[i]);\n"
245 native void wrapModel(
246 Object ret, Object copyFrom,
247 String[] propNames, Boolean[] propReadOnly, Boolean[] propConstant,
252 @JavaScriptBody(args = { "js" }, wait4js = false, body =
253 "delete js['ko4j'];\n" +
254 "for (var p in js) {\n" +
259 private static native void clean(Object js);
261 @JavaScriptBody(args = { "o" }, body = "return o['ko4j'] ? o['ko4j'] : o;")
262 private static native Object toModelImpl(Object wrapper);
263 static Object toModel(Object wrapper) {
264 Object o = toModelImpl(wrapper);
265 if (o instanceof Knockout) {
266 return ((Knockout)o).get();