Search in sources :

Example 1 with JsArray

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);
    }
}
Also used : JsonArray(elemental.json.JsonArray) JsArray(com.vaadin.client.flow.collection.JsArray) NodeList(com.vaadin.client.flow.nodefeature.NodeList) JsonObject(elemental.json.JsonObject)

Example 2 with JsArray

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);
}
Also used : JsArray(com.vaadin.client.flow.collection.JsArray) StateNode(com.vaadin.client.flow.StateNode) JsonObject(elemental.json.JsonObject)

Example 3 with JsArray

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)));
}
Also used : ShadowRoot(elemental.dom.ShadowRoot) JsWeakMap(com.vaadin.client.flow.collection.JsWeakMap) Reactive(com.vaadin.client.flow.reactive.Reactive) DomApi(com.vaadin.client.flow.dom.DomApi) Json(elemental.json.Json) NodeProperties(com.vaadin.flow.internal.nodefeature.NodeProperties) JsonArray(elemental.json.JsonArray) ListSpliceEvent(com.vaadin.client.flow.nodefeature.ListSpliceEvent) NodeMap(com.vaadin.client.flow.nodefeature.NodeMap) NodeFeatures(com.vaadin.flow.internal.nodefeature.NodeFeatures) StateNode(com.vaadin.client.flow.StateNode) MapProperty(com.vaadin.client.flow.nodefeature.MapProperty) HTMLCollection(elemental.html.HTMLCollection) JsonValue(elemental.json.JsonValue) NodeFeature(com.vaadin.client.flow.nodefeature.NodeFeature) NodeList(com.vaadin.client.flow.nodefeature.NodeList) EventRemover(elemental.events.EventRemover) JsSet(com.vaadin.client.flow.collection.JsSet) Node(elemental.dom.Node) JsCollections(com.vaadin.client.flow.collection.JsCollections) Element(elemental.dom.Element) JsonObject(elemental.json.JsonObject) JsArray(com.vaadin.client.flow.collection.JsArray) EventRemover(elemental.events.EventRemover) NodeList(com.vaadin.client.flow.nodefeature.NodeList) NodeMap(com.vaadin.client.flow.nodefeature.NodeMap)

Example 4 with JsArray

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);
}
Also used : InitialPropertiesHandler(com.vaadin.client.InitialPropertiesHandler) StateTree(com.vaadin.client.flow.StateTree) JsArray(com.vaadin.client.flow.collection.JsArray) ConstantPool(com.vaadin.client.flow.ConstantPool) StateNode(com.vaadin.client.flow.StateNode) ExistingElementMap(com.vaadin.client.ExistingElementMap) JsonObject(elemental.json.JsonObject) Registry(com.vaadin.client.Registry)

Example 5 with JsArray

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));
}
Also used : JsonArray(elemental.json.JsonArray) JsArray(com.vaadin.client.flow.collection.JsArray) JsonObject(elemental.json.JsonObject)

Aggregations

JsArray (com.vaadin.client.flow.collection.JsArray)7 JsonObject (elemental.json.JsonObject)6 StateNode (com.vaadin.client.flow.StateNode)4 JsonArray (elemental.json.JsonArray)4 NodeList (com.vaadin.client.flow.nodefeature.NodeList)3 JsonValue (elemental.json.JsonValue)3 ExistingElementMap (com.vaadin.client.ExistingElementMap)2 InitialPropertiesHandler (com.vaadin.client.InitialPropertiesHandler)2 ConstantPool (com.vaadin.client.flow.ConstantPool)2 StateTree (com.vaadin.client.flow.StateTree)2 JsCollections (com.vaadin.client.flow.collection.JsCollections)2 JsSet (com.vaadin.client.flow.collection.JsSet)2 JsWeakMap (com.vaadin.client.flow.collection.JsWeakMap)2 DomApi (com.vaadin.client.flow.dom.DomApi)2 ListSpliceEvent (com.vaadin.client.flow.nodefeature.ListSpliceEvent)2 MapProperty (com.vaadin.client.flow.nodefeature.MapProperty)2 NodeMap (com.vaadin.client.flow.nodefeature.NodeMap)2 Reactive (com.vaadin.client.flow.reactive.Reactive)2 NodeFeatures (com.vaadin.flow.internal.nodefeature.NodeFeatures)2 NodeProperties (com.vaadin.flow.internal.nodefeature.NodeProperties)2