use of com.vaadin.client.flow.StateNode in project flow by vaadin.
the class MapPropertyTest method setValue_defaultUpdateValueStrategy_eventIsFiredOnlyOnce.
@Test
public void setValue_defaultUpdateValueStrategy_eventIsFiredOnlyOnce() {
TestTree tree = new TestTree();
StateNode node = new StateNode(13, tree);
MapProperty property = new MapProperty("foo", node.getMap(NodeFeatures.ELEMENT_PROPERTIES), false);
AtomicReference<MapPropertyChangeEvent> capture = new AtomicReference<>();
property.addChangeListener(capture::set);
property.setValue("bar");
Assert.assertNotNull(capture.get());
// reset
capture.set(null);
// set the same value again
property.setValue("bar");
Assert.assertNull(capture.get());
}
use of com.vaadin.client.flow.StateNode in project flow by vaadin.
the class MapProperty method syncToServer.
/**
* Sets the value of this property and synchronizes the value to the server.
*
* @param newValue
* the new value to set.
*/
public void syncToServer(Object newValue) {
Object currentValue = hasValue() ? getValue() : null;
if (Objects.equals(newValue, currentValue)) {
// in case we are here with the same value that has been set from
// the server then we unlock client side updates already here via
// unmarking the server update flag. It allows another client side
// potential change for the same property being propagated to the
// server once the server value is set successfully (e.g. mutation
// the same property from its observer).
isServerUpdate = false;
}
if (!(Objects.equals(newValue, currentValue) && hasValue()) && !isServerUpdate) {
StateNode node = getMap().getNode();
StateTree tree = node.getTree();
if (tree.isActive(node)) {
doSetValue(newValue);
tree.sendNodePropertySyncToServer(this);
} else {
/*
* Fire an fake event to reset the property value back in the
* DOM element: we don't know how exactly set this property but
* it has to be set to the property value because of listener
* added to the property during binding.
*/
eventRouter.fireEvent(new MapPropertyChangeEvent(this, currentValue, currentValue));
// Flush is needed because we are out of normal lifecycle which
// call the flush() automatically.
Reactive.flush();
}
}
}
use of com.vaadin.client.flow.StateNode in project flow by vaadin.
the class GwtTemplateBinderTest method testServerEventHandlerInNgFor.
public void testServerEventHandlerInNgFor() {
TestElementTemplateNode parent = TestElementTemplateNode.create("div");
String operation = "operation";
// ============= create <li *ngFor> ===============================
String collectionVar = "items";
TestForTemplateNode templateNode = TestTemplateNode.create(ForTemplateNode.TYPE);
int templateId = 42;
registry.getTemplateRegistry().register(templateId, templateNode);
TestElementTemplateNode forChild = TestElementTemplateNode.create("li");
templateNode.setCollectionVariable(collectionVar);
forChild.addEventHandler("click", "$server." + operation + "()");
int forChildId = 11;
registry.getTemplateRegistry().register(forChildId, forChild);
templateNode.setChildrenIds(new double[] { forChildId });
parent.setChildrenIds(new double[] { templateId });
NodeMap model = stateNode.getMap(NodeFeatures.TEMPLATE_MODELMAP);
MapProperty property = model.getProperty(collectionVar);
StateNode modelNode = new StateNode(1, tree);
property.setValue(modelNode);
// ================== now modelNode is NG FOR model node ==========
StateNode varNode = new StateNode(2, tree);
com.vaadin.client.flow.nodefeature.NodeList modelList = modelNode.getList(NodeFeatures.TEMPLATE_MODELLIST);
// add one item to the "collection" model
modelList.add(0, varNode);
stateNode.getList(NodeFeatures.CLIENT_DELEGATE_HANDLERS).set(0, operation);
Element element = createElement(parent);
Reactive.flush();
MouseEvent event = (MouseEvent) Browser.getDocument().createEvent(Events.MOUSE);
event.initMouseEvent("click", true, true, Browser.getWindow(), 0, 0, 0, 0, 0, false, false, false, false, 0, element);
Browser.getDocument().getBody().appendChild(element);
element.getElementsByTagName("li").item(0).dispatchEvent(event);
assertEquals(1, serverMethods.size());
assertNotNull(serverMethods.get(operation));
assertEquals(stateNode.getId(), serverRpcNodes.get(operation).getId());
}
use of com.vaadin.client.flow.StateNode in project flow by vaadin.
the class GwtTemplateBinderTest method testChildSlot.
public void testChildSlot() {
TestElementTemplateNode templateNode = TestElementTemplateNode.create("div");
TestTemplateNode childSlot = TestTemplateNode.create(ChildSlotNode.TYPE);
int childId = 67;
registry.getTemplateRegistry().register(childId, childSlot);
templateNode.setChildrenIds(new double[] { childId });
Element element = createElement(templateNode);
Reactive.flush();
assertEquals(1, DomApi.wrap(element).getChildNodes().length());
assertEquals(Node.COMMENT_NODE, DomApi.wrap(element).getChildNodes().get(0).getNodeType());
StateNode childContentNode = new StateNode(79, stateNode.getTree());
childContentNode.getMap(NodeFeatures.ELEMENT_DATA).getProperty(NodeProperties.TAG).setValue("span");
stateNode.getMap(NodeFeatures.TEMPLATE).getProperty(TemplateMap.CHILD_SLOT_CONTENT).setValue(childContentNode);
Reactive.flush();
assertEquals(2, element.getChildNodes().getLength());
assertEquals(Node.COMMENT_NODE, element.getChildNodes().item(0).getNodeType());
assertEquals(Node.ELEMENT_NODE, element.getChildNodes().item(1).getNodeType());
assertEquals("SPAN", DomApi.wrap(element).getLastElementChild().getTagName());
stateNode.getMap(NodeFeatures.TEMPLATE).getProperty(TemplateMap.CHILD_SLOT_CONTENT).setValue(null);
Reactive.flush();
assertEquals(1, element.getChildNodes().getLength());
assertEquals(Node.COMMENT_NODE, element.getChildNodes().item(0).getNodeType());
}
use of com.vaadin.client.flow.StateNode in project flow by vaadin.
the class GwtTemplateBinderTest method testNgFor_unregister_noUpdates.
public void testNgFor_unregister_noUpdates() {
TestElementTemplateNode parent = TestElementTemplateNode.create("div");
String textVar = "text";
// create 3 children for the parent: <div/><li
// *ngFor>{{text}}</li><span/>
StateNode modelNode = createNgForModelNode(parent, "div", "li", "span", "items", textVar);
StateNode varNode = new StateNode(2, tree);
varNode.getMap(NodeFeatures.TEMPLATE_MODELMAP).getProperty(textVar).setValue("foo");
com.vaadin.client.flow.nodefeature.NodeList modelList = modelNode.getList(NodeFeatures.TEMPLATE_MODELLIST);
modelList.add(0, varNode);
Element element = createElement(parent);
Reactive.flush();
String htmlBeforeUregister = element.getOuterHTML();
stateNode.unregister();
Reactive.flush();
StateNode varNode1 = new StateNode(3, tree);
varNode1.getMap(NodeFeatures.TEMPLATE_MODELMAP).getProperty(textVar).setValue("bar");
modelList.add(1, varNode);
Reactive.flush();
assertEquals(htmlBeforeUregister, element.getOuterHTML());
}
Aggregations