diff options
author | Julien Dramaix <julien.dramaix@gmail.com> | 2011-05-28 21:39:20 +0000 |
---|---|---|
committer | Julien Dramaix <julien.dramaix@gmail.com> | 2011-05-28 21:39:20 +0000 |
commit | a9107c728d464a59cc1cc7a718ea39edca85409c (patch) | |
tree | 13ac0b090dfbe0d1de492433e64934b27ee72453 /gwtquery-core | |
parent | 43b4f70e6343de72c87a5d8166ff516ed2a3c277 (diff) | |
download | gwtquery-a9107c728d464a59cc1cc7a718ea39edca85409c.tar.gz gwtquery-a9107c728d464a59cc1cc7a718ea39edca85409c.zip |
- add prop() method (patch from Alessandro Bollini)
- add tests
Diffstat (limited to 'gwtquery-core')
3 files changed, 139 insertions, 1 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 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<GQuery, LazyGQuery> { * 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<GQuery, LazyGQuery> { }
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 <code>true</code> if at least one element is matched and the
+ * specified boolean property is set to <code>true</code> on the first
+ * matched element; <code>false</code> 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 <code>GQuery</code> 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 <code>closure</code> 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 <code>Boolean</code> value or an
+ * object whose textual representation is converted to a
+ * <code>Boolean</code> value; <code>null</code> return values are
+ * ignored
+ *
+ * @return this <code>GQuery</code> 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<GQuery, LazyGQuery> { 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 @@ -1622,6 +1622,47 @@ public interface LazyGQuery<T> extends LazyBase<T>{ LazyGQuery<T> prevUntil(String selector); /** + * Accesses a boolean property on the first matched element. + * + * @param key the name of the boolean property to be accessed + * + * @return <code>true</code> if at least one element is matched and the + * specified boolean property is set to <code>true</code> on the first + * matched element; <code>false</code> 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 <code>GQuery</code> object + * + */ + LazyGQuery<T> 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 <code>closure</code> 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 <code>Boolean</code> value or an + * object whose textual representation is converted to a + * <code>Boolean</code> value; <code>null</code> return values are + * ignored + * + * @return this <code>GQuery</code> object + * + */ + LazyGQuery<T> prop(String key, Function closure); + + /** * Put a {@link Function} at the end of the Effects queue. * * Example: 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("<input id=\"checkBox1\" type=\"checkbox\" checked=\"checked\" /> <input id=\"checkBox2\" type=\"checkbox\" />"); + + 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'"); |