import java.util.ArrayList;
import java.util.List;
-import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.JsArrayMixed;
-import com.google.gwt.query.client.plugins.ajax.Ajax;
import com.google.gwt.dom.client.Element;
+import com.google.gwt.query.client.plugins.ajax.Ajax;
+import com.google.gwt.query.client.plugins.ajax.Ajax.Settings;
/**
* Class to implement the GQuery API static methods.
super(gq);
}
- public static void ajax(Properties settings) {
- ajax(null, null, null, settings);
+ public static void ajax(Properties p) {
+ ajax(p);
}
- public static void ajax(final Function onSuccess, Properties settings) {
- ajax(null, onSuccess, null, settings);
+ public static void ajax(String url, Settings settings) {
+ Ajax.ajax(url, settings);
+ }
+
+ public GQuery load(String url, Properties data, final Function onSuccess) {
+ return as(Ajax.Ajax).load(url, data, onSuccess);
}
- public static void ajax(final Function onSuccess, final Function onError,
- Properties settings) {
- ajax(null, onSuccess, onError, settings);
+ public GQuery load(String url) {
+ return as(Ajax.Ajax).load(url, null, null);
}
- private static Ajax ajaxImpl;
-
- public static void ajax(String url, Function onSuccess, Function onError,
- Properties settings) {
- if (ajaxImpl == null) {
- ajaxImpl = GWT.create(Ajax.class);
- }
- ajaxImpl.ajax(url, onSuccess, onError, settings);
+ public static void get(String url, Properties data, final Function onSuccess) {
+ Ajax.get(url, data, onSuccess);
+ }
+
+ public static void post(String url, Properties data, final Function onSuccess) {
+ Ajax.post(url, data, onSuccess);
+ }
+
+ public static void getJSON(String url, Properties data, final Function onSuccess) {
+ Ajax.getJSON(url, data, onSuccess);
}
public static boolean contains(Element a, Element b) {
import com.google.gwt.query.client.plugins.Events;\r
import com.google.gwt.query.client.plugins.Plugin;\r
import com.google.gwt.query.client.plugins.Widgets;\r
+import com.google.gwt.query.client.plugins.ajax.Ajax;\r
import com.google.gwt.query.client.plugins.effects.PropertiesAnimation.Easing;\r
import com.google.gwt.query.client.plugins.events.EventsListener;\r
import com.google.gwt.user.client.DOM;\r
// GqUi.attachWidget(w);\r
}\r
}\r
- if (newNodes.size() > g.get().getLength()) {\r
+ if (newNodes.size() > g.size()) {\r
g.setArray(newNodes);\r
}\r
return this;\r
/**\r
* Bind a function to the load event of each matched element.\r
*/\r
+ @Deprecated\r
public GQuery load(Function f) {\r
return bind(Event.ONLOAD, null, f);\r
}\r
+ \r
+ /**\r
+ * Load data from the server and place the returned HTML into the matched element.\r
+ * \r
+ * The url allows us to specify a portion of the remote document to be inserted. \r
+ * This is achieved with a special syntax for the url parameter. \r
+ * If one or more space characters are included in the string, the portion of \r
+ * the string following the first space is assumed to be a GQuery selector that \r
+ * determines the content to be loaded.\r
+ * \r
+ */\r
+ public GQuery load(String url, Properties data, final Function onSuccess) {\r
+ return as(Ajax.Ajax).load(url, data, onSuccess);\r
+ }\r
+ \r
+ /**\r
+ * Load data from the server and place the returned HTML into the matched element.\r
+ * \r
+ * The url allows us to specify a portion of the remote document to be inserted. \r
+ * This is achieved with a special syntax for the url parameter. \r
+ * If one or more space characters are included in the string, the portion of \r
+ * the string following the first space is assumed to be a GQuery selector that \r
+ * determines the content to be loaded.\r
+ * \r
+ */\r
+ public GQuery load(String url) {\r
+ return load(url, null, null);\r
+ }\r
\r
/**\r
* Reduce the set of matched elements to all elements before a given position.\r
public GQuery unbind(int eventbits) {\r
return as(Events).unbind(eventbits);\r
}\r
+ \r
+ /**\r
+ * Removes all events that match the eventList.\r
+ */\r
+ public GQuery unbind(String eventList) {\r
+ return as(Events).unbind(eventList);\r
+ }\r
\r
/**\r
* Remove all event delegation that have been bound using\r
import com.google.gwt.dom.client.Node;\r
import com.google.gwt.dom.client.NodeList;\r
import com.google.gwt.query.client.js.JsNodeArray;\r
+import com.google.gwt.query.client.js.JsObjectArray;\r
import com.google.gwt.query.client.js.JsRegexp;\r
import com.google.gwt.query.client.js.JsUtils;\r
\r
}\r
\r
// pseudo selectors which are computed by gquery\r
- JsRegexp p = new JsRegexp(".*:((visible|hidden)|((radio|checkbox)\\s*(,|$))).*", "i");\r
+ JsRegexp p = new JsRegexp("(.*):((visible|hidden)|((button|checkbox|file|hidden|image|password|radio|reset|submit|text)\\s*(,|$)))(.*)", "i");\r
\r
public NodeList<Element> select(String selector, Node ctx) {\r
if (p.test(selector)) {\r
JsNodeArray res = JsNodeArray.create();\r
for (String s : selector.trim().split("\\s*,\\s*")) {\r
NodeList<Element> nodes;\r
- if (s.endsWith(":visible")) {\r
- nodes = filterByVisibility(select(s.substring(0, s.length() - 8), ctx), true);\r
- } else if (s.endsWith(":hidden")) {\r
- nodes = filterByVisibility(select(s.substring(0, s.length() - 7), ctx), false);\r
- } else if (s.contains(":radio")) {\r
- nodes = select(s.replace(":radio", "[type='radio']"), ctx);\r
- } else if (s.contains(":checkbox")) {\r
- nodes = select(s.replace(":checkbox", "[type='checkbox']"), ctx);\r
+ JsObjectArray<String> a = p.match(s);\r
+ if (a.get(0) != null) {\r
+ if (s.endsWith(":visible")) {\r
+ nodes = filterByVisibility(select(s.substring(0, s.length() - 8), ctx), true);\r
+ } else if (s.endsWith(":hidden")) {\r
+ nodes = filterByVisibility(select(s.substring(0, s.length() - 7), ctx), false);\r
+ } else {\r
+ nodes = select(a.get(1) + "[type=" + a.get(2) + "]", ctx);\r
+ } \r
} else {\r
nodes = select(s, ctx);\r
}\r
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.query.client.js.JsNamedArray;
/**
* Runtime selector engine implementation for browsers with native
public class SelectorEngineNative extends SelectorEngineImpl {
// querySelectorAll unsupported selectors
- public static String NATIVE_EXCEPTIONS_REGEXP = "(^[\\./]/.*)|(.*(:contains|!=|:first([^-]|$)|:last([^-]|$)|:even|:odd)).*";
+ public static String NATIVE_EXCEPTIONS_REGEXP = "(^[\\./]/.*)|(.*(:contains|:first([^-]|$)|:last([^-]|$)|:even|:odd)).*";
private static HasSelector impl;
+ static JsNamedArray<String> cache;
+
public SelectorEngineNative() {
if (impl == null) {
impl = GWT.create(HasSelector.class);
public NodeList<Element> select(String selector, Node ctx) {
// querySelectorAllImpl does not support ids starting with a digit.
- if (selector.matches("#[\\w\\-]+")) {
- return SelectorEngine.veryQuickId(selector.substring(1), ctx);
- } else if (!SelectorEngine.hasQuerySelector || selector.matches(NATIVE_EXCEPTIONS_REGEXP)) {
+// if (selector.matches("#[\\w\\-]+")) {
+// return SelectorEngine.veryQuickId(selector.substring(1), ctx);
+// } else
+ if (selector.contains("!=")) {
+ if (cache == null) {
+ cache = JsNamedArray.create();
+ }
+ String xsel = cache.get(selector);
+ if (xsel == null) {
+ xsel = selector.replaceAll("(\\[\\w+)!(=[^\\]]+\\])", ":not($1$2)");
+ cache.put(selector, xsel);
+ }
+ selector = xsel;
+ }
+
+ if (!SelectorEngine.hasQuerySelector || selector.matches(NATIVE_EXCEPTIONS_REGEXP)) {
return impl.select(selector, ctx);
} else {
try {
*/
public class Events extends GQuery {
- public static final Class<Events> Events = Events.class;
-
- static {
- GQuery.registerPlugin(Events.class, new Plugin<Events>() {
+ public static final Class<Events> Events =
+ registerPlugin(Events.class, new Plugin<Events>() {
public Events init(GQuery gq) {
return new Events(gq);
}
});
- }
/**
* Don't apply events on text and comment nodes !!
import com.google.gwt.http.client.RequestException;
import com.google.gwt.http.client.Response;
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.builders.JsonBuilder;
import com.google.gwt.query.client.js.JsUtils;
+import com.google.gwt.query.client.plugins.Plugin;
/**
* Ajax class for GQuery.
* This implementation is almost thought to be used as an alternative to
* the GWT-XHR, GWT-XML and GWT-JSON modules.
*
- * This class is not a plugin because in jquery it does not extends the jquery
- * object, but we prefer this name-space in order to centralize the jquery core
- * features in a common folder.
- *
*/
-public class Ajax {
+public class Ajax extends GQuery {
/**
* Ajax Settings object
*/
public interface Settings extends JsonBuilder {
- String getType();
- Settings setType(String t);
- String getUrl();
- Settings setUrl(String u);
+ String getContentType();
+ Element getContext();
Properties getData();
- Settings setData(Properties p);
String getDataString();
- Settings setDataString(String d);
String getDataType();
- Settings setDataType(String t);
+ Function getError();
+ Properties getHeaders();
+ String getPassword();
+ Function getSuccess();
int getTimeout();
- Settings setTimeout(int t);
+ String getType();
+ String getUrl();
String getUsername();
- Settings setUsername(String u);
- String getPassword();
- Settings setPassword(String p);
- String getContentType();
Settings setContentType(String t);
- Properties getHeaders();
- Settings setHeaders(Properties p);
- Element getContext();
Settings setContext(Element e);
- Function getSuccess();
- Settings setSuccess(Function f);
- Function getError();
+ Settings setData(Properties p);
+ Settings setDataString(String d);
+ Settings setDataType(String t);
Settings setError(Function f);
+ Settings setHeaders(Properties p);
+ Settings setPassword(String p);
+ Settings setSuccess(Function f);
+ Settings setTimeout(int t);
+ Settings setType(String t);
+ Settings setUrl(String u);
+ Settings setUsername(String u);
}
- public void ajax(String url, Function onSuccess, Function onError, Properties p) {
- Settings settings = GWT.create(Settings.class);
- settings.load(p);
- ajax(url, onSuccess, onError, settings);
- }
-
- public void ajax(String url, Function onSuccess, Function onError) {
- ajax(url, onSuccess, onError, (Settings)null);
- }
-
- public void ajax(String url, Function onSuccess, Function onError, Settings settings) {
- if (settings == null) {
- settings = GWT.create(Settings.class);
- }
- settings.setUrl(url).setSuccess(onSuccess).setError(onError);
- ajax(settings);
+ public static final Class<Ajax> Ajax =
+ registerPlugin(Ajax.class, new Plugin<Ajax>() {
+ public Ajax init(GQuery gq) {
+ return new Ajax(gq);
+ }
+ });
+
+ public static void ajax(Properties p) {
+ Settings s = createSettings();
+ s.load(p);
+ ajax(s);
}
/**
* @param onError the function to execute on error
* @param settings a Properties object with the configuration of the Ajax request.
*/
- public void ajax(Settings settings) {
+ public static void ajax(Settings settings) {
Method httpMethod = RequestBuilder.POST;
String method = settings.getType();
if ("get".equalsIgnoreCase(method)) {
}
r.setCallback(new RequestCallback() {
+ public void onError(Request request, Throwable exception) {
+ if (onError != null) {
+ onError.f(null, exception.getMessage(), request, null, exception);
+ }
+ }
+
public void onResponseReceived(Request request, Response response) {
if (response.getStatusCode() > 202) {
if (onError != null) {
onSuccess.f(retData, "success", request, response);
}
}
-
- public void onError(Request request, Throwable exception) {
- if (onError != null) {
- onError.f(null, exception.getMessage(), request, null, exception);
- }
- }
});
try {
}
}
}
+
+ public static void ajax(String url, Function onSuccess, Function onError) {
+ ajax(url, onSuccess, onError, (Settings)null);
+ }
+
+ public static void ajax(String url, Function onSuccess, Function onError, Settings s) {
+ if (s == null) {
+ s = createSettings();
+ }
+ s.setUrl(url).setSuccess(onSuccess).setError(onError);
+ ajax(s);
+ }
+
+ public static void ajax(String url, Properties p) {
+ Settings s = createSettings();
+ s.load(p);
+ s.setUrl(url);
+ ajax(s);
+ }
+
+ public static void ajax(String url, Settings settings) {
+ ajax(settings.setUrl(url));
+ }
+
+ public static Settings createSettings() {
+ return createSettings($$(""));
+ }
+
+ public static Settings createSettings(String prop) {
+ return createSettings($$(prop));
+ }
+
+ public static Settings createSettings(Properties p) {
+ Settings s = GWT.create(Settings.class);
+ s.load(p);
+ return s;
+ }
+
+ public static void get(String url, Properties data, final Function onSuccess) {
+ Settings s = createSettings();
+ s.setUrl(url);
+ s.setDataType("txt");
+ s.setType("get");
+ s.setData(data);
+ s.setSuccess(onSuccess);
+ ajax(s);
+ }
+
+ public static void getJSON(String url, Properties data, final Function onSuccess) {
+ Settings s = createSettings();
+ s.setUrl(url);
+ s.setDataType("json");
+ s.setType("post");
+ s.setData(data);
+ s.setSuccess(onSuccess);
+ ajax(s);
+ }
+
+ public static void post(String url, Properties data, final Function onSuccess) {
+ Settings s = createSettings();
+ s.setUrl(url);
+ s.setDataType("txt");
+ s.setType("post");
+ s.setData(data);
+ s.setSuccess(onSuccess);
+ ajax(s);
+ }
+
+ protected Ajax(GQuery gq) {
+ super(gq);
+ }
+
+ public Ajax load(String url, Properties data, final Function onSuccess) {
+ Settings s = createSettings();
+ final String filter = url.contains(" ") ? url.replaceFirst("^[^\\s]+\\s+", "") : "";
+ s.setUrl(url.replaceAll("\\s+.+$", ""));
+ s.setDataType("html");
+ s.setType("get");
+ s.setData(data);
+ s.setSuccess(new Function() {
+ public void f() {
+ GQuery d = $(getData()[0].toString());
+ Ajax.this.empty().append(filter.isEmpty() ? d : d.filter(filter));
+ if (onSuccess != null) {
+ onSuccess.setElement(Ajax.this.get(0));
+ onSuccess.f();
+ }
+ }
+ });
+ ajax(s);
+ return this;
+ }
}
if (selector.matches("#[\\w\\-]+")) {\r
sw.println("return "\r
+ wrap(method, "veryQuickId(\"" + selector.substring(1) + "\", root)") + ";");\r
+ } else if (selector.contains("!=")) {\r
+ sw.println("return "\r
+ + wrap(method, "querySelectorAll(\"" \r
+ + selector.replaceAll("(\\[\\w+)!(=[^\\]]+\\])", ":not($1$2)")\r
+ + "\", root)") + ";");\r
} else if (selector.matches(SelectorEngineNative.NATIVE_EXCEPTIONS_REGEXP)) {\r
super.generateMethodBody(sw, method, treeLogger, hasContext);\r
} else {\r
assertEquals("{\"$x\":22.60,\"$y\":\".0\",\"h\":\"#y\"}", Properties
.wrapPropertiesString("$x:22.60,$y:.0,h:#y"));
}
+
+ public void testtt() {
+ String selector = "div[class!=madeup]";
+ if (selector.matches(".*\\[\\w+!=[^\\]]+\\].*")) {
+ System.out.println("OK");
+ selector = selector.replaceAll("(\\[\\w+)!(=[^\\]]+\\])", ":not($1$2)");
+ }
+ System.out.println(selector);
+ }
}