use of com.vaadin.client.flow.StateNode in project flow by vaadin.
the class InitialPropertiesHandler method collectInitialProperties.
private void collectInitialProperties(Double id, JsMap<Double, JsMap<String, Object>> properties) {
StateNode node = getRegistry().getStateTree().getNode(id.intValue());
if (node.hasFeature(NodeFeatures.ELEMENT_PROPERTIES)) {
JsMap<String, Object> map = JsCollections.map();
node.getMap(NodeFeatures.ELEMENT_PROPERTIES).forEachProperty((property, name) -> map.set(name, property.getValue()));
properties.set(id, map);
}
}
use of com.vaadin.client.flow.StateNode in project flow by vaadin.
the class PolymerUtils method doHandlePropertyChange.
private static void doHandlePropertyChange(MapProperty property, JsonValue value) {
String propertyName = property.getName();
StateNode node = property.getMap().getNode();
StateNode root = getFirstParentWithDomNode(node);
if (root == null) {
Console.warn("Root node for node " + node.getId() + " could not be found");
return;
}
JsonValue modelTree = createModelTree(property.getValue());
if (isPolymerElement((Element) root.getDomNode())) {
String path = getNotificationPath(root, node, propertyName);
if (path != null) {
setProperty((Element) root.getDomNode(), path, modelTree);
}
return;
}
WidgetUtil.setJsProperty(value, propertyName, modelTree);
}
use of com.vaadin.client.flow.StateNode in project flow by vaadin.
the class PolymerUtils method createModelTree.
/**
* 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 createModelTree(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 createModelTree(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::createModelTree);
if (convert instanceof JsonObject && !((JsonObject) convert).hasKey("nodeId")) {
((JsonObject) convert).put("nodeId", node.getId());
registerChangeHandlers(node, feature, convert);
}
return convert;
} else if (object instanceof MapProperty) {
MapProperty property = (MapProperty) object;
if (property.getMap().getId() == NodeFeatures.BASIC_TYPE_VALUE) {
return createModelTree(property.getValue());
} else {
JsonObject convertedObject = Json.createObject();
convertedObject.put(property.getName(), createModelTree(property.getValue()));
return convertedObject;
}
} else {
return WidgetUtil.crazyJsoCast(object);
}
}
use of com.vaadin.client.flow.StateNode in project flow by vaadin.
the class SimpleElementBindingStrategy method handleChildrenSplice.
private void handleChildrenSplice(ListSpliceEvent event, BindingContext context) {
Node htmlNode = context.htmlNode;
if (event.isClear()) {
/*
* When a full clear event is fired, all nodes must be removed,
* including the nodes the server doesn't know about.
*/
removeAllChildren(htmlNode);
} else {
JsArray<?> remove = event.getRemove();
for (int i = 0; i < remove.length(); i++) {
StateNode childNode = (StateNode) remove.get(i);
Node child = childNode.getDomNode();
assert child != null : "Can't find element to remove";
if (DomApi.wrap(child).getParentNode() == htmlNode) {
DomApi.wrap(htmlNode).removeChild(child);
}
/*
* If the client-side element is not inside the parent the
* server thought it should be (because of client-side-only DOM
* changes), nothing is done at this point. If the server
* appends the element to a new parent, that will override the
* client DOM in the code below.
*/
}
}
JsArray<?> add = event.getAdd();
if (!add.isEmpty()) {
addChildren(event.getIndex(), context, add);
}
}
use of com.vaadin.client.flow.StateNode in project flow by vaadin.
the class SimpleElementBindingStrategy method handleDomEvent.
private void handleDomEvent(Event event, BindingContext context) {
assert context != null;
Node element = context.htmlNode;
StateNode node = context.node;
assert element instanceof Element : "Cannot handle DOM event for a Node";
String type = event.getType();
NodeMap listenerMap = getDomEventListenerMap(node);
ConstantPool constantPool = node.getTree().getRegistry().getConstantPool();
String expressionConstantKey = (String) listenerMap.getProperty(type).getValue();
assert expressionConstantKey != null;
assert constantPool.has(expressionConstantKey);
JsonObject expressionSettings = constantPool.get(expressionConstantKey);
String[] expressions = expressionSettings.keys();
JsonObject eventData;
JsSet<String> synchronizeProperties = JsCollections.set();
if (expressions.length == 0) {
eventData = null;
} else {
eventData = Json.createObject();
}
for (String expressionString : expressions) {
if (expressionString.startsWith(JsonConstants.SYNCHRONIZE_PROPERTY_TOKEN)) {
String property = expressionString.substring(JsonConstants.SYNCHRONIZE_PROPERTY_TOKEN.length());
synchronizeProperties.add(property);
} else if (expressionString.equals(JsonConstants.MAP_STATE_NODE_EVENT_DATA)) {
// map event.target to the closest state node
int targetNodeId = getClosestStateNodeIdToEventTarget(node, event.getTarget());
eventData.put(JsonConstants.MAP_STATE_NODE_EVENT_DATA, targetNodeId);
} else if (expressionString.startsWith(JsonConstants.MAP_STATE_NODE_EVENT_DATA)) {
// map element returned by JS to the closest state node
String jsEvaluation = expressionString.substring(JsonConstants.MAP_STATE_NODE_EVENT_DATA.length());
EventExpression expression = getOrCreateExpression(jsEvaluation);
JsonValue expressionValue = expression.evaluate(event, (Element) element);
// find the closest state node matching the expression value
int targetNodeId = getClosestStateNodeIdToDomNode(node.getTree(), expressionValue, jsEvaluation);
eventData.put(expressionString, targetNodeId);
} else {
EventExpression expression = getOrCreateExpression(expressionString);
JsonValue expressionValue = expression.evaluate(event, (Element) element);
eventData.put(expressionString, expressionValue);
}
}
JsArray<Runnable> commands = JsCollections.array();
synchronizeProperties.forEach(name -> commands.push(getSyncPropertyCommand(name, context)));
Consumer<String> sendCommand = debouncePhase -> {
commands.forEach(Runnable::run);
sendEventToServer(node, type, eventData, debouncePhase);
};
boolean sendNow = resolveFilters(element, type, expressionSettings, eventData, sendCommand);
if (sendNow) {
// Send if there were not filters or at least one matched
sendCommand.accept(null);
}
}
Aggregations