use of com.vaadin.client.flow.collection.JsArray in project flow by vaadin.
the class TreeChangeProcessor method processSpliceChange.
private static void processSpliceChange(JsonObject change, StateNode node) {
int nsId = (int) change.getNumber(JsonConstants.CHANGE_FEATURE);
NodeList list = node.getList(nsId);
int index = (int) change.getNumber(JsonConstants.CHANGE_SPLICE_INDEX);
int remove;
if (change.hasKey(JsonConstants.CHANGE_SPLICE_REMOVE)) {
remove = (int) change.getNumber(JsonConstants.CHANGE_SPLICE_REMOVE);
} else {
remove = 0;
}
if (change.hasKey(JsonConstants.CHANGE_SPLICE_ADD)) {
JsonArray addJson = change.getArray(JsonConstants.CHANGE_SPLICE_ADD);
JsArray<Object> add = ClientJsonCodec.jsonArrayAsJsArray(addJson);
list.splice(index, remove, add);
} else if (change.hasKey(JsonConstants.CHANGE_SPLICE_ADD_NODES)) {
JsonArray addNodes = change.getArray(JsonConstants.CHANGE_SPLICE_ADD_NODES);
int length = addNodes.length();
JsArray<StateNode> add = JsCollections.array();
StateTree tree = node.getTree();
for (int i = 0; i < length; i++) {
int childId = (int) addNodes.getNumber(i);
StateNode child = tree.getNode(childId);
assert child != null : "No child node found with id " + childId;
child.setParent(node);
add.set(i, child);
}
list.splice(index, remove, add);
} else {
list.splice(index, remove);
}
}
use of com.vaadin.client.flow.collection.JsArray in project flow by vaadin.
the class PolymerUtils method doHandleListChange.
private static void doHandleListChange(ListSpliceEvent event, JsonValue value) {
JsArray<?> add = event.getAdd();
int index = event.getIndex();
int remove = event.getRemove().length();
StateNode node = event.getSource().getNode();
StateNode root = getFirstParentWithDomNode(node);
if (root == null) {
Console.warn("Root node for node " + node.getId() + " could not be found");
return;
}
JsArray<Object> array = JsCollections.array();
add.forEach(item -> array.push(createModelTree(item)));
if (isPolymerElement((Element) root.getDomNode())) {
String path = getNotificationPath(root, node, null);
if (path != null) {
splice((Element) root.getDomNode(), path, index, remove, WidgetUtil.crazyJsoCast(array));
return;
}
}
@SuppressWarnings("unchecked") JsArray<Object> payload = (JsArray<Object>) value;
payload.spliceArray(index, remove, array);
}
use of com.vaadin.client.flow.collection.JsArray in project flow by vaadin.
the class PolymerUtils method registerChangeHandlers.
private static void registerChangeHandlers(StateNode node, NodeFeature feature, JsonValue value) {
JsArray<EventRemover> registrations = JsCollections.array();
if (node.hasFeature(NodeFeatures.ELEMENT_PROPERTIES)) {
assert feature instanceof NodeMap : "Received an inconsistent NodeFeature for a node that has a ELEMENT_PROPERTIES feature. It should be NodeMap, but it is: " + feature;
NodeMap map = (NodeMap) feature;
registerPropertyChangeHandlers(value, registrations, map);
registerPropertyAddHandler(value, registrations, map);
} else if (node.hasFeature(NodeFeatures.TEMPLATE_MODELLIST)) {
assert feature instanceof NodeList : "Received an inconsistent NodeFeature for a node that has a TEMPLATE_MODELLIST feature. It should be NodeList, but it is: " + feature;
NodeList list = (NodeList) feature;
registrations.push(list.addSpliceListener(event -> handleListChange(event, value)));
}
assert !registrations.isEmpty() : "Node should have ELEMENT_PROPERTIES or TEMPLATE_MODELLIST feature";
registrations.push(node.addUnregisterListener(event -> registrations.forEach(EventRemover::remove)));
}
use of com.vaadin.client.flow.collection.JsArray in project flow by vaadin.
the class GwtTemplateBinderTest method gwtSetUp.
@Override
protected void gwtSetUp() throws Exception {
super.gwtSetUp();
registry = new Registry() {
{
set(TemplateRegistry.class, new TemplateRegistry());
set(ConstantPool.class, new ConstantPool());
set(ExistingElementMap.class, new ExistingElementMap());
set(InitialPropertiesHandler.class, new InitialPropertiesHandler(this));
set(StateTree.class, new StateTree(this) {
@Override
public void sendTemplateEventToServer(StateNode node, String methodName, JsArray<?> argValues) {
serverMethods.put(methodName, argValues);
serverRpcNodes.put(methodName, node);
}
});
}
};
tree = registry.getStateTree();
/**
* This state node is ALWAYS a template !!!
*/
stateNode = new StateNode(0, tree) {
@Override
public boolean hasFeature(int id) {
if (id == NodeFeatures.TEMPLATE) {
return true;
}
return super.hasFeature(id);
}
};
// Use the most common model structure by default
JsonObject modelType = Json.createObject();
modelType.put(MODEL_KEY, "String");
setModelType(stateNode, modelType);
}
use of com.vaadin.client.flow.collection.JsArray in project flow by vaadin.
the class GwtStateTreeTest method testSendTemplateEventToServer_delegateToServerConnector.
@SuppressWarnings({ "unchecked", "rawtypes" })
public void testSendTemplateEventToServer_delegateToServerConnector() {
StateNode node = new StateNode(0, tree);
tree.registerNode(node);
JsArray array = getArgArray();
tree.sendTemplateEventToServer(node, "foo", array, -1);
JsonObject object = Json.createObject();
object.put("key", "value");
array.set(array.length(), WidgetUtil.crazyJsCast(object));
array.set(array.length(), getNativeArray());
TestServerConnector serverConnector = (TestServerConnector) registry.getServerConnector();
assertEquals(node, serverConnector.node);
assertEquals("foo", serverConnector.methodName);
JsonArray args = serverConnector.args;
assertEquals(true, args.getBoolean(0));
assertEquals("bar", args.getString(1));
assertEquals(46.2, args.getNumber(2));
assertEquals(object, args.getObject(3));
assertEquals("item", ((JsonArray) args.getObject(4)).getString(0));
}
Aggregations