Search in sources :

Example 1 with ExistingElementMap

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

the class SimpleElementBindingStrategy method bindChildren.

private EventRemover bindChildren(BindingContext context) {
    NodeList children = context.node.getList(NodeFeatures.ELEMENT_CHILDREN);
    for (int i = 0; i < children.length(); i++) {
        StateNode childNode = (StateNode) children.get(i);
        ExistingElementMap existingElementMap = childNode.getTree().getRegistry().getExistingElementMap();
        Node child = existingElementMap.getElement(childNode.getId());
        if (child != null) {
            existingElementMap.remove(childNode.getId());
            childNode.setDomNode(child);
            context.binderContext.createAndBind(childNode);
        } else {
            child = context.binderContext.createAndBind(childNode);
            DomApi.wrap(context.htmlNode).appendChild(child);
        }
    }
    return children.addSpliceListener(e -> {
        /*
             * Handle lazily so we can create the children we need to insert.
             * The change that gives a child node an element tag name might not
             * yet have been applied at this point.
             */
        Reactive.addFlushListener(() -> handleChildrenSplice(e, context));
    });
}
Also used : NodeList(com.vaadin.client.flow.nodefeature.NodeList) StateNode(com.vaadin.client.flow.StateNode) Node(elemental.dom.Node) StateNode(com.vaadin.client.flow.StateNode) ExistingElementMap(com.vaadin.client.ExistingElementMap)

Example 2 with ExistingElementMap

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

the class SimpleElementBindingStrategy method addChildren.

private void addChildren(int index, BindingContext context, JsArray<?> add) {
    NodeList nodeChildren = context.node.getList(NodeFeatures.ELEMENT_CHILDREN);
    Node beforeRef;
    if (index == 0) {
        // Insert at the first position
        beforeRef = DomApi.wrap(context.htmlNode).getFirstChild();
    } else if (index <= nodeChildren.length() && index > 0) {
        StateNode previousSibling = getPreviousSibling(index, context);
        // Insert before the next sibling of the current node
        beforeRef = previousSibling == null ? null : DomApi.wrap(previousSibling.getDomNode()).getNextSibling();
    } else {
        // Insert at the end
        beforeRef = null;
    }
    for (int i = 0; i < add.length(); i++) {
        Object newChildObject = add.get(i);
        StateNode newChild = (StateNode) newChildObject;
        ExistingElementMap existingElementMap = newChild.getTree().getRegistry().getExistingElementMap();
        Node childNode = existingElementMap.getElement(newChild.getId());
        if (childNode != null) {
            existingElementMap.remove(newChild.getId());
            newChild.setDomNode(childNode);
            context.binderContext.createAndBind(newChild);
        } else {
            childNode = context.binderContext.createAndBind(newChild);
            DomApi.wrap(context.htmlNode).insertBefore(childNode, beforeRef);
        }
        beforeRef = DomApi.wrap(childNode).getNextSibling();
    }
}
Also used : NodeList(com.vaadin.client.flow.nodefeature.NodeList) StateNode(com.vaadin.client.flow.StateNode) Node(elemental.dom.Node) StateNode(com.vaadin.client.flow.StateNode) ExistingElementMap(com.vaadin.client.ExistingElementMap) JavaScriptObject(com.google.gwt.core.client.JavaScriptObject) JsonObject(elemental.json.JsonObject)

Example 3 with ExistingElementMap

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

the class GwtBasicElementBinderTest method testAddTemplateChild.

public void testAddTemplateChild() {
    final int templateId = 43;
    TestElementTemplateNode templateNode = TestElementTemplateNode.create("child");
    TemplateRegistry templates = new TemplateRegistry();
    templates.register(templateId, templateNode);
    Registry registry = new Registry() {

        {
            set(TemplateRegistry.class, templates);
            set(ExistingElementMap.class, new ExistingElementMap());
        }
    };
    StateTree stateTree = new StateTree(registry);
    StateNode templateStateNode = new StateNode(345, stateTree);
    templateStateNode.getMap(NodeFeatures.TEMPLATE).getProperty(NodeProperties.ROOT_TEMPLATE_ID).setValue(Double.valueOf(templateId));
    StateNode parentElementNode = new StateNode(94, stateTree);
    parentElementNode.getMap(NodeFeatures.ELEMENT_DATA).getProperty(NodeProperties.TAG).setValue("div");
    parentElementNode.getList(NodeFeatures.ELEMENT_CHILDREN).add(0, templateStateNode);
    Element element = Browser.getDocument().createElement("div");
    Binder.bind(parentElementNode, element);
    Reactive.flush();
    assertEquals(1, element.getChildElementCount());
    assertEquals("CHILD", element.getFirstElementChild().getTagName());
}
Also used : TemplateRegistry(com.vaadin.client.flow.template.TemplateRegistry) TestElementTemplateNode(com.vaadin.client.flow.template.TestElementTemplateNode) Element(elemental.dom.Element) ExistingElementMap(com.vaadin.client.ExistingElementMap) Registry(com.vaadin.client.Registry) TemplateRegistry(com.vaadin.client.flow.template.TemplateRegistry)

Example 4 with ExistingElementMap

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

the class GwtBasicElementBinderTest method testAttachExistingElement.

public void testAttachExistingElement() {
    Binder.bind(node, element);
    StateNode childNode = createChildNode("child");
    String tag = (String) childNode.getMap(NodeFeatures.ELEMENT_DATA).getProperty(NodeProperties.TAG).getValue();
    ExistingElementMap existingElementMap = node.getTree().getRegistry().getExistingElementMap();
    // create and add an existing element
    Element span = Browser.getDocument().createElement(tag);
    element.appendChild(span);
    existingElementMap.add(childNode.getId(), span);
    children.add(0, childNode);
    Reactive.flush();
    // nothing has changed: no new child
    assertEquals("No new child should appear in the element", 1, element.getChildElementCount());
    Element childElement = element.getFirstElementChild();
    assertEquals(tag, childElement.getTagName().toLowerCase(Locale.ENGLISH));
    assertSame(span, childElement);
    assertEquals("child", childElement.getId());
    assertNull(existingElementMap.getElement(childNode.getId()));
}
Also used : Element(elemental.dom.Element) ExistingElementMap(com.vaadin.client.ExistingElementMap)

Example 5 with ExistingElementMap

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

the class GwtMultipleBindingTest method gwtSetUp.

@Override
protected void gwtSetUp() throws Exception {
    super.gwtSetUp();
    Reactive.reset();
    registry = new Registry() {

        {
            set(ConstantPool.class, new ConstantPool());
            set(ExistingElementMap.class, new ExistingElementMap());
        }
    };
    tree = new StateTree(registry) {

        @Override
        public void sendTemplateEventToServer(StateNode node, String methodName, JsArray<?> argValues) {
        }
    };
    node = new TestStateNode(tree);
    // populate "element data" feature to be able to bind node as a plain
    // element
    node.getMap(NodeFeatures.ELEMENT_DATA);
    element = Browser.getDocument().createElement("div");
}
Also used : ExistingElementMap(com.vaadin.client.ExistingElementMap) Registry(com.vaadin.client.Registry)

Aggregations

ExistingElementMap (com.vaadin.client.ExistingElementMap)7 Registry (com.vaadin.client.Registry)4 StateNode (com.vaadin.client.flow.StateNode)3 JsonObject (elemental.json.JsonObject)3 NodeList (com.vaadin.client.flow.nodefeature.NodeList)2 Element (elemental.dom.Element)2 Node (elemental.dom.Node)2 JavaScriptObject (com.google.gwt.core.client.JavaScriptObject)1 InitialPropertiesHandler (com.vaadin.client.InitialPropertiesHandler)1 ConstantPool (com.vaadin.client.flow.ConstantPool)1 StateTree (com.vaadin.client.flow.StateTree)1 JsArray (com.vaadin.client.flow.collection.JsArray)1 TemplateRegistry (com.vaadin.client.flow.template.TemplateRegistry)1 TestElementTemplateNode (com.vaadin.client.flow.template.TestElementTemplateNode)1 JsElement (elemental.js.dom.JsElement)1 JsonArray (elemental.json.JsonArray)1 Test (org.junit.Test)1