Search in sources :

Example 1 with JsSet

use of com.vaadin.client.flow.collection.JsSet in project flow by vaadin.

the class SimpleElementBindingStrategy method handleDomEvent.

private void handleDomEvent(Event event, BindingContext context) {
    assert context != null;
    Node element = context.htmlNode;
    StateNode node = context.node;
    assert element instanceof Element : "Cannot handle DOM event for a Node";
    String type = event.getType();
    NodeMap listenerMap = getDomEventListenerMap(node);
    ConstantPool constantPool = node.getTree().getRegistry().getConstantPool();
    String expressionConstantKey = (String) listenerMap.getProperty(type).getValue();
    assert expressionConstantKey != null;
    assert constantPool.has(expressionConstantKey);
    JsonObject expressionSettings = constantPool.get(expressionConstantKey);
    String[] expressions = expressionSettings.keys();
    JsonObject eventData;
    JsSet<String> synchronizeProperties = JsCollections.set();
    if (expressions.length == 0) {
        eventData = null;
    } else {
        eventData = Json.createObject();
    }
    for (String expressionString : expressions) {
        if (expressionString.startsWith(JsonConstants.SYNCHRONIZE_PROPERTY_TOKEN)) {
            String property = expressionString.substring(JsonConstants.SYNCHRONIZE_PROPERTY_TOKEN.length());
            synchronizeProperties.add(property);
        } else if (expressionString.equals(JsonConstants.MAP_STATE_NODE_EVENT_DATA)) {
            // map event.target to the closest state node
            int targetNodeId = getClosestStateNodeIdToEventTarget(node, event.getTarget());
            eventData.put(JsonConstants.MAP_STATE_NODE_EVENT_DATA, targetNodeId);
        } else if (expressionString.startsWith(JsonConstants.MAP_STATE_NODE_EVENT_DATA)) {
            // map element returned by JS to the closest state node
            String jsEvaluation = expressionString.substring(JsonConstants.MAP_STATE_NODE_EVENT_DATA.length());
            EventExpression expression = getOrCreateExpression(jsEvaluation);
            JsonValue expressionValue = expression.evaluate(event, (Element) element);
            // find the closest state node matching the expression value
            int targetNodeId = getClosestStateNodeIdToDomNode(node.getTree(), expressionValue, jsEvaluation);
            eventData.put(expressionString, targetNodeId);
        } else {
            EventExpression expression = getOrCreateExpression(expressionString);
            JsonValue expressionValue = expression.evaluate(event, (Element) element);
            eventData.put(expressionString, expressionValue);
        }
    }
    JsArray<Runnable> commands = JsCollections.array();
    synchronizeProperties.forEach(name -> commands.push(getSyncPropertyCommand(name, context)));
    Consumer<String> sendCommand = debouncePhase -> {
        commands.forEach(Runnable::run);
        sendEventToServer(node, type, eventData, debouncePhase);
    };
    boolean sendNow = resolveFilters(element, type, expressionSettings, eventData, sendCommand);
    if (sendNow) {
        // Send if there were not filters or at least one matched
        sendCommand.accept(null);
    }
}
Also used : Json(elemental.json.Json) CSSStyleDeclaration(elemental.css.CSSStyleDeclaration) JsonArray(elemental.json.JsonArray) ListSpliceEvent(com.vaadin.client.flow.nodefeature.ListSpliceEvent) MapProperty(com.vaadin.client.flow.nodefeature.MapProperty) ForEachCallback(com.vaadin.client.flow.collection.JsMap.ForEachCallback) JsonValue(elemental.json.JsonValue) UpdatableModelProperties(com.vaadin.client.flow.model.UpdatableModelProperties) Event(elemental.events.Event) StateTree(com.vaadin.client.flow.StateTree) JsonType(elemental.json.JsonType) DomNode(com.vaadin.client.flow.dom.DomNode) NodeMap(com.vaadin.client.flow.nodefeature.NodeMap) JsMap(com.vaadin.client.flow.collection.JsMap) NodeList(com.vaadin.client.flow.nodefeature.NodeList) JsSet(com.vaadin.client.flow.collection.JsSet) ExistingElementMap(com.vaadin.client.ExistingElementMap) DomTokenList(com.vaadin.client.flow.dom.DomElement.DomTokenList) JsArray(com.vaadin.client.flow.collection.JsArray) Console(com.vaadin.client.Console) ApplicationConfiguration(com.vaadin.client.ApplicationConfiguration) EventTarget(elemental.events.EventTarget) Browser(elemental.client.Browser) Computation(com.vaadin.client.flow.reactive.Computation) JsWeakMap(com.vaadin.client.flow.collection.JsWeakMap) Reactive(com.vaadin.client.flow.reactive.Reactive) LitUtils(com.vaadin.client.LitUtils) DomElement(com.vaadin.client.flow.dom.DomElement) PolymerUtils(com.vaadin.client.PolymerUtils) NodeProperties(com.vaadin.flow.internal.nodefeature.NodeProperties) StateNode(com.vaadin.client.flow.StateNode) ElementUtil(com.vaadin.client.ElementUtil) Supplier(java.util.function.Supplier) NativeFunction(com.vaadin.client.flow.util.NativeFunction) WidgetUtil(com.vaadin.client.WidgetUtil) JsFunction(jsinterop.annotations.JsFunction) Node(elemental.dom.Node) Command(com.vaadin.client.Command) Element(elemental.dom.Element) JsonConstants(com.vaadin.flow.shared.JsonConstants) JavaScriptObject(com.google.gwt.core.client.JavaScriptObject) DomApi(com.vaadin.client.flow.dom.DomApi) NodeFeatures(com.vaadin.flow.internal.nodefeature.NodeFeatures) Consumer(java.util.function.Consumer) Scheduler(com.google.gwt.core.client.Scheduler) ConstantPool(com.vaadin.client.flow.ConstantPool) InitialPropertiesHandler(com.vaadin.client.InitialPropertiesHandler) EventRemover(elemental.events.EventRemover) JsCollections(com.vaadin.client.flow.collection.JsCollections) JsonObject(elemental.json.JsonObject) DomNode(com.vaadin.client.flow.dom.DomNode) StateNode(com.vaadin.client.flow.StateNode) Node(elemental.dom.Node) DomElement(com.vaadin.client.flow.dom.DomElement) Element(elemental.dom.Element) StateNode(com.vaadin.client.flow.StateNode) JsonValue(elemental.json.JsonValue) JsonObject(elemental.json.JsonObject) ConstantPool(com.vaadin.client.flow.ConstantPool) NodeMap(com.vaadin.client.flow.nodefeature.NodeMap)

Aggregations

JavaScriptObject (com.google.gwt.core.client.JavaScriptObject)1 Scheduler (com.google.gwt.core.client.Scheduler)1 ApplicationConfiguration (com.vaadin.client.ApplicationConfiguration)1 Command (com.vaadin.client.Command)1 Console (com.vaadin.client.Console)1 ElementUtil (com.vaadin.client.ElementUtil)1 ExistingElementMap (com.vaadin.client.ExistingElementMap)1 InitialPropertiesHandler (com.vaadin.client.InitialPropertiesHandler)1 LitUtils (com.vaadin.client.LitUtils)1 PolymerUtils (com.vaadin.client.PolymerUtils)1 WidgetUtil (com.vaadin.client.WidgetUtil)1 ConstantPool (com.vaadin.client.flow.ConstantPool)1 StateNode (com.vaadin.client.flow.StateNode)1 StateTree (com.vaadin.client.flow.StateTree)1 JsArray (com.vaadin.client.flow.collection.JsArray)1 JsCollections (com.vaadin.client.flow.collection.JsCollections)1 JsMap (com.vaadin.client.flow.collection.JsMap)1 ForEachCallback (com.vaadin.client.flow.collection.JsMap.ForEachCallback)1 JsSet (com.vaadin.client.flow.collection.JsSet)1 JsWeakMap (com.vaadin.client.flow.collection.JsWeakMap)1