diff options
author | Manuel Carrasco Moñino <manolo@apache.org> | 2015-01-30 14:24:50 +0100 |
---|---|---|
committer | Manuel Carrasco Moñino <manolo@apache.org> | 2015-01-30 14:24:50 +0100 |
commit | 525c70c19c62b8b0b0a9e67ca464dc09c580f3d2 (patch) | |
tree | 10a696c929b94999900d1c6e0da2fa225266a9f1 | |
parent | 0b4ca15d994c78cc38e35cbe691f02b73f1fb54b (diff) | |
parent | 1feb9ec1fb76507e3377377a8ccb317495e4d161 (diff) | |
download | gwtquery-525c70c19c62b8b0b0a9e67ca464dc09c580f3d2.tar.gz gwtquery-525c70c19c62b8b0b0a9e67ca464dc09c580f3d2.zip |
Merge pull request #340 from manolo/mcm_fixes
Some fixes which should be in current version
5 files changed, 81 insertions, 18 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..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 @@ -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<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); } 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/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")); |