aboutsummaryrefslogtreecommitdiffstats
path: root/gwtquery-core/src
diff options
context:
space:
mode:
authorManolo Carrasco <manolo@apache.org>2011-04-13 10:20:57 +0000
committerManolo Carrasco <manolo@apache.org>2011-04-13 10:20:57 +0000
commita523c5d1c282e53d9be32b9ab3556d4d634300a8 (patch)
tree0ffb75529516dce29b88b978e0b451e37da88505 /gwtquery-core/src
parent287f728c3ff9467adbcf2287dd45ea606145bf74 (diff)
downloadgwtquery-a523c5d1c282e53d9be32b9ab3556d4d634300a8.tar.gz
gwtquery-a523c5d1c282e53d9be32b9ab3556d4d634300a8.zip
remove java.util.Map in GQuery class (closest method)
Diffstat (limited to 'gwtquery-core/src')
-rw-r--r--gwtquery-core/src/main/java/com/google/gwt/query/client/GQuery.java45
-rw-r--r--gwtquery-core/src/main/java/com/google/gwt/query/client/LazyGQuery.java15
-rw-r--r--gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/events/EventsListener.java16
-rw-r--r--gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryCoreTest.java33
4 files changed, 49 insertions, 60 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 4698bcae..e946c434 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
@@ -19,6 +19,10 @@ import static com.google.gwt.query.client.plugins.Effects.Effects;
import static com.google.gwt.query.client.plugins.Events.Events;
import static com.google.gwt.query.client.plugins.SimpleNamedQueue.SimpleNamedQueue;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.core.client.JsArray;
@@ -32,9 +36,9 @@ 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.TextAreaElement;
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;
@@ -43,6 +47,7 @@ import com.google.gwt.query.client.impl.DocumentStyleImpl;
import com.google.gwt.query.client.impl.SelectorEngine;
import com.google.gwt.query.client.js.JsCache;
import com.google.gwt.query.client.js.JsMap;
+import com.google.gwt.query.client.js.JsNamedArray;
import com.google.gwt.query.client.js.JsNodeArray;
import com.google.gwt.query.client.js.JsUtils;
import com.google.gwt.query.client.plugins.Effects;
@@ -56,12 +61,6 @@ import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.GqUi;
import com.google.gwt.user.client.ui.Widget;
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-
/**
* GwtQuery is a GWT clone of the popular jQuery library.
*/
@@ -1013,7 +1012,7 @@ public class GQuery implements Lazy<GQuery, LazyGQuery> {
* @param selector
* @return
*/
- public Map<String, List<Element>> closest(String[] selectors) {
+ public JsNamedArray<NodeList<Element>> closest(String[] selectors) {
return closest(selectors, null);
}
@@ -1027,56 +1026,46 @@ public class GQuery implements Lazy<GQuery, LazyGQuery> {
* @param selector
* @return
*/
- public Map<String, List<Element>> closest(String[] selectors, Node context) {
- Map<String, List<Element>> results = new HashMap<String, List<Element>>();
+ public JsNamedArray<NodeList<Element>> closest(String[] selectors, Node context) {
+ JsNamedArray<NodeList<Element>> results = JsNamedArray.create();
if (context == null) {
context = currentContext;
}
Element first = get(0);
-
if (first != null && selectors != null && selectors.length > 0) {
-
- Map<String, GQuery> matches = new HashMap<String, GQuery>();
-
+ JsNamedArray<GQuery> matches = JsNamedArray.create();
for (String selector : selectors) {
- if (!matches.containsKey(selector)) {
+ if (!matches.exists(selector)) {
matches.put(selector, selector.matches(POS_REGEX) ? $(selector,
context) : null);
}
}
Element current = first;
-
while (current != null && current.getOwnerDocument() != null
&& current != context) {
// for each selector, check if the current element match it.
- for (String selector : matches.keySet()) {
+ for (String selector : matches.keys()) {
GQuery pos = matches.get(selector);
-
boolean match = pos != null ? pos.index(current) > -1
: $(current).is(selector);
if (match) {
-
- List<Element> elementsMatchingSelector = results.get(selector);
-
+ JsNodeArray elementsMatchingSelector = results.get(selector).cast();
if (elementsMatchingSelector == null) {
- elementsMatchingSelector = new ArrayList<Element>();
+ elementsMatchingSelector = JsNodeArray.create();
results.put(selector, elementsMatchingSelector);
}
-
- elementsMatchingSelector.add(current);
+ elementsMatchingSelector.addNode(current);
}
}
current = current.getParentElement();
}
-
}
-
return results;
}
@@ -2305,6 +2294,7 @@ public class GQuery implements Lazy<GQuery, LazyGQuery> {
*/
@SuppressWarnings("unchecked")
public <W> List<W> map(Function f) {
+ @SuppressWarnings("rawtypes")
ArrayList ret = new ArrayList();
for (int i = 0; i < elements().length; i++) {
Object o = f.f(elements()[i], i);
@@ -3899,7 +3889,7 @@ public class GQuery implements Lazy<GQuery, LazyGQuery> {
}
private GQuery domManip(String htmlString, int func) {
- HashMap<Document, GQuery> cache = new HashMap<Document, GQuery>();
+ JsMap<Document, GQuery> cache = JsMap.createObject().cast();
for (Element e : elements()) {
Document d = e.getNodeType() == Node.DOCUMENT_NODE ? e.<Document> cast()
: e.getOwnerDocument();
@@ -3910,7 +3900,6 @@ public class GQuery implements Lazy<GQuery, LazyGQuery> {
}
domManip(g.clone(), func, e);
}
-
return this;
}
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 6fe42c1a..a499cff9 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
@@ -17,6 +17,9 @@ package com.google.gwt.query.client;
import static com.google.gwt.query.client.plugins.Effects.Effects;
import static com.google.gwt.query.client.plugins.Events.Events;
import static com.google.gwt.query.client.plugins.SimpleNamedQueue.SimpleNamedQueue;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.core.client.JsArray;
@@ -30,9 +33,9 @@ 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.TextAreaElement;
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;
@@ -41,6 +44,7 @@ import com.google.gwt.query.client.impl.DocumentStyleImpl;
import com.google.gwt.query.client.impl.SelectorEngine;
import com.google.gwt.query.client.js.JsCache;
import com.google.gwt.query.client.js.JsMap;
+import com.google.gwt.query.client.js.JsNamedArray;
import com.google.gwt.query.client.js.JsNodeArray;
import com.google.gwt.query.client.js.JsUtils;
import com.google.gwt.query.client.plugins.Effects;
@@ -53,11 +57,6 @@ import com.google.gwt.user.client.EventListener;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.GqUi;
import com.google.gwt.user.client.ui.Widget;
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
import com.google.gwt.query.client.LazyBase;
public interface LazyGQuery<T> extends LazyBase<T>{
@@ -433,7 +432,7 @@ public interface LazyGQuery<T> extends LazyBase<T>{
* @param selector
* @return
*/
- Map<String, List<Element>> closest(String[] selectors);
+ JsNamedArray<NodeList<Element>> closest(String[] selectors);
/**
* Returns a {@link Map} object as key a selector and as value the list of
@@ -445,7 +444,7 @@ public interface LazyGQuery<T> extends LazyBase<T>{
* @param selector
* @return
*/
- Map<String, List<Element>> closest(String[] selectors, Node context);
+ JsNamedArray<NodeList<Element>> closest(String[] selectors, Node context);
/**
* Get the first ancestor element that matches the selector (for each matched
diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/events/EventsListener.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/events/EventsListener.java
index d673025a..27b34f37 100644
--- a/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/events/EventsListener.java
+++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/events/EventsListener.java
@@ -25,7 +25,9 @@ import java.util.Map;
import com.google.gwt.core.client.Duration;
import com.google.gwt.dom.client.Element;
import com.google.gwt.dom.client.EventTarget;
+import com.google.gwt.dom.client.NodeList;
import com.google.gwt.query.client.Function;
+import com.google.gwt.query.client.js.JsNamedArray;
import com.google.gwt.query.client.js.JsObjectArray;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Event;
@@ -117,7 +119,6 @@ public class EventsListener implements EventListener {
@Override
public boolean fire(Event event) {
-
if (isEmpty()) {
return true;
}
@@ -134,11 +135,11 @@ public class EventsListener implements EventListener {
return true;
}
- Map<String, List<Element>> realCurrentTargetBySelector = $(eventTarget).closest(
+ JsNamedArray<NodeList<Element>> realCurrentTargetBySelector = $(eventTarget).closest(
selectors, liveContextElement);
// nothing match the selectors
- if (realCurrentTargetBySelector.isEmpty()) {
+ if (realCurrentTargetBySelector.length() == 0) {
return true;
}
@@ -146,15 +147,16 @@ public class EventsListener implements EventListener {
com.google.gwt.query.client.plugins.events.Event gqEvent = com.google.gwt.query.client.plugins.events.Event.create(event);
- for (String cssSelector : realCurrentTargetBySelector.keySet()) {
+ for (String cssSelector : realCurrentTargetBySelector.keys()) {
List<BindFunction> bindFunctions = bindFunctionBySelector.get(cssSelector);
-
if (bindFunctions == null){
continue;
}
for (BindFunction f : bindFunctions) {
- for (Element element : realCurrentTargetBySelector.get(cssSelector)) {
+ NodeList<Element> n = realCurrentTargetBySelector.get(cssSelector);
+ if (n != null ) for (int i = 0; i < n.getLength(); i++) {
+ Element element = n.getItem(i);
gqEvent.setCurrentElementTarget(element);
boolean subResult = f.fire(gqEvent);
result &= subResult;
@@ -168,9 +170,7 @@ public class EventsListener implements EventListener {
// trick to reset the right currentTarget on the original event on ie
gqEvent.setCurrentElementTarget(liveContextElement);
-
return result;
-
}
/**
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 ed8d3b80..63084030 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
@@ -19,9 +19,14 @@ import static com.google.gwt.query.client.GQuery.$;
import static com.google.gwt.query.client.GQuery.$$;
import static com.google.gwt.query.client.GQuery.document;
+import java.util.List;
+
+import junit.framework.Assert;
+
import com.google.gwt.dom.client.Document;
import com.google.gwt.dom.client.Element;
import com.google.gwt.dom.client.Node;
+import com.google.gwt.dom.client.NodeList;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.junit.client.GWTTestCase;
@@ -29,6 +34,7 @@ import com.google.gwt.query.client.css.CSS;
import com.google.gwt.query.client.css.RGBColor;
import com.google.gwt.query.client.impl.SelectorEngineImpl;
import com.google.gwt.query.client.impl.SelectorEngineSizzle;
+import com.google.gwt.query.client.js.JsNamedArray;
import com.google.gwt.query.client.js.JsNodeArray;
import com.google.gwt.query.client.js.JsUtils;
import com.google.gwt.user.client.DOM;
@@ -39,11 +45,6 @@ import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextArea;
-import junit.framework.Assert;
-
-import java.util.List;
-import java.util.Map;
-
/**
* Test class for testing gwtquery-core api.
*/
@@ -1204,24 +1205,24 @@ public class GQueryCoreTest extends GWTTestCase {
String html = "<div id='mainDiv'><div id='subDiv' class='test'><div id='subSubDiv'><p id='mainP'><span id='testSpan' class='test'><input id='firstInput' type='text'></input></span></p></div></div></div>";
$(e).html(html);
- Map<String, List<Element>> close = $("input", e).closest(new String[]{"p","div", ".test", "#unknown"});
+ JsNamedArray<NodeList<Element>> close = $("input", e).closest(new String[]{"p","div", ".test", "#unknown"});
- assertEquals(3, close.size());
+ assertEquals(3, close.length());
assertNotNull(close.get("p"));
- assertEquals(1,close.get("p").size());
- assertEquals("mainP", close.get("p").get(0).getId());
+ assertEquals(1,close.get("p").getLength());
+ assertEquals("mainP", close.get("p").getItem(0).getId());
assertNotNull(close.get("div"));
- assertEquals(3,close.get("div").size());
- assertEquals("subSubDiv", close.get("div").get(0).getId());
- assertEquals("subDiv", close.get("div").get(1).getId());
- assertEquals("mainDiv", close.get("div").get(2).getId());
+ assertEquals(3,close.get("div").getLength());
+ assertEquals("subSubDiv", close.get("div").getItem(0).getId());
+ assertEquals("subDiv", close.get("div").getItem(1).getId());
+ assertEquals("mainDiv", close.get("div").getItem(2).getId());
assertNotNull(close.get(".test"));
- assertEquals(2,close.get(".test").size());
- assertEquals("testSpan", close.get(".test").get(0).getId());
- assertEquals("subDiv", close.get(".test").get(1).getId());
+ assertEquals(2,close.get(".test").getLength());
+ assertEquals("testSpan", close.get(".test").getItem(0).getId());
+ assertEquals("subDiv", close.get(".test").getItem(1).getId());
assertNull(close.get("#unknown"));