diff options
-rw-r--r-- | gwtquery-core/src/main/java/com/google/gwt/query/client/GQuery.java | 69 | ||||
-rw-r--r-- | gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryCoreTestGwt.java | 31 |
2 files changed, 88 insertions, 12 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 194cb643..e64fdfb9 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 @@ -25,18 +25,9 @@ import com.google.gwt.core.client.JavaScriptObject; import com.google.gwt.core.client.JsArray; import com.google.gwt.core.client.JsArrayMixed; import com.google.gwt.core.client.JsArrayString; -import com.google.gwt.dom.client.BodyElement; -import com.google.gwt.dom.client.ButtonElement; -import com.google.gwt.dom.client.Document; -import com.google.gwt.dom.client.Element; -import com.google.gwt.dom.client.InputElement; -import com.google.gwt.dom.client.Node; -import com.google.gwt.dom.client.NodeList; -import com.google.gwt.dom.client.OptionElement; -import com.google.gwt.dom.client.SelectElement; +import com.google.gwt.dom.client.*; import com.google.gwt.dom.client.Style.Display; import com.google.gwt.dom.client.Style.HasCssName; -import com.google.gwt.dom.client.TextAreaElement; import com.google.gwt.query.client.css.CSS; import com.google.gwt.query.client.css.HasCssValue; import com.google.gwt.query.client.css.TakesCssValue; @@ -3006,12 +2997,66 @@ public class GQuery implements Lazy<GQuery, LazyGQuery> { * returned object contains two integer properties, top and left. The method works only with * visible elements. */ - public com.google.gwt.query.client.GQuery.Offset offset() { + public Offset offset() { Element e = get(0); return e == null ? new Offset(0, 0) : new Offset(e.getAbsoluteLeft(), e.getAbsoluteTop()); } /** + * Set the current coordinates of every element in the set of matched elements, relative to the document. + */ + public GQuery offset(Offset offset) { + assert offset() != null : "offset cannot be null"; + return offset(offset.top, offset.left); + } + + /** + * Set the current coordinates of every element in the set of matched elements, relative to the document. + */ + public GQuery offset(int top, int left) { + for (Element element : elements()){ + GQuery $element = $(element); + + String position = $element.css("position", true); + if ("static".equals(position)) { + css("position", "relative"); + } + + Offset curOffset = $element.offset(); + String curCSSTop = $element.css("top", true); + String curCSSLeft = $element.css("left", true); + long curTop = 0; + long curLeft = 0; + + if (("absolute".equals(position) || "fixed".equals(position)) && ("auto".equals(curCSSTop) || "auto".equals + (curCSSLeft))) { + Offset curPosition = $element.position(); + curTop = curPosition.top; + curLeft = curPosition.left; + } else { + try { + curTop = Long.parseLong(curCSSTop); + } catch (NumberFormatException e) { + curTop = 0; + } + + try { + curLeft = Long.parseLong(curCSSLeft); + } catch (NumberFormatException e) { + curLeft = 0; + } + } + + long newTop = top - curOffset.top + curTop; + long newLeft = left - curOffset.left + curLeft; + + $element.css("top", "" + newTop).css("left", "" + newLeft); + } + + return this; + } + + /** * Returns a GQuery collection with the positioned parent of the first matched element. This is * the first parent of the element that has position (as in relative or absolute). This method * only works with visible elements. @@ -3154,7 +3199,7 @@ public class GQuery implements Lazy<GQuery, LazyGQuery> { * contains two Integer properties, top and left. For accurate calculations make sure to use pixel * values for margins, borders and padding. This method only works with visible elements. */ - public com.google.gwt.query.client.GQuery.Offset position() { + public Offset position() { if (isEmpty()) { return new Offset(0, 0); } diff --git a/gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryCoreTestGwt.java b/gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryCoreTestGwt.java index a893741c..34fafb99 100644 --- a/gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryCoreTestGwt.java +++ b/gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryCoreTestGwt.java @@ -25,6 +25,7 @@ import java.util.Collections; import java.util.Comparator; import java.util.List; +import com.google.gwt.query.client.GQuery.Offset; import junit.framework.Assert; import com.google.gwt.dom.client.Document; @@ -493,6 +494,36 @@ public class GQueryCoreTestGwt extends GWTTestCase { assertHtmlEquals(expected, $(e).html()); } + public void testOffset(){ + $(e).html( + "<div id='id1' style='padding-left:10px; padding-top:20px;'><div id='id2'>Content 1</div></div>"); + + Offset parentOffset = $("#id1", e).offset(); + + GQuery g = $("#id2", e); + Offset initialOffset = g.offset(); + + assertEquals(10 + parentOffset.left, initialOffset.left); + assertEquals(20 + parentOffset.top, initialOffset.top); + + g.offset(10, 0); + + Offset offset = g.offset(); + assertEquals(0, offset.left); + assertEquals(10, offset.top); + + //css control + String top = g.css("top", true); + String left = g.css("left", true); + + int expectedTop = 10 - initialOffset.top; + int expectedLeft = 0 - initialOffset.left; + + assertEquals(""+expectedTop+"px", top); + assertEquals(""+expectedLeft+"px", left); + + } + public void testOpacity() { $(e).html( "<p id='id1' style='opacity: 0.6; filter: alpha(opacity=60)'>Content 1</p>"); |