From 84811d17bf8bc0d00064356d5a301c69220a5698 Mon Sep 17 00:00:00 2001 From: Manolo Carrasco Date: Fri, 25 Feb 2011 10:32:55 +0000 Subject: [PATCH] add scrollTo and scrollIntoView methods --- .../com/google/gwt/query/client/GQuery.java | 48 ++++++++++++++++++- .../google/gwt/query/client/LazyGQuery.java | 31 +++++++++++- 2 files changed, 75 insertions(+), 4 deletions(-) diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/GQuery.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/GQuery.java index e34cf1cc..fdf7f716 100644 --- a/gwtquery-core/src/main/java/com/google/gwt/query/client/GQuery.java +++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/GQuery.java @@ -526,6 +526,10 @@ public class GQuery implements Lazy { private static native String[] jsArrayToString0(JsArrayString array) /*-{ return array; }-*/; + + private static native void scrollIntoViewImpl(Node n) /*-{ + if (n) n.scrollIntoView() + }-*/; private static native T[] reinterpretCast(NodeList nl) /*-{ return nl; @@ -2173,6 +2177,31 @@ public class GQuery implements Lazy { public GQuery scroll(Function...f) { return bindOrFire(Event.ONSCROLL, null, f); } + + /** + * Scrolls the first matched element into view. + */ + public GQuery scrollIntoView() { + scrollIntoViewImpl(get(0)); + return this; + } + + /** + * Scrolls the first matched element into view. + * + * If ensure == true, it crawls up the DOM hierarchy, adjusting the scrollLeft and + * scrollTop properties of each scroll-able element to ensure that the + * specified element is completely in view. It adjusts each scroll position by + * the minimum amount necessary. + */ + public GQuery scrollIntoView(boolean ensure) { + if (ensure) { + DOM.scrollIntoView((com.google.gwt.user.client.Element)get(0)); + } else { + scrollIntoView(); + } + return this; + } /** * Gets the scroll left offset of the first matched element. This method works @@ -2190,7 +2219,7 @@ public class GQuery implements Lazy { } /** - * When a value is passed in, the scroll left offset is set to that value on + * The scroll left offset is set to the passed value on * all matched elements. This method works for both visible and hidden * elements. */ @@ -2204,6 +2233,21 @@ public class GQuery implements Lazy { } return this; } + + /** + * + * Scrolls the contents of all matched elements to the specified co-ordinate + * becoming the top left corner of the viewable area. + * + * This method is only useful where there are areas of the document not viewable within + * the current viewable area of the window and the visible property + * of the window's scrollbar must be set to true. + * + */ + public GQuery scrollTo(int left, int top) { + scrollLeft(left).scrollTop(top); + return this; + } /** * Gets the scroll top offset of the first matched element. This method works @@ -2221,7 +2265,7 @@ public class GQuery implements Lazy { } /** - * When a value is passed in, the scroll top offset is set to that value on + * The scroll top offset is set to the passed value on * all matched elements. This method works for both visible and hidden * elements. */ diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/LazyGQuery.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/LazyGQuery.java index 38a31b24..7a3c2e9b 100644 --- a/gwtquery-core/src/main/java/com/google/gwt/query/client/LazyGQuery.java +++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/LazyGQuery.java @@ -997,6 +997,21 @@ public interface LazyGQuery extends LazyBase{ */ LazyGQuery scroll(Function...f); + /** + * Scrolls the first matched element into view. + */ + LazyGQuery scrollIntoView(); + + /** + * Scrolls the first matched element into view. + * + * If ensure == true, it crawls up the DOM hierarchy, adjusting the scrollLeft and + * scrollTop properties of each scroll-able element to ensure that the + * specified element is completely in view. It adjusts each scroll position by + * the minimum amount necessary. + */ + LazyGQuery scrollIntoView(boolean ensure); + /** * Gets the scroll left offset of the first matched element. This method works * for both visible and hidden elements. @@ -1004,12 +1019,24 @@ public interface LazyGQuery extends LazyBase{ int scrollLeft(); /** - * When a value is passed in, the scroll left offset is set to that value on + * The scroll left offset is set to the passed value on * all matched elements. This method works for both visible and hidden * elements. */ LazyGQuery scrollLeft(int left); + /** + * + * Scrolls the contents of all matched elements to the specified co-ordinate + * becoming the top left corner of the viewable area. + * + * This method is only useful where there are areas of the document not viewable within + * the current viewable area of the window and the visible property + * of the window's scrollbar must be set to true. + * + */ + LazyGQuery scrollTo(int left, int top); + /** * Gets the scroll top offset of the first matched element. This method works * for both visible and hidden elements. @@ -1017,7 +1044,7 @@ public interface LazyGQuery extends LazyBase{ int scrollTop(); /** - * When a value is passed in, the scroll top offset is set to that value on + * The scroll top offset is set to the passed value on * all matched elements. This method works for both visible and hidden * elements. */ -- 2.39.5