use of com.vaadin.client.flow.StateNode in project flow by vaadin.
the class GwtTemplateBinderTest method testJSExpresisonsWithSubProperties.
public void testJSExpresisonsWithSubProperties() {
JsonObject beanType = Json.createObject();
beanType.put(MODEL_KEY, "String");
JsonObject modelType = Json.createObject();
modelType.put(MODEL_KEY, beanType);
setModelType(stateNode, modelType);
// create binding with expression : : key.key ? key.key+'@bar.com'
// :'foo'
TestTextTemplate templateNode = TestTextTemplate.create(TestBinding.createTextValueBinding(MODEL_KEY + "." + MODEL_KEY + " ? " + MODEL_KEY + "." + MODEL_KEY + "+'@bar.com' : 'foo'"));
Node domNode = createText(templateNode);
NodeMap parentMap = stateNode.getMap(NodeFeatures.TEMPLATE_MODELMAP);
StateNode childNode = new StateNode(458, stateNode.getTree());
NodeMap childMap = childNode.getMap(NodeFeatures.TEMPLATE_MODELMAP);
parentMap.getProperty(MODEL_KEY).setValue(childNode);
childMap.getProperty(MODEL_KEY).setValue(null);
Reactive.flush();
assertEquals("foo", domNode.getTextContent());
childMap.getProperty(MODEL_KEY).setValue("value");
Reactive.flush();
assertEquals("value@bar.com", domNode.getTextContent());
}
use of com.vaadin.client.flow.StateNode in project flow by vaadin.
the class PolymerUtils method convertToJson.
/**
* 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 convertToJson(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 convertToJson(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::convertToJson);
if (convert instanceof JsonObject && !((JsonObject) convert).hasKey("nodeId")) {
((JsonObject) convert).put("nodeId", node.getId());
}
return convert;
} else if (object instanceof MapProperty) {
MapProperty property = (MapProperty) object;
if (property.getMap().getId() == NodeFeatures.BASIC_TYPE_VALUE) {
return convertToJson(property.getValue());
} else {
JsonObject convertedObject = Json.createObject();
convertedObject.put(property.getName(), convertToJson(property.getValue()));
return convertedObject;
}
} else {
return WidgetUtil.crazyJsoCast(object);
}
}
use of com.vaadin.client.flow.StateNode in project flow by vaadin.
the class ElementTemplateBindingStrategy method bind.
@Override
protected void bind(StateNode modelNode, Element element, int templateId, TemplateBinderContext context) {
ElementTemplateNode templateNode = (ElementTemplateNode) getTemplateNode(modelNode.getTree(), templateId);
bindProperties(modelNode, templateNode, element);
bindClassNames(modelNode, templateNode, element);
bindAttributes(modelNode, templateNode, element);
JsArray<Double> children = templateNode.getChildrenIds();
if (children != null) {
for (int i = 0; i < children.length(); i++) {
int childTemplateId = children.get(i).intValue();
Node child = createAndBind(modelNode, childTemplateId, context);
DomApi.wrap(element).appendChild(child);
}
}
registerEventHandlers(context.getTemplateRoot(), templateNode, element);
MapProperty overrideProperty = modelNode.getMap(NodeFeatures.TEMPLATE_OVERRIDES).getProperty(String.valueOf(templateNode.getId()));
if (overrideProperty.hasValue()) {
/*
* Bind right away. We don't need to listen to property value
* changes since the value will never change once it has been set.
*/
bindOverrideNode(element, overrideProperty, context);
} else {
/*
* React in case an override nodes appears later on.
*/
EventRemover remover = overrideProperty.addChangeListener(e -> Reactive.addFlushListener(() -> bindOverrideNode(element, overrideProperty, context)));
/*
* Should preferably remove the change listener immediately when the
* first event is fired, but Java makes it so difficult to reference
* the remover from inside the event handler.
*/
modelNode.addUnregisterListener(e -> remover.remove());
}
if (element == context.getTemplateRoot().getDomNode()) {
// Only bind element.$server for the template root element using the
// data in the state node. Other elements might get their own
// $server through an override node
ServerEventHandlerBinder.bindServerEventHandlerNames(element, context.getTemplateRoot());
}
}
use of com.vaadin.client.flow.StateNode in project flow by vaadin.
the class ElementTemplateBindingStrategy method bindOverrideNode.
private void bindOverrideNode(Element element, MapProperty overrideProperty, BinderContext context) {
StateNode overrideNode = (StateNode) overrideProperty.getValue();
/*
* bind checks that the we haven't already bound the same state node
* previously
*/
context.bind(overrideNode, element);
}
use of com.vaadin.client.flow.StateNode in project flow by vaadin.
the class SimpleElementBindingStrategy method verifyAttachedElement.
private boolean verifyAttachedElement(Element element, StateNode attachNode, String id, String address, BindingContext context) {
StateNode node = context.node;
String tag = getTag(attachNode);
boolean failure = false;
if (element == null) {
failure = true;
Console.warn(ELEMENT_ATTACH_ERROR_PREFIX + address + " is not found. The requested tag name is '" + tag + "'");
} else if (!ElementUtil.hasTag(element, tag)) {
failure = true;
Console.warn(ELEMENT_ATTACH_ERROR_PREFIX + address + " has the wrong tag name '" + element.getTagName() + "', the requested tag name is '" + tag + "'");
}
if (failure) {
node.getTree().sendExistingElementWithIdAttachToServer(node, attachNode.getId(), -1, id);
return false;
}
if (!node.hasFeature(NodeFeatures.SHADOW_ROOT_DATA)) {
return true;
}
NodeMap map = node.getMap(NodeFeatures.SHADOW_ROOT_DATA);
StateNode shadowRootNode = (StateNode) map.getProperty(NodeProperties.SHADOW_ROOT).getValue();
if (shadowRootNode == null) {
return true;
}
NodeList list = shadowRootNode.getList(NodeFeatures.ELEMENT_CHILDREN);
Integer existingId = null;
for (int i = 0; i < list.length(); i++) {
StateNode stateNode = (StateNode) list.get(i);
Node domNode = stateNode.getDomNode();
if (domNode.equals(element)) {
existingId = stateNode.getId();
break;
}
}
if (existingId != null) {
Console.warn(ELEMENT_ATTACH_ERROR_PREFIX + address + " has been already attached previously via the node id='" + existingId + "'");
node.getTree().sendExistingElementWithIdAttachToServer(node, attachNode.getId(), existingId, id);
return false;
}
return true;
}
Aggregations