ko4j/src/main/java/org/netbeans/html/ko4j/Knockout.java
author Jaroslav Tulach <jtulach@netbeans.org>
Tue, 09 Dec 2014 08:33:22 +0100
branchgc
changeset 892 de02b7c13379
parent 890 5bea583947da
parent 886 88d62267a0b5
child 898 18e17d0cd066
child 900 2ee22312e414
permissions -rw-r--r--
Bringing the most recent changes in default branch to gc
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
jtulach@877
    45
import java.lang.ref.ReferenceQueue;
jtulach@870
    46
import java.lang.ref.WeakReference;
jaroslav@131
    47
import net.java.html.js.JavaScriptBody;
jaroslav@163
    48
import net.java.html.js.JavaScriptResource;
jaroslav@37
    49
import net.java.html.json.Model;
jtulach@838
    50
import org.netbeans.html.json.spi.FunctionBinding;
jtulach@838
    51
import org.netbeans.html.json.spi.PropertyBinding;
jaroslav@37
    52
jaroslav@54
    53
/** This is an implementation package - just
jaroslav@54
    54
 * include its JAR on classpath and use official {@link Context} API
jaroslav@54
    55
 * to access the functionality.
jaroslav@54
    56
 * <p>
jaroslav@54
    57
 * Provides binding between {@link Model models} and knockout.js running
jaroslav@54
    58
 * inside a JavaFX WebView. 
jaroslav@37
    59
 *
jtulach@790
    60
 * @author Jaroslav Tulach
jaroslav@37
    61
 */
jtulach@816
    62
@JavaScriptResource("knockout-3.2.0.debug.js")
jtulach@870
    63
final class Knockout extends WeakReference<Object> {
jtulach@877
    64
    private static final ReferenceQueue<Object> QUEUE = new ReferenceQueue();
jtulach@877
    65
    
jtulach@877
    66
    private PropertyBinding[] props;
jtulach@877
    67
    private FunctionBinding[] funcs;
jtulach@877
    68
    private Object js;
jtulach@871
    69
    private Object strong;
jtulach@868
    70
jtulach@877
    71
    public Knockout(Object model, Object js, PropertyBinding[] props, FunctionBinding[] funcs) {
jtulach@877
    72
        super(model, QUEUE);
jtulach@877
    73
        this.js = js;
jtulach@870
    74
        this.props = new PropertyBinding[props.length];
jtulach@870
    75
        for (int i = 0; i < props.length; i++) {
jtulach@870
    76
            this.props[i] = props[i].weak();
jtulach@870
    77
        }
jtulach@870
    78
        this.funcs = new FunctionBinding[funcs.length];
jtulach@870
    79
        for (int i = 0; i < funcs.length; i++) {
jtulach@870
    80
            this.funcs[i] = funcs[i].weak();
jtulach@870
    81
        }
jtulach@868
    82
    }
jtulach@868
    83
    
jtulach@877
    84
    static void cleanUp() {
jtulach@877
    85
        for (;;) {
jtulach@877
    86
            Knockout ko = (Knockout)QUEUE.poll();
jtulach@877
    87
            if (ko == null) {
jtulach@877
    88
                return;
jtulach@877
    89
            }
jtulach@877
    90
            clean(ko.js);
jtulach@877
    91
            ko.js = null;
jtulach@877
    92
            ko.props = null;
jtulach@877
    93
            ko.funcs = null;
jtulach@877
    94
        }
jtulach@877
    95
    }
jtulach@877
    96
    
jtulach@871
    97
    final void hold() {
jtulach@871
    98
        strong = get();
jtulach@871
    99
    }
jtulach@871
   100
    
jtulach@868
   101
    final Object getValue(int index) {
jtulach@868
   102
        return props[index].getValue();
jtulach@868
   103
    }
jtulach@868
   104
    
jtulach@868
   105
    final void setValue(int index, Object v) {
jtulach@870
   106
        if (v instanceof Knockout) {
jtulach@870
   107
            v = ((Knockout)v).get();
jtulach@870
   108
        }
jtulach@868
   109
        props[index].setValue(v);
jtulach@868
   110
    }
jtulach@868
   111
    
jtulach@868
   112
    final void call(int index, Object data, Object ev) {
jtulach@868
   113
        funcs[index].call(data, ev);
jtulach@868
   114
    }
jtulach@868
   115
    
jaroslav@570
   116
    @JavaScriptBody(args = { "model", "prop", "oldValue", "newValue" }, 
jaroslav@570
   117
        wait4js = false,
jaroslav@570
   118
        body =
jaroslav@276
   119
          "if (model) {\n"
jaroslav@276
   120
        + "  var koProp = model[prop];\n"
jaroslav@276
   121
        + "  if (koProp && koProp['valueHasMutated']) {\n"
jaroslav@567
   122
        + "    if ((oldValue !== null || newValue !== null)) {\n"
jaroslav@567
   123
        + "      koProp['valueHasMutated'](newValue);\n"
jaroslav@567
   124
        + "    } else if (koProp['valueHasMutated']) {\n"
jaroslav@567
   125
        + "      koProp['valueHasMutated']();\n"
jaroslav@567
   126
        + "    }\n"
jaroslav@276
   127
        + "  }\n"
jaroslav@276
   128
        + "}\n"
jaroslav@276
   129
    )
jaroslav@567
   130
    public native static void valueHasMutated(
jaroslav@567
   131
        Object model, String prop, Object oldValue, Object newValue
jaroslav@567
   132
    );
jaroslav@37
   133
jtulach@871
   134
    @JavaScriptBody(args = { "bindings" }, body = 
jtulach@861
   135
        "ko['cleanNode'](window['document']['body']);\n" +
jtulach@871
   136
        "ko['applyBindings'](bindings);\n" +
jtulach@871
   137
        "return bindings['ko4j'];\n"
jaroslav@570
   138
    )
jtulach@871
   139
    native static Object applyBindings(Object bindings);
jaroslav@276
   140
    
jaroslav@574
   141
    @JavaScriptBody(args = { "cnt" }, body = 
jaroslav@574
   142
        "var arr = new Array(cnt);\n" +
jaroslav@574
   143
        "for (var i = 0; i < cnt; i++) arr[i] = new Object();\n" +
jaroslav@574
   144
        "return arr;\n"
jaroslav@574
   145
    )
jaroslav@574
   146
    native static Object[] allocJS(int cnt);
jaroslav@574
   147
    
jaroslav@276
   148
    @JavaScriptBody(
jaroslav@276
   149
        javacall = true,
jaroslav@570
   150
        wait4js = false,
jtulach@877
   151
        args = { "thiz", "ret", "propNames", "propReadOnly", "propValues", "funcNames" },
jaroslav@570
   152
        body = 
jtulach@877
   153
          "Object.defineProperty(ret, 'ko4j', { value : thiz });\n"
jtulach@868
   154
        + "function koComputed(index, name, readOnly, value) {\n"
jtulach@861
   155
        + "  var trigger = ko['observable']()['extend']({'notify':'always'});"
jaroslav@279
   156
        + "  function realGetter() {\n"
jtulach@877
   157
        + "    var self = ret['ko4j'];\n"
jaroslav@437
   158
        + "    try {\n"
jtulach@877
   159
        + "      var v = self ? self.@org.netbeans.html.ko4j.Knockout::getValue(I)(index) : null;\n"
jaroslav@437
   160
        + "      return v;\n"
jaroslav@437
   161
        + "    } catch (e) {\n"
jtulach@871
   162
        + "      alert(\"Cannot call getValue on \" + self + \" prop: \" + name + \" error: \" + e);\n"
jaroslav@437
   163
        + "    }\n"
jaroslav@279
   164
        + "  }\n"
jaroslav@279
   165
        + "  var activeGetter = function() { return value; };\n"
jaroslav@437
   166
        + "  var bnd = {\n"
jtulach@672
   167
        + "    'read': function() {\n"
jtulach@816
   168
        + "      trigger();\n"
jaroslav@437
   169
        + "      var r = activeGetter();\n"
jaroslav@437
   170
        + "      activeGetter = realGetter;\n"
jtulach@758
   171
        + "      if (r) try { var br = r.valueOf(); } catch (err) {}\n"
jtulach@764
   172
        + "      return br === undefined ? r: br;\n"
jaroslav@437
   173
        + "    },\n"
jtulach@672
   174
        + "    'owner': ret\n"
jaroslav@276
   175
        + "  };\n"
jaroslav@276
   176
        + "  if (!readOnly) {\n"
jtulach@672
   177
        + "    bnd['write'] = function(val) {\n"
jtulach@877
   178
        + "      var self = ret['ko4j'];\n"
jtulach@877
   179
        + "      if (!self) return;\n"
jtulach@870
   180
        + "      var model = val['ko4j'];\n"
jtulach@880
   181
        + "      var s = ret['ko4j'];\n"
jtulach@880
   182
        + "      s.@org.netbeans.html.ko4j.Knockout::setValue(ILjava/lang/Object;)(index, model ? model : val);\n"
jaroslav@437
   183
        + "    };\n"
jaroslav@437
   184
        + "  };\n"
jtulach@672
   185
        + "  var cmpt = ko['computed'](bnd);\n"
jtulach@672
   186
        + "  cmpt['valueHasMutated'] = function(val) {\n"
jaroslav@567
   187
        + "    if (arguments.length === 1) activeGetter = function() { return val; };\n"
jtulach@861
   188
        + "    trigger['valueHasMutated']();\n"
jaroslav@567
   189
        + "  };\n"
jaroslav@567
   190
        + "  ret[name] = cmpt;\n"
jaroslav@276
   191
        + "}\n"
jaroslav@276
   192
        + "for (var i = 0; i < propNames.length; i++) {\n"
jtulach@868
   193
        + "  koComputed(i, propNames[i], propReadOnly[i], propValues[i]);\n"
jaroslav@276
   194
        + "}\n"
jtulach@868
   195
        + "function koExpose(index, name) {\n"
jaroslav@276
   196
        + "  ret[name] = function(data, ev) {\n"
jtulach@877
   197
        + "    var self = ret['ko4j'];\n"
jtulach@877
   198
        + "    if (!self) return;\n"
jtulach@871
   199
        + "    self.@org.netbeans.html.ko4j.Knockout::call(ILjava/lang/Object;Ljava/lang/Object;)(index, data, ev);\n"
jaroslav@276
   200
        + "  };\n"
jaroslav@276
   201
        + "}\n"
jaroslav@276
   202
        + "for (var i = 0; i < funcNames.length; i++) {\n"
jtulach@868
   203
        + "  koExpose(i, funcNames[i]);\n"
jaroslav@276
   204
        + "}\n"
jaroslav@276
   205
        )
jaroslav@574
   206
    static native void wrapModel(
jtulach@868
   207
        Knockout self,
jtulach@870
   208
        Object ret, 
jtulach@868
   209
        String[] propNames, boolean[] propReadOnly, Object propValues,
jtulach@868
   210
        String[] funcNames
jaroslav@276
   211
    );
jaroslav@430
   212
    
jtulach@877
   213
    @JavaScriptBody(args = { "js" }, wait4js = false, body = 
jtulach@877
   214
        "delete js['ko4j'];\n" +
jtulach@877
   215
        "for (var p in js) {\n" +
jtulach@877
   216
        "  delete js[p];\n" +
jtulach@877
   217
        "};\n" +
jtulach@877
   218
        "\n"
jtulach@877
   219
    )
jtulach@877
   220
    private static native void clean(Object js);
jtulach@877
   221
    
jtulach@870
   222
    @JavaScriptBody(args = { "o" }, body = "return o['ko4j'] ? o['ko4j'] : o;")
jaroslav@570
   223
    private static native Object toModelImpl(Object wrapper);
jaroslav@570
   224
    static Object toModel(Object wrapper) {
jtulach@870
   225
        Object o = toModelImpl(wrapper);
jtulach@870
   226
        if (o instanceof Knockout) {
jtulach@870
   227
            return ((Knockout)o).get();
jtulach@870
   228
        } else {
jtulach@870
   229
            return o;
jtulach@870
   230
        }
jaroslav@570
   231
    }
jaroslav@37
   232
}