summaryrefslogtreecommitdiffstats
path: root/client
diff options
context:
space:
mode:
authorArtur <artur@vaadin.com>2017-01-13 10:26:11 +0200
committerGitHub <noreply@github.com>2017-01-13 10:26:11 +0200
commit8fca887996e063470379dcf3b47d6bcff3c9839c (patch)
tree33badf44bf23dbc76f1b7830dc0a583302a51f17 /client
parent0c471fbab5149b2bf7851631706cef2fda29a1d9 (diff)
downloadvaadin-framework-8fca887996e063470379dcf3b47d6bcff3c9839c.tar.gz
vaadin-framework-8fca887996e063470379dcf3b47d6bcff3c9839c.zip
Support for defining the root element for Javascript components (#8143)
Fixes #7005
Diffstat (limited to 'client')
-rw-r--r--client/src/main/java/com/vaadin/client/ConnectorMap.java12
-rw-r--r--client/src/main/java/com/vaadin/client/JavaScriptConnectorHelper.java55
-rw-r--r--client/src/main/java/com/vaadin/client/ui/AbstractConnector.java16
-rw-r--r--client/src/main/java/com/vaadin/client/ui/JavaScriptComponentConnector.java6
-rw-r--r--client/src/main/java/com/vaadin/client/ui/JavaScriptWidget.java16
5 files changed, 80 insertions, 25 deletions
diff --git a/client/src/main/java/com/vaadin/client/ConnectorMap.java b/client/src/main/java/com/vaadin/client/ConnectorMap.java
index d0dc4d9ab5..f79aa0250f 100644
--- a/client/src/main/java/com/vaadin/client/ConnectorMap.java
+++ b/client/src/main/java/com/vaadin/client/ConnectorMap.java
@@ -126,21 +126,9 @@ public class ConnectorMap {
ComponentDetail componentDetail = GWT.create(ComponentDetail.class);
idToComponentDetail.put(id, componentDetail);
componentDetail.setConnector(connector);
- if (connector instanceof ComponentConnector) {
- ComponentConnector pw = (ComponentConnector) connector;
- Widget widget = pw.getWidget();
- Profiler.enter("ConnectorMap.setConnectorId");
- setConnectorId(widget.getElement(), id);
- Profiler.leave("ConnectorMap.setConnectorId");
- }
Profiler.leave("ConnectorMap.registerConnector");
}
- private static native void setConnectorId(Element el, String id)
- /*-{
- el.tkPid = id;
- }-*/;
-
/**
* Gets the connector id using a DOM element - the element should be the
* root element for a connector, otherwise no id will be found. Use
diff --git a/client/src/main/java/com/vaadin/client/JavaScriptConnectorHelper.java b/client/src/main/java/com/vaadin/client/JavaScriptConnectorHelper.java
index b9f4ae8962..1d4939f40d 100644
--- a/client/src/main/java/com/vaadin/client/JavaScriptConnectorHelper.java
+++ b/client/src/main/java/com/vaadin/client/JavaScriptConnectorHelper.java
@@ -53,6 +53,7 @@ public class JavaScriptConnectorHelper {
private int tag;
private String initFunctionName;
+ private String tagName;
public JavaScriptConnectorHelper(ServerConnector connector) {
this.connector = connector;
@@ -153,14 +154,8 @@ public class JavaScriptConnectorHelper {
}
protected boolean initJavaScript() {
- ApplicationConfiguration conf = connector.getConnection()
- .getConfiguration();
- ArrayList<String> attemptedNames = new ArrayList<>();
- Integer tag = Integer.valueOf(this.tag);
- while (tag != null) {
- String serverSideClassName = conf.getServerSideClassNameForTag(tag);
- String initFunctionName = serverSideClassName.replaceAll("\\.",
- "_");
+ ArrayList<String> initFunctionNames = getPotentialInitFunctionNames();
+ for (String initFunctionName : initFunctionNames) {
if (tryInitJs(initFunctionName, getConnectorWrapper())) {
getLogger().info("JavaScript connector initialized using "
+ initFunctionName);
@@ -169,12 +164,10 @@ public class JavaScriptConnectorHelper {
} else {
getLogger().warning("No JavaScript function " + initFunctionName
+ " found");
- attemptedNames.add(initFunctionName);
- tag = conf.getParentTag(tag.intValue());
}
}
getLogger().info("No JavaScript init for connector found");
- showInitProblem(attemptedNames);
+ showInitProblem(initFunctionNames);
return false;
}
@@ -408,7 +401,7 @@ public class JavaScriptConnectorHelper {
delete state[key];
}
}
-
+
for(var key in input) {
if (input.hasOwnProperty(key)) {
state[key] = input[key];
@@ -505,6 +498,44 @@ public class JavaScriptConnectorHelper {
return initFunctionName;
}
+ private ArrayList<String> getPotentialInitFunctionNames() {
+ ApplicationConfiguration conf = connector.getConnection()
+ .getConfiguration();
+ ArrayList<String> initFunctionNames = new ArrayList<String>();
+ Integer tag = Integer.valueOf(this.tag);
+ while (tag != null) {
+ String initFunctionName = conf.getServerSideClassNameForTag(tag);
+ initFunctionName = initFunctionName.replaceAll("\\.", "_");
+ initFunctionNames.add(initFunctionName);
+ tag = conf.getParentTag(tag);
+ }
+ return initFunctionNames;
+ }
+
+ public String getTagName() {
+ if (tagName != null) {
+ return tagName;
+ }
+ for (String initFunctionName : getPotentialInitFunctionNames()) {
+ tagName = getTagJs(initFunctionName);
+ if (tagName != null) {
+ return tagName;
+ }
+ }
+ // No tagName found, use default
+ tagName = "div";
+ return tagName;
+ }
+
+ private static native String getTagJs(String initFunctionName)
+ /*-{
+ if ($wnd[initFunctionName] && typeof $wnd[initFunctionName].tag == 'string') {
+ return $wnd[initFunctionName].tag;
+ } else {
+ return null;
+ }
+ }-*/;
+
private static Logger getLogger() {
return Logger.getLogger(JavaScriptConnectorHelper.class.getName());
}
diff --git a/client/src/main/java/com/vaadin/client/ui/AbstractConnector.java b/client/src/main/java/com/vaadin/client/ui/AbstractConnector.java
index 552e4eb424..e078f3f126 100644
--- a/client/src/main/java/com/vaadin/client/ui/AbstractConnector.java
+++ b/client/src/main/java/com/vaadin/client/ui/AbstractConnector.java
@@ -23,10 +23,12 @@ import java.util.List;
import java.util.Set;
import com.google.gwt.core.client.JsArrayString;
+import com.google.gwt.dom.client.Element;
import com.google.gwt.event.shared.GwtEvent;
import com.google.gwt.event.shared.HandlerManager;
import com.google.gwt.event.shared.HandlerRegistration;
import com.vaadin.client.ApplicationConnection;
+import com.vaadin.client.ComponentConnector;
import com.vaadin.client.FastStringMap;
import com.vaadin.client.FastStringSet;
import com.vaadin.client.JsArrayObject;
@@ -118,6 +120,15 @@ public abstract class AbstractConnector
this.connection = connection;
id = connectorId;
+ // Doing this here because we want to run it after connection and id has
+ // been set but before init() is called to enable e.g.
+ // JavaScriptConnector to use connection when determining the tag name
+ if (this instanceof ComponentConnector) {
+ setConnectorId(
+ ((ComponentConnector) this).getWidget().getElement(),
+ connectorId);
+ }
+
addStateChangeHandler(this);
if (Profiler.isEnabled()) {
Profiler.enter(
@@ -131,6 +142,11 @@ public abstract class AbstractConnector
Profiler.leave("AbstractConnector.doInit");
}
+ private static native void setConnectorId(Element el, String id)
+ /*-{
+ el.tkPid = id;
+ }-*/;
+
/**
* Called when the connector has been initialized. Override this method to
* perform initialization of the connector.
diff --git a/client/src/main/java/com/vaadin/client/ui/JavaScriptComponentConnector.java b/client/src/main/java/com/vaadin/client/ui/JavaScriptComponentConnector.java
index 28f05fa7ca..a2daef273c 100644
--- a/client/src/main/java/com/vaadin/client/ui/JavaScriptComponentConnector.java
+++ b/client/src/main/java/com/vaadin/client/ui/JavaScriptComponentConnector.java
@@ -15,6 +15,7 @@
*/
package com.vaadin.client.ui;
+import com.google.gwt.user.client.ui.Widget;
import com.vaadin.client.JavaScriptConnectorHelper;
import com.vaadin.client.communication.HasJavaScriptConnectorHelper;
import com.vaadin.shared.ui.Connect;
@@ -35,6 +36,11 @@ public final class JavaScriptComponentConnector extends
};
@Override
+ protected Widget createWidget() {
+ return new JavaScriptWidget(helper.getTagName());
+ }
+
+ @Override
public JavaScriptWidget getWidget() {
return (JavaScriptWidget) super.getWidget();
}
diff --git a/client/src/main/java/com/vaadin/client/ui/JavaScriptWidget.java b/client/src/main/java/com/vaadin/client/ui/JavaScriptWidget.java
index f5b5ca9c68..002827ed40 100644
--- a/client/src/main/java/com/vaadin/client/ui/JavaScriptWidget.java
+++ b/client/src/main/java/com/vaadin/client/ui/JavaScriptWidget.java
@@ -21,8 +21,22 @@ import com.google.gwt.dom.client.Document;
import com.google.gwt.user.client.ui.Widget;
public class JavaScriptWidget extends Widget {
+
+ /**
+ * Creats a JavaScriptWidget based on a &lt;div&gt; element
+ */
public JavaScriptWidget() {
- setElement(Document.get().createDivElement());
+ this("div");
+ }
+
+ /**
+ * Creates a JavaScriptWidget based on an element with the given tag
+ *
+ * @param tagName
+ * the tag to use for the element
+ */
+ public JavaScriptWidget(String tagName) {
+ setElement(Document.get().createElement(tagName));
}
public void showNoInitFound(ArrayList<String> attemptedNames) {