diff options
author | Ray Cromwell <cromwellian@gmail.com> | 2009-05-05 00:12:58 +0000 |
---|---|---|
committer | Ray Cromwell <cromwellian@gmail.com> | 2009-05-05 00:12:58 +0000 |
commit | 2020aa708fca88b683ff19438de72aa08b268d2d (patch) | |
tree | 69c03b7d2319a1a43cb1920ef892183fa89f9c69 /gwtquery-core/src | |
parent | 5d89867da687e9b4f01f13bfb2b71a0ad51198e8 (diff) | |
download | gwtquery-2020aa708fca88b683ff19438de72aa08b268d2d.tar.gz gwtquery-2020aa708fca88b683ff19438de72aa08b268d2d.zip |
Added lazy generator
Diffstat (limited to 'gwtquery-core/src')
14 files changed, 1345 insertions, 22 deletions
diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/Query.gwt.xml b/gwtquery-core/src/main/java/com/google/gwt/query/Query.gwt.xml index a09eeafc..0b503ae6 100644 --- a/gwtquery-core/src/main/java/com/google/gwt/query/Query.gwt.xml +++ b/gwtquery-core/src/main/java/com/google/gwt/query/Query.gwt.xml @@ -16,6 +16,10 @@ ]]>
</property-provider>
+ <generate-with class="com.google.gwt.query.rebind.LazyGenerator">
+ <when-type-assignable class="com.google.gwt.query.client.Lazy"/>
+ </generate-with>
+
<generate-with class="com.google.gwt.query.rebind.SelectorGeneratorJS">
<when-type-assignable class="com.google.gwt.query.client.Selectors"/>
<any>
@@ -78,4 +82,4 @@ </all>
</replace-with>
-</module>
\ No newline at end of file +</module>
diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/$.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/$.java index c57777dc..a3c56323 100644 --- a/gwtquery-core/src/main/java/com/google/gwt/query/client/$.java +++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/$.java @@ -18,13 +18,18 @@ package com.google.gwt.query.client; import com.google.gwt.dom.client.Element; import com.google.gwt.dom.client.Node; import com.google.gwt.dom.client.NodeList; +import com.google.gwt.user.client.Event; /** - * A facade class of forwarding functions which allow end users to refer to - * the GQuery class as '$' if they desire. + * A facade class of forwarding functions which allow end users to refer to the + * GQuery class as '$' if they desire. */ public class $ { + public static LazyGQuery lazy() { + return GQuery.$().lazy(); + } + public static GQuery $(String selectorOrHtml) { return GQuery.$(selectorOrHtml); } @@ -87,8 +92,6 @@ public class $ { /** * Registers a GQuery plugin. - * @param plugin - * @param pluginFactory */ public static void registerPlugin(Class<? extends GQuery> plugin, Plugin<? extends GQuery> pluginFactory) { 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 127b451e..574ab8c7 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,10 +45,9 @@ import java.util.Map; /**
* Gwt Query is a GWT clone of the popular jQuery library.
*/
-public class GQuery {
+public class GQuery implements Lazy<GQuery, LazyGQuery> {
/**
- *
* A POJO used to store the top/left CSS positioning values of an element.
*/
public static class Offset {
@@ -86,9 +85,7 @@ public class GQuery { public native JavaScriptObject get(int id) /*-{
return this[id];
- }-*/; /*-{
- delete this[name];
- }-*/
+ }-*/;
public DataCache getCache(int id) {
return get(id).cast();
@@ -207,6 +204,13 @@ public class GQuery { FUNC_BEFORE = 3;
/**
+ * Create an empty GQuery object.
+ */
+ public static GQuery $() {
+ return new GQuery(JSArray.create());
+ }
+
+ /**
* This function accepts a string containing a CSS selector which is then used
* to match a set of elements, or it accepts raw HTML creating a GQuery
* element containing those elements.
@@ -741,6 +745,22 @@ public class GQuery { }
/**
+ * Set a single style property to a value, on all matched elements using
+ * type-safe overhead-free enumerations.
+ *
+ * @param property a CSS property type
+ * @param value a legal value from the type T
+ * @param <T> inferred from the CSS property type
+ * @return
+ */
+// public <T> GQuery css(CssProperty<T> property, T value) {
+// for(Element e : elements()) {
+// property.set(e.getStyle(), value);
+// }
+// return this;
+
+ // }
+ /**
* Return a style property on the first matched element.
*/
public String css(String name) {
@@ -873,6 +893,10 @@ public class GQuery { return this;
}
+ public LazyGQuery lazy() {
+ return GWT.create(GQuery.class);
+ }
+
/**
* Revert the most recent 'destructive' operation, changing the set of matched
* elements to its previous state (right before the destructive operation).
@@ -921,7 +945,7 @@ public class GQuery { * Fade out all matched elements by adjusting their opacity.
*/
public GQuery fadeOut(int millisecs) {
- return $(as(Effects).fadeOut(millisecs));
+ return as(Effects).fadeOut(millisecs);
}
/**
@@ -2219,10 +2243,11 @@ public class GQuery { }
for (Element e : wrap.elements()) {
Node n = e;
- while (n.getFirstChild() != null) {
+ while (n.getFirstChild() != null
+ && n.getFirstChild().getNodeType() == Node.ELEMENT_NODE) {
n = n.getFirstChild();
- $((Element) n).append(this);
}
+ $((Element) n).append(this);
}
return this;
}
diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/JsClosure.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/JsClosure.java new file mode 100644 index 00000000..b8d51f33 --- /dev/null +++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/JsClosure.java @@ -0,0 +1,20 @@ +package com.google.gwt.query.client; + +import com.google.gwt.core.client.JavaScriptObject; + +/** + * Overlay type representing a Javascript closure. + */ +public class JsClosure extends JavaScriptObject { + + protected JsClosure() { + } + + /** + * Invoke the closure with no arguments and expecting no return value. + */ + public final native void invoke() /*-{ + return this(); + }-*/; + +} diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/Lazy.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/Lazy.java new file mode 100644 index 00000000..5f2347bb --- /dev/null +++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/Lazy.java @@ -0,0 +1,18 @@ +package com.google.gwt.query.client; + +/** + * A tagging interface which triggers the LazyGenerator for type T. + * LazyGenerator creates an implementation of Type T for the class by + * forwarding method calls to the class which implements the Lazy interface. + * Methods in the generated class do not execute but instead queue up a + * deferred execution of the method. + */ +public interface Lazy<S, T extends LazyBase<S>> { + + /** + * Create a lazy instance of the current class. Most implementing classes + * will automate this by simply invoking GWT.create(). + * @return + */ + T lazy(); +} diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/LazyBase.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/LazyBase.java new file mode 100644 index 00000000..2f34bbdd --- /dev/null +++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/LazyBase.java @@ -0,0 +1,9 @@ +package com.google.gwt.query.client; + +/** + * All lazy interfaces must extend this baseclass. This ensures the done() + * method exists and returns an executable function. + */ +public interface LazyBase<S> { + Function done(); +} 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 new file mode 100644 index 00000000..c30773fc --- /dev/null +++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/LazyGQuery.java @@ -0,0 +1,907 @@ +package com.google.gwt.query.client; + +import com.google.gwt.dom.client.Node; +import com.google.gwt.dom.client.Element; +//import com.google.gwt.query.client.css.CssProperty; + +/** + * Created by IntelliJ IDEA. User: ray Date: May 2, 2009 Time: 10:48:07 PM To + * change this template use File | Settings | File Templates. + */ +public interface LazyGQuery extends LazyBase<GQuery> { + + /** + * Add elements to the set of matched elements if they are not included yet. + */ + + LazyGQuery add(String selector); + + /** + * Adds the specified classes to each matched element. Add elements to the set + * of matched elements if they are not included yet. + */ + LazyGQuery add(GQuery previousObject); + + /** + * Adds the specified classes to each matched element. + */ + LazyGQuery addClass(String... classes); + + /** + * Insert content after each of the matched elements. The elements must + * already be inserted into the document (you can't insert an element after + * another if it's not in the page). + */ + LazyGQuery after(Node n); + + /** + * Insert content after each of the matched elements. The elements must + * already be inserted into the document (you can't insert an element after + * another if it's not in the page). + */ + LazyGQuery after(String html); + + /** + * Insert content after each of the matched elements. The elements must + * already be inserted into the document (you can't insert an element after + * another if it's not in the page). + */ + LazyGQuery after(GQuery query); + + /** + * Add the previous selection to the current selection. Useful for traversing + * elements, and then adding something that was matched before the last + * traversal. + */ + LazyGQuery andSelf(); + + /** + * Append content to the inside of every matched element. This operation is + * similar to doing an appendChild to all the specified elements, adding them + * into the document. + */ + LazyGQuery append(String html); + + /** + * Append content to the inside of every matched element. This operation is + * similar to doing an appendChild to all the specified elements, adding them + * into the document. + */ + LazyGQuery append(Node n); + + /** + * Append content to the inside of every matched element. This operation is + * similar to doing an appendChild to all the specified elements, adding them + * into the document. + */ + LazyGQuery append(GQuery query); + + /** + * Append all of the matched elements to another, specified, set of elements. + * This operation is, essentially, the reverse of doing a regular + * $(A).append(B), in that instead of appending B to A, you're appending A to + * B. + */ + LazyGQuery appendTo(GQuery other); + + /** + * Convert to Plugin interface provided by Class literal. + */ + <T extends LazyGQuery> T as(Class<T> plugin); + + /** + * Set a single property to a value, on all matched elements. + */ + LazyGQuery attr(String key, String value); + + /** + * Set a key/value object as properties to all matched elements. + * + * Example: $("img").attr(new Properties("src: 'test.jpg', alt: 'Test + * Image'")) + */ + LazyGQuery attr(Properties properties); + + /** + * Set a single property to a computed value, on all matched elements. + */ + LazyGQuery attr(String key, Function closure); + + /** + * Insert content before each of the matched elements. The elements must + * already be inserted into the document (you can't insert an element before + * another if it's not in the page). + */ + LazyGQuery before(Node n); + + /** + * Insert content before each of the matched elements. The elements must + * already be inserted into the document (you can't insert an element before + * another if it's not in the page). + */ + LazyGQuery before(GQuery query); + + /** + * Insert content before each of the matched elements. The elements must + * already be inserted into the document (you can't insert an element before + * another if it's not in the page). + */ + LazyGQuery before(String html); + + /** + * Binds a handler to a particular Event (like Event.ONCLICK) for each matched + * element. + * + * The event handler is passed as a Function that you can use to prevent + * default behaviour. To stop both default action and event bubbling, the + * function event handler has to return false. + * + * You can pass an additional Object data to your Function as the second + * parameter + */ + LazyGQuery bind(int eventbits, Object data, Function f); + + /** + * Bind a function to the blur event of each matched element. + */ + LazyGQuery blur(Function f); + + /** + * Trigger a blur event. + */ + LazyGQuery blur(); + + /** + * Bind a function to the change event of each matched element. + */ + LazyGQuery change(Function f); + + /** + * Trigger a change event. + */ + LazyGQuery change(); + + /** + * Get a set of elements containing all of the unique children of each of the + * matched set of elements. This set is filtered with the expressions that + * will cause only elements matching any of the selectors to be collected. + */ + LazyGQuery children(String... filters); + + /** + * Get a set of elements containing all of the unique immediate children of + * each of the matched set of elements. Also note: while parents() will look + * at all ancestors, children() will only consider immediate child elements. + */ + LazyGQuery children(); + + /** + * Trigger a click event. + */ + LazyGQuery click(); + + /** + * Triggers the click event of each matched element. Causes all of the + * functions that have been bound to that click event to be executed. + */ + LazyGQuery click(Function f); + + /** + * Clone matched DOM Elements and select the clones. This is useful for moving + * copies of the elements to another location in the DOM. + */ + LazyGQuery clone(); + + /** + * Filter the set of elements to those that contain the specified text. + */ + LazyGQuery contains(String text); + + /** + * Find all the child nodes inside the matched elements (including text + * nodes), or the content document, if the element is an iframe. + */ + LazyGQuery contents(); + + /** + * Set a single style property to a value, on all matched elements using + * type-safe overhead-free enumerations. + * + * @param property a CSS property type + * @param value a legal value from the type T + * @param <T> inferred from the CSS property type + * @return + */ +// <T> LazyGQuery css(CssProperty<T> property, T value); + + /** + * Set a key/value object as style properties to all matched elements. This + * serves as the best way to set a large number of style properties on all + * matched elements. + * + * Example: $(".item").css(Properties.create("color: 'red', background: + * 'blue'")) + */ + LazyGQuery css(Properties properties); + + /** + * Set a single style property to a value, on all matched elements. + */ + LazyGQuery css(String prop, String val); + + /** + * Trigger a double click event. + */ + LazyGQuery dblclick(); + + /** + * Bind a function to the dblclick event of each matched element. + */ + LazyGQuery dblclick(Function f); + + /** + * Removes a queued function from the front of the queue and executes it. + */ + LazyGQuery dequeue(String type); + + /** + * Removes a queued function from the front of the FX queue and executes it. + */ + LazyGQuery dequeue(); + + /** + * Run one or more Functions over each element of the GQuery. + */ + LazyGQuery each(Function... f); + + /** + * Remove all child nodes from the set of matched elements. + */ + LazyGQuery empty(); + + /** + * Revert the most recent 'destructive' operation, changing the set of matched + * elements to its previous state (right before the destructive operation). + */ + LazyGQuery end(); + + /** + * Reduce GQuery to element in the specified position. + */ + LazyGQuery eq(int pos); + + /** + * Trigger an error event. + */ + LazyGQuery error(); + + /** + * Bind a function to the error event of each matched element. + */ + LazyGQuery error(Function f); + + /** + * Fade in all matched elements by adjusting their opacity. + */ + LazyGQuery fadeIn(int millisecs); + + /** + * Fade in all matched elements by adjusting their opacity. The effect will + * take 1000 milliseconds to complete + */ + LazyGQuery fadeIn(); + + /** + * Fade out all matched elements by adjusting their opacity. + */ + LazyGQuery fadeOut(int millisecs); + + /** + * Fade out all matched elements by adjusting their opacity. The effect will + * take 1000 milliseconds to complete + */ + LazyGQuery fadeOut(); + + /** + * Removes all elements from the set of matched elements that do not match the + * specified function. The function is called with a context equal to the + * current element. If the function returns false, then the element is removed + * - anything else and the element is kept. + */ + LazyGQuery filter(Predicate filterFn); + + /** + * Removes all elements from the set of matched elements that do not pass the + * specified css expression. This method is used to narrow down the results of + * a search. Provide a comma-separated list of expressions to apply multiple + * filters at once. + */ + LazyGQuery filter(String... filters); + + /** + * Searches for all elements that match the specified css expression. This + * method is a good way to find additional descendant elements with which to + * process. + * + * Provide a comma-separated list of expressions to apply multiple filters at + * once. + */ + LazyGQuery find(String... filters); + + /** + * Trigger a focus event. + */ + LazyGQuery focus(); + + /** + * Bind a function to the focus event of each matched element. + */ + + LazyGQuery focus(Function f); + + /** + * Return the previous set of matched elements prior to the last destructive + * operation (e.g. query) + */ + LazyGQuery getPreviousObject(); + + /** + * Returns true any of the specified classes are present on any of the matched + * Reduce the set of matched elements to all elements after a given position. + * The position of the element in the set of matched elements starts at 0 and + * goes to length - 1. + */ + LazyGQuery gt(int pos); + + /** + * Set the height of every element in the matched set. + */ + LazyGQuery height(int height); + + /** + * Set the height style property of every matched element. It's useful for + * using 'percent' or 'em' units Example: $(".a").width("100%") + */ + LazyGQuery height(String height); + + /** + * Make invisible all matched elements. + */ + LazyGQuery hide(); + + /** + * Bind a function to the mouseover event of each matched element. A method + * for simulating hovering (moving the mouse on, and off, an object). This is + * a custom method which provides an 'in' to a frequent task. Whenever the + * mouse cursor is moved over a matched element, the first specified function + * is fired. Whenever the mouse moves off of the element, the second specified + * function fires. + */ + LazyGQuery hover(Function fover, Function fout); + + /** + * Set the innerHTML of every matched element. + */ + LazyGQuery html(String html); + + /** + * Insert all of the matched elements after another, specified, set of + * elements. + */ + LazyGQuery insertAfter(String selector); + + /** + * Insert all of the matched elements after another, specified, set of + * elements. + */ + LazyGQuery insertAfter(Element elem); + + /** + * Insert all of the matched elements after another, specified, set of + * elements. + */ + LazyGQuery insertAfter(GQuery query); + + /** + * Insert all of the matched elements before another, specified, set of + * elements. + * + * The elements must already be inserted into the document (you can't insert + * an element after another if it's not in the page). + */ + LazyGQuery insertBefore(Element item); + + /** + * Insert all of the matched elements before another, specified, set of + * elements. + * + * The elements must already be inserted into the document (you can't insert + * an element after another if it's not in the page). + */ + LazyGQuery insertBefore(GQuery query); + + /** + * Insert all of the matched elements before another, specified, set of + * elements. + * + * The elements must already be inserted into the document (you can't insert + * an element after another if it's not in the page). + */ + LazyGQuery insertBefore(String selector); + + /** + * Trigger a keydown event. + */ + LazyGQuery keydown(); + + /** + * Trigger a keypress event. + */ + LazyGQuery keypress(); + + /** + * Bind a function to the keypress event of each matched element. + */ + LazyGQuery keypressed(Function f); + + /** + * Trigger a keyup event. + */ + LazyGQuery keyup(); + + /** + * Bind a function to the keyup event of each matched element. + */ + LazyGQuery keyup(Function f); + + /** + * Bind a function to the load event of each matched element. + */ + LazyGQuery load(Function f); + + /** + * Reduce the set of matched elements to all elements before a given position. + * The position of the element in the set of matched elements starts at 0 and + * goes to length - 1. + */ + LazyGQuery lt(int pos); + + /** + * Bind a function to the mousedown event of each matched element. + */ + LazyGQuery mousedown(Function f); + + /** + * Bind a function to the mousemove event of each matched element. + */ + LazyGQuery mousemove(Function f); + + /** + * Bind a function to the mouseout event of each matched element. + */ + LazyGQuery mouseout(Function f); + + /** + * Bind a function to the mouseover event of each matched element. + */ + LazyGQuery mouseover(Function f); + + /** + * Bind a function to the mouseup event of each matched element. + */ + LazyGQuery mouseup(Function f); + + /** + * Get a set of elements containing the unique next siblings of each of the + * given set of elements. next only returns the very next sibling for each + * element, not all next siblings see {#nextAll}. + */ + LazyGQuery next(); + + /** + * Get a set of elements containing the unique next siblings of each of the + * given set of elements filtered by 1 or more selectors. next only returns + * the very next sibling for each element, not all next siblings see + * {#nextAll}. + */ + LazyGQuery next(String... selectors); + + /** + * Find all sibling elements after the current element. + */ + LazyGQuery nextAll(); + + /** + * Removes the specified Element from the set of matched elements. This method + * is used to remove a single Element from a jQuery object. + */ + LazyGQuery not(Element elem); + + /** + * Removes any elements inside the passed set of elements from the set of + * matched elements. + */ + LazyGQuery not(GQuery gq); + + /** + * Removes elements matching the specified expression from the set of matched + * elements. + */ + LazyGQuery not(String... filters); + + /** + * Returns a GQuery collection with the positioned parent of the first matched + * element. This is the first parent of the element that has position (as in + * relative or absolute). This method only works with visible elements. + */ + LazyGQuery offsetParent(); + + /** + * Binds a handler to a particular Event (like Event.ONCLICK) for each matched + * element. The handler is executed only once for each element. + * + * The event handler is passed as a Function that you can use to prevent + * default behaviour. To stop both default action and event bubbling, the + * function event handler has to return false. + * + * You can pass an additional Object data to your Function as the second + * parameter + */ + LazyGQuery one(int eventbits, Object data, Function f); + + /** + * Get a set of elements containing the unique parents of the matched set of + * elements. + */ + LazyGQuery parent(); + + /** + * Get a set of elements containing the unique parents of the matched set of + * elements. You may use an optional expressions to filter the set of parent + * elements that will match one of them. + */ + LazyGQuery parent(String... filters); + + /** + * Get a set of elements containing the unique ancestors of the matched set of + * elements (except for the root element). The matched elements are filtered, + * returning those that match any of the filters. + */ + LazyGQuery parents(String... filters); + + /** + * Get a set of elements containing the unique ancestors of the matched set of + * elements (except for the root element). + */ + LazyGQuery parents(); + + /** + * Prepend content to the inside of every matched element. This operation is + * the best way to insert elements inside, at the beginning, of all matched + * elements. + */ + LazyGQuery prepend(String html); + + /** + * Prepend content to the inside of every matched element. This operation is + * the best way to insert elements inside, at the beginning, of all matched + * elements. + */ + LazyGQuery prepend(GQuery query); + + /** + * Prepend content to the inside of every matched element. This operation is + * the best way to insert elements inside, at the beginning, of all matched + * elements. + */ + LazyGQuery prepend(Node n); + + /** + * Prepend all of the matched elements to another, specified, set of elements. + * This operation is, essentially, the reverse of doing a regular + * $(A).prepend(B), in that instead of prepending B to A, you're prepending A + * to B. + */ + LazyGQuery prependTo(GQuery elms); + + /** + * Get a set of elements containing the unique previous siblings of each of + * the matched set of elements. Only the immediately previous sibling is + * returned, not all previous siblings. + */ + LazyGQuery prev(); + + /** + * Get a set of elements containing the unique previous siblings of each of + * the matched set of elements filtered by selector. Only the immediately + * previous sibling is returned, not all previous siblings. + */ + LazyGQuery prev(String... selectors); + + /** + * Find all sibling elements in front of the current element. + */ + LazyGQuery prevAll(); + + /** + * Adds a new function, to be executed, onto the end of the queue of all + * matched elements. + */ + LazyGQuery queue(String type, Function data); + + /** + * Adds a new function, to be executed, onto the end of the queue of all + * matched elements in the FX queue. + */ + LazyGQuery queue(Function data); + + /** + * Removes all matched elements from the DOM. + */ + LazyGQuery remove(); + + /** + * Remove the named attribute from every element in the matched set. + */ + LazyGQuery removeAttr(String key); + + /** + * Removes the specified classes to each matched element. + */ + LazyGQuery removeClass(String... classes); + + /** + * Removes named data store from an element. + */ + LazyGQuery removeData(String name); + + /** + * Replaces the elements matched by the specified selector with the matched + * elements. This function is the complement to replaceWith() which does the + * same task with the parameters reversed. + */ + LazyGQuery replaceAll(GQuery query); + + /** + * Replaces the elements matched by the specified selector with the matched + * elements. This function is the complement to replaceWith() which does the + * same task with the parameters reversed. + */ + LazyGQuery replaceAll(String html); + + /** + * Replaces the elements matched by the specified selector with the matched + * elements. This function is the complement to replaceWith() which does the + * same task with the parameters reversed. + */ + LazyGQuery replaceAll(Element elem); + + /** + * Replaces all matched elements with the specified HTML or DOM elements. This + * returns the GQuery element that was just replaced, which has been removed + * from the DOM. + */ + LazyGQuery replaceWith(GQuery query); + + /** + * Replaces all matched elements with the specified HTML or DOM elements. This + * returns the GQuery element that was just replaced, which has been removed + * from the DOM. + */ + LazyGQuery replaceWith(String html); + + /** + * Replaces all matched elements with the specified HTML or DOM elements. This + * returns the GQuery element that was just replaced, which has been removed + * from the DOM. + */ + LazyGQuery replaceWith(Element elem); + + /** + * Bind a function to the scroll event of each matched element. + */ + LazyGQuery scroll(Function f); + + /** + * When a value is passed in, the scroll left offset is set to that value on + * all matched elements. This method works for both visible and hidden + * elements. + */ + LazyGQuery scrollLeft(int left); + + /** + * Gets the scroll left offset of the first matched element. This method works + * for both visible and hidden elements. + */ + int scrollLeft(); + + /** + * When a value is passed in, the scroll top offset is set to that value on + * all matched elements. This method works for both visible and hidden + * elements. + */ + LazyGQuery scrollTop(int top); + + /** + * Gets the scroll top offset of the first matched element. This method works + * for both visible and hidden elements. + */ + int scrollTop(); + + LazyGQuery select(); + + /** + * Return the number of elements in the matched set. Make visible all mached + * elements + */ + LazyGQuery show(); + + /** + * Get a set of elements containing all of the unique siblings of each of the + * matched set of elements. + */ + LazyGQuery siblings(); + + /** + * Get a set of elements containing all of the unique siblings of each of the + * matched set of elements filtered by the provided set of selectors. + */ + LazyGQuery siblings(String... selectors); + + /** + * Selects a subset of the matched elements. + */ + LazyGQuery slice(int start, int end); + + LazyGQuery submit(); + + /** + * Set the innerText of every matched element. + */ + LazyGQuery text(String txt); + + /** + * Toggle among two or more function calls every other click. + */ + LazyGQuery toggle(Function... fn); + + /** + * Adds or removes the specified classes to each matched element. + */ + LazyGQuery toggleClass(String... classes); + + /** + * Adds or removes the specified classes to each matched element. + */ + LazyGQuery toggleClass(String clz, boolean sw); + + /** + * Trigger an event of type eventbits on every matched element. + */ + LazyGQuery trigger(int eventbits, int... keys); + + /** + * Removes all events that match the eventbits. + */ + LazyGQuery unbind(int eventbits); + + /** + * Sets the value attribute of every matched element In the case of multivalue + * elements, all values are setted for other elements, only the first value is + * considered. + */ + LazyGQuery val(String... values); + + /** + * Set the width of every matched element. + */ + LazyGQuery width(int width); + + /** + * Wrap each matched element with the specified HTML content. This wrapping + * process is most useful for injecting additional structure into a document, + * without ruining the original semantic qualities of a document. This works + * by going through the first element provided (which is generated, on the + * fly, from the provided HTML) and finds the deepest descendant element + * within its structure -- it is that element that will enwrap everything + * else. + */ + LazyGQuery wrap(GQuery query); + + /** + * Wrap each matched element with the specified HTML content. This wrapping + * process is most useful for injecting additional structure into a document, + * without ruining the original semantic qualities of a document. This works + * by going through the first element provided (which is generated, on the + * fly, from the provided HTML) and finds the deepest descendant element + * within its structure -- it is that element that will enwrap everything + * else. + */ + LazyGQuery wrap(Element elem); + + /** + * Wrap each matched element with the specified HTML content. This wrapping + * process is most useful for injecting additional structure into a document, + * without ruining the original semantic qualities of a document. This works + * by going through the first element provided (which is generated, on the + * fly, from the provided HTML) and finds the deepest descendant element + * within its structure -- it is that element that will enwrap everything + * else. + */ + LazyGQuery wrap(String html); + + /** + * Wrap all the elements in the matched set into a single wrapper element. + * This is different from .wrap() where each element in the matched set would + * get wrapped with an element. This wrapping process is most useful for + * injecting additional structure into a document, without ruining the + * original semantic qualities of a document. + * + * This works by going through the first element provided (which is generated, + * on the fly, from the provided HTML) and finds the deepest descendant + * element within its structure -- it is that element that will enwrap + * everything else. + */ + LazyGQuery wrapAll(String html); + + /** + * Wrap all the elements in the matched set into a single wrapper element. + * This is different from .wrap() where each element in the matched set would + * get wrapped with an element. This wrapping process is most useful for + * injecting additional structure into a document, without ruining the + * original semantic qualities of a document. + * + * This works by going through the first element provided (which is generated, + * on the fly, from the provided HTML) and finds the deepest descendant + * element within its structure -- it is that element that will enwrap + * everything else. + */ + LazyGQuery wrapAll(Element elem); + + /** + * Wrap all the elements in the matched set into a single wrapper element. + * This is different from .wrap() where each element in the matched set would + * get wrapped with an element. This wrapping process is most useful for + * injecting additional structure into a document, without ruining the + * original semantic qualities of a document. + * + * This works by going through the first element provided (which is generated, + * on the fly, from the provided HTML) and finds the deepest descendant + * element within its structure -- it is that element that will enwrap + * everything else. + */ + LazyGQuery wrapAll(GQuery query); + + /** + * Wrap the inner child contents of each matched element (including text + * nodes) with an HTML structure. This wrapping process is most useful for + * injecting additional structure into a document, without ruining the + * original semantic qualities of a document. This works by going through the + * first element provided (which is generated, on the fly, from the provided + * HTML) and finds the deepest ancestor element within its structure -- it is + * that element that will enwrap everything else. + */ + LazyGQuery wrapInner(GQuery query); + + /** + * Wrap the inner child contents of each matched element (including text + * nodes) with an HTML structure. This wrapping process is most useful for + * injecting additional structure into a document, without ruining the + * original semantic qualities of a document. This works by going through the + * first element provided (which is generated, on the fly, from the provided + * HTML) and finds the deepest ancestor element within its structure -- it is + * that element that will enwrap everything else. + */ + LazyGQuery wrapInner(String html); + + /** + * Wrap the inner child contents of each matched element (including text + * nodes) with an HTML structure. This wrapping process is most useful for + * injecting additional structure into a document, without ruining the + * original semantic qualities of a document. This works by going through the + * first element provided (which is generated, on the fly, from the provided + * HTML) and finds the deepest ancestor element within its structure -- it is + * that element that will enwrap everything else. + */ + LazyGQuery wrapInner(Element elem); +} diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/Widgets.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/Widgets.java new file mode 100644 index 00000000..6fa411c5 --- /dev/null +++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/Widgets.java @@ -0,0 +1,75 @@ +package com.google.gwt.query.client; + +import com.google.gwt.dom.client.Element; +import com.google.gwt.dom.client.NodeList; +import com.google.gwt.user.client.ui.Button; + +/** + * Experimental Gwt Query plugin for integrating Gwt Widgets. + */ +public class Widgets extends GQuery { + /** + * Used to register the plugin. + */ + private static class WidgetsPlugin implements Plugin<Widgets> { + + public Widgets init(GQuery gq) { + return new Widgets(gq.get()); + } + } + + public static final Class<Widgets> Widgets = Widgets.class; + + static { + GQuery.registerPlugin(Widgets.class, new WidgetsPlugin()); + } + + public Widgets(Element element) { + super(element); + } + + public Widgets(JSArray elements) { + super(elements); + } + + public Widgets(NodeList list) { + super(list); + } + + /** + * Create a builder capable of instantiating a GWT Button object over + * every matched element. Call end() to execute builder and return to the + * current query object. + * @return a ButtonBuilder + */ + public ButtonBuilder button() { + return new ButtonBuilder("*"); + } + + public class ButtonBuilder { + + private String selector; + + private String label; + + private String labelSelector; + + public ButtonBuilder(String selector) { + this.selector=selector; + } + + public ButtonBuilder label(String label) { + this.labelSelector = label; + return this; + } + + public Widgets end() { + for(Element e: elements()) { + Button b = Button.wrap(e); + b.setText($(labelSelector, e).text()); + + } + return Widgets.this; + } + } +} diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/css/CSS.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/css/CSS.java new file mode 100644 index 00000000..4099fe87 --- /dev/null +++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/css/CSS.java @@ -0,0 +1,39 @@ +package com.google.gwt.query.client.css; + +/** + * Created by IntelliJ IDEA. User: ray Date: May 2, 2009 Time: 12:56:09 AM To + * change this template use File | Settings | File Templates. + */ +public class CSS { +// public static VerticalAlign VERTICAL_ALIGN; +// +// /** +// * Align the top of the aligned subtree with the top of the line box. +// */ +// public static TextAlign.TAlignValue LEFT; +// +// public static TextAlign.TAlignValue RIGHT; +// +// public static TextAlign TEXT_ALIGN; +// +// /** +// * Align the top of the aligned subtree with the top of the line box. +// */ +// public static VerticalAlign.VAlignValue TOP; +// +// public static VerticalAlign.VAlignValue BOTTOM; +// +// public static VerticalAlign.VAlignValue BASELINE; +// +// public static VerticalAlign.VAlignValue SUB; +// +// public static VerticalAlign.VAlignValue SUPER; +// +// public static VerticalAlign.VAlignValue TEXT_TOP; +// +// public static VerticalAlign.VAlignValue MIDDLE; +// +// public static VerticalAlign.VAlignValue TEXT_BOTTOM; +// +// public static VerticalAlign.VAlignValue INHERIT; +} diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/impl/SelectorEngineXPath.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/impl/SelectorEngineXPath.java index dafed4e4..b08a598b 100644 --- a/gwtquery-core/src/main/java/com/google/gwt/query/client/impl/SelectorEngineXPath.java +++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/impl/SelectorEngineXPath.java @@ -234,7 +234,7 @@ public class SelectorEngineXPath extends SelectorEngineImpl { if(!allAttr) return "";
return allAttr.replace(/(\w+)(\^|\$|\*|\||~)?=?([\w\u00C0-\uFFFF\s\-_\.]+)?/g,
function(a,b,c,d) {
- return @gwtquery.client.impl.SelectorEngineXPath::attrToXPath(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)(a,b || "",c || "",d || "");
+ return @com.google.gwt.query.client.impl.SelectorEngineXPath::attrToXPath(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)(a,b || "",c || "",d || "");
});
}-*/;
@@ -243,4 +243,4 @@ public class SelectorEngineXPath extends SelectorEngineImpl { if(!allAttr) return "";
return allAttr.replace(/\[(\w+)(\^|\$|\*|\||~)?=?([\w\u00C0-\uFFFF\s\-_\.]+)?\]/g, @com.google.gwt.query.client.impl.SelectorEngineXPath::attrToXPath(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;));
}-*/;
-}
\ No newline at end of file +}
diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/rebind/LazyGenerator.java b/gwtquery-core/src/main/java/com/google/gwt/query/rebind/LazyGenerator.java new file mode 100644 index 00000000..5ce1d828 --- /dev/null +++ b/gwtquery-core/src/main/java/com/google/gwt/query/rebind/LazyGenerator.java @@ -0,0 +1,221 @@ +/* + * Copyright 2009 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package com.google.gwt.query.rebind; + +import com.google.gwt.core.ext.Generator; +import com.google.gwt.core.ext.GeneratorContext; +import com.google.gwt.core.ext.TreeLogger; +import com.google.gwt.core.ext.UnableToCompleteException; +import com.google.gwt.core.ext.typeinfo.JClassType; +import com.google.gwt.core.ext.typeinfo.JMethod; +import com.google.gwt.core.ext.typeinfo.JParameter; +import com.google.gwt.core.ext.typeinfo.TypeOracle; +import com.google.gwt.core.ext.typeinfo.JGenericType; +import com.google.gwt.core.ext.typeinfo.JTypeParameter; +import com.google.gwt.user.rebind.ClassSourceFileComposerFactory; +import com.google.gwt.user.rebind.SourceWriter; + +import java.io.PrintWriter; + +/** + * Generate lazy implementations for interface delegated to parameterized type. + * The originating type implements Lazy<T> where T extends LazyBase<OriginType>. + * The generator generates an implementation of T where each method returns type + * T and queues up a closure which delegates execution of the method to the + * OriginType. + */ +public class LazyGenerator extends Generator { + + private JClassType lazyType = null; + + private JClassType lazyBaseType = null; + + public String generate(TreeLogger treeLogger, + GeneratorContext generatorContext, String requestedClass) + throws UnableToCompleteException { + TypeOracle oracle = generatorContext.getTypeOracle(); + lazyType = oracle.findType("com.google.gwt.query.client.Lazy"); + lazyBaseType = oracle.findType("com.google.gwt.query.client.LazyBase"); + + assert lazyType != null : "Can't find Lazy interface type"; + assert lazyBaseType != null : "Can't find LazyBase interface type"; + + JClassType requestedType = oracle.findType(requestedClass); + JClassType targetType = null; + JClassType nonLazyType = null; + + for (JClassType inf : requestedType.getImplementedInterfaces()) { + if (inf.isAssignableTo(lazyType)) { + nonLazyType = inf.isParameterized().getTypeArgs()[0]; + targetType = inf.isParameterized().getTypeArgs()[1]; + break; + } + } + + assert targetType != null : "Parameter of Lazy<T> not found"; + String genClass = targetType.getPackage().getName() + "." + + targetType.getSimpleSourceName() + getImplSuffix(); + + SourceWriter sw = getSourceWriter(treeLogger, generatorContext, + requestedType.getPackage().getName(), + targetType.getSimpleSourceName() + getImplSuffix(), + targetType.getQualifiedSourceName()); + if (sw != null) { + generatePreamble(sw, nonLazyType.getQualifiedSourceName(), treeLogger); + sw.indent(); + for (JMethod method : targetType.getMethods()) { + generateMethod(sw, method, nonLazyType.getQualifiedSourceName(), + genClass, treeLogger); + } + sw.outdent(); + generateDoneMethod(sw, nonLazyType.getQualifiedSourceName(), treeLogger); + sw.commit(treeLogger); + } + + return genClass; + } + + private void generatePreamble(SourceWriter sw, String nonLazyClass, + TreeLogger treeLogger) { + sw.indent(); + sw.println( + "private JsArray<JsClosure> closures = JsArray.createArray().cast();"); + sw.println("private " + nonLazyClass + " ctx;"); + sw.outdent(); + } + + public String getJSNIParams(JMethod method) { + + String reference = "("; + JParameter[] params = method.getParameters(); + for (int i = 0; i < params.length; i++) { + reference += params[i].getType().getJNISignature(); + } + reference += ")"; + return reference; + } + + public void generateMethod(SourceWriter sw, JMethod method, + String nonLazyClass, String genClass, TreeLogger logger) + throws UnableToCompleteException { + + JParameter[] params = method.getParameters(); + String thisClass = method.getEnclosingType().getQualifiedSourceName(); + JTypeParameter gType = method.getReturnType().isTypeParameter(); + + String retType = method.getReturnType() + .getParameterizedQualifiedSourceName(); + if (gType != null) { + retType = "<" + gType.getParameterizedQualifiedSourceName() + " extends " + + gType.getFirstBound().getQualifiedSourceName() + "> " + retType; + } + sw.print("public final native " + retType + " " + method.getName()); + sw.print("("); + int argNum = 0; + for (JParameter param : params) { + sw.print((argNum == 0 ? "" : ", ") + + param.getType().getParameterizedQualifiedSourceName() + " arg" + + argNum); + argNum++; + } + sw.println(") /*-{"); + + sw.indent(); + sw.println("var self=this;"); + sw.println("this.@" + genClass + "::closures.push("); + sw.indent(); + sw.println("function() {"); + sw.indent(); + sw.print("self.@" + genClass + "::ctx=self.@" + genClass + "::ctx.@" + + nonLazyClass + "::" + method.getName()); + sw.print(getJSNIParams(method)); + sw.print("("); + for (int i = 0; i < argNum; i++) { + sw.print("arg" + i + (i < argNum - 1 ? "," : "")); + } + + sw.println(");"); + sw.outdent(); + sw.println("}"); + sw.outdent(); + sw.println(");"); + sw.println("return this;"); + sw.outdent(); + sw.println("}-*/;"); + } + + protected String getImplSuffix() { + return "Impl"; + } + + protected SourceWriter getSourceWriter(TreeLogger logger, + GeneratorContext context, String packageName, String className, + String... interfaceNames) { + PrintWriter printWriter = context.tryCreate(logger, packageName, className); + if (printWriter == null) { + return null; + } + ClassSourceFileComposerFactory composerFactory + = new ClassSourceFileComposerFactory(packageName, className); + composerFactory.addImport("com.google.gwt.core.client.*"); + composerFactory.addImport("com.google.gwt.query.client.*"); + composerFactory.addImport("com.google.gwt.dom.client.Element"); + composerFactory.addImport("com.google.gwt.user.client.Event"); + composerFactory.addImport("com.google.gwt.query.client.Function"); + composerFactory.addImport("com.google.gwt.query.client.JsClosure"); + + for (String interfaceName : interfaceNames) { + composerFactory.addImplementedInterface(interfaceName); + } + + return composerFactory.createSourceWriter(context, printWriter); + } + + // used by benchmark harness + private void generateDoneMethod(SourceWriter sw, String nonLazyType, + TreeLogger treeLogger) { + sw.indent(); + sw.println("public Function done() {"); + sw.println("return new Function() {"); + sw.indent(); + + sw.println("public void f(Element e) {"); + sw.indent(); + sw.println("ctx = GQuery.$(e).as(" + nonLazyType + ".class);"); + sw.println("for (int i = 0; i < closures.length(); i++) {"); + sw.indent(); + sw.println("closures.get(i).invoke();"); + sw.outdent(); + sw.println("}"); + sw.outdent(); + sw.println("}"); + sw.println("public boolean f(Event e, Object data) {"); + sw.indent(); + sw.println("ctx = GQuery.$(e.getCurrentTarget());"); + sw.println("for (int i = 0; i < closures.length(); i++) {"); + sw.indent(); + sw.println("closures.get(i).invoke();"); + sw.outdent(); + sw.println("}"); + sw.outdent(); + sw.println("return true;"); + sw.println("}"); + sw.outdent(); + sw.println("};"); + sw.outdent(); + sw.println("}"); + } +} diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/rebind/SelectorGeneratorBase.java b/gwtquery-core/src/main/java/com/google/gwt/query/rebind/SelectorGeneratorBase.java index eeb90e2d..7701efc7 100644 --- a/gwtquery-core/src/main/java/com/google/gwt/query/rebind/SelectorGeneratorBase.java +++ b/gwtquery-core/src/main/java/com/google/gwt/query/rebind/SelectorGeneratorBase.java @@ -136,7 +136,7 @@ public abstract class SelectorGeneratorBase extends Generator { = new ClassSourceFileComposerFactory(packageName, className);
composerFactory.setSuperclass("com.google.gwt.query.client.SelectorEngine");
composerFactory.addImport("com.google.gwt.core.client.GWT");
- composerFactory.addImport("gwtquery.client.*");
+ composerFactory.addImport("com.google.gwt.query.client.*");
// composerFactory.addImport("com.google.gwt.query.client.JSArray");
composerFactory.addImport("com.google.gwt.dom.client.*");
diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/rebind/SelectorGeneratorJSOptimal.java b/gwtquery-core/src/main/java/com/google/gwt/query/rebind/SelectorGeneratorJSOptimal.java index 442648ac..b21bf16d 100644 --- a/gwtquery-core/src/main/java/com/google/gwt/query/rebind/SelectorGeneratorJSOptimal.java +++ b/gwtquery-core/src/main/java/com/google/gwt/query/rebind/SelectorGeneratorJSOptimal.java @@ -18,8 +18,8 @@ package com.google.gwt.query.rebind; import com.google.gwt.core.ext.TreeLogger;
import com.google.gwt.core.ext.UnableToCompleteException;
import com.google.gwt.core.ext.typeinfo.JMethod;
-import com.google.gwt.user.rebind.SourceWriter;
import com.google.gwt.query.client.Selector;
+import com.google.gwt.user.rebind.SourceWriter;
import java.util.regex.Pattern;
@@ -43,6 +43,8 @@ public class SelectorGeneratorJSOptimal extends SelectorGeneratorBase { protected static Pattern nonSpace = Pattern.compile("\\S/");
+ private static final String trimReStr = "^\\s+|\\s+$";
+
protected static Pattern trimRe = Pattern.compile(trimReStr);
protected static Pattern tplRe = Pattern.compile("\\{(\\d+)\\}");
@@ -66,7 +68,6 @@ public class SelectorGeneratorJSOptimal extends SelectorGeneratorBase { "n = byAttribute(n, \"{1}\", \"{3}\", \"{2}\", \"{0}\");"),
new RuleMatcher("^#([a-zA-Z_0-9-]+)", "n = byId(n, null, \"{0}\");")};
- private static final String trimReStr = "^\\s+|\\s+$";
protected void generateMethodBody(SourceWriter sw, JMethod method,
TreeLogger treeLogger, boolean hasContext)
@@ -190,4 +191,4 @@ public class SelectorGeneratorJSOptimal extends SelectorGeneratorBase { protected String getImplSuffix() {
return "JS" + super.getImplSuffix();
}
-}
\ No newline at end of file +}
diff --git a/gwtquery-core/src/test/java/com/google/gwt/query/client/GwtQueryCoreTest.java b/gwtquery-core/src/test/java/com/google/gwt/query/client/GwtQueryCoreTest.java index 27ea88a3..2882f6a2 100644 --- a/gwtquery-core/src/test/java/com/google/gwt/query/client/GwtQueryCoreTest.java +++ b/gwtquery-core/src/test/java/com/google/gwt/query/client/GwtQueryCoreTest.java @@ -562,17 +562,18 @@ public class GwtQueryCoreTest extends GWTTestCase { } public void testWrapMethod() { - String content = "<p>Test Paragraph.</p><div id=\"content\">Content</div>"; + String content = "<p>Test Paragraph.</p>"; + String wrapper = "<div id=\"content\">Content</div>"; String expected = "<div id=\"content\">Content<p>Test Paragraph.</p></div>"; $(e).html(content); - $("p", e).wrap($("div", e).get(0)); + $("p", e).wrap(wrapper); assertEquals(expected, $(e).html()); + $(e).html(content+wrapper); expected = "<b><p>Test Paragraph.</p></b><b><div id=\"content\">Content</div></b>"; - $(e).html(content); $("*", e).wrap("<b></b>"); assertEquals(expected, $(e).html()); } |