Search in sources :

Example 6 with NativeFunction

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

the class ResourceLoader method loadDynamicImport.

/**
 * Loads a dynamic import via the provided JS {@code expression} and reports
 * the result via the {@code resourceLoadListener}.
 *
 * @param expression
 *            the JS expression which returns a Promise
 * @param resourceLoadListener
 *            a listener to report the Promise result exection
 */
public void loadDynamicImport(String expression, ResourceLoadListener resourceLoadListener) {
    ResourceLoadEvent event = new ResourceLoadEvent(this, expression);
    NativeFunction function = new NativeFunction(expression);
    runPromiseExpression(expression, () -> function.call(null), () -> resourceLoadListener.onLoad(event), () -> resourceLoadListener.onError(event));
}
Also used : NativeFunction(com.vaadin.client.flow.util.NativeFunction)

Example 7 with NativeFunction

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

the class GwtEventHandlerTest method testClientCallablePromises.

public void testClientCallablePromises() {
    String methodName = "publishedMethod";
    node.getList(NodeFeatures.CLIENT_DELEGATE_HANDLERS).add(0, methodName);
    Binder.bind(node, element);
    Reactive.flush();
    ServerEventObject serverObject = ServerEventObject.get(element);
    NativeFunction publishedMethod = new NativeFunction("return this." + methodName + "()");
    Object promise0 = publishedMethod.apply(serverObject, JsCollections.array());
    assertNotNull(promise0);
    assertEquals(Integer.valueOf(0), serverPromiseIds.get(methodName));
    assertTrue(hasPromise(element, 0));
    Object promise1 = publishedMethod.apply(serverObject, JsCollections.array());
    assertEquals(Integer.valueOf(1), serverPromiseIds.get(methodName));
    assertTrue(hasPromise(element, 1));
    addThen(promise0, value -> {
        assertEquals("promise0", value);
        assertFalse("Promise handlers should be cleared", hasPromise(element, 0));
        completePromise(element, 1, false, null);
    });
    addCatch(promise1, message -> {
        assertEquals("Error: Something went wrong. Check server-side logs for more information.", message);
        assertFalse("Promise handlers should be cleared", hasPromise(element, 1));
        finishTest();
    });
    completePromise(element, 0, true, "promise0");
    delayTestFinish(100);
}
Also used : ServerEventObject(com.vaadin.client.flow.binding.ServerEventObject) NativeFunction(com.vaadin.client.flow.util.NativeFunction) ServerEventObject(com.vaadin.client.flow.binding.ServerEventObject) JsonObject(elemental.json.JsonObject)

Example 8 with NativeFunction

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

the class GwtEventHandlerTest method testPolymerMockedEventHandler.

public void testPolymerMockedEventHandler() {
    String methodName = "eventHandler";
    node.getList(NodeFeatures.POLYMER_SERVER_EVENT_HANDLERS).add(0, methodName);
    Binder.bind(node, element);
    Reactive.flush();
    NativeFunction mockedFunction = new NativeFunction("this." + methodName + "()");
    mockedFunction.apply(element, JsCollections.array());
    assertEquals(1, serverMethods.size());
    assertEquals(methodName, serverMethods.keySet().iterator().next());
    assertEquals(0, serverMethods.get(methodName).length());
    assertEquals(node, serverRpcNodes.get(methodName));
    assertEquals(Integer.valueOf(-1), serverPromiseIds.get(methodName));
}
Also used : NativeFunction(com.vaadin.client.flow.util.NativeFunction)

Example 9 with NativeFunction

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

the class GwtPolymerModelTest method setupMockSpliceMethod.

/**
 * Sets up mock splice method, that is called when model list is modified.
 * For each call, method stores call arguments in the element property named
 * argumentsArray.
 *
 * @param element
 *            html element to set the method to
 */
private void setupMockSpliceMethod(Element element) {
    NativeFunction function = NativeFunction.create("path", "start", "deleteCount", "items", "this.argumentsArray ? this.argumentsArray.push(arguments) : this.argumentsArray = [arguments]");
    WidgetUtil.setJsProperty(element, "splice", function);
}
Also used : NativeFunction(com.vaadin.client.flow.util.NativeFunction)

Example 10 with NativeFunction

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

the class AbstractTemplateStrategy method getModelBindingValue.

/**
 * Gets the value from the {@code node} for the {@code binding}.
 *
 * @param node
 *            the state node, not {@code null}
 * @param binding
 *            binding data, not {@code null}
 * @return map binding value, or an empty optional if no value for the
 *         binding
 */
private static Optional<Object> getModelBindingValue(StateNode node, Binding binding) {
    NodeMap model = node.getMap(NodeFeatures.TEMPLATE_MODELMAP);
    String key = binding.getValue();
    assert key != null;
    if (!node.hasFeature(NodeFeatures.TEMPLATE)) {
        /*
             * This is temporary legacy logic to support *ngFor bindings. JS
             * evaluation should be used in any case. But at the moment JS
             * evaluation doesn't work with *ngFor bindings so they are handled
             * here.
             *
             * TODO: remove this and update JS evaluation to support *ngFor.
             */
        String[] modelPathParts = key.split("\\.");
        // The last part is the propertyName
        for (int i = 0; i < modelPathParts.length - 1; i++) {
            StateNode n = (StateNode) model.getProperty(modelPathParts[i]).getValue();
            model = n.getMap(NodeFeatures.TEMPLATE_MODELMAP);
        }
        key = modelPathParts[modelPathParts.length - 1];
        return Optional.ofNullable(model.getProperty(key).getValue());
    } else {
        String expression = key;
        String modelDescriptorId = (String) node.getMap(NodeFeatures.TEMPLATE).getProperty(NodeProperties.MODEL_DESCRIPTOR).getValue();
        assert modelDescriptorId != null;
        JsonObject modelDescriptor = node.getTree().getRegistry().getConstantPool().get(modelDescriptorId);
        NativeFunction function = new NativeFunction("model", "with(model) { return " + expression + "}");
        BeanModelType type = new BeanModelType(modelDescriptor);
        Object proxy = type.createProxy(model);
        return Optional.ofNullable(function.call(null, proxy));
    }
}
Also used : NativeFunction(com.vaadin.client.flow.util.NativeFunction) StateNode(com.vaadin.client.flow.StateNode) JsonObject(elemental.json.JsonObject) NodeMap(com.vaadin.client.flow.nodefeature.NodeMap) JsonObject(elemental.json.JsonObject) BeanModelType(com.vaadin.client.flow.model.BeanModelType)

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