From a9107c728d464a59cc1cc7a718ea39edca85409c Mon Sep 17 00:00:00 2001 From: Julien Dramaix Date: Sat, 28 May 2011 21:39:20 +0000 Subject: - add prop() method (patch from Alessandro Bollini) - add tests --- .../java/com/google/gwt/query/client/GQuery.java | 80 +++++++++++++++++++++- .../com/google/gwt/query/client/LazyGQuery.java | 41 +++++++++++ .../google/gwt/query/client/GQueryCoreTest.java | 19 +++++ 3 files changed, 139 insertions(+), 1 deletion(-) (limited to 'gwtquery-core') 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 9c09993a..540ca157 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 @@ -1899,7 +1899,13 @@ public class GQuery implements Lazy { * trigger the event if no functions are provided. */ public GQuery focus(Function... f) { - return bindOrFire(Event.ONFOCUS, null, f); + bindOrFire(Event.ONFOCUS, null, f); + + if (f.length == 0 && size() > 0){ + //put the focus on the first element + get(0).focus(); + } + return this; } /** @@ -2863,6 +2869,76 @@ public class GQuery implements Lazy { } return pushStack(unique(result), "prevUntil", getSelector()); } + + /** + * Accesses a boolean property on the first matched element. + * + * @param key the name of the boolean property to be accessed + * + * @return true if at least one element is matched and the + * specified boolean property is set to true on the first + * matched element; false otherwise + * + */ + public boolean prop(String key) { + assert key != null : "Key is null"; + + return !isEmpty() && get(0).getPropertyBoolean(key); + } + + /** + * Sets a boolean property to a value on all matched elements. + * + * @param key the name of the boolean property to be set + * @param value the value the specified boolean property should be set to + * + * @return this GQuery object + * + */ + public GQuery prop(String key, boolean value) { + assert key != null : "Key is null"; + + for (final Element element : elements) { + element.setPropertyBoolean(key, value); + } + + return this; + } + + /** + * Sets a boolean property to a computed value on all matched elements. + * + * @param key the name of the boolean property to be set + * @param closure the closure to be used to compute the value the specified + * boolean property should be set to; the closure is + * {@linkplain Function#f(com.google.gwt.dom.client.Element, int) + * passed} the target element and its index as arguments and is + * expected to return either a Boolean value or an + * object whose textual representation is converted to a + * Boolean value; null return values are + * ignored + * + * @return this GQuery object + * + */ + public GQuery prop(String key, Function closure) { + assert key != null : "Key is null"; + assert closure != null : "Closure is null"; + + int i = 0; + + for (Element e : elements) { + + Object value = closure.f(e, i++); + + if (value != null) { + e.setPropertyBoolean(key, value instanceof Boolean ? (Boolean) value + : Boolean.valueOf(value.toString())); + } + } + + return this; + } /** * Put a {@link Function} at the end of the Effects queue. @@ -4117,5 +4193,7 @@ public class GQuery implements Lazy { dataCache.delete(id); } } + + } 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 704ec265..994f15da 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 @@ -1621,6 +1621,47 @@ public interface LazyGQuery extends LazyBase{ */ LazyGQuery prevUntil(String selector); + /** + * Accesses a boolean property on the first matched element. + * + * @param key the name of the boolean property to be accessed + * + * @return true if at least one element is matched and the + * specified boolean property is set to true on the first + * matched element; false otherwise + * + */ + boolean prop(String key); + + /** + * Sets a boolean property to a value on all matched elements. + * + * @param key the name of the boolean property to be set + * @param value the value the specified boolean property should be set to + * + * @return this GQuery object + * + */ + LazyGQuery prop(String key, boolean value); + + /** + * Sets a boolean property to a computed value on all matched elements. + * + * @param key the name of the boolean property to be set + * @param closure the closure to be used to compute the value the specified + * boolean property should be set to; the closure is + * {@linkplain Function#f(com.google.gwt.dom.client.Element, int) + * passed} the target element and its index as arguments and is + * expected to return either a Boolean value or an + * object whose textual representation is converted to a + * Boolean value; null return values are + * ignored + * + * @return this GQuery object + * + */ + LazyGQuery prop(String key, Function closure); + /** * Put a {@link Function} at the end of the Effects queue. * 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 2a25946f..eb6b36dc 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 @@ -511,6 +511,25 @@ public class GQueryCoreTest extends GWTTestCase { assertEquals(15, g.position().top); } + + public void testPropMethod(){ + $(e).html(" "); + + assertTrue($("#checkBox1",e).prop("checked")); + assertFalse($("#checkBox2",e).prop("checked")); + + $("#checkBox1",e).prop("checked", false); + $("#checkBox2",e).prop("checked", new Function() { + @Override + public Object f(Element e, int i) { + return Boolean.TRUE; + } + }); + + assertTrue($("#checkBox2",e).prop("checked")); + assertFalse($("#checkBox1",e).prop("checked")); + + } public void testProperties() { Properties p = $$("border:'1px solid black'"); -- cgit v1.2.3