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
     1 /**
     2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     3  *
     4  * Copyright 2013-2014 Oracle and/or its affiliates. All rights reserved.
     5  *
     6  * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
     7  * Other names may be trademarks of their respective owners.
     8  *
     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]"
    26  *
    27  * Contributor(s):
    28  *
    29  * The Original Software is NetBeans. The Initial Developer of the Original
    30  * Software is Oracle. Portions Copyright 2013-2014 Oracle. All Rights Reserved.
    31  *
    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.
    42  */
    43 package org.netbeans.html.ko4j;
    44 
    45 import net.java.html.js.JavaScriptBody;
    46 import net.java.html.js.JavaScriptResource;
    47 import net.java.html.json.Model;
    48 import org.apidesign.html.json.spi.FunctionBinding;
    49 import org.apidesign.html.json.spi.PropertyBinding;
    50 
    51 /** This is an implementation package - just
    52  * include its JAR on classpath and use official {@link Context} API
    53  * to access the functionality.
    54  * <p>
    55  * Provides binding between {@link Model models} and knockout.js running
    56  * inside a JavaFX WebView. 
    57  *
    58  * @author Jaroslav Tulach
    59  */
    60 @JavaScriptResource("knockout-2.2.1.js")
    61 final class Knockout {
    62     @JavaScriptBody(args = { "model", "prop", "oldValue", "newValue" }, 
    63         wait4js = false,
    64         body =
    65           "if (model) {\n"
    66         + "  var koProp = model[prop];\n"
    67         + "  if (koProp && koProp['valueHasMutated']) {\n"
    68         + "    if ((oldValue !== null || newValue !== null)) {\n"
    69         + "      koProp['valueHasMutated'](newValue);\n"
    70         + "    } else if (koProp['valueHasMutated']) {\n"
    71         + "      koProp['valueHasMutated']();\n"
    72         + "    }\n"
    73         + "  }\n"
    74         + "}\n"
    75     )
    76     public native static void valueHasMutated(
    77         Object model, String prop, Object oldValue, Object newValue
    78     );
    79 
    80     @JavaScriptBody(args = { "bindings" }, wait4js = false, body = 
    81         "ko.applyBindings(bindings);\n"
    82     )
    83     native static void applyBindings(Object bindings);
    84     
    85     @JavaScriptBody(args = { "cnt" }, body = 
    86         "var arr = new Array(cnt);\n" +
    87         "for (var i = 0; i < cnt; i++) arr[i] = new Object();\n" +
    88         "return arr;\n"
    89     )
    90     native static Object[] allocJS(int cnt);
    91     
    92     @JavaScriptBody(
    93         javacall = true,
    94         wait4js = false,
    95         args = {"ret", "model", "propNames", "propReadOnly", "propValues", "propArr", "funcNames", "funcArr"},
    96         body = 
    97           "ret['ko-fx.model'] = model;\n"
    98         + "function koComputed(name, readOnly, value, prop) {\n"
    99         + "  function realGetter() {\n"
   100         + "    try {\n"
   101         + "      var v = prop.@org.apidesign.html.json.spi.PropertyBinding::getValue()();\n"
   102         + "      return v;\n"
   103         + "    } catch (e) {\n"
   104         + "      alert(\"Cannot call getValue on \" + model + \" prop: \" + name + \" error: \" + e);\n"
   105         + "    }\n"
   106         + "  }\n"
   107         + "  var activeGetter = function() { return value; };\n"
   108         + "  var bnd = {\n"
   109         + "    'read': function() {\n"
   110         + "      var r = activeGetter();\n"
   111         + "      activeGetter = realGetter;\n"
   112         + "      if (r) try { var br = r.valueOf(); } catch (err) {}\n"
   113         + "      return br === undefined ? r: br;\n"
   114         + "    },\n"
   115         + "    'owner': ret\n"
   116         + "  };\n"
   117         + "  if (!readOnly) {\n"
   118         + "    bnd['write'] = function(val) {\n"
   119         + "      prop.@org.apidesign.html.json.spi.PropertyBinding::setValue(Ljava/lang/Object;)(val);\n"
   120         + "    };\n"
   121         + "  };\n"
   122         + "  var cmpt = ko['computed'](bnd);\n"
   123         + "  var vhm = cmpt['valueHasMutated'];\n"
   124         + "  cmpt['valueHasMutated'] = function(val) {\n"
   125         + "    if (arguments.length === 1) activeGetter = function() { return val; };\n"
   126         + "    vhm();\n"
   127         + "  };\n"
   128         + "  ret[name] = cmpt;\n"
   129         + "}\n"
   130         + "for (var i = 0; i < propNames.length; i++) {\n"
   131         + "  koComputed(propNames[i], propReadOnly[i], propValues[i], propArr[i]);\n"
   132         + "}\n"
   133         + "function koExpose(name, func) {\n"
   134         + "  ret[name] = function(data, ev) {\n"
   135         + "    func.@org.apidesign.html.json.spi.FunctionBinding::call(Ljava/lang/Object;Ljava/lang/Object;)(data, ev);\n"
   136         + "  };\n"
   137         + "}\n"
   138         + "for (var i = 0; i < funcNames.length; i++) {\n"
   139         + "  koExpose(funcNames[i], funcArr[i]);\n"
   140         + "}\n"
   141         )
   142     static native void wrapModel(
   143         Object ret, Object model,
   144         String[] propNames, boolean[] propReadOnly, Object propValues, PropertyBinding[] propArr,
   145         String[] funcNames, FunctionBinding[] funcArr
   146     );
   147     
   148     @JavaScriptBody(args = { "o" }, body = "return o['ko-fx.model'] ? o['ko-fx.model'] : o;")
   149     private static native Object toModelImpl(Object wrapper);
   150     static Object toModel(Object wrapper) {
   151         return toModelImpl(wrapper);
   152     }
   153     
   154     @JavaScriptBody(args = {}, body = "if (window.WebSocket) return true; else return false;")
   155     static final boolean areWebSocketsSupported() {
   156         return false;
   157     }
   158 }