import com.google.gwt.query.client.plugins.EventsListener;\r
import com.google.gwt.user.client.Event;\r
import com.google.gwt.user.client.Window;\r
+import com.google.gwt.user.client.ui.Button;\r
+import com.google.gwt.user.client.ui.HTML;\r
+import com.google.gwt.user.client.ui.TextBox;\r
+import com.google.gwt.user.client.ui.Widget;\r
\r
/**\r
* Gwt Query is a GWT clone of the popular jQuery library.\r
return new GQuery(JSArray.create(element));\r
}\r
\r
+ /**\r
+ * Wrap a GQuery around an existing widget.\r
+ */\r
+ public static GQuery $(Widget widget) {\r
+ GQuery q = new GQuery(JSArray.create(widget.getElement()));\r
+ q.data("widget", widget);\r
+ return q;\r
+ }\r
+\r
/**\r
* Wrap a GQuery around an event's target element.\r
*/\r
}\r
throw new RuntimeException("No plugin registered for class " + plugin.getName());\r
}\r
+ \r
+ /**\r
+ * Return a GWT Widget containing the first matched element.\r
+ * \r
+ * If the element is already associated to a widget it returns the original widget, \r
+ * otherwise a new GWT widget will be created depending on the tagName.\r
+ * \r
+ */\r
+ public Widget asWidget() {\r
+ // TODO: complete it and move to the Widget plugin\r
+ if (data("widget") == null) {\r
+ Element e = elements.getItem(0);\r
+ Widget w = null;\r
+ if ("div".equalsIgnoreCase(e.getTagName()) || "span".equalsIgnoreCase(e.getTagName())) {\r
+ w = HTML.wrap(e); \r
+ } else if ("button".equalsIgnoreCase(e.getTagName())) {\r
+ w = Button.wrap(e);\r
+ } else if ("text".equalsIgnoreCase(e.getTagName())) {\r
+ w = TextBox.wrap(e);\r
+ } else {\r
+ w = new HTML($(e).toString());\r
+ }\r
+ data(e, "widget", w);\r
+ }\r
+ return (Widget) data("widget");\r
+ }\r
\r
/**\r
* Set a key/value object as properties to all matched elements.\r
*\r
- * Example: $("img").attr(new Properties("src: 'test.jpg', alt: 'Test\r
- * Image'"))\r
+ * Example: $("img").attr(new Properties("src: 'test.jpg', alt: 'Test Image'"))\r
*/\r
public GQuery attr(Properties properties) {\r
for (Element e : elements()) {\r
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<T> extends LazyBase<T>{
*/
<T extends GQuery> T as(Class<T> 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<T> attr(Properties properties);
LazyGQuery<T> 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
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;
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)$("<button>Click-me</button>").appendTo(document).asWidget();
+ b2.addClickHandler(new ClickHandler() {
+ public void onClick(ClickEvent event) {
+ $(b1).css("color", "red");
+ }
+ });
+
+ (b2).click();
+ assertEquals("red", $(b1).css("color"));
+ }
}