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.2.0.debug.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 private PropertyBinding[] props;
71 private FunctionBinding[] funcs;
73 private Object strong;
75 public Knockout(Object model, Object js, PropertyBinding[] props, FunctionBinding[] funcs) {
78 this.props = new PropertyBinding[props.length];
79 for (int i = 0; i < props.length; i++) {
80 this.props[i] = props[i].weak();
82 this.funcs = new FunctionBinding[funcs.length];
83 for (int i = 0; i < funcs.length; i++) {
84 this.funcs[i] = funcs[i].weak();
89 static void cleanUp() {
91 Knockout ko = (Knockout)QUEUE.poll();
107 final Object getValue(int index) {
108 return props[index].getValue();
111 final void setValue(int index, Object v) {
112 if (v instanceof Knockout) {
113 v = ((Knockout)v).get();
115 props[index].setValue(v);
118 final void call(int index, Object data, Object ev) {
119 funcs[index].call(data, ev);
122 @JavaScriptBody(args = { "model", "prop", "oldValue", "newValue" },
126 + " var koProp = model[prop];\n"
127 + " if (koProp && koProp['valueHasMutated']) {\n"
128 + " if ((oldValue !== null || newValue !== null)) {\n"
129 + " koProp['valueHasMutated'](newValue);\n"
130 + " } else if (koProp['valueHasMutated']) {\n"
131 + " koProp['valueHasMutated']();\n"
136 public native static void valueHasMutated(
137 Object model, String prop, Object oldValue, Object newValue
140 @JavaScriptBody(args = { "id", "bindings" }, body =
141 "var d = window['document'];\n" +
142 "var e = id ? d['getElementById'](id) : d['body'];\n" +
143 "ko['cleanNode'](e);\n" +
144 "ko['applyBindings'](bindings, e);\n" +
145 "return bindings['ko4j'];\n"
147 native static Object applyBindings(String id, Object bindings);
149 @JavaScriptBody(args = { "cnt" }, body =
150 "var arr = new Array(cnt);\n" +
151 "for (var i = 0; i < cnt; i++) arr[i] = new Object();\n" +
154 native static Object[] allocJS(int cnt);
160 args = { "ret", "propNames", "propReadOnly", "propValues", "funcNames" },
162 "Object.defineProperty(ret, 'ko4j', { value : this });\n"
163 + "function koComputed(index, name, readOnly, value) {\n"
164 + " var trigger = ko['observable']()['extend']({'notify':'always'});"
165 + " function realGetter() {\n"
166 + " var self = ret['ko4j'];\n"
168 + " var v = self ? self.@org.netbeans.html.ko4j.Knockout::getValue(I)(index) : null;\n"
171 + " alert(\"Cannot call getValue on \" + self + \" prop: \" + name + \" error: \" + e);\n"
174 + " var activeGetter = function() { return value; };\n"
176 + " 'read': function() {\n"
178 + " var r = activeGetter();\n"
179 + " activeGetter = realGetter;\n"
180 + " if (r) try { var br = r.valueOf(); } catch (err) {}\n"
181 + " return br === undefined ? r: br;\n"
185 + " if (!readOnly) {\n"
186 + " bnd['write'] = function(val) {\n"
187 + " var self = ret['ko4j'];\n"
188 + " if (!self) return;\n"
189 + " var model = val['ko4j'];\n"
190 + " self.@org.netbeans.html.ko4j.Knockout::setValue(ILjava/lang/Object;)(index, model ? model : val);\n"
193 + " var cmpt = ko['computed'](bnd);\n"
194 + " cmpt['valueHasMutated'] = function(val) {\n"
195 + " if (arguments.length === 1) activeGetter = function() { return val; };\n"
196 + " trigger['valueHasMutated']();\n"
198 + " ret[name] = cmpt;\n"
200 + "for (var i = 0; i < propNames.length; i++) {\n"
201 + " koComputed(i, propNames[i], propReadOnly[i], propValues[i]);\n"
203 + "function koExpose(index, name) {\n"
204 + " ret[name] = function(data, ev) {\n"
205 + " var self = ret['ko4j'];\n"
206 + " if (!self) return;\n"
207 + " self.@org.netbeans.html.ko4j.Knockout::call(ILjava/lang/Object;Ljava/lang/Object;)(index, data, ev);\n"
210 + "for (var i = 0; i < funcNames.length; i++) {\n"
211 + " koExpose(i, funcNames[i]);\n"
214 native void wrapModel(
216 String[] propNames, boolean[] propReadOnly, Object propValues,
220 @JavaScriptBody(args = { "js" }, wait4js = false, body =
221 "delete js['ko4j'];\n" +
222 "for (var p in js) {\n" +
227 private static native void clean(Object js);
229 @JavaScriptBody(args = { "o" }, body = "return o['ko4j'] ? o['ko4j'] : o;")
230 private static native Object toModelImpl(Object wrapper);
231 static Object toModel(Object wrapper) {
232 Object o = toModelImpl(wrapper);
233 if (o instanceof Knockout) {
234 return ((Knockout)o).get();