import com.google.gwt.query.client.js.JsUtils;\r
import com.google.gwt.query.client.plugins.Plugin;\r
import com.google.gwt.query.client.plugins.events.EventsListener;\r
+import com.google.gwt.query.client.plugins.widgets.WidgetsUtils;\r
import com.google.gwt.user.client.DOM;\r
import com.google.gwt.user.client.Event;\r
import com.google.gwt.user.client.EventListener;\r
import com.google.gwt.user.client.Window;\r
+import com.google.gwt.user.client.ui.GqUi;\r
import com.google.gwt.user.client.ui.Widget;\r
\r
import java.util.ArrayList;\r
* whenever you create a new iframe and you want to add dynamic content to it.\r
*/\r
public GQuery empty() {\r
- // TODO: add memory leak cleanup, remove event handlers, and\r
- // data caches\r
+ // TODO: add memory leak cleanup, remove event handlers\r
for (Element e : elements()) {\r
if (e.getNodeType() == Element.DOCUMENT_NODE) {\r
emptyDocument(e.<Document> cast());\r
} else {\r
- while (e.getFirstChild() != null) {\r
- e.removeChild(e.getFirstChild());\r
+ Node c = e.getFirstChild();\r
+ while (c != null) {\r
+ $(c).unbind(0);\r
+ removeData(c.<Element>cast(), null);\r
+ Widget w = getAssociatedWidget(e);\r
+ if (w != null) {\r
+ w.removeFromParent();\r
+ } else {\r
+ e.removeChild(c);\r
+ c = e.getFirstChild();\r
+ }\r
}\r
}\r
}\r
*/\r
public GQuery remove() {\r
for (Element e : elements()) {\r
- // TODO: cleanup event bindings\r
+ $(e).unbind(0);\r
removeData(e, null);\r
- if (e.getParentNode() != null) {\r
+ Widget w = getAssociatedWidget(e);\r
+ if (w != null && w.isAttached()) {\r
+ w.removeFromParent();\r
+ } else if (e.getParentNode() != null) {\r
e.getParentNode().removeChild(e);\r
}\r
}\r
// e.getOwnerDocument();\r
if (e.getNodeType() == Node.DOCUMENT_NODE) {\r
e = e.<Document> cast().getBody();\r
- }\r
+ } \r
for (int j = 0; j < g.size(); j++) {\r
+ Widget w = getAssociatedWidget(g.get(j));\r
+ GqUi.detachWidget(w);\r
Node n = g.get(j);\r
if (g.size() > 1) {\r
n = n.cloneNode(true);\r
newNodes.addNode(e.getParentNode().insertBefore(n, e));\r
break;\r
}\r
+ GqUi.attachWidget(w);\r
}\r
}\r
if (newNodes.size() > g.size()) {\r
import com.google.gwt.query.client.plugins.widgets.ButtonWidgetFactory;\r
import com.google.gwt.query.client.plugins.widgets.DateBoxWidgetFactory;\r
import com.google.gwt.query.client.plugins.widgets.DisclosurePanelWidgetFactory;\r
+import com.google.gwt.query.client.plugins.widgets.WidgetsUtils;\r
import com.google.gwt.query.client.plugins.widgets.DisclosurePanelWidgetFactory.DisclosurePanelOptions;\r
import com.google.gwt.query.client.plugins.widgets.ListBoxWidgetFactory;\r
import com.google.gwt.query.client.plugins.widgets.ListBoxWidgetFactory.ListBoxOptions;\r
import com.google.gwt.user.client.ui.TabPanel;\r
import com.google.gwt.user.client.ui.TextBox;\r
import com.google.gwt.user.client.ui.Widget;\r
-import com.google.gwt.user.client.ui.WidgetsUtils;\r
import com.google.gwt.user.datepicker.client.DateBox;\r
\r
/**\r
.createArray().cast();
for (int i = 0; i < elementEvents.length(); i++) {
BindFunction listener = elementEvents.get(i);
-
- if (!listener.hasEventType(eventbits) || (namespace != null && namespace.length() != 0 && !listener.nameSpace.equals(namespace))) {
- newList.add(listener);
+ boolean matchNS = namespace == null || namespace.isEmpty() || listener.nameSpace.equals(namespace);
+ boolean matchEV = eventbits <= 0 || listener.hasEventType(eventbits);
+ if (matchNS && matchEV) {
+ continue;
}
+ newList.add(listener);
}
elementEvents = newList;
}
import com.google.gwt.dom.client.Element;
import com.google.gwt.user.client.ui.Button;
-import com.google.gwt.user.client.ui.WidgetsUtils;
/**
* Factory used to create a {@link Button} widget. A {@link Button} is created
import com.google.gwt.dom.client.Element;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.query.client.GQuery;
-import com.google.gwt.user.client.ui.WidgetsUtils;
import com.google.gwt.user.datepicker.client.DateBox;
import com.google.gwt.user.datepicker.client.DateBox.DefaultFormat;
import com.google.gwt.user.client.ui.DisclosurePanel;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.user.client.ui.Label;
-import com.google.gwt.user.client.ui.WidgetsUtils;
public class DisclosurePanelWidgetFactory implements
WidgetFactory<DisclosurePanel> {
import com.google.gwt.dom.client.SelectElement;
import com.google.gwt.query.client.GQuery;
import com.google.gwt.user.client.ui.ListBox;
-import com.google.gwt.user.client.ui.WidgetsUtils;
/**
* Factory used to create a {@link ListBox} widget.
import com.google.gwt.dom.client.Element;
import com.google.gwt.query.client.GQuery;
import com.google.gwt.user.client.ui.PasswordTextBox;
-import com.google.gwt.user.client.ui.WidgetsUtils;
/**
* Factory used to create a {@link PasswordTextBox} widget. A
import com.google.gwt.dom.client.Element;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.RichTextArea;
-import com.google.gwt.user.client.ui.WidgetsUtils;
/**
* Factory used to create a {@link RichTextArea} widget.
import com.google.gwt.user.client.ui.MultiWordSuggestOracle;
import com.google.gwt.user.client.ui.SuggestBox;
import com.google.gwt.user.client.ui.SuggestOracle;
-import com.google.gwt.user.client.ui.WidgetsUtils;
/**
* Factory used to create a {@link SuggestBox} widget.
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.user.client.ui.TabPanel;
import com.google.gwt.user.client.ui.Widget;
-import com.google.gwt.user.client.ui.WidgetsUtils;
/**
* Factory used to create a {@link Button} widget. A {@link Button} is created
import com.google.gwt.dom.client.Element;
import com.google.gwt.query.client.GQuery;
import com.google.gwt.user.client.ui.TextBox;
-import com.google.gwt.user.client.ui.WidgetsUtils;
/**
* Factory used to create a {@link TextBox} widget. A {@link TextBox} is created
--- /dev/null
+package com.google.gwt.query.client.plugins.widgets;
+
+import com.google.gwt.dom.client.Element;
+import com.google.gwt.query.client.GQuery;
+import com.google.gwt.user.client.ui.GqUi;
+import com.google.gwt.user.client.ui.Widget;
+
+public class WidgetsUtils {
+
+ private static final String[] appendingTags = {
+ "td", "th", "li"};
+
+
+ /**
+ * Test if the tag name of the element is one of tag names given in parameter
+ *
+ * @param tagNames
+ * @return
+ */
+ public static boolean matchesTags(Element e, String... tagNames) {
+
+ assert e != null : "Element cannot be null";
+
+ StringBuilder regExp = new StringBuilder("^(");
+ int tagNameLenght = tagNames != null ? tagNames.length : 0;
+ for (int i = 0; i < tagNameLenght; i++) {
+ regExp.append(tagNames[i].toUpperCase());
+ if (i < tagNameLenght - 1) {
+ regExp.append("|");
+ }
+ }
+ regExp.append(")$");
+
+ return e.getTagName().toUpperCase().matches(regExp.toString());
+
+ }
+
+ /**
+ * If the <code>oldElement</code> is a td, th, li tags, the new element will replaced its content.
+ * In other cases, the <code>oldElement</code> will be replaced by the <code>newElement</code>
+ * and the old element classes will be copied to the new element.
+ */
+ private static void replaceOrAppend(Element oldElement, Element newElement) {
+ assert oldElement != null && newElement != null;
+
+ if(matchesTags(oldElement, appendingTags)){
+ GQuery.$(oldElement).html("").append(newElement);
+ }else{
+ GQuery.$(oldElement).replaceWith(newElement);
+
+ String c = oldElement.getClassName();
+ if (!c.isEmpty()) {
+ newElement.addClassName(c);
+ }
+ }
+ }
+
+ /**
+ * Replace a dom element by a widget.
+ * If the widget implements Attachable the method attach will be called.
+ * Old element classes will be copied to the new widget.
+ */
+ public static void replaceOrAppend(Element e, Widget widget) {
+ assert e != null && widget != null;
+ GqUi.detachWidget(widget);
+ replaceOrAppend(e, widget.getElement());
+ GqUi.attachWidget(widget);
+ }
+}
--- /dev/null
+package com.google.gwt.user.client.ui;
+
+
+/**
+ * GQuery Utility class to access protected methods in ui package.
+ */
+public class GqUi {
+ public static void attachWidget(Widget widget) {
+ if (widget != null) {
+ widget.onAttach();
+ if (widget instanceof RichTextArea != true) {
+ RootPanel.detachOnWindowClose(widget);
+ }
+ }
+ }
+
+ public static void detachWidget(Widget widget){
+ if (widget != null) {
+ if (widget.isAttached()) {
+ widget.removeFromParent();
+ }
+ }
+ }
+}
+++ /dev/null
-package com.google.gwt.user.client.ui;
-
-import com.google.gwt.dom.client.Element;
-import com.google.gwt.query.client.GQuery;
-
-public class WidgetsUtils {
-
- private static final String[] appendingTags = {
- "td", "th", "li"};
-
-
- /**
- * Test if the tag name of the element is one of tag names given in parameter
- *
- * @param tagNames
- * @return
- */
- public static boolean matchesTags(Element e, String... tagNames) {
-
- assert e != null : "Element cannot be null";
-
- StringBuilder regExp = new StringBuilder("^(");
- int tagNameLenght = tagNames != null ? tagNames.length : 0;
- for (int i = 0; i < tagNameLenght; i++) {
- regExp.append(tagNames[i].toUpperCase());
- if (i < tagNameLenght - 1) {
- regExp.append("|");
- }
- }
- regExp.append(")$");
-
- return e.getTagName().toUpperCase().matches(regExp.toString());
-
- }
-
- /**
- * If the <code>oldElement</code> is a td, th, li tags, the new element will replaced its content.
- * In other cases, the <code>oldElement</code> will be replaced by the <code>newElement</code>
- * and the old element classes will be copied to the new element.
- */
- private static void replaceOrAppend(Element oldElement, Element newElement) {
- assert oldElement != null && newElement != null;
-
- if(matchesTags(oldElement, appendingTags)){
- GQuery.$(oldElement).html("").append(newElement);
- }else{
- GQuery.$(oldElement).replaceWith(newElement);
-
- String c = oldElement.getClassName();
- if (!c.isEmpty()) {
- newElement.addClassName(c);
- }
- }
- }
-
- /**
- * Replace a dom element by a widget.
- * If the widget implements Attachable the method attach will be called.
- * Old element classes will be copied to the new widget.
- */
- public static void replaceOrAppend(Element e, Widget widget) {
- assert e != null && widget != null;
-
- if (widget.isAttached()) {
- widget.removeFromParent();
- }
-
- replaceOrAppend(e, widget.getElement());
-
- widget.onAttach();
-
- if (widget instanceof RichTextArea != true) {
- RootPanel.detachOnWindowClose(widget);
- }
- }
-
-}
assertEquals(2, $(new Label(""), new TextArea()).size());
}
+
+ public void testGQueryWidgetManipulation() {
+
+ String content = "<div class='outer'></div>";
+ $(e).html(content);
+ Button b = new Button("b");
+ RootPanel.get().add(b);
+ assertTrue(b.isAttached());
+ $("button").remove();
+ assertFalse(b.isAttached());
+
+ Button b2 = new Button("b");
+ $(e).append($(b2));
+ assertTrue(b2.isAttached());
+
+ $(e).empty();
+
+ }
public void testGQueryMap() {
String content = "<p id='1'/><p/><p id='2'/><p id='4'/>";