Search in sources :

Example 61 with StateNode

use of com.vaadin.client.flow.StateNode in project flow by vaadin.

the class InitialPropertiesHandler method collectInitialProperties.

private void collectInitialProperties(Double id, JsMap<Double, JsMap<String, Object>> properties) {
    StateNode node = getRegistry().getStateTree().getNode(id.intValue());
    if (node.hasFeature(NodeFeatures.ELEMENT_PROPERTIES)) {
        JsMap<String, Object> map = JsCollections.map();
        node.getMap(NodeFeatures.ELEMENT_PROPERTIES).forEachProperty((property, name) -> map.set(name, property.getValue()));
        properties.set(id, map);
    }
}
Also used : StateNode(com.vaadin.client.flow.StateNode)

Example 62 with StateNode

use of com.vaadin.client.flow.StateNode in project flow by vaadin.

the class PolymerUtils method doHandlePropertyChange.

private static void doHandlePropertyChange(MapProperty property, JsonValue value) {
    String propertyName = property.getName();
    StateNode node = property.getMap().getNode();
    StateNode root = getFirstParentWithDomNode(node);
    if (root == null) {
        Console.warn("Root node for node " + node.getId() + " could not be found");
        return;
    }
    JsonValue modelTree = createModelTree(property.getValue());
    if (isPolymerElement((Element) root.getDomNode())) {
        String path = getNotificationPath(root, node, propertyName);
        if (path != null) {
            setProperty((Element) root.getDomNode(), path, modelTree);
        }
        return;
    }
    WidgetUtil.setJsProperty(value, propertyName, modelTree);
}
Also used : StateNode(com.vaadin.client.flow.StateNode) JsonValue(elemental.json.JsonValue)

Example 63 with StateNode

use of com.vaadin.client.flow.StateNode in project flow by vaadin.

the class PolymerUtils method createModelTree.

/**
 * Makes an attempt to convert an object into json.
 *
 * @param object
 *            the object to convert to json
 * @return json from object, {@code null} for null
 */
public static JsonValue createModelTree(Object object) {
    if (object instanceof StateNode) {
        StateNode node = (StateNode) object;
        NodeFeature feature = null;
        if (node.hasFeature(NodeFeatures.ELEMENT_PROPERTIES)) {
            feature = node.getMap(NodeFeatures.ELEMENT_PROPERTIES);
        } else if (node.hasFeature(NodeFeatures.TEMPLATE_MODELLIST)) {
            feature = node.getList(NodeFeatures.TEMPLATE_MODELLIST);
        } else if (node.hasFeature(NodeFeatures.BASIC_TYPE_VALUE)) {
            return createModelTree(node.getMap(NodeFeatures.BASIC_TYPE_VALUE).getProperty(NodeProperties.VALUE));
        }
        assert feature != null : "Don't know how to convert node without map or list features";
        JsonValue convert = feature.convert(PolymerUtils::createModelTree);
        if (convert instanceof JsonObject && !((JsonObject) convert).hasKey("nodeId")) {
            ((JsonObject) convert).put("nodeId", node.getId());
            registerChangeHandlers(node, feature, convert);
        }
        return convert;
    } else if (object instanceof MapProperty) {
        MapProperty property = (MapProperty) object;
        if (property.getMap().getId() == NodeFeatures.BASIC_TYPE_VALUE) {
            return createModelTree(property.getValue());
        } else {
            JsonObject convertedObject = Json.createObject();
            convertedObject.put(property.getName(), createModelTree(property.getValue()));
            return convertedObject;
        }
    } else {
        return WidgetUtil.crazyJsoCast(object);
    }
}
Also used : NodeFeature(com.vaadin.client.flow.nodefeature.NodeFeature) MapProperty(com.vaadin.client.flow.nodefeature.MapProperty) StateNode(com.vaadin.client.flow.StateNode) JsonValue(elemental.json.JsonValue) JsonObject(elemental.json.JsonObject)

Example 64 with StateNode

use of com.vaadin.client.flow.StateNode in project flow by vaadin.

the class SimpleElementBindingStrategy method handleChildrenSplice.

private void handleChildrenSplice(ListSpliceEvent event, BindingContext context) {
    Node htmlNode = context.htmlNode;
    if (event.isClear()) {
        /*
             * When a full clear event is fired, all nodes must be removed,
             * including the nodes the server doesn't know about.
             */
        removeAllChildren(htmlNode);
    } else {
        JsArray<?> remove = event.getRemove();
        for (int i = 0; i < remove.length(); i++) {
            StateNode childNode = (StateNode) remove.get(i);
            Node child = childNode.getDomNode();
            assert child != null : "Can't find element to remove";
            if (DomApi.wrap(child).getParentNode() == htmlNode) {
                DomApi.wrap(htmlNode).removeChild(child);
            }
        /*
                 * If the client-side element is not inside the parent the
                 * server thought it should be (because of client-side-only DOM
                 * changes), nothing is done at this point. If the server
                 * appends the element to a new parent, that will override the
                 * client DOM in the code below.
                 */
        }
    }
    JsArray<?> add = event.getAdd();
    if (!add.isEmpty()) {
        addChildren(event.getIndex(), context, add);
    }
}
Also used : DomNode(com.vaadin.client.flow.dom.DomNode) StateNode(com.vaadin.client.flow.StateNode) Node(elemental.dom.Node) StateNode(com.vaadin.client.flow.StateNode)

Example 65 with StateNode

use of com.vaadin.client.flow.StateNode 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

StateNode (com.vaadin.client.flow.StateNode)73 NodeMap (com.vaadin.client.flow.nodefeature.NodeMap)21 Element (elemental.dom.Element)21 Test (org.junit.Test)16 MapProperty (com.vaadin.client.flow.nodefeature.MapProperty)14 Node (elemental.dom.Node)13 JsonObject (elemental.json.JsonObject)11 StateTree (com.vaadin.client.flow.StateTree)9 DomNode (com.vaadin.client.flow.dom.DomNode)9 NodeList (com.vaadin.client.flow.nodefeature.NodeList)7 JsonValue (elemental.json.JsonValue)5 ExistingElementMap (com.vaadin.client.ExistingElementMap)4 JsArray (com.vaadin.client.flow.collection.JsArray)4 NativeFunction (com.vaadin.client.flow.util.NativeFunction)4 JsonArray (elemental.json.JsonArray)4 AtomicReference (java.util.concurrent.atomic.AtomicReference)4 NodeFeature (com.vaadin.client.flow.nodefeature.NodeFeature)3 JavaScriptObject (com.google.gwt.core.client.JavaScriptObject)2 Command (com.vaadin.client.Command)2 InitialPropertiesHandler (com.vaadin.client.InitialPropertiesHandler)2