From 50dd156b625de8bf90e1a5e90f7e5d5c09efd1cb Mon Sep 17 00:00:00 2001 From: Manolo Carrasco Date: Mon, 25 Oct 2010 15:53:39 +0000 Subject: [PATCH] added gquery constructor for widgets and basic element to widget methods --- .../com/google/gwt/query/client/GQuery.java | 42 ++++++++++++++++++- .../google/gwt/query/client/LazyGQuery.java | 36 ++++++++++------ .../gwt/query/client/GQueryCoreTest.java | 21 ++++++++++ 3 files changed, 85 insertions(+), 14 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 f2af2827..2b290b7a 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 @@ -45,6 +45,10 @@ import com.google.gwt.query.client.impl.DocumentStyleImpl; import com.google.gwt.query.client.plugins.EventsListener; import com.google.gwt.user.client.Event; import com.google.gwt.user.client.Window; +import com.google.gwt.user.client.ui.Button; +import com.google.gwt.user.client.ui.HTML; +import com.google.gwt.user.client.ui.TextBox; +import com.google.gwt.user.client.ui.Widget; /** * Gwt Query is a GWT clone of the popular jQuery library. @@ -186,6 +190,15 @@ public class GQuery implements Lazy { return new GQuery(JSArray.create(element)); } + /** + * Wrap a GQuery around an existing widget. + */ + public static GQuery $(Widget widget) { + GQuery q = new GQuery(JSArray.create(widget.getElement())); + q.data("widget", widget); + return q; + } + /** * Wrap a GQuery around an event's target element. */ @@ -602,12 +615,37 @@ public class GQuery implements Lazy { } throw new RuntimeException("No plugin registered for class " + plugin.getName()); } + + /** + * Return a GWT Widget containing the first matched element. + * + * If the element is already associated to a widget it returns the original widget, + * otherwise a new GWT widget will be created depending on the tagName. + * + */ + public Widget asWidget() { + // TODO: complete it and move to the Widget plugin + if (data("widget") == null) { + Element e = elements.getItem(0); + Widget w = null; + if ("div".equalsIgnoreCase(e.getTagName()) || "span".equalsIgnoreCase(e.getTagName())) { + w = HTML.wrap(e); + } else if ("button".equalsIgnoreCase(e.getTagName())) { + w = Button.wrap(e); + } else if ("text".equalsIgnoreCase(e.getTagName())) { + w = TextBox.wrap(e); + } else { + w = new HTML($(e).toString()); + } + data(e, "widget", w); + } + return (Widget) data("widget"); + } /** * Set a key/value object as properties to all matched elements. * - * Example: $("img").attr(new Properties("src: 'test.jpg', alt: 'Test - * Image'")) + * Example: $("img").attr(new Properties("src: 'test.jpg', alt: 'Test Image'")) */ public GQuery attr(Properties properties) { for (Element e : elements()) { diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/LazyGQuery.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/LazyGQuery.java index 149969e3..483d9c5d 100644 --- a/gwtquery-core/src/main/java/com/google/gwt/query/client/LazyGQuery.java +++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/LazyGQuery.java @@ -42,6 +42,10 @@ import com.google.gwt.query.client.impl.DocumentStyleImpl; import com.google.gwt.query.client.plugins.EventsListener; import com.google.gwt.user.client.Event; import com.google.gwt.user.client.Window; +import com.google.gwt.user.client.ui.Button; +import com.google.gwt.user.client.ui.HTML; +import com.google.gwt.user.client.ui.TextBox; +import com.google.gwt.user.client.ui.Widget; import com.google.gwt.query.client.LazyBase; public interface LazyGQuery extends LazyBase{ @@ -143,11 +147,19 @@ public interface LazyGQuery extends LazyBase{ */ T as(Class plugin); + /** + * Return a GWT Widget containing the first matched element. + * + * If the element is already associated to a widget it returns the original widget, + * otherwise a new GWT widget will be created depending on the tagName. + * + */ + Widget asWidget(); + /** * Set a key/value object as properties to all matched elements. * - * Example: $("img").attr(new Properties("src: 'test.jpg', alt: 'Test - * Image'")) + * Example: $("img").attr(new Properties("src: 'test.jpg', alt: 'Test Image'")) */ LazyGQuery attr(Properties properties); @@ -717,28 +729,28 @@ public interface LazyGQuery extends LazyBase{ LazyGQuery one(int eventbits, Object data, Function f); /** - * Get the current computed width for the first element in the set of matched elements, + * Get the current computed height for the first element in the set of matched elements, * including padding, border, but not the margin. */ - int outerWidth(); + int outerHeight(); /** - * Get the current computed width for the first element in the set of matched elements, - * including padding and border and optionally margin. + * Get the current computed height for the first element in the set of matched elements, + * including padding, border, and optionally margin. */ - int outerWidth(boolean includeMargin); + int outerHeight(boolean includeMargin); /** - * Get the current computed height for the first element in the set of matched elements, + * Get the current computed width for the first element in the set of matched elements, * including padding, border, but not the margin. */ - int outerHeight(); + int outerWidth(); /** - * Get the current computed height for the first element in the set of matched elements, - * including padding, border, and optionally margin. + * Get the current computed width for the first element in the set of matched elements, + * including padding and border and optionally margin. */ - int outerHeight(boolean includeMargin); + int outerWidth(boolean includeMargin); /** * Get a set of elements containing the unique parents of the matched set of diff --git a/gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryCoreTest.java b/gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryCoreTest.java index a77e9015..fd9d7dcc 100644 --- a/gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryCoreTest.java +++ b/gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryCoreTest.java @@ -21,10 +21,13 @@ import static com.google.gwt.query.client.GQuery.document; import com.google.gwt.dom.client.Document; import com.google.gwt.dom.client.Element; +import com.google.gwt.event.dom.client.ClickEvent; +import com.google.gwt.event.dom.client.ClickHandler; import com.google.gwt.junit.client.GWTTestCase; import com.google.gwt.query.client.impl.SelectorEngineImpl; import com.google.gwt.query.client.impl.SelectorEngineSizzle; import com.google.gwt.user.client.Event; +import com.google.gwt.user.client.ui.Button; import com.google.gwt.user.client.ui.HTML; import com.google.gwt.user.client.ui.RootPanel; @@ -755,5 +758,23 @@ public class GQueryCoreTest extends GWTTestCase { isAttachedToTheDOM = myNewElement.parents().filter("body").size() > 0; assertEquals(true, isAttachedToTheDOM); } + + public void testGQueryWidgets() { + final Button b1 = new Button("click-me"); + RootPanel.get().add(b1); + GQuery g = $(b1); + Button b2 = (Button) g.asWidget(); + assertEquals(b1, b2); + + b2 = (Button)$("").appendTo(document).asWidget(); + b2.addClickHandler(new ClickHandler() { + public void onClick(ClickEvent event) { + $(b1).css("color", "red"); + } + }); + + (b2).click(); + assertEquals("red", $(b1).css("color")); + } } -- 2.39.5