Search in sources :

Example 51 with NodeMap

use of com.vaadin.client.flow.nodefeature.NodeMap in project flow by vaadin.

the class TreeChangeProcessor method findProperty.

private static MapProperty findProperty(JsonObject change, StateNode node) {
    int nsId = (int) change.getNumber(JsonConstants.CHANGE_FEATURE);
    NodeMap map = node.getMap(nsId);
    String key = change.getString(JsonConstants.CHANGE_MAP_KEY);
    return map.getProperty(key);
}
Also used : NodeMap(com.vaadin.client.flow.nodefeature.NodeMap)

Example 52 with NodeMap

use of com.vaadin.client.flow.nodefeature.NodeMap 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)

Example 53 with NodeMap

use of com.vaadin.client.flow.nodefeature.NodeMap in project flow by vaadin.

the class GwtExecuteJavaScriptElementUtilsTest method testPopulateModelProperties_propertyIsNotDefined_addIntoPropertiesMap.

public void testPopulateModelProperties_propertyIsNotDefined_addIntoPropertiesMap() {
    ExecuteJavaScriptElementUtils.populateModelProperties(node, JsCollections.array("foo"));
    NodeMap map = node.getMap(NodeFeatures.ELEMENT_PROPERTIES);
    assertTrue(map.hasPropertyValue("foo"));
}
Also used : NodeMap(com.vaadin.client.flow.nodefeature.NodeMap)

Example 54 with NodeMap

use of com.vaadin.client.flow.nodefeature.NodeMap in project flow by vaadin.

the class GwtExecuteJavaScriptElementUtilsTest method testPopulateModelProperties_elementIsNotReadyAndPropertyIsDefined_syncToServerWhenElementBecomesReady.

public void testPopulateModelProperties_elementIsNotReadyAndPropertyIsDefined_syncToServerWhenElementBecomesReady() {
    node.setDomNode(null);
    mockWhenDefined(element);
    defineProperty(element, "foo");
    node.setNodeData(new UpdatableModelProperties(JsCollections.array("foo")));
    WidgetUtil.setJsProperty(element, "foo", "bar");
    ExecuteJavaScriptElementUtils.populateModelProperties(node, JsCollections.array("foo"));
    NodeMap map = node.getMap(NodeFeatures.ELEMENT_PROPERTIES);
    assertFalse(map.hasPropertyValue("foo"));
    node.setDomNode(element);
    runWhenDefined(element);
    Reactive.flush();
    assertTrue(map.hasPropertyValue("foo"));
    assertEquals("bar", tree.syncedProperty.getValue());
    assertEquals("foo", tree.syncedProperty.getName());
}
Also used : UpdatableModelProperties(com.vaadin.client.flow.model.UpdatableModelProperties) NodeMap(com.vaadin.client.flow.nodefeature.NodeMap)

Example 55 with NodeMap

use of com.vaadin.client.flow.nodefeature.NodeMap in project flow by vaadin.

the class GwtExecuteJavaScriptElementUtilsTest method setupShadowRoot.

private void setupShadowRoot() {
    setupParent(element);
    NodeMap map = node.getMap(NodeFeatures.SHADOW_ROOT_DATA);
    map.getProperty(NodeProperties.SHADOW_ROOT).setValue(new StateNode(34, tree));
}
Also used : StateNode(com.vaadin.client.flow.StateNode) NodeMap(com.vaadin.client.flow.nodefeature.NodeMap)

Aggregations

NodeMap (com.vaadin.client.flow.nodefeature.NodeMap)68 StateNode (com.vaadin.client.flow.StateNode)30 Element (elemental.dom.Element)21 MapProperty (com.vaadin.client.flow.nodefeature.MapProperty)20 Node (elemental.dom.Node)14 ChildSlotNode (com.vaadin.flow.template.angular.ChildSlotNode)10 ForTemplateNode (com.vaadin.flow.template.angular.ForTemplateNode)10 JsonObject (elemental.json.JsonObject)8 Test (org.junit.Test)6 DomElement (com.vaadin.client.flow.dom.DomElement)5 UpdatableModelProperties (com.vaadin.client.flow.model.UpdatableModelProperties)4 NodeList (com.vaadin.client.flow.nodefeature.NodeList)4 DomNode (com.vaadin.client.flow.dom.DomNode)3 NodeFeature (com.vaadin.client.flow.nodefeature.NodeFeature)3 Computation (com.vaadin.client.flow.reactive.Computation)3 NativeFunction (com.vaadin.client.flow.util.NativeFunction)3 JsonValue (elemental.json.JsonValue)3 Command (com.vaadin.client.Command)2 ConstantPool (com.vaadin.client.flow.ConstantPool)2 JsArray (com.vaadin.client.flow.collection.JsArray)2