From: Manolo Carrasco Date: Sun, 23 Jan 2011 07:14:19 +0000 (+0000) Subject: Moving cur method to styleImpl and adding it to the GQuery api X-Git-Tag: release-1.3.2~568 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=e480992376b7e47ab4c5a88c2986c32698813123;p=gwtquery.git Moving cur method to styleImpl and adding it to the GQuery api --- diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/GQUtils.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/GQUtils.java index 31f5050c..77c5ffb1 100644 --- a/gwtquery-core/src/main/java/com/google/gwt/query/client/GQUtils.java +++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/GQUtils.java @@ -31,32 +31,11 @@ import com.google.gwt.user.client.DOM; public class GQUtils { /** - * Returns the numeric value of a css property. - * - * The parameter force has a special meaning: - * - When force is false, returns the value of the css property defined - * in the set of style attributes. - * - Otherwise it returns the real computed value. + * Use the method in the gquery class $(elem).cur(prop, force); */ + @Deprecated public static double cur(Element elem, String prop, boolean force) { - if (elem.getPropertyString(prop) != null - && (elem.getStyle() == null || elem.getStyle().getProperty(prop) == null)) { - return elem.getPropertyDouble(prop); - } - GQuery g = GQuery.$(elem); - String val = g.css(prop, force); - if ("thick".equalsIgnoreCase(val)) { - return (5); - } else if ("medium".equalsIgnoreCase(val)) { - return (3); - } else if ("thin".equalsIgnoreCase(val)) { - return (1); - } - if (!val.matches("^[\\d\\.]+.*$")) { - val = g.css(prop, false); - } - val = val.trim().replaceAll("[^\\d\\.\\-]+.*$", ""); - return val.length() == 0 ? 0 : Double.parseDouble(val); + return GQuery.$(elem).cur(prop, force); } /** 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 7e14e4ac..cf0111b4 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 @@ -473,8 +473,6 @@ public class GQuery implements Lazy { /** * We will use the fact as GWT use the widget itself as EventListener ! * If no Widget associated with the element, this method returns null. - * @param e - * @return */ protected static Widget getAssociatedWidget(Element e){ EventListener listener = DOM.getEventListener((com.google.gwt.user.client.Element) e); @@ -1001,6 +999,25 @@ public class GQuery implements Lazy { } return this; } + + /** + * Returns the numeric value of a css property. + */ + public double cur(String prop) { + return cur(prop, false); + } + + /** + * Returns the numeric value of a css property. + * + * The parameter force has a special meaning: + * - When force is false, returns the value of the css property defined + * in the set of style attributes. + * - When true returns the real computed value. + */ + public double cur(String prop, boolean force) { + return styleImpl.cur(get(0), prop, force); + } /** * Returns value at named data store for the element, as set by data(name, @@ -1477,7 +1494,7 @@ public class GQuery implements Lazy { * Returns the computed left position of the first element matched. */ public int left() { - return (int) GQUtils.cur(get(0), "left", true); + return (int)cur("left", true); } /** @@ -1698,7 +1715,7 @@ public class GQuery implements Lazy { public int outerHeight(boolean includeMargin){ int outerHeight = get(0).getOffsetHeight(); //height including padding and border if (includeMargin){ - outerHeight+=GQUtils.cur( get(0), "marginTop", true)+GQUtils.cur( get(0), "marginBottom", true); + outerHeight += cur("marginTop", true) + cur("marginBottom", true); } return outerHeight; } @@ -1718,7 +1735,7 @@ public class GQuery implements Lazy { public int outerWidth(boolean includeMargin){ int outerWidth = get(0).getOffsetWidth(); //width including padding and border if (includeMargin){ - outerWidth+=GQUtils.cur( get(0), "marginRight", true)+GQUtils.cur( get(0), "marginLeft", true); + outerWidth += cur("marginRight", true) + cur("marginLeft", true); } return outerWidth; } @@ -1788,29 +1805,29 @@ public class GQuery implements Lazy { // Get correct offsets Offset offset = offset(); Offset parentOffset = null; - if ("body".equalsIgnoreCase(offsetParent.getNodeName()) - || "html".equalsIgnoreCase(offsetParent.getNodeName())) { + if (offsetParent == body || offsetParent == (Node)document) { parentOffset = new Offset(0, 0); } else { parentOffset = $(offsetParent).offset(); } // Subtract element margins - int topMargin = (int) GQUtils.cur(element, "marginTop", true); + int topMargin = (int)styleImpl.cur(element, "marginTop", true); + // TODO: move this check to styleImpl // When margin-left = auto, Safari and chrome return a value while IE and // Firefox return 0 // force the margin-left to 0 if margin-left = auto. int leftMargin = 0; if (!"auto".equals(element.getStyle().getMarginLeft())) { - leftMargin = (int) GQUtils.cur(element, "marginLeft", true); + leftMargin = (int)styleImpl.cur(element, "marginLeft", true); } offset = offset.add(-leftMargin, -topMargin); // Add offsetParent borders - int parentOffsetBorderTop = (int) GQUtils.cur(offsetParent, + int parentOffsetBorderTop = (int)styleImpl.cur(offsetParent, "borderTopWidth", true); - int parentOffsetBorderLeft = (int) GQUtils.cur(offsetParent, + int parentOffsetBorderLeft = (int)styleImpl.cur(offsetParent, "borderLeftWidth", true); parentOffset = parentOffset.add(parentOffsetBorderLeft, parentOffsetBorderTop); @@ -2315,7 +2332,7 @@ public class GQuery implements Lazy { * Returns the computed left position of the first element matched. */ public int top() { - return (int) GQUtils.cur(get(0), "top", true); + return (int)cur("top", true); } /** 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 2e6ae276..e1ee4d39 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 @@ -328,6 +328,21 @@ public interface LazyGQuery extends LazyBase{ */ LazyGQuery css(TakesPercentage cssProperty, Percentage value); + /** + * Returns the numeric value of a css property. + */ + double cur(String prop); + + /** + * Returns the numeric value of a css property. + * + * The parameter force has a special meaning: + * - When force is false, returns the value of the css property defined + * in the set of style attributes. + * - When true returns the real computed value. + */ + double cur(String prop, boolean force); + /** * Returns value at named data store for the element, as set by data(name, * value). diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/impl/DocumentStyleImpl.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/impl/DocumentStyleImpl.java index e4e4485a..3eec5c5c 100644 --- a/gwtquery-core/src/main/java/com/google/gwt/query/client/impl/DocumentStyleImpl.java +++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/impl/DocumentStyleImpl.java @@ -38,16 +38,45 @@ public class DocumentStyleImpl { public static native String hyphenize(String name) /*-{ return name.replace(/([A-Z])/g, "-$1" ).toLowerCase(); }-*/; - + + /** + * Returns the numeric value of a css property. + * + * The parameter force has a special meaning: + * - When force is false, returns the value of the css property defined + * in the set of style attributes. + * - Otherwise it returns the real computed value. + */ + public double cur(Element elem, String prop, boolean force) { + if (elem.getPropertyString(prop) != null + && (elem.getStyle() == null || elem.getStyle().getProperty(prop) == null)) { + return elem.getPropertyDouble(prop); + } + String val = curCSS(elem, prop, force); + if ("thick".equalsIgnoreCase(val)) { + return (5); + } else if ("medium".equalsIgnoreCase(val)) { + return (3); + } else if ("thin".equalsIgnoreCase(val)) { + return (1); + } + if (!val.matches("^[\\d\\.]+.*$")) { + val = curCSS(elem, prop, false); + } + val = val.trim().replaceAll("[^\\d\\.\\-]+.*$", ""); + return val.length() == 0 ? 0 : Double.parseDouble(val); + } + /** * Return the string value of a css property of an element. * - * The parameter force has a special meaning here: - When force is false, - * returns the value of the css property defined in the style attribute of the - * element. - Otherwise it returns the real computed value. + * The parameter force has a special meaning: + * - When force is false, returns the value of the css property defined + * in the set of style attributes. + * - Otherwise it returns the real computed value. * - * For instance if you define 'display=none' not in the element style but in - * the css stylesheet, it returns an empty string unless you pass the + * For instance if you do not define 'display=none' in the element style but in + * the css stylesheet, it will return an empty string unless you pass the * parameter force=true. */ public String curCSS(Element elem, String name, boolean force) { @@ -71,7 +100,6 @@ public class DocumentStyleImpl { } } - /** * Fix style property names. */ diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/PropertiesAnimation.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/PropertiesAnimation.java index b5f40aeb..dfbc27b9 100755 --- a/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/PropertiesAnimation.java +++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/PropertiesAnimation.java @@ -15,17 +15,16 @@ */ package com.google.gwt.query.client.plugins; +import java.util.ArrayList; + import com.google.gwt.animation.client.Animation; import com.google.gwt.dom.client.Element; import com.google.gwt.query.client.Function; -import com.google.gwt.query.client.GQUtils; import com.google.gwt.query.client.GQuery; import com.google.gwt.query.client.JSArray; import com.google.gwt.query.client.Properties; import com.google.gwt.query.client.Regexp; -import java.util.ArrayList; - /** * Animation effects on any numeric CSS property. */ @@ -90,7 +89,7 @@ public class PropertiesAnimation extends Animation { if (hidden){ g.show(); } - double start = GQUtils.cur(e, key, true), end = start; + double start = g.cur(key, true), end = start; if ("show".equals(val)) { g.saveCssAttrs(key); @@ -113,7 +112,7 @@ public class PropertiesAnimation extends Animation { if (!"px".equals(unit)) { double to = end == 0 ? 1 : end; g.css(key, to + unit); - start = to * start / GQUtils.cur(e, key, true); + start = to * start / g.cur(key, true); g.css(key, start + unit); } if (parts.getStr(1) != null) { diff --git a/gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryCoreTest.java b/gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryCoreTest.java index 99cdf452..c00563a0 100644 --- a/gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryCoreTest.java +++ b/gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryCoreTest.java @@ -739,10 +739,10 @@ public class GQueryCoreTest extends GWTTestCase { assertEquals(122, g.height()); assertEquals(120, g.clientWidth()); assertEquals(120, g.clientHeight()); - assertEquals(100, (int)GQUtils.cur(g.get(0), "width", false)); - assertEquals(100, (int)GQUtils.cur(g.get(0), "height", false)); - assertEquals(100, (int)GQUtils.cur(g.get(0), "width", true)); - assertEquals(100, (int)GQUtils.cur(g.get(0), "height", true)); + assertEquals(100d, g.cur("width", false)); + assertEquals(100d, g.cur("height", false)); + assertEquals(100d, g.cur("width", true)); + assertEquals(100d, g.cur("height", true)); assertEquals("100px", g.css("width")); assertEquals("100px", g.css("height")); assertEquals("100px", g.get(0).getStyle().getProperty("width")); diff --git a/gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryEventsTest.java b/gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryEventsTest.java index c2260521..4dea5f52 100644 --- a/gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryEventsTest.java +++ b/gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryEventsTest.java @@ -217,30 +217,30 @@ public class GQueryEventsTest extends GWTTestCase { $("p", e, Events.Events).trigger(Event.ONCLICK); assertEquals("red", $("p", e).css("color")); assertEquals("green", $("p", e).css("background-color")); - assertEquals(24.0d, GQUtils.cur($("p", e).get(0), "fontSize", true)); + assertEquals(24.0d, $("p", e).cur("fontSize", true)); $("p", e).css("color","").css("background","").css("fontSize", "12px"); assertFalse("red".equalsIgnoreCase($("p", e).css("color"))); assertFalse("green".equalsIgnoreCase($("p", e).css("background-color"))); - assertEquals(12.0d, GQUtils.cur($("p", e).get(0), "fontSize", true)); + assertEquals(12.0d, $("p", e).cur("fontSize", true)); $("p", e, Events.Events).unbind("click.first.namespace"); $("p", e, Events.Events).trigger(Event.ONCLICK); assertFalse("red".equalsIgnoreCase($("p", e).css("color"))); assertEquals("green", $("p", e).css("background-color")); - assertEquals(24.0d, GQUtils.cur($("p", e).get(0), "fontSize", true)); + assertEquals(24.0d, $("p", e).cur("fontSize", true)); $("p", e).css("color","").css("background","").css("fontSize", "12px"); assertFalse("red".equalsIgnoreCase($("p", e).css("color"))); assertFalse("green".equalsIgnoreCase($("p", e).css("background-color"))); - assertEquals(12.0d, GQUtils.cur($("p", e).get(0), "fontSize", true)); + assertEquals(12.0d, $("p", e).cur("fontSize", true)); $("p", e, Events.Events).unbind("click"); $("p", e, Events.Events).trigger(Event.ONCLICK); assertFalse("red".equalsIgnoreCase($("p", e).css("color"))); assertFalse("green".equalsIgnoreCase($("p", e).css("background-color"))); - assertEquals(12.0d, GQUtils.cur($("p", e).get(0), "fontSize", true)); + assertEquals(12.0d, $("p", e).cur("fontSize", true)); } public void testSubmitEvent() { @@ -293,7 +293,6 @@ public class GQueryEventsTest extends GWTTestCase { RootPanel.get().remove(b); } - public void testUnbindMultipleEvents() { String content = "

content

"; $(e).html(content);