this.left = left;\r
this.top = top;\r
}\r
- \r
+\r
public Offset add(int left, int top) {\r
return new Offset(this.left + left, this.top + top);\r
}\r
- \r
+\r
public String toString() {\r
return top + "+" + left;\r
}\r
}\r
\r
public static final BodyElement body = Document.get().getBody();\r
- \r
+\r
public static final Document document = Document.get();\r
public static boolean fxOff = false;\r
- \r
+\r
public static Class<GQuery> GQUERY = GQuery.class;\r
\r
public static final Element window = window();\r
FUNC_BEFORE = 3;\r
\r
private static final String OLD_DATA_PREFIX = "old-";\r
- \r
+\r
private static JsMap<Class<? extends GQuery>, Plugin<? extends GQuery>>\r
plugins;;\r
\r
* This function accepts a string containing a CSS selector which is then used\r
* to match a set of elements, or it accepts raw HTML creating a GQuery\r
* element containing those elements.\r
- * The second parameter is the context to use for the selector, or \r
+ * The second parameter is the context to use for the selector, or\r
* the document where the new elements will be created.\r
*/\r
public static GQuery $(String selectorOrHtml, Node ctx) {\r
}\r
return new GQuery(select(selectorOrHtml, ctx));\r
}\r
- \r
+\r
/**\r
* This function accepts a string containing a CSS selector which is then used\r
* to match a set of elements, or it accepts raw HTML creating a GQuery\r
}\r
return name != null ? d.getObject(name) : id;\r
}\r
- \r
+\r
protected static String[] jsArrayToString(JsArrayString array) {\r
if (GWT.isScript()) {\r
return jsArrayToString0(array);\r
return res;\r
}\r
\r
+ private native static Document getContentDocument(Node n) /*-{\r
+ var d = n.contentDocument || n.contentWindow.document;\r
+ if (!d.body) @com.google.gwt.query.client.GQuery::emptyDocument(Lcom/google/gwt/dom/client/Document;)(d);\r
+ return d;\r
+ }-*/;\r
+\r
private static boolean hasClass(Element e, String clz) {\r
- return ((" " + e.getClassName() + " ").matches(".*\\s" + clz + "\\s.*"));\r
+ return e.getClassName().matches("(^|.*\\s)" + clz + "(\\s.*|$)");\r
}\r
\r
private static GQuery innerHtml(String html) {\r
private static native String[] jsArrayToString0(JsArrayString array) /*-{\r
return array;\r
}-*/;\r
- \r
+\r
private static native <T extends Node> T[] reinterpretCast(NodeList<T> nl) /*-{\r
return nl;\r
}-*/;\r
\r
+ private static native void emptyDocument(Document d) /*-{\r
+ d.open(); d.write("<head/><body/>"); d.close();\r
+ }-*/;\r
+\r
private static NodeList<Element> select(String selector, Node context) {\r
if (engine == null) {\r
engine = new SelectorEngine();\r
\r
/**\r
* Add elements to the set of matched elements if they are not included yet.\r
- * It also update the selector appending the new one. \r
+ * It also update the selector appending the new one.\r
*/\r
public GQuery add(GQuery previousObject) {\r
return pushStack(unique(merge(elements, previousObject.elements)), "add",\r
}\r
\r
/**\r
- * All of the matched set of elements will be inserted at the end \r
+ * All of the matched set of elements will be inserted at the end\r
* of the element(s) specified by the parameter other.\r
- * \r
+ *\r
* The operation $(A).appendTo(B) is, essentially, the reverse of doing a regular\r
* $(A).append(B), instead of appending B to A, you're appending A to B.\r
*/\r
}\r
\r
/**\r
- * All of the matched set of elements will be inserted at the end \r
+ * All of the matched set of elements will be inserted at the end\r
* of the element(s) specified by the parameter other.\r
- * \r
+ *\r
* The operation $(A).appendTo(B) is, essentially, the reverse of doing a regular\r
* $(A).append(B), instead of appending B to A, you're appending A to B.\r
*/\r
}\r
\r
/**\r
- * All of the matched set of elements will be inserted at the end \r
+ * All of the matched set of elements will be inserted at the end\r
* of the element(s) specified by the parameter other.\r
- * \r
+ *\r
* The operation $(A).appendTo(B) is, essentially, the reverse of doing a regular\r
* $(A).append(B), instead of appending B to A, you're appending A to B.\r
*/\r
\r
/**\r
* Binds a set of handlers to a particular Event for each matched element.\r
- * \r
+ *\r
* The event handlers are passed as Functions that you can use to prevent\r
* default behavior. To stop both default action and event bubbling, the\r
* function event handler has to return false.\r
- * \r
+ *\r
* You can pass an additional Object data to your Function as the second\r
* parameter\r
- * \r
+ *\r
*/\r
public GQuery bind(int eventbits, final Object data, final Function...funcs) {\r
return as(Events).bind(eventbits, data, funcs);\r
public GQuery change(Function...f) {\r
return bindOrFire(Event.ONCHANGE, null, f);\r
}\r
- \r
+\r
/**\r
* Get a set of elements containing all of the unique immediate children of\r
* each of the matched set of elements. Also note: while parents() will look\r
}\r
\r
/**\r
- * Returns the inner height of the first matched element, including padding \r
+ * Returns the inner height of the first matched element, including padding\r
* but not the vertical scrollbar height, border, or margin.\r
*/\r
public int clientHeight() {\r
}\r
\r
/**\r
- * Returns the inner width of the first matched element, including padding \r
+ * Returns the inner width of the first matched element, including padding\r
* but not the vertical scrollbar width, border, or margin.\r
*/\r
public int clientWidth() {\r
public String css(String name) {\r
return styleImpl.curCSS(get(0), name, false);\r
}\r
- \r
+\r
/**\r
* Return a style property on the first matched element.\r
- * \r
+ *\r
* The parameter force has a special meaning here:\r
* - When force is false, returns the value of the css property defined\r
- * in the style attribute of the element. \r
+ * in the style attribute of the element.\r
* - Otherwise it returns the real computed value.\r
- * \r
+ *\r
* For instance if you define 'display=none' not in the element style\r
* but in the css stylesheet, it returns an empty string unless you\r
- * pass the parameter force=true. \r
+ * pass the parameter force=true.\r
*/\r
public String css(String name, boolean force) {\r
return styleImpl.curCSS(get(0), name, force);\r
}\r
return this;\r
}\r
- \r
+\r
/**\r
* Set CSS property on every matched element using type-safe enumerations.\r
*/\r
}\r
}\r
return this;\r
- } \r
- \r
+ }\r
+\r
/**\r
* Returns the working set of nodes as a Java array. <b>Do NOT</b attempt to\r
* modify this array, e.g. assign to its elements, or call Arrays.sort()\r
\r
/**\r
* Remove all child nodes from the set of matched elements.\r
+ * In the case of a document element, it removes all the content.\r
+ * You should call empty() when you create a new iframe and you\r
+ * want to add dynamic content to it.\r
*/\r
public GQuery empty() {\r
- // TODO: add memory leak cleanup, remove event handlers, and \r
+ // TODO: add memory leak cleanup, remove event handlers, and\r
// data caches\r
for (Element e : elements()) {\r
- while (e.getFirstChild() != null) {\r
- e.removeChild(e.getFirstChild());\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
+ }\r
}\r
}\r
return this;\r
*/\r
public GQuery fadeOut(Function... f) {\r
return $(as(Effects).fadeOut(f));\r
- } \r
- \r
+ }\r
+\r
/**\r
* Fade out all matched elements by adjusting their opacity.\r
*/\r
public GQuery gt(int pos) {\r
return $(slice(pos + 1, -1));\r
}\r
- \r
+\r
/**\r
* Returns true any of the specified classes are present on any of the matched\r
* elements.\r
public GQuery insertBefore(Element item) {\r
return insertBefore($(item));\r
}\r
- \r
+\r
/**\r
* Insert all of the matched elements before another, specified, set of\r
* elements.\r
query.before(e);\r
}\r
return this;\r
- } \r
+ }\r
\r
/**\r
* Insert all of the matched elements before another, specified, set of\r
*/\r
public boolean is(String... filters) {\r
return filter(filters).size() > 0;\r
- } \r
+ }\r
\r
/**\r
* Bind a set of functions to the keydown event of each matched element.\r
*/\r
public GQuery keydown(int key) {\r
return trigger(Event.ONKEYDOWN, key);\r
- } \r
+ }\r
\r
/**\r
* Bind a set of functions to the keypress event of each matched element.\r
*/\r
public GQuery keypress(Function...f) {\r
return bindOrFire(Event.ONKEYPRESS, null, f);\r
- } \r
+ }\r
\r
/**\r
* Trigger a keypress event passing the key pushed.\r
public GQuery keyup(int key) {\r
return trigger(Event.ONKEYUP, key);\r
}\r
- \r
+\r
/**\r
* Returns the computed left position of the first element matched.\r
*/\r
public int left() {\r
return (int) GQUtils.cur(get(0), "left", true);\r
- } \r
- \r
+ }\r
+\r
/**\r
* Returns the number of elements currently matched. The size function will\r
* return the same value.\r
*/\r
public int length() {\r
return size();\r
- } \r
- \r
+ }\r
+\r
/**\r
* Bind a function to the load event of each matched element.\r
*/\r
public GQuery load(Function f) {\r
return bind(Event.ONLOAD, null, f);\r
- } \r
+ }\r
\r
/**\r
* Reduce the set of matched elements to all elements before a given position.\r
*/\r
public GQuery lt(int pos) {\r
return $(slice(0, pos));\r
- } \r
+ }\r
\r
/**\r
* Bind a set of functions to the mousedown event of each matched element.\r
*/\r
public GQuery mousedown(Function...f) {\r
return bindOrFire(Event.ONMOUSEDOWN, null, f);\r
- } \r
- \r
+ }\r
+\r
/**\r
* Bind a set of functions to the mousemove event of each matched element.\r
* Or trigger the event if no functions are provided.\r
*/\r
public GQuery mousemove(Function...f) {\r
return bindOrFire(Event.ONMOUSEMOVE, null, f);\r
- } \r
+ }\r
\r
/**\r
* Bind a set of functions to the mouseout event of each matched element.\r
*/\r
public GQuery offsetParent() {\r
Element offParent = GQUtils.or(elements.getItem(0).getOffsetParent(), body);\r
- while (offParent != null \r
+ while (offParent != null\r
&& !"body".equalsIgnoreCase(offParent.getTagName())\r
- && !"html".equalsIgnoreCase(offParent.getTagName()) \r
+ && !"html".equalsIgnoreCase(offParent.getTagName())\r
&& "static".equals(styleImpl.curCSS(offParent, "position", true))) {\r
offParent = offParent.getOffsetParent();\r
}\r
public GQuery prepend(String html) {\r
return domManip(html, document, FUNC_PREPEND);\r
}\r
- \r
+\r
/**\r
- * All of the matched set of elements will be inserted at the beginning \r
+ * All of the matched set of elements will be inserted at the beginning\r
* of the element(s) specified by the parameter other.\r
- * \r
+ *\r
* The operation $(A).prependTo(B) is, essentially, the reverse of doing a regular\r
* $(A).prepend(B), instead of prepending B to A, you're prepending A to B.\r
*/\r
}\r
\r
/**\r
- * All of the matched set of elements will be inserted at the beginning \r
+ * All of the matched set of elements will be inserted at the beginning\r
* of the element(s) specified by the parameter other.\r
- * \r
+ *\r
* The operation $(A).prependTo(B) is, essentially, the reverse of doing a regular\r
* $(A).prepend(B), instead of prepending B to A, you're prepending A to B.\r
*/\r
}\r
\r
/**\r
- * All of the matched set of elements will be inserted at the beginning \r
+ * All of the matched set of elements will be inserted at the beginning\r
* of the element(s) specified by the parameter other.\r
- * \r
+ *\r
* The operation $(A).prependTo(B) is, essentially, the reverse of doing a regular\r
* $(A).prepend(B), instead of prepending B to A, you're prepending A to B.\r
*/\r
}\r
return this;\r
}\r
- \r
+\r
/**\r
* Replaces the elements matched by the specified selector with the matched\r
* elements. This function is the complement to replaceWith() which does the\r
*/\r
public GQuery replaceWith(Element elem) {\r
return replaceWith($(elem));\r
- } \r
- \r
+ }\r
+\r
/**\r
* Replaces all matched elements with the specified HTML or DOM elements. This\r
* returns the GQuery element that was just replaced, which has been removed\r
}\r
}\r
}\r
- \r
+\r
/**\r
* Restore a set of previously saved Css properties in every matched element.\r
*/\r
*/\r
public GQuery show() {\r
for (Element e : elements()) {\r
- styleImpl.setStyleProperty(e, "display", \r
+ styleImpl.setStyleProperty(e, "display",\r
GQUtils.or((String) data(e, "oldDisplay", null), ""));\r
// When the display=none is in the stylesheet.\r
if (!styleImpl.isVisible(e)) {\r
return click(new Function() {\r
int click = 0;\r
public boolean f(Event e) {\r
- int n = fn.length == 1 ? 0 : (click++ % fn.length); \r
+ int n = fn.length == 1 ? 0 : (click++ % fn.length);\r
return fn[n].f(e);\r
}\r
});\r
/**\r
* Adds or removes the specified classes to each matched element\r
* depending on the value of the switch argument.\r
- * \r
+ *\r
* if addOrRemove is true, the class is added and in the case of\r
* false it is removed.\r
*/\r
}\r
return this;\r
}\r
- \r
+\r
/**\r
* Returns the computed left position of the first element matched.\r
*/\r
public int top() {\r
return (int) GQUtils.cur(get(0), "top", true);\r
- } \r
+ }\r
\r
/**\r
* Produces a string representation of the matched elements.\r
\r
/**\r
* Trigger a set of events on each matched element.\r
- * \r
- * For keyboard events you can pass a second parameter which represents \r
- * the key-code of the pushed key. \r
- * \r
+ *\r
+ * For keyboard events you can pass a second parameter which represents\r
+ * the key-code of the pushed key.\r
+ *\r
* Example: fire(Event.ONCLICK | Event.ONFOCUS)\r
* Example: fire(Event.ONKEYDOWN. 'a');\r
*/\r
} else if ("input".equalsIgnoreCase(name)) {\r
InputElement ie = InputElement.as(e);\r
String type = ie.getType();\r
- if ("radio".equalsIgnoreCase((type)) \r
+ if ("radio".equalsIgnoreCase((type))\r
|| "checkbox".equalsIgnoreCase(type)) {\r
if ("checkbox".equalsIgnoreCase(type)) {\r
for (String val : values) {\r
}\r
\r
/**\r
- * Bind Handlers or fire Events for each matched element. \r
+ * Bind Handlers or fire Events for each matched element.\r
*/\r
private GQuery bindOrFire(int eventbits, final Object data, final Function...funcs) {\r
if (funcs.length == 0) {\r
return bind(eventbits, data, funcs);\r
}\r
}\r
- \r
- private GQuery domManip(String htmlString, int func) {\r
- GQuery ret = $();\r
- HashMap<Document, GQuery> cache = new HashMap<Document, GQuery>();\r
- for (Element e: elements()) {\r
- Document d = e.getNodeType() == Node.DOCUMENT_NODE ? e.<Document>cast() : e.getOwnerDocument();\r
- GQuery g = cache.get(d);\r
- if (g == null) {\r
- g = cleanHtmlString(htmlString, d);\r
- cache.put(d, g);\r
- }\r
- ret.add(domManip(g, func));\r
- }\r
- return ret;\r
- }\r
\r
private GQuery domManip(GQuery g, int func) {\r
JSArray newNodes = JSArray.create();\r
for (int i = 0; i < elements().length; i++) {\r
Element e = elements()[i];\r
e.getOwnerDocument();\r
- if (e.getNodeType() == Node.DOCUMENT_NODE) { \r
+ if (e.getNodeType() == Node.DOCUMENT_NODE) {\r
e = e.<Document>cast().getBody();\r
}\r
for (int j = 0; j < g.size(); j++) {\r
return domManip(html, func);\r
}\r
\r
- private native Document getContentDocument(Node n) /*-{\r
- var d = n.contentDocument || n.contentWindow.document;\r
- if (!d.body) d.write("<body/>");\r
- return d;\r
- }-*/;\r
- \r
+ private GQuery domManip(String htmlString, int func) {\r
+ GQuery ret = $();\r
+ HashMap<Document, GQuery> cache = new HashMap<Document, GQuery>();\r
+ for (Element e: elements()) {\r
+ Document d = e.getNodeType() == Node.DOCUMENT_NODE ? e.<Document>cast() : e.getOwnerDocument();\r
+ GQuery g = cache.get(d);\r
+ if (g == null) {\r
+ g = cleanHtmlString(htmlString, d);\r
+ cache.put(d, g);\r
+ }\r
+ ret.add(domManip(g, func));\r
+ }\r
+ return ret;\r
+ }\r
+\r
private native Element getPreviousSiblingElement(Element elem) /*-{\r
var sib = elem.previousSibling;\r
while (sib && sib.nodeType != 1)\r
}\r
return res;\r
}\r
- \r
+\r
private void removeData(Element item, String name) {\r
if (dataCache == null) {\r
windowData = JavaScriptObject.createObject().cast();\r
dataCache.delete(id);\r
}\r
}\r
- \r
+\r
}\r