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));
});
}
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();
}
}
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());
}
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()));
}
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");
}
Aggregations