diff options
Diffstat (limited to 'gwtquery-core')
6 files changed, 168 insertions, 21 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 418bfdb0..47bee852 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 @@ -33,6 +33,7 @@ import com.google.gwt.dom.client.OptionElement; import com.google.gwt.dom.client.SelectElement; import com.google.gwt.dom.client.Style.Display; import com.google.gwt.dom.client.TextAreaElement; +import com.google.gwt.query.client.builders.JsonBuilder; import com.google.gwt.query.client.css.HasCssValue; import com.google.gwt.query.client.css.TakesCssValue; import com.google.gwt.query.client.css.TakesCssValue.CssSetter; @@ -58,6 +59,7 @@ import com.google.gwt.query.client.plugins.events.EventsListener; import com.google.gwt.query.client.plugins.widgets.WidgetsUtils; import com.google.gwt.regexp.shared.MatchResult; import com.google.gwt.regexp.shared.RegExp; +import com.google.gwt.safehtml.shared.SafeHtml; import com.google.gwt.user.client.DOM; import com.google.gwt.user.client.Event; import com.google.gwt.user.client.EventListener; @@ -285,6 +287,9 @@ public class GQuery implements Lazy<GQuery, LazyGQuery> { if (JsUtils.isElement(o)) { return $(JsUtils.<Element> cast(o)); } + if (o instanceof JsonBuilder) { + return new GQuery(((JsonBuilder) o).<Element>getDataImpl()); + } if (o instanceof JavaScriptObject) { return $((JavaScriptObject) o); } @@ -337,6 +342,13 @@ public class GQuery implements Lazy<GQuery, LazyGQuery> { public static GQuery $(String selectorOrHtml) { return $(selectorOrHtml, document); } + + /** + * This function accepts a SafeHtml creating a GQuery element containing those elements. + */ + public static GQuery $(SafeHtml safeHtml) { + return $(safeHtml.asString()); + } /** * This function accepts a string containing a CSS selector which is then used to match a set of @@ -892,6 +904,14 @@ public class GQuery implements Lazy<GQuery, LazyGQuery> { public GQuery after(String html) { return domManip(html, DomMan.AFTER); } + + /** + * Insert content after each of the matched elements. The elements must already be inserted into + * the document (you can't insert an element after another if it's not in the page). + */ + public GQuery after(SafeHtml safeHtml) { + return after(safeHtml.asString()); + } private void allNextSiblingElements(Element firstChildElement, JsNodeArray result, Element elem, GQuery until, String filterSelector) { @@ -1132,6 +1152,14 @@ public class GQuery implements Lazy<GQuery, LazyGQuery> { public GQuery append(String html) { return domManip(html, DomMan.APPEND); } + + /** + * Append content to the inside of every matched element. This operation is similar to doing an + * appendChild to all the specified elements, adding them into the document. + */ + public GQuery append(SafeHtml safeHtml) { + return append(safeHtml.asString()); + } /** * All of the matched set of elements will be inserted at the end of the element(s) specified by @@ -1170,6 +1198,17 @@ public class GQuery implements Lazy<GQuery, LazyGQuery> { $(html).append(this); return this; } + + /** + * All of the matched set of elements will be inserted at the end of the element(s) specified by + * the parameter other. + * + * The operation $(A).appendTo(B) is, essentially, the reverse of doing a regular $(A).append(B), + * instead of appending B to A, you're appending A to B. + */ + public GQuery appendTo(SafeHtml safeHtml) { + return appendTo(safeHtml.asString()); + } /** * Convert to Plugin interface provided by Class literal. @@ -2515,6 +2554,13 @@ public class GQuery implements Lazy<GQuery, LazyGQuery> { } return this; } + + /** + * Set the innerHTML of every matched element. + */ + public GQuery html(SafeHtml safeHtml) { + return html(safeHtml.asString()); + } /** * Get the id of the first matched element. @@ -4929,6 +4975,17 @@ public class GQuery implements Lazy<GQuery, LazyGQuery> { public GQuery wrap(String html) { return wrap($(html)); } + + /** + * Wrap each matched element with the specified SafeHtml content. This wrapping process is most useful + * for injecting additional structure into a document, without ruining the original semantic + * qualities of a document. This works by going through the first element provided (which is + * generated, on the fly, from the provided SafeHtml) and finds the deepest descendant element within + * its structure -- it is that element that will enwrap everything else. + */ + public GQuery wrap(SafeHtml safeHtml) { + return wrap(safeHtml.asString()); + } /** * Wrap all the elements in the matched set into a single wrapper element. This is different from @@ -4984,6 +5041,20 @@ public class GQuery implements Lazy<GQuery, LazyGQuery> { public GQuery wrapAll(String html) { return wrapAll($(html)); } + + /** + * Wrap all the elements in the matched set into a single wrapper element. This is different from + * .wrap() where each element in the matched set would get wrapped with an element. This wrapping + * process is most useful for injecting additional structure into a document, without ruining the + * original semantic qualities of a document. + * + * This works by going through the first element provided (which is generated, on the fly, from + * the provided SafeHtml) and finds the deepest descendant element within its structure -- it is that + * element that will enwrap everything else. + */ + public GQuery wrapAll(SafeHtml safeHtml) { + return wrapAll(safeHtml.asString()); + } /** * Wrap the inner child contents of each matched element (including text nodes) with an HTML @@ -5023,6 +5094,18 @@ public class GQuery implements Lazy<GQuery, LazyGQuery> { public GQuery wrapInner(String html) { return wrapInner($(html)); } + + /** + * Wrap the inner child contents of each matched element (including text nodes) with an SafeHtml + * structure. This wrapping process is most useful for injecting additional structure into a + * document, without ruining the original semantic qualities of a document. This works by going + * through the first element provided (which is generated, on the fly, from the provided SafeHtml) and + * finds the deepest ancestor element within its structure -- it is that element that will enwrap + * everything else. + */ + public GQuery wrapInner(SafeHtml safeHtml) { + return wrapInner(safeHtml.asString()); + } private String join(String chr, String... values) { String value = ""; diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/ajax/Ajax.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/ajax/Ajax.java index eb58f93d..a9bebd48 100644 --- a/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/ajax/Ajax.java +++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/ajax/Ajax.java @@ -29,6 +29,8 @@ import com.google.gwt.query.client.Promise; import com.google.gwt.query.client.builders.JsonBuilder; import com.google.gwt.query.client.js.JsUtils; import com.google.gwt.query.client.plugins.Plugin; +import com.google.gwt.query.client.plugins.deferred.PromiseFunction; +import com.google.gwt.user.client.Timer; import com.google.gwt.user.client.ui.FormPanel; /** @@ -311,6 +313,9 @@ public class Ajax extends GQuery { return get(url, (IsProperties) data, null); } + /** + * @deprecated Use promises instead + */ public static Promise get(String url, IsProperties data, Function onSuccess) { Settings s = createSettings(); s.setUrl(url); @@ -386,11 +391,15 @@ public class Ajax extends GQuery { } public static Promise loadScript(final String url, Function success) { - return ajax(createSettings() - .setUrl(url) - .setType("get") - .setDataType("loadscript") - .setSuccess(success)); + if (!GWT.isClient() || $("script[src^='" + url + "']").isEmpty()) { + return ajax(createSettings() + .setUrl(url) + .setType("get") + .setDataType("loadscript") + .setSuccess(success)); + } else { + return Deferred().resolve().promise(); + } } public static Promise post(String url, IsProperties data) { @@ -452,4 +461,46 @@ public class Ajax extends GQuery { ajax(s); return this; } + + /** + * Load an external resource using the link tag element. + * It is appended to the head of the document. + * + * @param rel Specifies the relationship between the current document and the linked document. + * @param url Specifies the location of the linked document + * @return a Promise which will be resolved when the external resource has been loaded. + */ + public static Promise loadLink(final String rel, final String url) { + GQuery link = $("link[rel='" + rel + "'][href^='" + url + "']"); + if (link.isEmpty()) { + return new PromiseFunction() { + public void f(final Deferred dfd) { + GQuery link = $("<link rel='" + rel + "' href='" + url + "'/>"); + link.on("load", new Function() { + public void f() { + // load event is fired before the imported stuff has actually + // being ready, we delay it to be sure it is ready. + new Timer() { + public void run() { + dfd.resolve(); + } + }.schedule(100); + } + }); + $(document.getHead()).append(link); + } + }; + } else { + return Deferred().resolve().promise(); + } + } + + /** + * Load an external html resource using the link tag element, it sets + * the relationship between the current document as 'import'. + * It is very useful to import web-components. + */ + public static Promise importHtml(String url) { + return loadLink("import", url); + } } 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 94dcc501..48285d8e 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 @@ -14,7 +14,6 @@ package com.google.gwt.query.client.plugins.events; import static com.google.gwt.query.client.GQuery.$; -import static com.google.gwt.query.client.GQuery.console; import com.google.gwt.core.client.Duration; import com.google.gwt.dom.client.Element; @@ -118,6 +117,10 @@ public class EventsListener implements EventListener { } public boolean fire(Event event, Object[] eventData) { + return fire(event, event.getTypeInt(), event.getType(), eventData); + } + + public boolean fire(Event event, int typeInt, String type, Object[] eventData) { if (times != 0) { times--; Object[] arguments; @@ -211,7 +214,7 @@ public class EventsListener implements EventListener { } @Override - public boolean fire(Event event, Object[] eventData) { + public boolean fire(Event event, int typeInt, String type, Object[] eventData) { if (isEmpty()) { return true; } @@ -231,7 +234,7 @@ public class EventsListener implements EventListener { JsObjectArray<BindFunction> bindFunctions = bindFunctionBySelector.get(cssSelector); for (int i = 0; bindFunctions != null && i < bindFunctions.length(); i++) { BindFunction f = bindFunctions.get(i); - if (f.hasEventType(event.getTypeInt()) || f.isTypeOf(event.getType())) { + if (f.hasEventType(typeInt) || f.isTypeOf(type)) { validSelectors.add(cssSelector); break; } @@ -252,7 +255,7 @@ public class EventsListener implements EventListener { JsObjectArray<BindFunction> bindFunctions = bindFunctionBySelector.get(cssSelector); for (int i = 0; bindFunctions != null && i < bindFunctions.length(); i++) { BindFunction f = bindFunctions.get(i); - if (f.hasEventType(event.getTypeInt()) || f.isTypeOf(event.getType())) { + if (f.hasEventType(typeInt) || f.isTypeOf(type)) { NodeList<Element> n = realCurrentTargetBySelector.get(cssSelector); for (int j = 0; n != null && j < n.getLength(); j++) { Element element = n.getItem(j); @@ -552,15 +555,15 @@ public class EventsListener implements EventListener { * it's useful for special events. */ public void dispatchEvent(Event event, String eventName) { - int etype = Event.getTypeInt(eventName); + int typeInt = Event.getTypeInt(eventName); Object[] handlerData = $(element).data(EVENT_DATA); for (int i = 0, l = elementEvents.length(); i < l; i++) { BindFunction listener = elementEvents.get(i); String namespace = JsUtils.prop(event, "namespace"); - boolean matchEV = listener != null && (listener.hasEventType(etype) || listener.isTypeOf(eventName)); + boolean matchEV = listener != null && (listener.hasEventType(typeInt) || listener.isTypeOf(eventName)); boolean matchNS = matchEV && (isNullOrEmpty(namespace) || listener.nameSpace.equals(namespace)); if (matchEV && matchNS) { - if (!listener.fire(event, handlerData)) { + if (!listener.fire(event, typeInt, eventName, handlerData)) { event.stopPropagation(); event.preventDefault(); } @@ -642,9 +645,7 @@ public class EventsListener implements EventListener { } public void onBrowserEvent(Event event) { -// console.log("onBrowser", event.getType(), event, element); if (JsUtils.isDefaultPrevented(event)) { - console.log("RETTT"); return; } double now = Duration.currentTimeMillis(); diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/events/GqEvent.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/events/GqEvent.java index 8493fd81..59757bb6 100644 --- a/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/events/GqEvent.java +++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/events/GqEvent.java @@ -17,6 +17,7 @@ package com.google.gwt.query.client.plugins.events; import com.google.gwt.dom.client.Element; import com.google.gwt.query.client.GQuery; +import com.google.gwt.query.client.js.JsUtils; import com.google.gwt.user.client.Event; /** @@ -110,4 +111,10 @@ public class GqEvent extends Event { public static final GqEvent as(Event e) { return e.cast(); } + + public static Event create(Event event, String eventName) { + event = create(event); + JsUtils.prop(event, "type", eventName); + return event; + } } diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/rebind/JsniBundleGenerator.java b/gwtquery-core/src/main/java/com/google/gwt/query/rebind/JsniBundleGenerator.java index cfd9893e..eb1511e4 100644 --- a/gwtquery-core/src/main/java/com/google/gwt/query/rebind/JsniBundleGenerator.java +++ b/gwtquery-core/src/main/java/com/google/gwt/query/rebind/JsniBundleGenerator.java @@ -131,11 +131,16 @@ public class JsniBundleGenerator extends Generator { try { if (!src.matches("(?i)https?://.*")) { String file = path + "/" + src; - logger.log(TreeLogger.INFO, getClass().getSimpleName() - + " - importing external javascript: " + file); - in = this.getClass().getClassLoader().getResourceAsStream(file); if (in == null) { + // If we didn't find the resource relative to the package, assume it is absolute. + file = src; + in = this.getClass().getClassLoader().getResourceAsStream(file); + } + if (in != null) { + logger.log(TreeLogger.INFO, getClass().getSimpleName() + + " - importing external javascript: " + file); + } else { logger.log(TreeLogger.ERROR, "Unable to read javascript file: " + file); } } else { 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 54181621..3c76a811 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 @@ -578,8 +578,8 @@ public class GQueryCoreTestGwt extends GWTTestCase { public void testPropMethod(){ $(e).html("<input id=\"checkBox1\" type=\"checkbox\" checked=\"checked\" /> <input id=\"checkBox2\" type=\"checkbox\" />"); - assertEquals(true, $("#checkBox1",e).prop("checked")); - assertEquals(false, $("#checkBox2",e).prop("checked")); + assertEquals(Boolean.TRUE, $("#checkBox1",e).prop("checked")); + assertEquals(Boolean.FALSE, $("#checkBox2",e).prop("checked")); $("#checkBox1",e).prop("checked", false); $("#checkBox2",e).prop("checked", new Function() { @@ -588,8 +588,8 @@ public class GQueryCoreTestGwt extends GWTTestCase { return Boolean.TRUE; } }); - assertEquals(true, $("#checkBox2",e).prop("checked")); - assertEquals(false, $("#checkBox1",e).prop("checked")); + assertEquals(Boolean.TRUE, $("#checkBox2",e).prop("checked")); + assertEquals(Boolean.FALSE, $("#checkBox1",e).prop("checked")); $(window).prop("foo", 234); assertEquals(234d, $(window).prop("foo")); |