*/
package com.google.gwt.query.client;
-import com.google.gwt.dom.client.Element;
+import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.ui.Widget;
/**
* Override this for methods which invoke a cancel action.
+ *
+ * @param e takes a com.google.gwt.user.client.Element.
+ *
*/
public void cancel(Element e) {
}
+ /**
+ * Override this for methods which invoke a cancel action.
+ *
+ * @param e takes a com.google.gwt.dom.client.Element.
+ *
+ */
+ public void cancel(com.google.gwt.dom.client.Element e) {
+ cancel((Element)e);
+ }
+
/**
* Override this to define a function which does not need any parameter.
*/
public void f() {
- throw new RuntimeException("You have to override the adequate method to handle this action, or you have to override 'public void f()' to avoid this error");
+ throw new RuntimeException("You have to override the adequate method to handle " +
+ "this action, or you have to override 'public void f()' to avoid this error");
}
/**
* Override this for GQuery methods which loop over matched elements and
* invoke a callback on each element.
+ *
+ * @param e takes a com.google.gwt.user.client.Element.
+ *
*/
- public <W> W f(Element e, int i) {
+ public Object f(Element e, int i) {
Widget w = GQuery.getAssociatedWidget(e);
if (w != null){
f(w, i);
} else {
- f((com.google.gwt.user.client.Element)e);
+ f(e);
}
return null;
}
+ /**
+ * Override this for GQuery methods which loop over matched elements and
+ * invoke a callback on each element.
+ *
+ * @param e takes a com.google.gwt.dom.client.Element.
+ *
+ */
+ public Object f(com.google.gwt.dom.client.Element e, int i) {
+ return f(e.<Element>cast(), i);
+ }
+
/**
* Override this for GQuery methods which loop over matched widgets and
* invoke a callback on each widget.
+ *
+ * NOTE: If your query has non-widget elements you might need to override
+ * 'public void f()' or 'public void f(Element e)' to handle these elements and
+ * avoid a runtime exception.
*/
- public <W> W f(Widget w, int i) {
- f(w.getElement());
+ public Object f(Widget w, int i) {
+ f(w);
return null;
}
* Override this method for bound event handlers.
*/
public boolean f(Event e) {
- f((com.google.gwt.user.client.Element)e.getCurrentEventTarget().cast());
+ f((Element)e.getCurrentEventTarget().cast());
return true;
}
* Override this for GQuery methods which take a callback and do not expect a
* return value.
*
- * @param e takes a com.google.gwt.dom.client.Element
+ * @param e takes a com.google.gwt.user.client.Element
*/
public void f(Element e) {
Widget w = GQuery.getAssociatedWidget(e);
* Override this for GQuery methods which take a callback and do not expect a
* return value.
*
- * @param e takes a com.google.gwt.user.client.Element
+ * @param e takes a com.google.gwt.dom.client.Element
*/
- public void f(com.google.gwt.user.client.Element e) {
+ public void f(com.google.gwt.dom.client.Element e) {
f((Element)e);
}
* Override this for GQuery methods which take a callback, but do not expect a
* return value, apply to a single widget.
*
- * NOTE: If your query is returning non-widget elements you might need to override
+ * NOTE: If your query has non-widget elements you might need to override
* 'public void f()' or 'public void f(Element e)' to handle these elements and
- * avoid a runtime exception.
+ * avoid a runtime exception.
*/
public void f(Widget w){
+ // Do not call f(e) here to avoid loop
f();
}
return this;\r
}\r
\r
+ /**\r
+ * Get the id of the first matched element.\r
+ */\r
+ public String id() {\r
+ return attr("id");\r
+ }\r
+\r
+ /**\r
+ * Set the id of the first matched element.\r
+ */\r
+ public GQuery id(String id) {\r
+ return eq(0).attr("id", id);\r
+ }\r
+\r
/**\r
* Find the index of the specified Element.\r
*/\r
/**\r
* Pass each element in the current matched set through a function, producing\r
* a new array containing the return values.\r
+ * When the call to the function returns a null it is not added to the array.\r
*/\r
public <W> List<W> map(Function f) {\r
ArrayList<W> ret = new ArrayList<W>();\r
for (int i = 0; i < elements().length; i++) {\r
- W o = f.<W>f(elements()[i], i);\r
+ @SuppressWarnings("unchecked")\r
+ W o = (W)f.f(elements()[i], i);\r
if (o != null) {\r
ret.add(o);\r
}\r
import static com.google.gwt.query.client.GQuery.$$;
import static com.google.gwt.query.client.GQuery.document;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Comparator;
import java.util.List;
import junit.framework.Assert;
assertNull(close.get("#unknown"));
}
+
+ public void testMap() {
+ String html = "<div class='d' id='6'></div><span class='s' id='5'></span><p class='p' id='4'></p><em class='d' id='3'></em><b class='s' id='2'></b><i class='p' id='1'></i><strong></strong>";
+ $(e).html(html);
+
+ GQuery c = $(e).children();
+ assertEquals(8, c.size());
+
+ // A list of lists containing tag,class,id, remove elements without id
+ List<List<String>> m = $(e).children().map(new Function() {
+ @SuppressWarnings("unchecked")
+ public List<String> f(Element e, int i) {
+ // map does not add to the list null elements
+ if ($(e).attr("id").isEmpty() || $(e).attr("class").isEmpty()) {
+ return null;
+ }
+ List<String> attrs = new ArrayList<String>();
+ attrs.add(e.getTagName());
+ attrs.add($(e).attr("class"));
+ attrs.add($(e).attr("id"));
+ return attrs;
+ }
+ });
+ assertEquals(6, m.size());
+
+ // Sort the list by id
+ assertEquals("div", m.get(0).get(0).toLowerCase());
+ assertEquals("i", m.get(5).get(0).toLowerCase());
+ Collections.sort(m, new Comparator<List<String>>() {
+ public int compare(List<String> o1, List<String> o2) {
+ return o1.get(2).compareTo(o2.get(2));
+ }
+ });
+ assertEquals("div", m.get(5).get(0).toLowerCase());
+ assertEquals("i", m.get(0).get(0).toLowerCase());
+ }
}