]> source.dussan.org Git - gwtquery.git/commitdiff
added gquery constructor for widgets and basic element to widget methods
authorManolo Carrasco <manolo@apache.org>
Mon, 25 Oct 2010 15:53:39 +0000 (15:53 +0000)
committerManolo Carrasco <manolo@apache.org>
Mon, 25 Oct 2010 15:53:39 +0000 (15:53 +0000)
gwtquery-core/src/main/java/com/google/gwt/query/client/GQuery.java
gwtquery-core/src/main/java/com/google/gwt/query/client/LazyGQuery.java
gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryCoreTest.java

index f2af28279d40724e8f4264ae817eabd0675b6706..2b290b7a4d01c0732082ec44cab16cf4a3cd3cbc 100644 (file)
@@ -45,6 +45,10 @@ import com.google.gwt.query.client.impl.DocumentStyleImpl;
 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
@@ -186,6 +190,15 @@ public class GQuery implements Lazy<GQuery, LazyGQuery> {
     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
@@ -602,12 +615,37 @@ public class GQuery implements Lazy<GQuery, LazyGQuery> {
     }\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
index 149969e3d7a96c46946fa8a707e950dd3c0296aa..483d9c5d593445470585f25870dc51f0b97fb8fc 100644 (file)
@@ -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<T> extends LazyBase<T>{
@@ -143,11 +147,19 @@ 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);
 
@@ -717,28 +729,28 @@ public interface LazyGQuery<T> extends LazyBase<T>{
   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
index a77e9015566d395f6a87cfb8d86f98a54d97e1e5..fd9d7dcc6702bef630c2b7fbc1eadbdc52996fa8 100644 (file)
@@ -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)$("<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"));
+  }
 
 }