Skip to content

Commit

Permalink
feat: Pass Java class info to browser in dev mode
Browse files Browse the repository at this point in the history
  • Loading branch information
Artur- committed Mar 6, 2024
1 parent 258484c commit 8e46bf5
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import com.vaadin.client.flow.binding.Binder;
import com.vaadin.client.flow.dom.DomApi;
import com.vaadin.client.flow.util.NativeFunction;
import com.vaadin.flow.internal.nodefeature.NodeFeatures;

import elemental.client.Browser;
import elemental.dom.Element;
Expand Down Expand Up @@ -166,6 +167,12 @@ private native void publishJavascriptMethods(String applicationId,
client.getByNodeId = $entry(function(nodeId) {
return ap.@ApplicationConnection::getDomElementByNodeId(*)(nodeId);
});
client.getNodeInfo = $entry(function(nodeId) {
return {
element: ap.@ApplicationConnection::getDomElementByNodeId(*)(nodeId),
javaClass: ap.@ApplicationConnection::getJavaClass(*)(nodeId)
};
});
client.getNodeId = $entry(function(element) {
return ap.@ApplicationConnection::getNodeId(*)(element);
});
Expand Down Expand Up @@ -222,6 +229,14 @@ private native void publishJavascriptMethods(String applicationId,
private Node getDomElementByNodeId(int id) {
StateNode node = registry.getStateTree().getNode(id);
return node == null ? null : node.getDomNode();

}

private String getJavaClass(int id) {
StateNode node = registry.getStateTree().getNode(id);
return node == null ? null
: node.getMap(NodeFeatures.ELEMENT_DATA).getProperty("jc")
.getValueOrDefault(null);
}

private int getNodeId(Element element) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import com.vaadin.flow.internal.ReflectionCache;
import com.vaadin.flow.internal.StateNode;
import com.vaadin.flow.internal.StateTree;
import com.vaadin.flow.internal.nodefeature.ElementData;
import com.vaadin.flow.internal.nodefeature.VirtualChildrenList;
import com.vaadin.flow.router.Router;
import com.vaadin.flow.server.Attributes;
Expand Down Expand Up @@ -245,6 +246,17 @@ && isAttachedToParent(component.getElement(), parent)) {
component.getElement().isEnabled());
}
}

if (!attachEvent.getUI().getSession().getConfiguration()
.isProductionMode()) {
StateNode n = component.getElement().getNode();
if (n.hasFeature(ElementData.class)) {
n.getFeature(ElementData.class)
.setJavaClass(component.getClass());
}

}

}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import java.io.Serializable;

import com.vaadin.flow.component.Component;
import com.vaadin.flow.internal.StateNode;

import elemental.json.JsonValue;
Expand Down Expand Up @@ -105,4 +106,12 @@ public JsonValue getPayload() {
public boolean allowsChanges() {
return isVisible();
}

public void setJavaClass(Class<? extends Component> componentClass) {
put(NodeProperties.JAVA_CLASS, componentClass.getName());
}

public String getJavaClass() {
return getOrDefault(NodeProperties.JAVA_CLASS, (String)null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ public final class NodeProperties {
*/
public static final String PAYLOAD = "payload";

/**
* Key for {@link ElementData#getJavaClass()}.
*/
public static final String JAVA_CLASS = "jc";

/**
* Key for {@link TextNodeMap#getText()}.
*/
Expand Down

0 comments on commit 8e46bf5

Please sign in to comment.