Search in sources :

Example 1 with DataKeyMapper

use of com.vaadin.flow.data.provider.DataKeyMapper in project flow-components by vaadin.

the class LitRenderer method createJsRendererFunction.

private Registration createJsRendererFunction(Element container, DataKeyMapper<SOURCE> keyMapper, String rendererName) {
    ReturnChannelRegistration returnChannel = container.getNode().getFeature(ReturnChannelMap.class).registerChannel(arguments -> {
        // Invoked when the client calls one of the client callables
        String handlerName = arguments.getString(0);
        String itemKey = arguments.getString(1);
        JsonArray args = arguments.getArray(2);
        SerializableBiConsumer<SOURCE, JsonArray> handler = clientCallables.get(handlerName);
        SOURCE item = keyMapper.get(itemKey);
        handler.accept(item, args);
    });
    JsonArray clientCallablesArray = JsonUtils.listToJson(new ArrayList<>(clientCallables.keySet()));
    List<Registration> registrations = new ArrayList<>();
    // Since the renderer is set manually on the client-side, an attach
    // listener for the host component is required so that the renderer gets
    // applied even when the host component gets a new Web Component
    // instance (for example on detach + reattach).
    // 
    // The attach listener needs to be released when the Renderer instance
    // is no longer used so the registration is cleared by the renderer
    // registration.
    registrations.add(container.addAttachListener(e -> {
        setElementRenderer(container, rendererName, templateExpression, returnChannel, clientCallablesArray, propertyNamespace);
    }));
    // Call once initially
    setElementRenderer(container, rendererName, templateExpression, returnChannel, clientCallablesArray, propertyNamespace);
    // Get the renderer function cleared when the LitRenderer is
    // unregistered
    registrations.add(() -> container.executeJs("window.Vaadin.unsetLitRenderer(this, $0, $1)", rendererName, propertyNamespace));
    return () -> registrations.forEach(Registration::remove);
}
Also used : JsonArray(elemental.json.JsonArray) SerializableBiConsumer(com.vaadin.flow.function.SerializableBiConsumer) ValueProvider(com.vaadin.flow.function.ValueProvider) Registration(com.vaadin.flow.shared.Registration) ReturnChannelRegistration(com.vaadin.flow.internal.nodefeature.ReturnChannelRegistration) JsonArray(elemental.json.JsonArray) HashMap(java.util.HashMap) SerializableConsumer(com.vaadin.flow.function.SerializableConsumer) ArrayList(java.util.ArrayList) Objects(java.util.Objects) DataKeyMapper(com.vaadin.flow.data.provider.DataKeyMapper) ReturnChannelMap(com.vaadin.flow.internal.nodefeature.ReturnChannelMap) List(java.util.List) JsonUtils(com.vaadin.flow.internal.JsonUtils) JsonSerializer(com.vaadin.flow.internal.JsonSerializer) Map(java.util.Map) Element(com.vaadin.flow.dom.Element) Optional(java.util.Optional) Pattern(java.util.regex.Pattern) UI(com.vaadin.flow.component.UI) JsModule(com.vaadin.flow.component.dependency.JsModule) DataGenerator(com.vaadin.flow.data.provider.DataGenerator) ReturnChannelRegistration(com.vaadin.flow.internal.nodefeature.ReturnChannelRegistration) Registration(com.vaadin.flow.shared.Registration) ReturnChannelRegistration(com.vaadin.flow.internal.nodefeature.ReturnChannelRegistration) ArrayList(java.util.ArrayList) ReturnChannelMap(com.vaadin.flow.internal.nodefeature.ReturnChannelMap)

Aggregations

UI (com.vaadin.flow.component.UI)1 JsModule (com.vaadin.flow.component.dependency.JsModule)1 DataGenerator (com.vaadin.flow.data.provider.DataGenerator)1 DataKeyMapper (com.vaadin.flow.data.provider.DataKeyMapper)1 Element (com.vaadin.flow.dom.Element)1 SerializableBiConsumer (com.vaadin.flow.function.SerializableBiConsumer)1 SerializableConsumer (com.vaadin.flow.function.SerializableConsumer)1 ValueProvider (com.vaadin.flow.function.ValueProvider)1 JsonSerializer (com.vaadin.flow.internal.JsonSerializer)1 JsonUtils (com.vaadin.flow.internal.JsonUtils)1 ReturnChannelMap (com.vaadin.flow.internal.nodefeature.ReturnChannelMap)1 ReturnChannelRegistration (com.vaadin.flow.internal.nodefeature.ReturnChannelRegistration)1 Registration (com.vaadin.flow.shared.Registration)1 JsonArray (elemental.json.JsonArray)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Objects (java.util.Objects)1 Optional (java.util.Optional)1