From a115f777053b8558dd5d440ebcc7f36a8fbb2fed Mon Sep 17 00:00:00 2001 From: Manolo Carrasco Date: Wed, 28 Jan 2015 17:34:46 +0100 Subject: Adding a couple of methods to import html --- .../google/gwt/query/client/plugins/ajax/Ajax.java | 49 +++++++++++++++++++--- 1 file changed, 44 insertions(+), 5 deletions(-) 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..814a58e2 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,16 @@ 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)); + GQuery script = $("script[src^='" + url + "']"); + if (script.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 +462,33 @@ public class Ajax extends GQuery { ajax(s); return this; } + + public static Promise loadLink(String rel, String url) { + GQuery link = $("link[rel='" + rel + "'][href^='" + url + "']"); + if (link.isEmpty()) { + return new PromiseFunction() { + public void f(Deferred dfd) { + GQuery link = $(""); + 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(); + } + } + + public static Promise importHtml(String url) { + return loadLink("import", url); + } } -- cgit v1.2.3 From 57c1dbd68ce52b97789784ff16eae2d333f59789 Mon Sep 17 00:00:00 2001 From: Manolo Carrasco Date: Thu, 29 Jan 2015 11:15:02 +0100 Subject: Fix event delegation in special events --- .../query/client/plugins/events/EventsListener.java | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) 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..e48ba9bd 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 @@ -118,6 +118,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 +215,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 +235,7 @@ public class EventsListener implements EventListener { JsObjectArray 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 +256,7 @@ public class EventsListener implements EventListener { JsObjectArray 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 n = realCurrentTargetBySelector.get(cssSelector); for (int j = 0; n != null && j < n.getLength(); j++) { Element element = n.getItem(j); @@ -552,15 +556,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 +646,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(); -- cgit v1.2.3 From 23429096299a7de4b8f0b6538ee947c385bff106 Mon Sep 17 00:00:00 2001 From: Manolo Carrasco Date: Thu, 29 Jan 2015 11:16:45 +0100 Subject: Ability to create new events with different type --- .../java/com/google/gwt/query/client/plugins/events/GqEvent.java | 7 +++++++ 1 file changed, 7 insertions(+) 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; + } } -- cgit v1.2.3 From 48e377fad3d655638e2e3d7192bba43abc214981 Mon Sep 17 00:00:00 2001 From: Manolo Carrasco Date: Thu, 29 Jan 2015 11:17:44 +0100 Subject: Handle JsonBuilder wrappers in $ method --- gwtquery-core/src/main/java/com/google/gwt/query/client/GQuery.java | 4 ++++ .../com/google/gwt/query/client/plugins/events/EventsListener.java | 1 - 2 files changed, 4 insertions(+), 1 deletion(-) 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..ba59cadd 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; @@ -285,6 +286,9 @@ public class GQuery implements Lazy { if (JsUtils.isElement(o)) { return $(JsUtils. cast(o)); } + if (o instanceof JsonBuilder) { + return new GQuery(((JsonBuilder) o).getDataImpl()); + } if (o instanceof JavaScriptObject) { return $((JavaScriptObject) o); } 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 e48ba9bd..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; -- cgit v1.2.3 From c6a06834dc0ed891c1dec4ab58332abd22865ca8 Mon Sep 17 00:00:00 2001 From: Manolo Carrasco Date: Thu, 29 Jan 2015 12:36:07 +0100 Subject: Fix tests --- .../src/main/java/com/google/gwt/query/client/GQuery.java | 2 +- .../main/java/com/google/gwt/query/client/plugins/ajax/Ajax.java | 7 +++---- .../test/java/com/google/gwt/query/client/GQueryCoreTestGwt.java | 8 ++++---- 3 files changed, 8 insertions(+), 9 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 ba59cadd..92641bf8 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 @@ -287,7 +287,7 @@ public class GQuery implements Lazy { return $(JsUtils. cast(o)); } if (o instanceof JsonBuilder) { - return new GQuery(((JsonBuilder) o).getDataImpl()); + return new GQuery(((JsonBuilder) o).getDataImpl()); } if (o instanceof JavaScriptObject) { return $((JavaScriptObject) o); 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 814a58e2..429b00eb 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 @@ -391,8 +391,7 @@ public class Ajax extends GQuery { } public static Promise loadScript(final String url, Function success) { - GQuery script = $("script[src^='" + url + "']"); - if (script.isEmpty()) { + if (!GWT.isClient() || $("script[src^='" + url + "']").isEmpty()) { return ajax(createSettings() .setUrl(url) .setType("get") @@ -463,11 +462,11 @@ public class Ajax extends GQuery { return this; } - public static Promise loadLink(String rel, String url) { + 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(Deferred dfd) { + public void f(final Deferred dfd) { GQuery link = $(""); link.on("load", new Function() { public void f() { 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(" "); - 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")); -- cgit v1.2.3 From 1feb9ec1fb76507e3377377a8ccb317495e4d161 Mon Sep 17 00:00:00 2001 From: Manolo Carrasco Date: Fri, 30 Jan 2015 14:24:11 +0100 Subject: Adding javadoc to new methods --- .../java/com/google/gwt/query/client/plugins/ajax/Ajax.java | 13 +++++++++++++ 1 file changed, 13 insertions(+) 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 429b00eb..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 @@ -462,6 +462,14 @@ public class Ajax extends GQuery { 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()) { @@ -487,6 +495,11 @@ public class Ajax extends GQuery { } } + /** + * 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); } -- cgit v1.2.3