Search in sources :

Example 6 with StateTree

use of com.vaadin.client.flow.StateTree in project flow by vaadin.

the class MapProperty method syncToServer.

/**
 * Sets the value of this property and synchronizes the value to the server.
 *
 * @param newValue
 *            the new value to set.
 */
public void syncToServer(Object newValue) {
    Object currentValue = hasValue() ? getValue() : null;
    if (Objects.equals(newValue, currentValue)) {
        // in case we are here with the same value that has been set from
        // the server then we unlock client side updates already here via
        // unmarking the server update flag. It allows another client side
        // potential change for the same property being propagated to the
        // server once the server value is set successfully (e.g. mutation
        // the same property from its observer).
        isServerUpdate = false;
    }
    if (!(Objects.equals(newValue, currentValue) && hasValue()) && !isServerUpdate) {
        StateNode node = getMap().getNode();
        StateTree tree = node.getTree();
        if (tree.isActive(node)) {
            doSetValue(newValue);
            tree.sendNodePropertySyncToServer(this);
        } else {
            /*
                 * Fire an fake event to reset the property value back in the
                 * DOM element: we don't know how exactly set this property but
                 * it has to be set to the property value because of listener
                 * added to the property during binding.
                 */
            eventRouter.fireEvent(new MapPropertyChangeEvent(this, currentValue, currentValue));
            // Flush is needed because we are out of normal lifecycle which
            // call the flush() automatically.
            Reactive.flush();
        }
    }
}
Also used : StateTree(com.vaadin.client.flow.StateTree) StateNode(com.vaadin.client.flow.StateNode)

Example 7 with StateTree

use of com.vaadin.client.flow.StateTree 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 8 with StateTree

use of com.vaadin.client.flow.StateTree 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());
}
Also used : StateTree(com.vaadin.client.flow.StateTree) StateNode(com.vaadin.client.flow.StateNode) Node(elemental.dom.Node)

Example 9 with StateTree

use of com.vaadin.client.flow.StateTree 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);
}
Also used : JsonArray(elemental.json.JsonArray) StateTree(com.vaadin.client.flow.StateTree) StateNode(com.vaadin.client.flow.StateNode) JsElement(elemental.js.dom.JsElement) Test(org.junit.Test)

Example 10 with StateTree

use of com.vaadin.client.flow.StateTree in project flow by vaadin.

the class NodeMapTest method getProperty_returnPropertyWhichAlwaysUpdatesTheValue.

@Test
public void getProperty_returnPropertyWhichAlwaysUpdatesTheValue() {
    NodeMap map = new NodeMap(NodeFeatures.ELEMENT_PROPERTIES, new StateNode(0, new StateTree(null)));
    MapProperty property = map.getProperty("innerHTML");
    AtomicReference<MapPropertyChangeEvent> capture = new AtomicReference<>();
    property.addChangeListener(capture::set);
    property.setValue("foo");
    Assert.assertNotNull(capture.get());
    // reset
    capture.set(null);
    // set the same value again
    property.setValue("foo");
    Assert.assertNotNull(capture.get());
}
Also used : StateTree(com.vaadin.client.flow.StateTree) StateNode(com.vaadin.client.flow.StateNode) AtomicReference(java.util.concurrent.atomic.AtomicReference) Test(org.junit.Test)

Aggregations

StateTree (com.vaadin.client.flow.StateTree)12 StateNode (com.vaadin.client.flow.StateNode)8 Test (org.junit.Test)4 JsonArray (elemental.json.JsonArray)3 Registry (com.vaadin.client.Registry)2 ConstantPool (com.vaadin.client.flow.ConstantPool)2 JsElement (elemental.js.dom.JsElement)2 JsonObject (elemental.json.JsonObject)2 ApplicationConfiguration (com.vaadin.client.ApplicationConfiguration)1 CustomScheduler (com.vaadin.client.CustomScheduler)1 ExistingElementMap (com.vaadin.client.ExistingElementMap)1 InitialPropertiesHandler (com.vaadin.client.InitialPropertiesHandler)1 UILifecycle (com.vaadin.client.UILifecycle)1 URIResolver (com.vaadin.client.URIResolver)1 ServerConnector (com.vaadin.client.communication.ServerConnector)1 ExecuteJavaScriptProcessor (com.vaadin.client.flow.ExecuteJavaScriptProcessor)1 JsArray (com.vaadin.client.flow.collection.JsArray)1 Node (elemental.dom.Node)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1