use of com.vaadin.client.flow.StateNode in project flow by vaadin.
the class AbstractTemplateStrategy method getModelBindingValue.
/**
* Gets the value from the {@code node} for the {@code binding}.
*
* @param node
* the state node, not {@code null}
* @param binding
* binding data, not {@code null}
* @return map binding value, or an empty optional if no value for the
* binding
*/
private static Optional<Object> getModelBindingValue(StateNode node, Binding binding) {
NodeMap model = node.getMap(NodeFeatures.TEMPLATE_MODELMAP);
String key = binding.getValue();
assert key != null;
if (!node.hasFeature(NodeFeatures.TEMPLATE)) {
/*
* This is temporary legacy logic to support *ngFor bindings. JS
* evaluation should be used in any case. But at the moment JS
* evaluation doesn't work with *ngFor bindings so they are handled
* here.
*
* TODO: remove this and update JS evaluation to support *ngFor.
*/
String[] modelPathParts = key.split("\\.");
// The last part is the propertyName
for (int i = 0; i < modelPathParts.length - 1; i++) {
StateNode n = (StateNode) model.getProperty(modelPathParts[i]).getValue();
model = n.getMap(NodeFeatures.TEMPLATE_MODELMAP);
}
key = modelPathParts[modelPathParts.length - 1];
return Optional.ofNullable(model.getProperty(key).getValue());
} else {
String expression = key;
String modelDescriptorId = (String) node.getMap(NodeFeatures.TEMPLATE).getProperty(NodeProperties.MODEL_DESCRIPTOR).getValue();
assert modelDescriptorId != null;
JsonObject modelDescriptor = node.getTree().getRegistry().getConstantPool().get(modelDescriptorId);
NativeFunction function = new NativeFunction("model", "with(model) { return " + expression + "}");
BeanModelType type = new BeanModelType(modelDescriptor);
Object proxy = type.createProxy(model);
return Optional.ofNullable(function.call(null, proxy));
}
}
use of com.vaadin.client.flow.StateNode in project flow by vaadin.
the class AbstractTemplateStrategy method createAndBind.
/**
* Creates and binds a DOM node for the given state node and
* {@code templateId} using binder {@code context}.
*
* @param modelNode
* the state node with model data for which to create a DOM node,
* not <code>null</code>
* @param templateId
* template id
* @param context
* binder context
*
* @return the DOM node, not <code>null</code>
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
protected static Node createAndBind(StateNode modelNode, int templateId, TemplateBinderContext context) {
JsArray<AbstractTemplateStrategy> strategies = context.getStrategies(strategy -> strategy instanceof AbstractTemplateStrategy<?>);
StateTree tree = modelNode.getTree();
for (int i = 0; i < strategies.length(); i++) {
AbstractTemplateStrategy strategy = strategies.get(i);
if (strategy.isApplicable(tree, templateId)) {
Node domNode = strategy.create(tree, templateId);
strategy.bind(modelNode, domNode, templateId, context);
return domNode;
}
}
throw new IllegalArgumentException("Unsupported template type: " + getTemplateNode(tree, templateId).getType());
}
use of com.vaadin.client.flow.StateNode in project flow by vaadin.
the class ClientJsonCodecTest method decodeStateNode_node.
@Test
public void decodeStateNode_node() {
StateTree tree = new StateTree(null);
StateNode node = new StateNode(43, tree);
tree.registerNode(node);
JsElement element = new JsElement() {
};
node.setDomNode(element);
JsonArray json = JsonUtils.createArray(Json.create(JsonCodec.NODE_TYPE), Json.create(node.getId()));
StateNode decoded = ClientJsonCodec.decodeStateNode(tree, json);
Assert.assertSame(node, decoded);
}
use of com.vaadin.client.flow.StateNode in project flow by vaadin.
the class MapPropertyTest method setValue_alwaysUpdateValue_eventIsAlwaysFired.
@Test
public void setValue_alwaysUpdateValue_eventIsAlwaysFired() {
TestTree tree = new TestTree();
StateNode node = new StateNode(13, tree);
MapProperty property = new MapProperty("foo", node.getMap(NodeFeatures.ELEMENT_PROPERTIES), true);
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("foo");
Assert.assertNotNull(capture.get());
}
use of com.vaadin.client.flow.StateNode in project flow by vaadin.
the class MapPropertyTest method removeValue_updateFromServerIsAppliedViaSyncToServer_syncToServerUpdatesValue.
@Test
public void removeValue_updateFromServerIsAppliedViaSyncToServer_syncToServerUpdatesValue() {
TestTree tree = new TestTree();
StateNode node = new StateNode(7, tree);
MapProperty property = node.getMap(NodeFeatures.ELEMENT_PROPERTIES).getProperty("foo");
property.setValue("bar");
property.removeValue();
property.syncToServer(null);
property.syncToServer("baz");
Assert.assertEquals("baz", property.getValue());
}
Aggregations