ko4j/src/main/java/org/netbeans/html/ko4j/Knockout.java
author Jaroslav Tulach <jtulach@netbeans.org>
Sat, 02 Aug 2014 12:59:31 +0200
changeset 790 30f20d9c0986
parent 764 2224f99d1172
child 809 57c6bd4c5261
permissions -rw-r--r--
Fixing Javadoc to succeed on JDK8
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
 *
jtulach@790
    58
 * @author Jaroslav Tulach
jaroslav@37
    59
 */
jaroslav@163
    60
@JavaScriptResource("knockout-2.2.1.js")
jaroslav@276
    61
final class Knockout {
jaroslav@570
    62
    @JavaScriptBody(args = { "model", "prop", "oldValue", "newValue" }, 
jaroslav@570
    63
        wait4js = false,
jaroslav@570
    64
        body =
jaroslav@276
    65
          "if (model) {\n"
jaroslav@276
    66
        + "  var koProp = model[prop];\n"
jaroslav@276
    67
        + "  if (koProp && koProp['valueHasMutated']) {\n"
jaroslav@567
    68
        + "    if ((oldValue !== null || newValue !== null)) {\n"
jaroslav@567
    69
        + "      koProp['valueHasMutated'](newValue);\n"
jaroslav@567
    70
        + "    } else if (koProp['valueHasMutated']) {\n"
jaroslav@567
    71
        + "      koProp['valueHasMutated']();\n"
jaroslav@567
    72
        + "    }\n"
jaroslav@276
    73
        + "  }\n"
jaroslav@276
    74
        + "}\n"
jaroslav@276
    75
    )
jaroslav@567
    76
    public native static void valueHasMutated(
jaroslav@567
    77
        Object model, String prop, Object oldValue, Object newValue
jaroslav@567
    78
    );
jaroslav@37
    79
jaroslav@570
    80
    @JavaScriptBody(args = { "bindings" }, wait4js = false, body = 
jaroslav@570
    81
        "ko.applyBindings(bindings);\n"
jaroslav@570
    82
    )
jaroslav@276
    83
    native static void applyBindings(Object bindings);
jaroslav@276
    84
    
jaroslav@574
    85
    @JavaScriptBody(args = { "cnt" }, body = 
jaroslav@574
    86
        "var arr = new Array(cnt);\n" +
jaroslav@574
    87
        "for (var i = 0; i < cnt; i++) arr[i] = new Object();\n" +
jaroslav@574
    88
        "return arr;\n"
jaroslav@574
    89
    )
jaroslav@574
    90
    native static Object[] allocJS(int cnt);
jaroslav@574
    91
    
jaroslav@276
    92
    @JavaScriptBody(
jaroslav@276
    93
        javacall = true,
jaroslav@570
    94
        wait4js = false,
jaroslav@574
    95
        args = {"ret", "model", "propNames", "propReadOnly", "propValues", "propArr", "funcNames", "funcArr"},
jaroslav@570
    96
        body = 
jaroslav@574
    97
          "ret['ko-fx.model'] = model;\n"
jaroslav@279
    98
        + "function koComputed(name, readOnly, value, prop) {\n"
jaroslav@279
    99
        + "  function realGetter() {\n"
jaroslav@437
   100
        + "    try {\n"
jaroslav@437
   101
        + "      var v = prop.@org.apidesign.html.json.spi.PropertyBinding::getValue()();\n"
jaroslav@437
   102
        + "      return v;\n"
jaroslav@437
   103
        + "    } catch (e) {\n"
jaroslav@437
   104
        + "      alert(\"Cannot call getValue on \" + model + \" prop: \" + name + \" error: \" + e);\n"
jaroslav@437
   105
        + "    }\n"
jaroslav@279
   106
        + "  }\n"
jaroslav@279
   107
        + "  var activeGetter = function() { return value; };\n"
jaroslav@437
   108
        + "  var bnd = {\n"
jtulach@672
   109
        + "    'read': function() {\n"
jaroslav@437
   110
        + "      var r = activeGetter();\n"
jaroslav@437
   111
        + "      activeGetter = realGetter;\n"
jtulach@758
   112
        + "      if (r) try { var br = r.valueOf(); } catch (err) {}\n"
jtulach@764
   113
        + "      return br === undefined ? r: br;\n"
jaroslav@437
   114
        + "    },\n"
jtulach@672
   115
        + "    'owner': ret\n"
jaroslav@276
   116
        + "  };\n"
jaroslav@276
   117
        + "  if (!readOnly) {\n"
jtulach@672
   118
        + "    bnd['write'] = function(val) {\n"
jaroslav@276
   119
        + "      prop.@org.apidesign.html.json.spi.PropertyBinding::setValue(Ljava/lang/Object;)(val);\n"
jaroslav@437
   120
        + "    };\n"
jaroslav@437
   121
        + "  };\n"
jtulach@672
   122
        + "  var cmpt = ko['computed'](bnd);\n"
jtulach@672
   123
        + "  var vhm = cmpt['valueHasMutated'];\n"
jtulach@672
   124
        + "  cmpt['valueHasMutated'] = function(val) {\n"
jaroslav@567
   125
        + "    if (arguments.length === 1) activeGetter = function() { return val; };\n"
jaroslav@567
   126
        + "    vhm();\n"
jaroslav@567
   127
        + "  };\n"
jaroslav@567
   128
        + "  ret[name] = cmpt;\n"
jaroslav@276
   129
        + "}\n"
jaroslav@276
   130
        + "for (var i = 0; i < propNames.length; i++) {\n"
jaroslav@279
   131
        + "  koComputed(propNames[i], propReadOnly[i], propValues[i], propArr[i]);\n"
jaroslav@276
   132
        + "}\n"
jaroslav@276
   133
        + "function koExpose(name, func) {\n"
jaroslav@276
   134
        + "  ret[name] = function(data, ev) {\n"
jaroslav@276
   135
        + "    func.@org.apidesign.html.json.spi.FunctionBinding::call(Ljava/lang/Object;Ljava/lang/Object;)(data, ev);\n"
jaroslav@276
   136
        + "  };\n"
jaroslav@276
   137
        + "}\n"
jaroslav@276
   138
        + "for (var i = 0; i < funcNames.length; i++) {\n"
jaroslav@276
   139
        + "  koExpose(funcNames[i], funcArr[i]);\n"
jaroslav@276
   140
        + "}\n"
jaroslav@276
   141
        )
jaroslav@574
   142
    static native void wrapModel(
jaroslav@574
   143
        Object ret, Object model,
jaroslav@279
   144
        String[] propNames, boolean[] propReadOnly, Object propValues, PropertyBinding[] propArr,
jaroslav@275
   145
        String[] funcNames, FunctionBinding[] funcArr
jaroslav@276
   146
    );
jaroslav@430
   147
    
jaroslav@430
   148
    @JavaScriptBody(args = { "o" }, body = "return o['ko-fx.model'] ? o['ko-fx.model'] : o;")
jaroslav@570
   149
    private static native Object toModelImpl(Object wrapper);
jaroslav@570
   150
    static Object toModel(Object wrapper) {
jaroslav@570
   151
        return toModelImpl(wrapper);
jaroslav@570
   152
    }
jaroslav@590
   153
    
jaroslav@590
   154
    @JavaScriptBody(args = {}, body = "if (window.WebSocket) return true; else return false;")
jaroslav@590
   155
    static final boolean areWebSocketsSupported() {
jaroslav@590
   156
        return false;
jaroslav@590
   157
    }
jaroslav@37
   158
}