ko4j/src/main/java/org/netbeans/html/ko4j/Knockout.java
author Jaroslav Tulach <jtulach@netbeans.org>
Mon, 14 Dec 2015 05:52:22 +0100
changeset 1031 86218dd9270b
parent 1029 e737d579349a
child 1035 3534e15dc446
child 1055 c61d247f087a
permissions -rw-r--r--
#257130: Switching to minified version of knockout.js
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;
jtulach@1007
    47
import java.util.Collections;
jtulach@1007
    48
import java.util.HashSet;
jtulach@1007
    49
import java.util.Set;
jaroslav@131
    50
import net.java.html.js.JavaScriptBody;
jaroslav@163
    51
import net.java.html.js.JavaScriptResource;
jaroslav@37
    52
import net.java.html.json.Model;
jtulach@838
    53
import org.netbeans.html.json.spi.FunctionBinding;
jtulach@838
    54
import org.netbeans.html.json.spi.PropertyBinding;
jaroslav@37
    55
jaroslav@54
    56
/** This is an implementation package - just
jaroslav@54
    57
 * include its JAR on classpath and use official {@link Context} API
jaroslav@54
    58
 * to access the functionality.
jaroslav@54
    59
 * <p>
jaroslav@54
    60
 * Provides binding between {@link Model models} and knockout.js running
jaroslav@54
    61
 * inside a JavaFX WebView. 
jaroslav@37
    62
 *
jtulach@790
    63
 * @author Jaroslav Tulach
jaroslav@37
    64
 */
jtulach@1031
    65
@JavaScriptResource("knockout-3.4.0.js")
jtulach@870
    66
final class Knockout extends WeakReference<Object> {
jtulach@877
    67
    private static final ReferenceQueue<Object> QUEUE = new ReferenceQueue();
jtulach@1007
    68
    private static final Set<Knockout> active = Collections.synchronizedSet(new HashSet<Knockout>());
jtulach@1020
    69
jtulach@1020
    70
    @JavaScriptBody(args = {"object", "property"}, body = 
jtulach@1020
    71
        "var ret;\n" + 
jtulach@1020
    72
        "if (property === null) ret = object;\n" + 
jtulach@1020
    73
        "else if (object === null) ret = null;\n" + 
jtulach@1020
    74
        "else ret = object[property];\n" + 
jtulach@1020
    75
        "return ret ? ko.utils.unwrapObservable(ret) : null;"
jtulach@1020
    76
    )
jtulach@1020
    77
    static Object getProperty(Object object, String property) {
jtulach@1020
    78
        return null;
jtulach@1020
    79
    }
jtulach@877
    80
    
jtulach@877
    81
    private PropertyBinding[] props;
jtulach@877
    82
    private FunctionBinding[] funcs;
jtulach@877
    83
    private Object js;
jtulach@871
    84
    private Object strong;
jtulach@868
    85
jtulach@877
    86
    public Knockout(Object model, Object js, PropertyBinding[] props, FunctionBinding[] funcs) {
jtulach@877
    87
        super(model, QUEUE);
jtulach@877
    88
        this.js = js;
jtulach@870
    89
        this.props = new PropertyBinding[props.length];
jtulach@870
    90
        for (int i = 0; i < props.length; i++) {
jtulach@870
    91
            this.props[i] = props[i].weak();
jtulach@870
    92
        }
jtulach@870
    93
        this.funcs = new FunctionBinding[funcs.length];
jtulach@870
    94
        for (int i = 0; i < funcs.length; i++) {
jtulach@870
    95
            this.funcs[i] = funcs[i].weak();
jtulach@870
    96
        }
jtulach@1007
    97
        active.add(this);
jtulach@868
    98
    }
jtulach@868
    99
    
jtulach@877
   100
    static void cleanUp() {
jtulach@877
   101
        for (;;) {
jtulach@877
   102
            Knockout ko = (Knockout)QUEUE.poll();
jtulach@877
   103
            if (ko == null) {
jtulach@877
   104
                return;
jtulach@877
   105
            }
jtulach@1007
   106
            active.remove(ko);
jtulach@877
   107
            clean(ko.js);
jtulach@877
   108
            ko.js = null;
jtulach@877
   109
            ko.props = null;
jtulach@877
   110
            ko.funcs = null;
jtulach@877
   111
        }
jtulach@877
   112
    }
jtulach@877
   113
    
jtulach@871
   114
    final void hold() {
jtulach@871
   115
        strong = get();
jtulach@871
   116
    }
jtulach@871
   117
    
jtulach@868
   118
    final Object getValue(int index) {
jtulach@868
   119
        return props[index].getValue();
jtulach@868
   120
    }
jtulach@868
   121
    
jtulach@868
   122
    final void setValue(int index, Object v) {
jtulach@870
   123
        if (v instanceof Knockout) {
jtulach@870
   124
            v = ((Knockout)v).get();
jtulach@870
   125
        }
jtulach@868
   126
        props[index].setValue(v);
jtulach@868
   127
    }
jtulach@868
   128
    
jtulach@868
   129
    final void call(int index, Object data, Object ev) {
jtulach@868
   130
        funcs[index].call(data, ev);
jtulach@868
   131
    }
jtulach@868
   132
    
jaroslav@570
   133
    @JavaScriptBody(args = { "model", "prop", "oldValue", "newValue" }, 
jaroslav@570
   134
        wait4js = false,
jaroslav@570
   135
        body =
jaroslav@276
   136
          "if (model) {\n"
jaroslav@276
   137
        + "  var koProp = model[prop];\n"
jaroslav@276
   138
        + "  if (koProp && koProp['valueHasMutated']) {\n"
jaroslav@567
   139
        + "    if ((oldValue !== null || newValue !== null)) {\n"
jaroslav@567
   140
        + "      koProp['valueHasMutated'](newValue);\n"
jaroslav@567
   141
        + "    } else if (koProp['valueHasMutated']) {\n"
jaroslav@567
   142
        + "      koProp['valueHasMutated']();\n"
jaroslav@567
   143
        + "    }\n"
jaroslav@276
   144
        + "  }\n"
jaroslav@276
   145
        + "}\n"
jaroslav@276
   146
    )
jaroslav@567
   147
    public native static void valueHasMutated(
jaroslav@567
   148
        Object model, String prop, Object oldValue, Object newValue
jaroslav@567
   149
    );
jaroslav@37
   150
jtulach@908
   151
    @JavaScriptBody(args = { "id", "bindings" }, body = 
jtulach@908
   152
        "var d = window['document'];\n" +
jtulach@908
   153
        "var e = id ? d['getElementById'](id) : d['body'];\n" +
jtulach@908
   154
        "ko['cleanNode'](e);\n" +
jtulach@908
   155
        "ko['applyBindings'](bindings, e);\n" +
jtulach@871
   156
        "return bindings['ko4j'];\n"
jaroslav@570
   157
    )
jtulach@908
   158
    native static Object applyBindings(String id, Object bindings);
jaroslav@276
   159
    
jaroslav@574
   160
    @JavaScriptBody(args = { "cnt" }, body = 
jaroslav@574
   161
        "var arr = new Array(cnt);\n" +
jaroslav@574
   162
        "for (var i = 0; i < cnt; i++) arr[i] = new Object();\n" +
jaroslav@574
   163
        "return arr;\n"
jaroslav@574
   164
    )
jaroslav@574
   165
    native static Object[] allocJS(int cnt);
jaroslav@574
   166
    
jaroslav@276
   167
    @JavaScriptBody(
jaroslav@276
   168
        javacall = true,
jtulach@900
   169
        keepAlive = false,
jaroslav@570
   170
        wait4js = false,
jtulach@1028
   171
        args = { "ret", "copyFrom", "propNames", "propReadOnly", "propValues", "funcNames" },
jaroslav@570
   172
        body = 
jtulach@898
   173
          "Object.defineProperty(ret, 'ko4j', { value : this });\n"
jtulach@868
   174
        + "function koComputed(index, name, readOnly, value) {\n"
jtulach@1028
   175
        + "  var orig = copyFrom ? copyFrom[name] : null;\n"
jtulach@1028
   176
        + "  if (!ko['isObservable'](orig)) {\n"
jtulach@1028
   177
        + "    orig = null;\n"
jtulach@1028
   178
        + "    var trigger = ko['observable']()['extend']({'notify':'always'});\n"
jtulach@1028
   179
        + "  } else {\n"
jtulach@1028
   180
        + "    var trigger = orig;\n"
jtulach@1028
   181
        + "  }\n"
jaroslav@279
   182
        + "  function realGetter() {\n"
jtulach@877
   183
        + "    var self = ret['ko4j'];\n"
jaroslav@437
   184
        + "    try {\n"
jtulach@877
   185
        + "      var v = self ? self.@org.netbeans.html.ko4j.Knockout::getValue(I)(index) : null;\n"
jaroslav@437
   186
        + "      return v;\n"
jaroslav@437
   187
        + "    } catch (e) {\n"
jtulach@871
   188
        + "      alert(\"Cannot call getValue on \" + self + \" prop: \" + name + \" error: \" + e);\n"
jaroslav@437
   189
        + "    }\n"
jaroslav@279
   190
        + "  }\n"
jtulach@1028
   191
        + "  var activeGetter = orig ? orig : function() { return value; };\n"
jaroslav@437
   192
        + "  var bnd = {\n"
jtulach@672
   193
        + "    'read': function() {\n"
jtulach@816
   194
        + "      trigger();\n"
jtulach@1028
   195
        + "      if (orig) {\n"
jtulach@1028
   196
        + "        var r = orig();\n"
jtulach@1028
   197
        + "      } else {\n"
jtulach@1028
   198
        + "        var r = activeGetter();\n"
jtulach@1028
   199
        + "        activeGetter = realGetter;\n"
jtulach@1028
   200
        + "      }\n"
jtulach@758
   201
        + "      if (r) try { var br = r.valueOf(); } catch (err) {}\n"
jtulach@764
   202
        + "      return br === undefined ? r: br;\n"
jaroslav@437
   203
        + "    },\n"
jtulach@672
   204
        + "    'owner': ret\n"
jaroslav@276
   205
        + "  };\n"
jaroslav@276
   206
        + "  if (!readOnly) {\n"
jtulach@1028
   207
        + "    function write(val) {\n"
jtulach@1028
   208
        + "      if (orig) orig(val);\n"
jtulach@877
   209
        + "      var self = ret['ko4j'];\n"
jtulach@877
   210
        + "      if (!self) return;\n"
jtulach@1028
   211
        + "      var model = val ? val['ko4j'] : null;\n"
jtulach@925
   212
        + "      self.@org.netbeans.html.ko4j.Knockout::setValue(ILjava/lang/Object;)(index, model ? model : val);\n"
jaroslav@437
   213
        + "    };\n"
jtulach@1028
   214
        + "    bnd['write'] = write;\n"
jtulach@1028
   215
        + "    if (orig) {\n"
jtulach@1028
   216
        + "      write(orig());\n"
jtulach@1029
   217
        + "      orig.subscribe(write);\n"
jtulach@1028
   218
        + "    }\n"
jaroslav@437
   219
        + "  };\n"
jtulach@672
   220
        + "  var cmpt = ko['computed'](bnd);\n"
jtulach@672
   221
        + "  cmpt['valueHasMutated'] = function(val) {\n"
jaroslav@567
   222
        + "    if (arguments.length === 1) activeGetter = function() { return val; };\n"
jtulach@1028
   223
        + "    trigger(val);\n"
jaroslav@567
   224
        + "  };\n"
jaroslav@567
   225
        + "  ret[name] = cmpt;\n"
jaroslav@276
   226
        + "}\n"
jaroslav@276
   227
        + "for (var i = 0; i < propNames.length; i++) {\n"
jtulach@868
   228
        + "  koComputed(i, propNames[i], propReadOnly[i], propValues[i]);\n"
jaroslav@276
   229
        + "}\n"
jtulach@868
   230
        + "function koExpose(index, name) {\n"
jaroslav@276
   231
        + "  ret[name] = function(data, ev) {\n"
jtulach@877
   232
        + "    var self = ret['ko4j'];\n"
jtulach@877
   233
        + "    if (!self) return;\n"
jtulach@871
   234
        + "    self.@org.netbeans.html.ko4j.Knockout::call(ILjava/lang/Object;Ljava/lang/Object;)(index, data, ev);\n"
jaroslav@276
   235
        + "  };\n"
jaroslav@276
   236
        + "}\n"
jaroslav@276
   237
        + "for (var i = 0; i < funcNames.length; i++) {\n"
jtulach@868
   238
        + "  koExpose(i, funcNames[i]);\n"
jaroslav@276
   239
        + "}\n"
jaroslav@276
   240
        )
jtulach@898
   241
    native void wrapModel(
jtulach@1028
   242
        Object ret, Object copyFrom,
jtulach@1027
   243
        String[] propNames, Boolean[] propReadOnly, Object propValues,
jtulach@868
   244
        String[] funcNames
jaroslav@276
   245
    );
jaroslav@430
   246
    
jtulach@877
   247
    @JavaScriptBody(args = { "js" }, wait4js = false, body = 
jtulach@877
   248
        "delete js['ko4j'];\n" +
jtulach@877
   249
        "for (var p in js) {\n" +
jtulach@877
   250
        "  delete js[p];\n" +
jtulach@877
   251
        "};\n" +
jtulach@877
   252
        "\n"
jtulach@877
   253
    )
jtulach@877
   254
    private static native void clean(Object js);
jtulach@877
   255
    
jtulach@870
   256
    @JavaScriptBody(args = { "o" }, body = "return o['ko4j'] ? o['ko4j'] : o;")
jaroslav@570
   257
    private static native Object toModelImpl(Object wrapper);
jaroslav@570
   258
    static Object toModel(Object wrapper) {
jtulach@870
   259
        Object o = toModelImpl(wrapper);
jtulach@870
   260
        if (o instanceof Knockout) {
jtulach@870
   261
            return ((Knockout)o).get();
jtulach@870
   262
        } else {
jtulach@870
   263
            return o;
jtulach@870
   264
        }
jaroslav@570
   265
    }
jaroslav@37
   266
}