Search in sources :

Example 1 with NativeFunction

use of com.vaadin.client.flow.util.NativeFunction in project flow by vaadin.

the class SimpleElementBindingStrategy method setSubProperties.

private void setSubProperties(Element htmlNode, MapProperty property, String path) {
    String newPath = path.isEmpty() ? property.getName() : path + "." + property.getName();
    NativeFunction setValueFunction = NativeFunction.create("path", "value", "this.set(path, value)");
    if (property.getValue() instanceof StateNode) {
        StateNode subNode = (StateNode) property.getValue();
        if (subNode.hasFeature(NodeFeatures.TEMPLATE_MODELLIST)) {
            setValueFunction.call(htmlNode, newPath, PolymerUtils.convertToJson(subNode));
            addModelListChangeListener(htmlNode, subNode.getList(NodeFeatures.TEMPLATE_MODELLIST), newPath);
        } else {
            NativeFunction function = NativeFunction.create("path", "value", "this.set(path, {})");
            function.call(htmlNode, newPath);
            bindModelProperties(subNode, htmlNode, newPath);
        }
    } else {
        setValueFunction.call(htmlNode, newPath, property.getValue());
    }
}
Also used : NativeFunction(com.vaadin.client.flow.util.NativeFunction) StateNode(com.vaadin.client.flow.StateNode)

Example 2 with NativeFunction

use of com.vaadin.client.flow.util.NativeFunction in project flow by vaadin.

the class GwtMultipleBindingTest method testBindModelPropertiesDoubleBind.

public void testBindModelPropertiesDoubleBind() {
    String name = "custom-div";
    Element element = Browser.getDocument().createElement(name);
    WidgetUtil.setJsProperty(element, "localName", name);
    initPolymer(element);
    NativeFunction function = NativeFunction.create("");
    WidgetUtil.setJsProperty(element, "set", function);
    Binder.bind(node, element);
    node.getMap(NodeFeatures.ELEMENT_PROPERTIES).getProperty("foo").setValue("bar");
    Reactive.flush();
    node.setBound();
    Binder.bind(node, element);
}
Also used : Element(elemental.dom.Element) NativeFunction(com.vaadin.client.flow.util.NativeFunction)

Example 3 with NativeFunction

use of com.vaadin.client.flow.util.NativeFunction in project flow by vaadin.

the class SimpleElementBindingStrategy method attachShadow.

private void attachShadow(BindingContext context) {
    NodeMap map = context.node.getMap(NodeFeatures.SHADOW_ROOT_DATA);
    StateNode shadowRootNode = (StateNode) map.getProperty(NodeProperties.SHADOW_ROOT).getValue();
    if (shadowRootNode != null) {
        NativeFunction function = NativeFunction.create("element", "if ( element.shadowRoot ) { return element.shadowRoot; } " + "else { return element.attachShadow({'mode' : 'open'});}");
        Node shadowRoot = (Node) function.call(null, context.htmlNode);
        if (shadowRootNode.getDomNode() == null) {
            shadowRootNode.setDomNode(shadowRoot);
        }
        BindingContext newContext = new BindingContext(shadowRootNode, shadowRoot, context.binderContext);
        bindChildren(newContext);
    }
}
Also used : NativeFunction(com.vaadin.client.flow.util.NativeFunction) DomNode(com.vaadin.client.flow.dom.DomNode) StateNode(com.vaadin.client.flow.StateNode) Node(elemental.dom.Node) StateNode(com.vaadin.client.flow.StateNode) NodeMap(com.vaadin.client.flow.nodefeature.NodeMap)

Example 4 with NativeFunction

use of com.vaadin.client.flow.util.NativeFunction in project flow by vaadin.

the class ExecuteJavaScriptProcessor method invoke.

/**
 * Executes the actual invocation. This method is protected instead of
 * private for testing purposes.
 *
 * @param parameterNamesAndCode
 *            an array consisting of parameter names followed by the
 *            JavaScript expression to execute
 * @param parameters
 *            an array of parameter values
 * @param nodeParameters
 *            the node parameters
 */
protected void invoke(String[] parameterNamesAndCode, JsArray<Object> parameters, JsMap<Object, StateNode> nodeParameters) {
    assert parameterNamesAndCode.length == parameters.length() + 1;
    try {
        NativeFunction function = new NativeFunction(parameterNamesAndCode);
        function.apply(getContextExecutionObject(nodeParameters, () -> {
            if (!registry.getUILifecycle().isTerminated()) {
                registry.getUILifecycle().setState(UIState.TERMINATED);
            }
        }), parameters);
    } catch (Exception exception) {
        Console.reportStacktrace(exception);
        Console.error("Exception is thrown during JavaScript execution. Stacktrace will be dumped separately.");
        if (!registry.getApplicationConfiguration().isProductionMode()) {
            StringBuilder codeBuilder = new StringBuilder("[");
            String delimiter = "";
            for (String snippet : parameterNamesAndCode) {
                codeBuilder.append(delimiter).append(snippet);
                delimiter = ", ";
            }
            codeBuilder.append("]");
            String code = codeBuilder.toString();
            if (code.charAt(0) == '[') {
                code = code.substring(1);
            }
            if (code.charAt(code.length() - 1) == ']') {
                code = code.substring(0, code.length() - 1);
            }
            Console.error("The error has occurred in the JS code: '" + code + "'");
        }
    }
}
Also used : NativeFunction(com.vaadin.client.flow.util.NativeFunction)

Example 5 with NativeFunction

use of com.vaadin.client.flow.util.NativeFunction in project flow by vaadin.

the class ApplicationConnection method addDomSetListener.

private void addDomSetListener(int nodeId, JavaScriptObject callback) {
    registry.getStateTree().getNode(nodeId).addDomNodeSetListener(node -> {
        if (nodeId == node.getId()) {
            NativeFunction function = NativeFunction.create("callback", "callback();");
            function.call(null, callback);
            return true;
        }
        return false;
    });
}
Also used : NativeFunction(com.vaadin.client.flow.util.NativeFunction)

Aggregations

NativeFunction (com.vaadin.client.flow.util.NativeFunction)19 JsonObject (elemental.json.JsonObject)6 StateNode (com.vaadin.client.flow.StateNode)3 JsonArray (elemental.json.JsonArray)3 NodeMap (com.vaadin.client.flow.nodefeature.NodeMap)2 Element (elemental.dom.Element)2 ServerEventObject (com.vaadin.client.flow.binding.ServerEventObject)1 DomNode (com.vaadin.client.flow.dom.DomNode)1 BeanModelType (com.vaadin.client.flow.model.BeanModelType)1 Node (elemental.dom.Node)1