summaryrefslogtreecommitdiffstats
path: root/client
diff options
context:
space:
mode:
authorLeif Åstrand <leif@vaadin.com>2013-06-06 12:01:37 +0300
committerVaadin Code Review <review@vaadin.com>2013-06-10 10:48:23 +0000
commite7201fd30803dbbd231c9a26745b21d5b807abc0 (patch)
treea246e6860e75e25ad9e8a5b159bd6944c1567ba9 /client
parentd6fca7820739508387ae6db33af3a4a4fb5a846e (diff)
downloadvaadin-framework-e7201fd30803dbbd231c9a26745b21d5b807abc0.tar.gz
vaadin-framework-e7201fd30803dbbd231c9a26745b21d5b807abc0.zip
Allow using element resize listeners from javascript connectors (#11996)
Change-Id: I97b280c2e260752be87ce85d2eda81ec4f14d4fe
Diffstat (limited to 'client')
-rw-r--r--client/src/com/vaadin/client/JavaScriptConnectorHelper.java81
1 files changed, 80 insertions, 1 deletions
diff --git a/client/src/com/vaadin/client/JavaScriptConnectorHelper.java b/client/src/com/vaadin/client/JavaScriptConnectorHelper.java
index b144603be0..52d2eeb6dc 100644
--- a/client/src/com/vaadin/client/JavaScriptConnectorHelper.java
+++ b/client/src/com/vaadin/client/JavaScriptConnectorHelper.java
@@ -25,11 +25,13 @@ import java.util.Set;
import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.core.client.JsArray;
+import com.google.gwt.dom.client.Element;
import com.google.gwt.json.client.JSONArray;
-import com.google.gwt.user.client.Element;
import com.vaadin.client.communication.JavaScriptMethodInvocation;
import com.vaadin.client.communication.StateChangeEvent;
import com.vaadin.client.communication.StateChangeEvent.StateChangeHandler;
+import com.vaadin.client.ui.layout.ElementResizeEvent;
+import com.vaadin.client.ui.layout.ElementResizeListener;
import com.vaadin.shared.JavaScriptConnectorState;
import com.vaadin.shared.communication.MethodInvocation;
@@ -42,6 +44,7 @@ public class JavaScriptConnectorHelper {
private final Map<String, JavaScriptObject> rpcObjects = new HashMap<String, JavaScriptObject>();
private final Map<String, Set<String>> rpcMethods = new HashMap<String, Set<String>>();
+ private final Map<Element, Map<JavaScriptObject, ElementResizeListener>> resizeListeners = new HashMap<Element, Map<JavaScriptObject, ElementResizeListener>>();
private JavaScriptObject connectorWrapper;
private int tag;
@@ -204,9 +207,71 @@ public class JavaScriptConnectorHelper {
'translateVaadinUri': $entry(function(uri) {
return c.@com.vaadin.client.ApplicationConnection::translateVaadinUri(Ljava/lang/String;)(uri);
}),
+ 'addResizeListener': function(element, resizeListener) {
+ if (!element || element.nodeType != 1) throw "element must be defined";
+ if (typeof resizeListener != "function") throw "resizeListener must be defined";
+ $entry(h.@com.vaadin.client.JavaScriptConnectorHelper::addResizeListener(*)).call(h, element, resizeListener);
+ },
+ 'removeResizeListener': function(element, resizeListener) {
+ if (!element || element.nodeType != 1) throw "element must be defined";
+ if (typeof resizeListener != "function") throw "resizeListener must be defined";
+ $entry(h.@com.vaadin.client.JavaScriptConnectorHelper::removeResizeListener(*)).call(h, element, resizeListener);
+ }
};
}-*/;
+ // Called from JSNI to add a listener
+ private void addResizeListener(Element element,
+ final JavaScriptObject callbackFunction) {
+ Map<JavaScriptObject, ElementResizeListener> elementListeners = resizeListeners
+ .get(element);
+ if (elementListeners == null) {
+ elementListeners = new HashMap<JavaScriptObject, ElementResizeListener>();
+ resizeListeners.put(element, elementListeners);
+ }
+
+ ElementResizeListener listener = elementListeners.get(callbackFunction);
+ if (listener == null) {
+ LayoutManager layoutManager = LayoutManager.get(connector
+ .getConnection());
+ listener = new ElementResizeListener() {
+ @Override
+ public void onElementResize(ElementResizeEvent e) {
+ invokeElementResizeCallback(e.getElement(),
+ callbackFunction);
+ }
+ };
+ layoutManager.addElementResizeListener(element, listener);
+ elementListeners.put(callbackFunction, listener);
+ }
+ }
+
+ private static native void invokeElementResizeCallback(Element element,
+ JavaScriptObject callbackFunction)
+ /*-{
+ // Call with a simple event object and 'this' pointing to the global scope
+ callbackFunction.call($wnd, {'element': element});
+ }-*/;
+
+ // Called from JSNI to remove a listener
+ private void removeResizeListener(Element element,
+ JavaScriptObject callbackFunction) {
+ Map<JavaScriptObject, ElementResizeListener> listenerMap = resizeListeners
+ .get(element);
+ if (listenerMap == null) {
+ return;
+ }
+
+ ElementResizeListener listener = listenerMap.remove(callbackFunction);
+ if (listener != null) {
+ LayoutManager.get(connector.getConnection())
+ .removeElementResizeListener(element, listener);
+ if (listenerMap.isEmpty()) {
+ resizeListeners.remove(element);
+ }
+ }
+ }
+
private native void attachRpcMethod(JavaScriptObject rpc, String iface,
String method)
/*-{
@@ -377,6 +442,20 @@ public class JavaScriptConnectorHelper {
public void onUnregister() {
invokeIfPresent(connectorWrapper, "onUnregister");
+
+ if (!resizeListeners.isEmpty()) {
+ LayoutManager layoutManager = LayoutManager.get(connector
+ .getConnection());
+ for (Entry<Element, Map<JavaScriptObject, ElementResizeListener>> entry : resizeListeners
+ .entrySet()) {
+ Element element = entry.getKey();
+ for (ElementResizeListener listener : entry.getValue().values()) {
+ layoutManager
+ .removeElementResizeListener(element, listener);
+ }
+ }
+ resizeListeners.clear();
+ }
}
private static native void invokeIfPresent(