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));
}
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);
}
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));
}
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);
}
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));
}
}
Aggregations