import com.google.gwt.query.client.js.JsNamedArray;
import com.google.gwt.query.client.js.JsNodeArray;
import com.google.gwt.query.client.js.JsObjectArray;
-import com.google.gwt.query.client.js.JsRegexp;
import com.google.gwt.query.client.js.JsUtils;
import com.google.gwt.query.client.plugins.Effects;
import com.google.gwt.query.client.plugins.Events;
import com.google.gwt.query.client.plugins.effects.PropertiesAnimation.Easing;
import com.google.gwt.query.client.plugins.events.EventsListener;
import com.google.gwt.query.client.plugins.widgets.WidgetsUtils;
+import com.google.gwt.regexp.shared.RegExp;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.EventListener;
/**
* The body element in the current page.
*/
- public static final BodyElement body = Document.get().getBody();
+ public static final BodyElement body = GWT.isClient() ? Document.get().getBody() : null;
/**
* Object to store element data (public so as we can access to it from tests).
/**
* The document element in the current page.
*/
- public static final Document document = Document.get();
+ public static final Document document = GWT.isClient() ? Document.get() : null;
/**
* Static reference Effects plugin
// Sizzle POS regex : usefull in some methods
// TODO: Share this static with SelectorEngineSizzle
- private static final String POS_REGEX =
- ":(nth|eq|gt|lt|first|last|even|odd)(?:\\((\\d*)\\))?(?=[^\\-]|$)";
+ private static RegExp posRegex = RegExp.compile("^:(nth|eq|gt|lt|first|last|even|odd)(?:\\((\\d*)\\))?(?=[^\\-]|$)$");
/**
* Implementation class used for style manipulations.
*/
private static DocumentStyleImpl styleImpl;
- private static JsRegexp tagNameRegex = new JsRegexp("<([\\w:]+)");
+ private static RegExp tagNameRegex = RegExp.compile("<([\\w:]+)");
/**
* Static reference to the Widgets plugin
/**
* The window object.
*/
- public static final Element window = window();
+ public static final Element window = GWT.isClient() ? window() : null;
private static Element windowData = null;
protected static GQuery cleanHtmlString(String elem, Document doc) {
- String tag = tagNameRegex.exec(elem).get(1);
+ String tag = tagNameRegex.exec(elem).getGroup(1);
if (tag == null) {
return $(doc.createTextNode(elem));
}
}
public static <T extends GQuery> Class<T> registerPlugin(Class<T> plugin, Plugin<T> pluginFactory) {
- if (plugins == null) {
- plugins = JsMap.createObject().cast();
+ // TODO: decide whether change plugins type to java.util.list
+ // Right now we only test static methods in gquery, so this is only needed when initializing
+ // plugins shortcuts in gquery.
+ if (GWT.isClient()) {
+ if (plugins == null) {
+ plugins = JsMap.createObject().cast();
+ }
+ plugins.put(plugin, pluginFactory);
}
-
- plugins.put(plugin, pluginFactory);
return plugin;
}
context = currentContext;
}
- GQuery pos = selector.matches(POS_REGEX) ? $(selector, context) : null;
+ GQuery pos = posRegex.test(selector) ? $(selector, context) : null;
JsNodeArray result = JsNodeArray.create();
for (Element e : elements) {
JsNamedArray<GQuery> matches = JsNamedArray.create();
for (String selector : selectors) {
if (!matches.exists(selector)) {
- matches.put(selector, selector.matches(POS_REGEX) ? $(selector, context) : null);
+ matches.put(selector, posRegex.test(selector) ? $(selector, context) : null);
}
}
*/
package com.google.gwt.query.client.plugins.deferred;
-import com.google.gwt.core.shared.GWT;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
import com.google.gwt.query.client.Function;
-import com.google.gwt.query.client.Properties;
-import com.google.gwt.query.client.builders.JsonBuilder;
-import com.google.gwt.query.client.js.JsObjectArray;
/**
* Implementation of jQuery.Callbacks for gwtquery.
boolean f(Object ...objects);
}
- /**
- * Interface representing the options of a Callbacks collection.
- *
- * To create an implementation of this interface just call: Callbacks.createOptions()
- */
- public static interface CallbackOptions extends JsonBuilder {
- boolean getMemory();
- boolean getOnce();
- boolean getStopOnFalse();
- boolean getUnique();
- CallbackOptions setMemory();
- CallbackOptions setOnce();
- CallbackOptions setStopOnFalse();
- CallbackOptions setUnique();
- }
-
- public static CallbackOptions createOptions() {
- return GWT.create(CallbackOptions.class);
- }
-
- private JsObjectArray<Object> stack = JsObjectArray.create();
+ private List<Object> stack = new ArrayList<Object>();
private boolean done = false;
- private JsObjectArray<Object> memory = null;
+ private List<Object> memory = null;
- public final CallbackOptions opts;
+ private boolean isOnce, isMemory, isUnique, stopOnFalse;
- /**
- * Create a new Callbacks object with default options
- */
public Callbacks() {
- opts = createOptions();
- }
-
- /**
- * Create a new Callbacks object with given options
- */
- public Callbacks(CallbackOptions options) {
- opts = options;
+ this("");
}
-
+
/**
* Create a new Callbacks object with options given as a space delimited string.
*
* once, memory, unique, stopOnFalse
*/
public Callbacks(String options) {
- this();
- opts.load(Properties.create(options.replaceAll("[^\\S]+|$", ":true,")));
+ isOnce = options.contains("once");
+ isMemory = options.contains("memory");
+ isUnique = options.contains("unique");
+ stopOnFalse = options.contains("stopOnFalse");
}
/**
* lock
*/
public Callbacks lock() {
- if (!opts.getMemory()) {
+ if (!isMemory) {
disable();
}
stack = null;
*/
public Callbacks fire(Object... o) {
if (!done) {
- done = opts.getOnce();
- if (stack != null) for (Object c : stack.elements()) {
- if (!run(c, o) && opts.getStopOnFalse()) {
+ done = isOnce;
+ if (stack != null) for (Object c : stack) {
+ if (!run(c, o) && stopOnFalse) {
break;
}
}
- if (opts.getMemory()) {
- memory = JsObjectArray.create().add(o);
+ if (isMemory) {
+ memory = new ArrayList<Object>(Arrays.asList(o));
}
}
return this;
* Remove a callback or a collection of callbacks from a callback list.
*/
public Callbacks remove(Object... o) {
- stack.remove(o);
+ stack.removeAll(Arrays.asList(o));
return this;
}
private void addAll(Object...o) {
for (Object c : o) {
- if (!done && stack != null && c != null && (!opts.getUnique() || !stack.contains(c))) {
+ if (!done && stack != null && c != null && (!isUnique || !stack.contains(c))) {
stack.add(c);
}
// In jQuery add always is run when memory is true even when unique is set
- if (opts.getMemory() && memory != null) {
- run(c, memory.elements());
+ if (isMemory && memory != null) {
+ run(c, memory.toArray());
}
}
}
@SuppressWarnings({"unchecked", "rawtypes"})
private boolean run(Object c, Object...o) {
- // Unbox array into array, it happens when running filters in Promise.then
+ // Unbox array inside array when there is only an element.
+ // It happens when running filters in Promise.then()
if (o.length == 1 && o[0].getClass().isArray()) {
o = (Object[])o[0];
}
}
public String status() {
- return (stack == null ? 0 : stack.length()) + " " + done;
+ return (stack == null ? 0 : stack.size()) + " " + done;
}
-}
+}
\ No newline at end of file
import com.google.gwt.core.client.Duration;
import com.google.gwt.junit.client.GWTTestCase;
-import com.google.gwt.query.client.Promise.Deferred;
import com.google.gwt.query.client.plugins.ajax.Ajax;
-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.PromiseFunction;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.Timer;
}
}
- private String result = "";
- public void testCallbacks() {
- Function fn1 = new Function() {
- public Object f(Object...arguments) {
- String s = " f1:";
- for (Object o: arguments){
- s += " " + o;
- }
- result += s;
- return false;
- }
- };
-
- Callback fn2 = new Callback() {
- public boolean f(Object... objects) {
- String s = " f2:";
- for (Object o: objects){
- s += " " + o;
- }
- result += s;
- return false;
- }
- };
-
- com.google.gwt.core.client.Callback<Object, Object> fn3 = new com.google.gwt.core.client.Callback<Object, Object>() {
- public void onFailure(Object reason) {
- result += " f3_fail: " + reason;
- }
- public void onSuccess(Object objects) {
- String s = " f3_success:";
- for (Object o: (Object[])objects){
- s += " " + o;
- }
- result += s;
- }
- };
-
- result = "";
- Callbacks callbacks = new Callbacks();
- callbacks.add( fn1 );
- callbacks.fire( "foo" );
- assertEquals(" f1: foo", result);
-
- result = "";
- callbacks.add( fn2 );
- callbacks.fire( "bar" );
- assertEquals(" f1: bar f2: bar", result);
-
- result = "";
- callbacks.remove( fn2 );
- callbacks.fire( "foobar" );
- assertEquals(" f1: foobar", result);
-
- result = "";
- callbacks.add( fn1 );
- callbacks.fire( "foo" );
- assertEquals(" f1: foo f1: foo", result);
-
- result = "";
- callbacks = new Callbacks("unique");
- callbacks.add( fn1 );
- callbacks.add( fn1 );
- callbacks.fire( "foo" );
- assertEquals(" f1: foo", result);
-
- result = "";
- callbacks.add( fn3 );
- callbacks.fire( "bar" );
- assertEquals(" f1: bar f3_success: bar", result);
-
- result = "";
- callbacks = new Callbacks("memory");
- callbacks.add( fn1 );
- callbacks.fire( "foo" );
- callbacks.add( fn2 );
- callbacks.fire( "bar" );
- callbacks.remove(fn2);
- callbacks.fire( "foobar" );
- assertEquals(" f1: foo f2: foo f1: bar f2: bar f1: foobar", result);
-
- result = "";
- callbacks = new Callbacks("stopOnFalse");
- callbacks.add( fn1 );
- callbacks.add( fn2 );
- callbacks.fire( "bar" );
- assertEquals(" f1: bar", result);
-
- result = "";
- callbacks.disable();
- callbacks.fire( "bar" );
- assertEquals("", result);
-
- result = "";
- callbacks = new Callbacks("memory once unique");
- callbacks.add( fn1 );
- callbacks.add( fn1 );
- callbacks.fire( "bar" );
- assertEquals(" f1: bar", result);
- callbacks.fire( "foo" );
- assertEquals(" f1: bar", result);
- callbacks.add( fn2 );
- callbacks.add( fn2 );
- assertEquals(" f1: bar f2: bar f2: bar", result);
- callbacks.remove( fn1 );
- callbacks.add( fn1 );
- assertEquals(" f1: bar f2: bar f2: bar f1: bar", result);
- callbacks.remove( fn1 );
- callbacks.disable();
- callbacks.add( fn1 );
- assertEquals(" f1: bar f2: bar f2: bar f1: bar", result);
- }
-
public void testDeferredAjaxWhenDone() {
String url = "https://www.googleapis.com/blogger/v2/blogs/user_id/posts/post_id?callback=?&key=NO-KEY";
});
}
- public void testThen() {
- new PromiseFunction() {
- public void f(final Deferred dfd) {
- dfd.resolve(5);
- }
- }.done(new Function() {
- public void f() {
- assertEquals(5d, arguments(0));
- }
- }).then(new Function() {
- public Object f(Object... args) {
- return (Double)args[0] * 2;
- }
- }).done(new Function() {
- public void f() {
- assertEquals(10d, arguments(0));
- }
- });
- }
-
public void testDeferredAjaxThenDone() {
final String url = "https://www.googleapis.com/blogger/v2/blogs/user_id/posts/post_id?callback=?&key=NO-KEY";
});
}
- public void testDeferredAjaxThenFail() {
- delayTestFinish(5000);
- GQuery
- .when(new PromiseFunction() {
- public void f(Deferred dfd) {
- dfd.resolve("message");
- }
- })
- .then(new Function() {
- public Object f(Object... args) {
- return new PromiseFunction() {
- public void f(Deferred dfd) {
- dfd.resolve(arguments);
- }
- };
- }
- })
- .then(new Function() {
- public Object f(Object... args) {
- return new PromiseFunction() {
- public void f(Deferred dfd) {
- dfd.reject(arguments);
- }
- };
- }
- })
- .done(new Function() {
- public void f() {
- finishTest();
- fail();
- }
- })
- .fail(new Function() {
- public void f() {
- assertEquals("message", arguments(0));
- finishTest();
- }
- });
- }
-
public void testDeferredQueueDelay() {
final int delay = 300;
final double init = Duration.currentTimeMillis();
import junit.framework.Test;
import com.google.gwt.junit.tools.GWTTestSuite;
+import com.google.gwt.query.client.deferred.DeferredTestGwt;
import com.google.gwt.query.client.impl.SelectorEnginesTestGwt;
/**
GWTTestSuite suite = new GWTTestSuite( "GQuery Suite" );
suite.addTestSuite(GQueryAjaxTestGwt.class);
suite.addTestSuite(GQueryDeferredTestGwt.class);
+ suite.addTestSuite(DeferredTestGwt.class);
suite.addTestSuite(GQuerySelectorsTestGwt.class);
suite.addTestSuite(GQueryCoreTestGwt.class);
suite.addTestSuite(GQueryCssTestGwt.class);
suite.addTestSuite(SelectorEnginesTestGwt.class);
return suite;
}
-
-
}