aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorjdramaix <julien.dramaix@gmail.com>2013-12-06 00:26:55 +0100
committerjdramaix <julien.dramaix@gmail.com>2013-12-06 00:26:55 +0100
commit459fcfcc8115dcd158d0d8c600d8852a51a16320 (patch)
tree1770dd1e2d19e30b647830e2773127b9e993322a
parent0f3d35d4999c215d961197c86ee60319b648e696 (diff)
parentcf8a74bbc590f0acbc2503eeaeaa40843d218c5f (diff)
downloadgwtquery-459fcfcc8115dcd158d0d8c600d8852a51a16320.tar.gz
gwtquery-459fcfcc8115dcd158d0d8c600d8852a51a16320.zip
Merge branch 'master' of github.com:gwtquery/gwtquery into jd_fix_event_1
Conflicts: gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryEventsTestGwt.java
-rw-r--r--gwtquery-core/src/main/java/com/google/gwt/query/Query.gwt.xml5
-rw-r--r--gwtquery-core/src/main/java/com/google/gwt/query/QueryMin.gwt.xml5
-rw-r--r--gwtquery-core/src/main/java/com/google/gwt/query/client/Console.java101
-rw-r--r--gwtquery-core/src/main/java/com/google/gwt/query/client/GQuery.java16
-rw-r--r--gwtquery-core/src/main/java/com/google/gwt/query/client/LazyGQuery.java11
-rw-r--r--gwtquery-core/src/main/java/com/google/gwt/query/client/impl/ConsoleBrowser.java214
-rw-r--r--gwtquery-core/src/main/java/com/google/gwt/query/client/impl/SelectorEngineCssToXPath.java15
-rw-r--r--gwtquery-core/src/main/java/com/google/gwt/query/client/js/JsUtils.java12
-rw-r--r--gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/LazyEvents.java11
-rw-r--r--gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/deferred/FunctionDeferred.java73
-rw-r--r--gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/events/EventsListener.java1
-rw-r--r--gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryCoreTestGwt.java14
-rw-r--r--gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryEventsTestGwt.java8
-rw-r--r--gwtquery-core/src/test/java/com/google/gwt/query/client/GQuerySelectorsTestGwt.java16
-rw-r--r--gwtquery-core/src/test/java/com/google/gwt/query/client/deferred/DeferredTest.java53
-rw-r--r--gwtquery-core/src/test/java/com/google/gwt/query/client/impl/SelectorEnginesTest.java6
16 files changed, 506 insertions, 55 deletions
diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/Query.gwt.xml b/gwtquery-core/src/main/java/com/google/gwt/query/Query.gwt.xml
index a902fd99..520ceaa0 100644
--- a/gwtquery-core/src/main/java/com/google/gwt/query/Query.gwt.xml
+++ b/gwtquery-core/src/main/java/com/google/gwt/query/Query.gwt.xml
@@ -22,6 +22,11 @@
<when-type-assignable class="com.google.gwt.query.client.Browser"/>
</generate-with>
+ <!-- GQuery.console -->
+ <replace-with class="com.google.gwt.query.client.impl.ConsoleBrowser">
+ <when-type-assignable class="com.google.gwt.query.client.Console"/>
+ </replace-with>
+
<!-- JSNI Generator -->
<generate-with class="com.google.gwt.query.rebind.JsniBundleGenerator">
<when-type-assignable class="com.google.gwt.query.client.builders.JsniBundle"/>
diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/QueryMin.gwt.xml b/gwtquery-core/src/main/java/com/google/gwt/query/QueryMin.gwt.xml
index c67ec2af..6453aba3 100644
--- a/gwtquery-core/src/main/java/com/google/gwt/query/QueryMin.gwt.xml
+++ b/gwtquery-core/src/main/java/com/google/gwt/query/QueryMin.gwt.xml
@@ -52,6 +52,11 @@
<when-type-assignable class="com.google.gwt.query.client.Browser"/>
</generate-with>
+ <!-- GQuery.console -->
+ <replace-with class="com.google.gwt.query.client.impl.ConsoleBrowser">
+ <when-type-assignable class="com.google.gwt.query.client.Console"/>
+ </replace-with>
+
<!-- Selector Engines -->
<replace-with class="com.google.gwt.query.client.impl.SelectorEngineNativeMin">
<when-type-assignable class="com.google.gwt.query.client.impl.SelectorEngineImpl"/>
diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/Console.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/Console.java
new file mode 100644
index 00000000..52c71d35
--- /dev/null
+++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/Console.java
@@ -0,0 +1,101 @@
+/*
+ * Copyright 2013, The gwtquery team.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+package com.google.gwt.query.client;
+
+
+/**
+ * This interface represents the Window.console object.
+ */
+public interface Console {
+
+ /**
+ * Clears the console.
+ */
+ void clear();
+
+ /**
+ * Displays an interactive listing of the properties of a specified JavaScript object. This
+ * listing lets you use disclosure triangles to examine the contents of child objects.
+ */
+ void dir(Object arg);
+
+ /**
+ * Outputs an error message. You may use string substitution and additional arguments with this method.
+ */
+ void error(Object arg);
+
+ /**
+ * Creates a new inline group, indenting all following output by another level. To move back out a
+ * level, call groupEnd().
+ */
+ void group(Object arg);
+
+ /**
+ * Creates a new inline group, indenting all following output by another level; unlike group(),
+ * this starts with the inline group collapsed, requiring the use of a disclosure button to expand
+ * it. To move back out a level, call groupEnd().
+ */
+ void groupCollapsed(Object arg);
+
+ /**
+ * Exits the current inline group.
+ */
+ void groupEnd();
+
+ /**
+ * Informative logging information. You may use string substitution and additional arguments with
+ * this method.
+ */
+ void info(Object arg);
+
+ /**
+ * For general output of logging information. You may use string substitution and additional
+ * arguments with this method.
+ */
+ void log(Object arg);
+
+ /**
+ * Starts a JavaScript CPU profile with a name. To complete the profile, call console.profileEnd().
+ */
+ void profile(String title);
+
+ /**
+ * Stops the current JavaScript CPU profiling session, if one is in progress, and prints the report.
+ */
+ void profileEnd(String title);
+
+ /**
+ * Starts a timer with a name specified as an input parameter. Up to 10,000 simultaneous timers
+ * can run on a given page.
+ */
+ void time(String title);
+
+ /**
+ * Stops the specified timer and logs the elapsed time in seconds since its start.
+ */
+ void timeEnd(String title);
+
+ /**
+ * Outputs a timestamp.
+ */
+ void timeStamp(Object arg);
+
+ /**
+ * Outputs a warning message. You may use string substitution and additional arguments with this
+ * method.
+ */
+ void warn(Object arg);
+}
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 479f90ab..e4f76a35 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
@@ -123,6 +123,11 @@ public class GQuery implements Lazy<GQuery, LazyGQuery> {
public static final Browser browser = GWT.isClient() ? GWT.<Browser>create(Browser.class) : null;
/**
+ * An object with the same methods than window.console.
+ */
+ public static final Console console = GWT.isClient() ? GWT.<Console>create(Console.class) : null;
+
+ /**
* Object to store element data (public so as we can access to it from tests).
*/
public static JsCache dataCache = null;
@@ -703,13 +708,6 @@ public class GQuery implements Lazy<GQuery, LazyGQuery> {
}
/**
- * Dump an object to the window.console.log when available
- */
- public static void log(Object o) {
- JsUtils.log(o);
- }
-
- /**
* Perform an ajax request to the server using POST.
*/
public static Promise post(String url, Properties data, final Function onSuccess) {
@@ -2483,9 +2481,11 @@ public class GQuery implements Lazy<GQuery, LazyGQuery> {
* 'in' to a frequent task. Whenever the mouse cursor is moved over a matched element, the first
* specified function is fired. Whenever the mouse moves off of the element, the second specified
* function fires.
+ *
+ * Since GQuery 1.4.0, this method binds handlers for both mouseenter and mouseleave events.
*/
public GQuery hover(Function fover, Function fout) {
- return bind(Event.ONMOUSEOVER, null, fover).bind(Event.ONMOUSEOUT, null, fout);
+ return bind("mouseenter", null, fover).bind("mouseleave", null, fout);
}
/**
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 da0de164..2d9ce894 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
@@ -22,6 +22,7 @@ import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.core.client.JsArray;
import com.google.gwt.core.client.JsArrayMixed;
import com.google.gwt.core.client.JsArrayString;
+import com.google.gwt.core.client.ScriptInjector;
import com.google.gwt.dom.client.*;
import com.google.gwt.dom.client.Style.Display;
import com.google.gwt.dom.client.Style.HasCssName;
@@ -1116,6 +1117,8 @@ public interface LazyGQuery<T> extends LazyBase<T>{
* 'in' to a frequent task. Whenever the mouse cursor is moved over a matched element, the first
* specified function is fired. Whenever the mouse moves off of the element, the second specified
* function fires.
+ *
+ * Since GQuery 1.4.0, this method binds handlers for both mouseenter and mouseleave events.
*/
LazyGQuery<T> hover(Function fover, Function fout);
@@ -2228,6 +2231,14 @@ public interface LazyGQuery<T> extends LazyBase<T>{
LazyGQuery<T> trigger(int eventbits, int... keys);
/**
+ * Trigger a event in all matched elements.
+ *
+ * @param eventName An string representing the type of the event desired
+ * @param datas Additional parameters to pass along to the event handlers.
+ */
+ LazyGQuery<T> trigger(String eventName, Object... datas);
+
+ /**
* Removes all events that match the eventbits.
*/
LazyGQuery<T> unbind(int eventbits);
diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/impl/ConsoleBrowser.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/impl/ConsoleBrowser.java
new file mode 100644
index 00000000..ec63c852
--- /dev/null
+++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/impl/ConsoleBrowser.java
@@ -0,0 +1,214 @@
+/*
+ * Copyright 2013, The gwtquery team.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+package com.google.gwt.query.client.impl;
+
+import com.google.gwt.core.client.JavaScriptObject;
+import com.google.gwt.query.client.Console;
+import com.google.gwt.query.client.GQuery;
+
+/**
+ * Implementation of the Console interface based on the
+ * browser console.
+ */
+public class ConsoleBrowser implements Console {
+
+ /**
+ * http://whattheheadsaid.com/2011/04/internet-explorer-9s-problematic-console-object
+ */
+ private static class ConsoleIe8 extends ConsoleIe9 {
+ @Override
+ protected native void init()/*-{
+ Function.prototype.call.call($wnd.console.log, $wnd.console, Array.prototype.slice.call(arguments));
+ }-*/;
+ }
+
+ /**
+ * See: http://whattheheadsaid.com/2011/04/internet-explorer-9s-problematic-console-object
+ */
+ private static class ConsoleIe9 extends ConsoleImpl {
+ public ConsoleIe9(){
+ init();
+ }
+
+ protected native void init()/*-{
+ [ "log", "info", "warn", "error", "dir", "clear", "profile", "profileEnd" ]
+ .forEach(function(method) {
+ $wnd.console[method] = this.call($wnd.console[method], $wnd.console);
+ }, Function.prototype.bind);
+ }-*/;
+
+ @Override
+ public void group(Object arg) {}
+ @Override
+ public void groupCollapsed(Object arg) {}
+ @Override
+ public void groupEnd() {}
+ @Override
+ public void time(String title) {}
+ @Override
+ public void timeStamp(Object arg) {}
+ @Override
+ public void timeEnd(String title) {}
+ }
+
+ /**
+ * Default implementation: webkit, opera, FF, ie10
+ */
+ private static class ConsoleImpl {
+ public native void clear() /*-{
+ $wnd.console.clear();
+ }-*/;
+
+ public native void dir(Object arg) /*-{
+ $wnd.console.dir(arg);
+ }-*/;
+
+ public native void error(Object arg) /*-{
+ $wnd.console.error(arg);
+ }-*/;
+
+ public native void group(Object arg) /*-{
+ $wnd.console.group(arg);
+ }-*/;
+
+ public native void groupCollapsed(Object arg) /*-{
+ $wnd.console.groupCollapsed(arg);
+ }-*/;
+
+ public native void groupEnd() /*-{
+ $wnd.console.groupEnd();
+ }-*/;
+
+ public native void info(Object arg) /*-{
+ $wnd.console.info(arg);
+ }-*/;
+
+ public native void log(Object arg) /*-{
+ $wnd.console.log(arg);
+ }-*/;
+
+ public native void profile(String title) /*-{
+ $wnd.console.profile(title);
+ }-*/;
+
+ public native void profileEnd(String title) /*-{
+ $wnd.console.profileEnd(title);
+ }-*/;
+
+ public native void time(String title) /*-{
+ $wnd.console.time(title);
+ }-*/;
+
+ public native void timeEnd(String title) /*-{
+ $wnd.console.timeEnd(title);
+ }-*/;
+
+ public native void timeStamp(Object arg) /*-{
+ $wnd.console.timeStamp(arg);
+ }-*/;
+
+ public native void warn(Object arg) /*-{
+ $wnd.console.warn(arg);
+ }-*/;
+ }
+
+ private ConsoleImpl impl;
+
+ public ConsoleBrowser() {
+ impl = GQuery.browser.ie8? new ConsoleIe8(): GQuery.browser.ie9? new ConsoleIe9(): new ConsoleImpl();
+ }
+
+ @Override
+ public void clear() {
+ impl.clear();
+ }
+
+ @Override
+ public void dir(Object arg) {
+ impl.dir(toJs(arg));
+ }
+
+ @Override
+ public void error(Object arg) {
+ impl.error(toJs(arg));
+ }
+
+ @Override
+ public void group(Object arg) {
+ impl.group(toJs(arg));
+ }
+
+ @Override
+ public void groupCollapsed(Object arg) {
+ impl.groupCollapsed(toJs(arg));
+ }
+
+ @Override
+ public void groupEnd() {
+ impl.groupEnd();
+ }
+
+ @Override
+ public void info(Object arg) {
+ impl.info(toJs(arg));
+ }
+
+ @Override
+ public void log(Object arg) {
+ impl.log(toJs(arg));
+ }
+
+ @Override
+ public void profile(String title) {
+ impl.profile(title);
+ }
+
+ @Override
+ public void profileEnd(String title) {
+ impl.profileEnd(title);
+ }
+
+ @Override
+ public void time(String title) {
+ impl.time(title);
+ }
+
+ @Override
+ public void timeEnd(String title) {
+ impl.timeEnd(title);
+ }
+
+ @Override
+ public void timeStamp(Object arg) {
+ impl.timeStamp(toJs(arg));
+ }
+
+ @Override
+ public void warn(Object arg) {
+ impl.warn(toJs(arg));
+ }
+
+ /**
+ * Don't pass GWT Objects to JS methods
+ */
+ private Object toJs(Object arg) {
+ if (arg instanceof JavaScriptObject || arg instanceof String) {
+ return arg;
+ } else {
+ return String.valueOf(arg);
+ }
+ }
+}
diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/impl/SelectorEngineCssToXPath.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/impl/SelectorEngineCssToXPath.java
index e5ec73a6..a78c1b5d 100644
--- a/gwtquery-core/src/main/java/com/google/gwt/query/client/impl/SelectorEngineCssToXPath.java
+++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/impl/SelectorEngineCssToXPath.java
@@ -78,27 +78,29 @@ public class SelectorEngineCssToXPath extends SelectorEngineImpl {
String s1 = s.get(1);
String s2 = s.get(2);
- boolean noPrefix = s1 == null || s1.length() == 0;
+ boolean afterAttr = "]".equals(s1);
+ String prefix = afterAttr ? s1 : "*";
+ boolean noPrefix = afterAttr || s1 == null || s1.length() == 0 ;
if ("n".equals(s2)) {
return s1;
}
if ("even".equals(s2)) {
- return "*[position() mod 2=0 and position()>=0]" + (noPrefix ? "" : "/self::" + s1);
+ return prefix + "[position() mod 2=0 and position()>=0]" + (noPrefix ? "" : "/self::" + s1);
}
if ("odd".equals(s2)) {
- String prefix = noPrefix ? "" : s1;
+ prefix = afterAttr ? prefix : noPrefix ? "" : s1;
return prefix + "[(count(preceding-sibling::*) + 1) mod 2=1]";
}
if (!s2.contains("n")){
- return "*[position() = "+s2+"]" + (noPrefix ? "" : "/self::" + s1);
+ return prefix + "[position() = "+s2+"]" + (noPrefix ? "" : "/self::" + s1);
}
String[] t = s2.replaceAll("^([0-9]*)n.*?([0-9]*)?$", "$1+$2").split("\\+");
String t0 = t[0];
String t1 = t.length > 1 ? t[1] : "0";
- return "*[(position()-" + t1 + ") mod " + t0 + "=0 and position()>=" + t1 + "]" + (noPrefix ? "" : "/self::" + s1);
+ return prefix + "[(position()-" + t1 + ") mod " + t0 + "=0 and position()>=" + t1 + "]" + (noPrefix ? "" : "/self::" + s1);
}
};
@@ -135,7 +137,7 @@ public class SelectorEngineCssToXPath extends SelectorEngineImpl {
// :not
"(.+):not\\(([^\\)]*)\\)", rc_Not,
// :nth-child
- "([a-zA-Z0-9\\_\\-\\*]*):nth-child\\(([^\\)]*)\\)", rc_nth_child,
+ "([a-zA-Z0-9\\_\\-\\*]*|\\]):nth-child\\(([^\\)]*)\\)", rc_nth_child,
// :contains(selectors)
":contains\\(([^\\)]*)\\)", "[contains(string(.),'$1')]",
// |= attrib
@@ -270,5 +272,4 @@ public class SelectorEngineCssToXPath extends SelectorEngineImpl {
return elm;
}
}
-
}
diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/js/JsUtils.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/js/JsUtils.java
index 9790b12f..ecd61d22 100644
--- a/gwtquery-core/src/main/java/com/google/gwt/query/client/js/JsUtils.java
+++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/js/JsUtils.java
@@ -26,7 +26,6 @@ import com.google.gwt.dom.client.NodeList;
import com.google.gwt.query.client.Function;
import com.google.gwt.query.client.GQuery;
import com.google.gwt.query.client.Properties;
-import com.google.gwt.query.client.plugins.ajax.Ajax;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.DOM;
@@ -520,15 +519,4 @@ public class JsUtils {
}
return ret;
}
-
- /**
- * Dump an object to the browser console.
- *
- * TODO: enable console in IE8 && IE9
- * http://snipplr.com/view/67830/
- */
- public static native void log(Object o) /*-{
- if ($wnd.console && typeof $wnd.console.log === 'function') $wnd.console.log('' + o);
- }-*/;
-
}
diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/LazyEvents.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/LazyEvents.java
index 741cfb85..c0689af6 100644
--- a/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/LazyEvents.java
+++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/LazyEvents.java
@@ -13,6 +13,7 @@
*/
package com.google.gwt.query.client.plugins;
import com.google.gwt.dom.client.Element;
+import com.google.gwt.dom.client.FormElement;
import com.google.gwt.dom.client.NativeEvent;
import com.google.gwt.dom.client.Node;
import com.google.gwt.query.client.Function;
@@ -132,12 +133,20 @@ public interface LazyEvents<T> extends LazyBase<T>{
/**
* Trigger a html event in all matched elements.
*
- * @param htmlEvent An string representing the html event desired
+ * @param htmlEvent A string representing the desired html event.
* @functions a set of function to run if the event is not canceled.
*/
LazyEvents<T> triggerHtmlEvent(String htmlEvent, Function... functions);
/**
+ * Trigger a html event in all matched elements.
+ *
+ * @param htmlEvent An string representing the desired html event.
+ * @functions a set of function to run if the event is not canceled.
+ */
+ LazyEvents<T> triggerHtmlEvent(String htmlEvent, Object[] datas, Function... functions);
+
+ /**
* Removes all handlers, that matches the events bits passed, from each element.
*
* Example: unbind(Event.ONCLICK | Event.ONMOUSEOVER)
diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/deferred/FunctionDeferred.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/deferred/FunctionDeferred.java
index 3b40fdce..771749f8 100644
--- a/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/deferred/FunctionDeferred.java
+++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/deferred/FunctionDeferred.java
@@ -21,29 +21,36 @@ import com.google.gwt.query.client.Promise.Deferred;
* execution in pipelined processes.
*
* They have access to the associated deferred object via a method which
- * will be called only in the case the previous promise is resolved
+ * will be called only in the case the previous promise is resolved.
+ *
+ * We can reuse the function and it will create new promises or will return
+ * the last one depending on the value of cache. It is very useful for caching
+ * server calls which we just want execute once like login, etc.
*
* <pre>
- * Promise doSomething = new PromiseFunction() {
+ * Function login = new FunctionDeferred() {
* @Override
* public void f(Deferred dfd) {
- * dfd.notify("hi");
- * dfd.resolve("done");
+ * dfd.resolve("logged in as james-bond at " + new Date());
* }
- * };
- *
- * doSomething.then(new FunctionDeferred() {
- * public void f(Deferred dfd) {
- * dfd.resolve("deferred " + arguments(0));
+ * }.withCache(RESOLVED);
+ *
+ * when(login)
+ * .then(new Function() {
+ * public void f() {
+ * String loginMessage = arguments(0);
* }
* });
* </pre>
*/
public abstract class FunctionDeferred extends Function {
-
+
+ public static enum CacheType {NONE, ALL, RESOLVED, REJECTED};
+
protected Deferred dfd;
public Function resolve, reject;
-
+ private CacheType cache = CacheType.NONE;
+
/**
* This function is called once the the previous promise in the
* pipe is resolved, and the new created deferred is available.
@@ -51,20 +58,46 @@ public abstract class FunctionDeferred extends Function {
* You have to override it, and resolve the new promise
*/
protected abstract void f(Deferred dfd);
-
+
/**
* This function is called when the previous promise in the pipe
* is resolved.
*/
public final Object f(Object... args) {
- return new PromiseFunction() {
- public void f(Deferred dfd) {
- FunctionDeferred.this.resolve = resolve;
- FunctionDeferred.this.reject = reject;
- FunctionDeferred.this.dfd = dfd;
- FunctionDeferred.this.f(dfd);
- }
- };
+ return dfd != null &&
+ (cache == CacheType.ALL ||
+ cache == CacheType.RESOLVED && dfd.promise().isResolved() ||
+ cache == CacheType.REJECTED && dfd.promise().isRejected())
+ ? dfd.promise()
+ : new PromiseFunction() {
+ public void f(Deferred dfd) {
+ FunctionDeferred.this.resolve = resolve;
+ FunctionDeferred.this.reject = reject;
+ FunctionDeferred.this.dfd = dfd;
+ FunctionDeferred.this.f(dfd);
+ }
+ };
}
+ /**
+ * Configure whether cache the promise the first time it is resolved
+ * or create new promises each time the function is executed.
+ *
+ * By default cache is disabled.
+ */
+ public FunctionDeferred withCache(CacheType type) {
+ cache = type;
+ return this;
+ }
+
+ /**
+ * Reset the cache so as a new invocation to this function will
+ * execute it instead of restoring old values from cache.
+ */
+ public FunctionDeferred resetCache() {
+ if (dfd != null && !dfd.promise().isPending()) {
+ dfd = null;
+ }
+ return this;
+ }
}
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 8ade0c8c..039ce0d1 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
@@ -820,5 +820,4 @@ public class EventsListener implements EventListener {
function.clean();
}
}
-
}
diff --git a/gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryCoreTestGwt.java b/gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryCoreTestGwt.java
index c86b67e4..50f99d6f 100644
--- a/gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryCoreTestGwt.java
+++ b/gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryCoreTestGwt.java
@@ -2049,4 +2049,18 @@ public class GQueryCoreTestGwt extends GWTTestCase {
assertEquals(ulParentsNumber + 2, result.size());
}
+ // issue #216 : https://github.com/gwtquery/gwtquery/issues/216
+ public void testDataAsInteger() {
+ $(e).html("<div id='target'></div>");
+
+ GQuery target = $("#target", e);
+
+ $("#target", e).data("intValue", new Integer(1));
+
+ Integer value = $("#target", e).data("intValue", Integer.class);
+
+ assertEquals(1, value.intValue());
+
+ }
+
}
diff --git a/gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryEventsTestGwt.java b/gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryEventsTestGwt.java
index 61e05ac9..b781d1d9 100644
--- a/gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryEventsTestGwt.java
+++ b/gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryEventsTestGwt.java
@@ -1596,6 +1596,14 @@ public class GQueryEventsTestGwt extends GWTTestCase {
assertEquals(1, handler.invokationCounter);
}
+ public void testIssue226() {
+ $(e).html("<div id='target'>");
+ GQuery target = $("#target", e);
+
+ // this should not throw a NPE
+ target.undelegate("li", "click");
+ }
+
// issue 25 : https://github.com/gwtquery/gwtquery/issues/25
public void testDelegateAfterUndelegateWithoutParameter() {
$(e).html(
diff --git a/gwtquery-core/src/test/java/com/google/gwt/query/client/GQuerySelectorsTestGwt.java b/gwtquery-core/src/test/java/com/google/gwt/query/client/GQuerySelectorsTestGwt.java
index efd8953d..08897f56 100644
--- a/gwtquery-core/src/test/java/com/google/gwt/query/client/GQuerySelectorsTestGwt.java
+++ b/gwtquery-core/src/test/java/com/google/gwt/query/client/GQuerySelectorsTestGwt.java
@@ -15,17 +15,13 @@
*/
package com.google.gwt.query.client;
-import static com.google.gwt.query.client.GQuery.$;
-import static com.google.gwt.query.client.GQuery.body;
-import static com.google.gwt.query.client.GQuery.document;
+import static com.google.gwt.query.client.GQuery.*;
import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.JsArray;
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.junit.DoNotRunWith;
-import com.google.gwt.junit.Platform;
import com.google.gwt.junit.client.GWTTestCase;
import com.google.gwt.query.client.impl.SelectorEngineCssToXPath;
import com.google.gwt.query.client.impl.SelectorEngineImpl;
@@ -407,7 +403,7 @@ public class GQuerySelectorsTestGwt extends GWTTestCase {
public void testOddEvenNthChild(){
$(e).append(
- "<div id='parent'><div id='first' class='evenClass'>branchA target</div><div id='second' class='oddClass'>branchA target</div><div id='third' class='evenClass'>branchA target</div><div id='fourth' class='oddClass'>branchA target</div></div>");
+ "<div id='parent'><div id='first' class='evenClass' role='treeItem'>branchA target</div><div id='second' class='oddClass' role='treeItem'>branchA target</div><div id='third' class='evenClass' role='treeItem'>branchA target</div><div id='fourth' class='oddClass' role='treeItem'>branchA target</div></div>");
GQuery odd = $("#parent > div:odd",e);
assertEquals(2, odd.size());
@@ -449,6 +445,14 @@ public class GQuerySelectorsTestGwt extends GWTTestCase {
assertEquals("second", secondAndFourth.eq(0).attr("id"));
assertEquals("fourth", secondAndFourth.eq(1).attr("id"));
+ // odd and even with attribute filters
+ secondAndFourth = $("div[role=treeItem]:odd", e);
+ assertEquals("second", secondAndFourth.eq(0).attr("id"));
+ assertEquals("fourth", secondAndFourth.eq(1).attr("id"));
+
+ GQuery treeItemFirstAndThird = $("div[role=treeItem]:even", e);
+ assertEquals("first", treeItemFirstAndThird.eq(0).attr("id"));
+ assertEquals("third", treeItemFirstAndThird.eq(1).attr("id"));
}
private void assertArrayContains(Object result, Object... array) {
diff --git a/gwtquery-core/src/test/java/com/google/gwt/query/client/deferred/DeferredTest.java b/gwtquery-core/src/test/java/com/google/gwt/query/client/deferred/DeferredTest.java
index 45eb6064..096e31bd 100644
--- a/gwtquery-core/src/test/java/com/google/gwt/query/client/deferred/DeferredTest.java
+++ b/gwtquery-core/src/test/java/com/google/gwt/query/client/deferred/DeferredTest.java
@@ -24,6 +24,7 @@ import com.google.gwt.query.client.GQuery;
import com.google.gwt.query.client.Promise.Deferred;
import com.google.gwt.query.client.plugins.deferred.Callbacks;
import com.google.gwt.query.client.plugins.deferred.Callbacks.Callback;
+import com.google.gwt.query.client.plugins.deferred.FunctionDeferred.CacheType;
import com.google.gwt.query.client.plugins.deferred.FunctionDeferred;
import com.google.gwt.query.client.plugins.deferred.PromiseFunction;
@@ -371,4 +372,56 @@ public class DeferredTest extends GWTTestCase {
});
}
+ private Boolean deferredData;
+
+ public void testFunctionDeferredCache() {
+
+ FunctionDeferred cachedFunction = new FunctionDeferred() {
+ protected void f(Deferred dfd) {
+ dfd.resolve(deferredData);
+ }
+ };
+
+ Function setDeferredDataToTrue = new Function(){
+ public void f() {
+ deferredData = true;
+ }
+ };
+
+ Function setDeferredDataToFalse = new Function() {
+ public void f() {
+ deferredData = false;
+ }
+ };
+
+ Function assertDeferredDataIsFalse = new Function() {
+ public void f() {
+ Boolean data = arguments(0);
+ assertFalse(data);
+ }
+ };
+
+ Function assertDeferredDataIsTrue = new Function() {
+ public void f() {
+ Boolean data = arguments(0);
+ assertTrue(data);
+ }
+ };
+
+ when(setDeferredDataToTrue, cachedFunction.withCache(CacheType.ALL))
+ .always(setDeferredDataToFalse)
+ .done(assertDeferredDataIsTrue)
+ .then(cachedFunction)
+ .done(assertDeferredDataIsTrue)
+ .then(cachedFunction.withCache(CacheType.REJECTED))
+ .done(assertDeferredDataIsFalse)
+ .always(setDeferredDataToTrue)
+ .then(cachedFunction.withCache(CacheType.RESOLVED))
+ .done(assertDeferredDataIsFalse)
+ .then(cachedFunction.resetCache())
+ .done(assertDeferredDataIsTrue)
+ .then(cachedFunction)
+ .done(assertDeferredDataIsTrue)
+ ;
+ }
}
diff --git a/gwtquery-core/src/test/java/com/google/gwt/query/client/impl/SelectorEnginesTest.java b/gwtquery-core/src/test/java/com/google/gwt/query/client/impl/SelectorEnginesTest.java
index 7b8199ee..765dcfab 100644
--- a/gwtquery-core/src/test/java/com/google/gwt/query/client/impl/SelectorEnginesTest.java
+++ b/gwtquery-core/src/test/java/com/google/gwt/query/client/impl/SelectorEnginesTest.java
@@ -77,6 +77,12 @@ public class SelectorEnginesTest extends GWTTestCase {
assertEquals(".//div[@class='comment' and (contains(string(.),'John'))]",
sel.css2Xpath("div[@class='comment']:contains('John')"));
+
+ assertEquals(".//div[@role='treeItem' and (position() mod 2=0 and position()>=0)]",
+ sel.css2Xpath("div[role=treeItem]:odd"));
+
+ assertEquals(".//div[@role='treeItem' and ((count(preceding-sibling::*) + 1) mod 2=1)]",
+ sel.css2Xpath("div[role=treeItem]:even"));
}
}