Reviewers: jlabanca,


Please review this at http://gwt-code-reviews.appspot.com/1601803/

Affected files:
  M user/src/com/google/gwt/user/client/ui/NativeHorizontalScrollbar.java
  M user/src/com/google/gwt/user/client/ui/NativeVerticalScrollbar.java
  M user/src/com/google/gwt/user/client/ui/ScrollImpl.java
  M user/src/com/google/gwt/user/client/ui/ScrollPanel.java


Index: user/src/com/google/gwt/user/client/ui/NativeHorizontalScrollbar.java
diff --git a/user/src/com/google/gwt/user/client/ui/NativeHorizontalScrollbar.java b/user/src/com/google/gwt/user/client/ui/NativeHorizontalScrollbar.java index deaadaa50919b0ab4b5afb29b4da65741de9448e..a5ed423633adff3b0ffb433025581f27196c78a8 100644
--- a/user/src/com/google/gwt/user/client/ui/NativeHorizontalScrollbar.java
+++ b/user/src/com/google/gwt/user/client/ui/NativeHorizontalScrollbar.java
@@ -130,9 +130,6 @@ public class NativeHorizontalScrollbar extends AbstractNativeScrollbar implement
     Style style = resources.nativeHorizontalScrollbarStyle();
     style.ensureInjected();
     getScrollableElement().addClassName(style.nativeHorizontalScrollbar());
-
-    // Initialize the implementation.
-    ScrollImpl.get().initialize(scrollable, contentDiv);
   }

   public int getHorizontalScrollPosition() {
@@ -193,4 +190,18 @@ public class NativeHorizontalScrollbar extends AbstractNativeScrollbar implement
   protected Element getScrollableElement() {
     return scrollable;
   }
+
+  @Override
+  protected void onAttach() {
+    super.onAttach();
+    // Initialize the implementation.
+    ScrollImpl.get().onAttach(scrollable, contentDiv);
+  }
+
+  @Override
+  protected void onDetach() {
+    // Uninitialize the implementation.
+    ScrollImpl.get().onDetach(scrollable, contentDiv);
+    super.onDetach();
+  }
 }
Index: user/src/com/google/gwt/user/client/ui/NativeVerticalScrollbar.java
diff --git a/user/src/com/google/gwt/user/client/ui/NativeVerticalScrollbar.java b/user/src/com/google/gwt/user/client/ui/NativeVerticalScrollbar.java index e4743b1d97f4407dd3958ed6ab225cfecf1241bd..6077bc2195dcc8dc571993e060e8f1c4bc214580 100644
--- a/user/src/com/google/gwt/user/client/ui/NativeVerticalScrollbar.java
+++ b/user/src/com/google/gwt/user/client/ui/NativeVerticalScrollbar.java
@@ -130,9 +130,6 @@ public class NativeVerticalScrollbar extends AbstractNativeScrollbar implements
     Style style = resources.nativeVerticalScrollbarStyle();
     style.ensureInjected();
     getScrollableElement().addClassName(style.nativeVerticalScrollbar());
-
-    // Initialize the implementation.
-    ScrollImpl.get().initialize(scrollable, contentDiv);
   }

   public int getMaximumVerticalScrollPosition() {
@@ -193,4 +190,18 @@ public class NativeVerticalScrollbar extends AbstractNativeScrollbar implements
   protected Element getScrollableElement() {
     return scrollable;
   }
+
+  @Override
+  protected void onAttach() {
+    super.onAttach();
+    // Initialize the implementation.
+    ScrollImpl.get().onAttach(scrollable, contentDiv);
+  }
+
+  @Override
+  protected void onDetach() {
+    // Uninitialize the implementation.
+    ScrollImpl.get().onDetach(scrollable, contentDiv);
+    super.onDetach();
+  }
 }
Index: user/src/com/google/gwt/user/client/ui/ScrollImpl.java
diff --git a/user/src/com/google/gwt/user/client/ui/ScrollImpl.java b/user/src/com/google/gwt/user/client/ui/ScrollImpl.java index 06e8a20e50efc616e19ffeafc58626022146162c..0d6c43352074e64b716dbe14e917e8b68d481bf4 100644
--- a/user/src/com/google/gwt/user/client/ui/ScrollImpl.java
+++ b/user/src/com/google/gwt/user/client/ui/ScrollImpl.java
@@ -36,7 +36,15 @@ class ScrollImpl {
     }

     @Override
- public native void initialize(Element scrollable, Element container) /*-{
+    public native boolean isRtl(Element scrollable) /*-{
+      return scrollable.currentStyle.direction == 'rtl';
+    }-*/;
+
+    /**
+ * attached event handlers are stored in properties for detachement in {@link #onDetach(Element, Element)}
+     */
+    @Override
+    public native void onAttach(Element scrollable, Element container) /*-{
       // Remember the last scroll position.
       var scrollableElem = scrollable;
       scrollableElem.__lastScrollTop = scrollableElem.__lastScrollLeft = 0;
@@ -45,6 +53,7 @@ class ScrollImpl {
         scrollableElem.__lastScrollLeft = scrollableElem.scrollLeft;
       });
       scrollable.attachEvent('onscroll', scrollHandler);
+      scrollable.__scrollHandler = scrollHandler;

// Detect if the scrollable element or the container within it changes
       // size, either of which could affect the scroll position.
@@ -62,12 +71,24 @@ class ScrollImpl {
         }), 1);
       });
       scrollable.attachEvent('onresize', resizeHandler);
+      scrollable.__resizeHandler = resizeHandler;
       container.attachEvent('onresize', resizeHandler);
+      container.__resizeHandler = resizeHandler;
     }-*/;

+    /**
+ * attached event handlers {@link #onAttach(Element, Element)} are retrieved from properties, detached and afterwards the properties are cleared
+     */
     @Override
-    public native boolean isRtl(Element scrollable) /*-{
-      return scrollable.currentStyle.direction == 'rtl';
+    public native void onDetach(Element scrollable, Element container) /*-{
+      scrollable.detachEvent('onresize', scrollable.__resizeHandler);
+      scrollable.__resizeHandler = null;
+
+      scrollable.detachEvent('onscroll', scrollable.__scrollHandler);
+      scrollable.__scrollHandler = null;
+
+      container.detachEvent('onresize', container.__resizeHandler);
+      container.__resizeHandler = null;
     }-*/;
   }

@@ -104,16 +125,6 @@ class ScrollImpl {
   }

   /**
-   * Initialize a scrollable element.
-   *
-   * @param scrollable the scrollable element
-   * @param container the container
-   */
-  public void initialize(Element scrollable, Element container) {
-    // Overridden by ScrollImplTrident.
-  }
-
-  /**
* Check if the specified element has an RTL direction. We can't base this on * the current locale because the user can modify the direction at the DOM
    * level.
@@ -125,4 +136,22 @@ class ScrollImpl {
var computedStyle = $doc.defaultView.getComputedStyle(scrollable, null);
     return computedStyle.getPropertyValue('direction') == 'rtl';
   }-*/;
+
+  /**
+   * Initialize a scrollable element.
+   *
+   * @param scrollable the scrollable element
+   * @param container the container
+   */
+  public void onAttach(Element scrollable, Element container) {
+  }
+
+  /**
+   * Uninitialize a scrollable element.
+   *
+   * @param scrollable the scrollable element
+   * @param container the container
+   */
+  public void onDetach(Element scrollable, Element container) {
+  }
 }
Index: user/src/com/google/gwt/user/client/ui/ScrollPanel.java
diff --git a/user/src/com/google/gwt/user/client/ui/ScrollPanel.java b/user/src/com/google/gwt/user/client/ui/ScrollPanel.java index 394b92fd363bcc7ebab97dfd820a0a64267d166a..0bc1c864449f519b278a78653d20cce56311250a 100644
--- a/user/src/com/google/gwt/user/client/ui/ScrollPanel.java
+++ b/user/src/com/google/gwt/user/client/ui/ScrollPanel.java
@@ -315,6 +315,9 @@ public class ScrollPanel extends SimplePanel implements SourcesScrollEvents,
   protected void onAttach() {
     super.onAttach();

+    // Initialize the scrollable element.
+    ScrollImpl.get().onAttach(scrollableElem, containerElem);
+
     /*
* Attach the event listener in onAttach instead of onLoad so users cannot * accidentally override it. If the scrollable element is the same as the @@ -326,6 +329,9 @@ public class ScrollPanel extends SimplePanel implements SourcesScrollEvents,

   @Override
   protected void onDetach() {
+    // Uninitialize the scrollable element.
+    ScrollImpl.get().onDetach(scrollableElem, containerElem);
+
     /*
* Detach the event listener in onDetach instead of onUnload so users cannot
      * accidentally override it.
@@ -366,8 +372,5 @@ public class ScrollPanel extends SimplePanel implements SourcesScrollEvents,

     // Enable touch scrolling.
     setTouchScrollingDisabled(false);
-
-    // Initialize the scrollable element.
-    ScrollImpl.get().initialize(scrollableElem, containerElem);
   }
 }


--
http://groups.google.com/group/Google-Web-Toolkit-Contributors

Reply via email to