Search in sources :

Example 6 with ConstantPool

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

the class ServerEventObject method getEventData.

/**
 * Collect extra data for element event if any has been sent from the
 * server. Note! Data is sent in the array in the same order as defined on
 * the server side.
 *
 * @param event
 *            The fired Event
 * @param methodName
 *            Method name that is called
 * @param node
 *            Target node
 * @return Array of extra event data
 */
private JsonArray getEventData(Event event, String methodName, StateNode node) {
    if (node.getMap(NodeFeatures.POLYMER_EVENT_LISTENERS).hasPropertyValue(methodName)) {
        JsonArray dataArray = Json.createArray();
        ConstantPool constantPool = node.getTree().getRegistry().getConstantPool();
        String expressionConstantKey = (String) node.getMap(NodeFeatures.POLYMER_EVENT_LISTENERS).getProperty(methodName).getValue();
        JsArray<String> dataExpressions = constantPool.get(expressionConstantKey);
        for (int i = 0; i < dataExpressions.length(); i++) {
            String expression = dataExpressions.get(i);
            dataArray.set(i, getExpressionValue(event, node, expression));
        }
        return dataArray;
    }
    return null;
}
Also used : JsonArray(elemental.json.JsonArray) ConstantPool(com.vaadin.client.flow.ConstantPool)

Example 7 with ConstantPool

use of com.vaadin.client.flow.ConstantPool 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);
    }
}
Also used : Json(elemental.json.Json) CSSStyleDeclaration(elemental.css.CSSStyleDeclaration) JsonArray(elemental.json.JsonArray) ListSpliceEvent(com.vaadin.client.flow.nodefeature.ListSpliceEvent) MapProperty(com.vaadin.client.flow.nodefeature.MapProperty) ForEachCallback(com.vaadin.client.flow.collection.JsMap.ForEachCallback) JsonValue(elemental.json.JsonValue) UpdatableModelProperties(com.vaadin.client.flow.model.UpdatableModelProperties) Event(elemental.events.Event) StateTree(com.vaadin.client.flow.StateTree) JsonType(elemental.json.JsonType) DomNode(com.vaadin.client.flow.dom.DomNode) NodeMap(com.vaadin.client.flow.nodefeature.NodeMap) JsMap(com.vaadin.client.flow.collection.JsMap) NodeList(com.vaadin.client.flow.nodefeature.NodeList) JsSet(com.vaadin.client.flow.collection.JsSet) ExistingElementMap(com.vaadin.client.ExistingElementMap) DomTokenList(com.vaadin.client.flow.dom.DomElement.DomTokenList) JsArray(com.vaadin.client.flow.collection.JsArray) Console(com.vaadin.client.Console) ApplicationConfiguration(com.vaadin.client.ApplicationConfiguration) EventTarget(elemental.events.EventTarget) Browser(elemental.client.Browser) Computation(com.vaadin.client.flow.reactive.Computation) JsWeakMap(com.vaadin.client.flow.collection.JsWeakMap) Reactive(com.vaadin.client.flow.reactive.Reactive) LitUtils(com.vaadin.client.LitUtils) DomElement(com.vaadin.client.flow.dom.DomElement) PolymerUtils(com.vaadin.client.PolymerUtils) NodeProperties(com.vaadin.flow.internal.nodefeature.NodeProperties) StateNode(com.vaadin.client.flow.StateNode) ElementUtil(com.vaadin.client.ElementUtil) Supplier(java.util.function.Supplier) NativeFunction(com.vaadin.client.flow.util.NativeFunction) WidgetUtil(com.vaadin.client.WidgetUtil) JsFunction(jsinterop.annotations.JsFunction) Node(elemental.dom.Node) Command(com.vaadin.client.Command) Element(elemental.dom.Element) JsonConstants(com.vaadin.flow.shared.JsonConstants) JavaScriptObject(com.google.gwt.core.client.JavaScriptObject) DomApi(com.vaadin.client.flow.dom.DomApi) NodeFeatures(com.vaadin.flow.internal.nodefeature.NodeFeatures) Consumer(java.util.function.Consumer) Scheduler(com.google.gwt.core.client.Scheduler) ConstantPool(com.vaadin.client.flow.ConstantPool) InitialPropertiesHandler(com.vaadin.client.InitialPropertiesHandler) EventRemover(elemental.events.EventRemover) JsCollections(com.vaadin.client.flow.collection.JsCollections) JsonObject(elemental.json.JsonObject) DomNode(com.vaadin.client.flow.dom.DomNode) StateNode(com.vaadin.client.flow.StateNode) Node(elemental.dom.Node) DomElement(com.vaadin.client.flow.dom.DomElement) Element(elemental.dom.Element) StateNode(com.vaadin.client.flow.StateNode) JsonValue(elemental.json.JsonValue) JsonObject(elemental.json.JsonObject) ConstantPool(com.vaadin.client.flow.ConstantPool) NodeMap(com.vaadin.client.flow.nodefeature.NodeMap)

Example 8 with ConstantPool

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

the class GwtAtmoshperePushConnectionTest method gwtSetUp.

@Override
protected void gwtSetUp() throws Exception {
    super.gwtSetUp();
    initScheduler(new CustomScheduler());
    registry = new Registry() {

        {
            set(ConstantPool.class, new ConstantPool());
            set(StateTree.class, new StateTree(this));
            set(URIResolver.class, new URIResolver(this));
            set(UILifecycle.class, new UILifecycle());
            set(ApplicationConfiguration.class, new ApplicationConfiguration());
            set(MessageHandler.class, new MessageHandler(this));
            set(PushConfiguration.class, new PushConfiguration(this) {

                @Override
                public JsMap<String, String> getParameters() {
                    return JsCollections.map();
                }
            });
            set(ConnectionStateHandler.class, new DefaultConnectionStateHandler(this));
        }
    };
}
Also used : StateTree(com.vaadin.client.flow.StateTree) ConstantPool(com.vaadin.client.flow.ConstantPool) URIResolver(com.vaadin.client.URIResolver) CustomScheduler(com.vaadin.client.CustomScheduler) Registry(com.vaadin.client.Registry) UILifecycle(com.vaadin.client.UILifecycle) ApplicationConfiguration(com.vaadin.client.ApplicationConfiguration)

Aggregations

ConstantPool (com.vaadin.client.flow.ConstantPool)8 JsonObject (elemental.json.JsonObject)6 StateTree (com.vaadin.client.flow.StateTree)3 ApplicationConfiguration (com.vaadin.client.ApplicationConfiguration)2 ExistingElementMap (com.vaadin.client.ExistingElementMap)2 InitialPropertiesHandler (com.vaadin.client.InitialPropertiesHandler)2 Registry (com.vaadin.client.Registry)2 ValueMap (com.vaadin.client.ValueMap)2 StateNode (com.vaadin.client.flow.StateNode)2 JsArray (com.vaadin.client.flow.collection.JsArray)2 DomElement (com.vaadin.client.flow.dom.DomElement)2 NodeMap (com.vaadin.client.flow.nodefeature.NodeMap)2 Element (elemental.dom.Element)2 JsonArray (elemental.json.JsonArray)2 JsonValue (elemental.json.JsonValue)2 JavaScriptObject (com.google.gwt.core.client.JavaScriptObject)1 Scheduler (com.google.gwt.core.client.Scheduler)1 Command (com.vaadin.client.Command)1 Console (com.vaadin.client.Console)1 CustomScheduler (com.vaadin.client.CustomScheduler)1