* Extend this class to implement functions callbacks.
*/
public abstract class Function {
+
+ /**
+ * Override this to define a cancel action.
+ */
+ public void cancel(Element e) {
+ }
/**
* Override this for GQuery methods which loop over matched elements and
--- /dev/null
+/*
+ * 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.client;
+
+import java.util.HashSet;
+
+import com.google.gwt.core.client.JavaScriptObject;
+import com.google.gwt.core.client.JsArray;
+import com.google.gwt.dom.client.Element;
+import com.google.gwt.user.client.DOM;
+
+/**
+ * A bunch of utility methods for GQuery.
+ *
+ * These methods could be moved to $ class, but the class
+ * doesn't work right now.
+ */
+public class GQUtils {
+
+ /**
+ * Returns the numeric value of a css property.
+ *
+ * The parameter force has a special meaning:
+ * - When force is false, returns the value of the css property defined
+ * in the set of style attributes.
+ * - Otherwise it returns the real computed value.
+ */
+ public static double cur(Element elem, String prop, boolean force) {
+ if (elem.getPropertyString(prop) != null
+ && (elem.getStyle() == null || elem.getStyle().getProperty(prop) == null)) {
+ return elem.getPropertyDouble(prop);
+ }
+ GQuery g = GQuery.$(elem);
+ String val = g.css(prop, force);
+ if ("thick".equalsIgnoreCase(val)) {
+ return (5);
+ } else if ("medium".equalsIgnoreCase(val)) {
+ return (3);
+ } else if ("thin".equalsIgnoreCase(val)) {
+ return (1);
+ }
+ if (!val.matches("^[\\d\\.]+.*$")) {
+ val = g.css(prop, false);
+ }
+ val = val.trim().replaceAll("[^\\d\\.\\-]+.*$", "");
+ return val.length() == 0 ? 0 : Double.parseDouble(val);
+ }
+
+ /**
+ * Compare two numbers using javascript equality.
+ */
+ public static native boolean eq(double s1, double s2) /*-{
+ return s1 == s2;
+ }-*/;
+
+ /**
+ * Compare two objects using javascript equality.
+ */
+ public static native boolean eq(Object s1, Object s2) /*-{
+ return s1 == s2;
+ }-*/;
+
+ /**
+ * Load an external javascript library.
+ * The inserted script replaces the element with the
+ * given id in the document.
+ */
+ public static void loadScript(String url, String id) {
+ GQuery gs = GQuery.$(DOM.createElement("script"));
+ GQuery gp = GQuery.$("#" + id).parent();
+ if (gp.size() != 1) {
+ gp = GQuery.$(GQuery.document.getBody());
+ }
+ GQuery.$("#" + id).remove();
+ gp.append(gs.attr("src", url).attr("type", "text/javascript").attr("id", id));
+ }
+
+ /**
+ * Check if a number is true in the javascript scope.
+ */
+ public static native boolean truth(double a) /*-{
+ return a ? true : false;
+ }-*/;
+
+ /**
+ * Check if an object is true in the javascript scope.
+ */
+ public static native boolean truth(Object a) /*-{
+ return a ? true : false;
+ }-*/;
+
+ public static JsArray<Element> unique(JsArray<Element> a) {
+ JsArray<Element> ret = JavaScriptObject.createArray().cast();
+ HashSet<Integer> f = new HashSet<Integer>();
+ for (int i = 0; i < a.length(); i++) {
+ Element e = a.get(i);
+ if (!f.contains(e.hashCode())) {
+ f.add(e.hashCode());
+ ret.push(e);
+ }
+ }
+ return ret;
+ }
+
+}
import com.google.gwt.query.client.css.TakesLength;\r
import com.google.gwt.query.client.css.TakesPercentage;\r
import com.google.gwt.query.client.impl.DocumentStyleImpl;\r
-import com.google.gwt.query.client.impl.SelectorEngineImpl;\r
import com.google.gwt.user.client.Event;\r
import com.google.gwt.user.client.Window;\r
\r
}\r
}\r
\r
- /**\r
- * Returns the numeric value of an element css property\r
- */\r
- public static double cur(Element elem, String prop) {\r
- GQuery g = $(elem);\r
- String val = g.css(prop, true);\r
- if ("height".equalsIgnoreCase(prop)) {\r
- return elem.getClientHeight() - cur(elem, "paddingTop") - cur(elem, "paddingBottom");\r
- }\r
- if ("width".equalsIgnoreCase(prop)) {\r
- return elem.getClientWidth() - cur(elem, "paddingLeft") - cur(elem, "paddingRight");\r
- }\r
- if ("absolute".equalsIgnoreCase(g.css("position", true))) {\r
- if ("left".equalsIgnoreCase(prop)) {\r
- return g.offset().left;\r
- }\r
- if ("top".equalsIgnoreCase(prop)) {\r
- return g.offset().top;\r
- }\r
- }\r
- if ("opacity".equalsIgnoreCase(prop)) {\r
- return Double.parseDouble(val);\r
- }\r
- if (elem.getPropertyString(prop) != null\r
- && (elem.getStyle() == null || elem.getStyle().getProperty(prop) == null)) {\r
- return elem.getPropertyDouble(prop);\r
- }\r
- \r
- if ("thick".equalsIgnoreCase(val)) {\r
- return (5);\r
- } else if ("medium".equalsIgnoreCase(val)) {\r
- return (3);\r
- } else if ("thin".equalsIgnoreCase(val)) {\r
- return (1);\r
- }\r
- val = "0" + val.replaceAll("[^\\d\\.]+", "");\r
- return Double.parseDouble(val);\r
- }\r
- \r
/**\r
* Return a lazy version of the GQuery interface. Lazy function calls are\r
* simply queued up and not executed immediately.\r
postWrap = "</colgroup></table>";\r
}\r
// TODO: fix IE link tag serialization\r
+ // TODO: fix IE <script> tag\r
Element div = document.createDivElement();\r
div.setInnerHTML(preWrap + elem + postWrap);\r
Node n = div;\r
e.removeChild(e.getFirstChild());\r
}\r
}\r
-\r
return this;\r
}\r
\r
public GQuery parent() {\r
JSArray result = JSArray.create();\r
for (Element e : elements()) {\r
- result.addNode(e.getParentElement());\r
+ Element p = e.getParentElement();\r
+ if (p != null) {\r
+ result.addNode(p);\r
+ }\r
}\r
return new GQuery(unique(result));\r
}\r
* only works on arrays of DOM elements, not strings or numbers.\r
*/\r
public JSArray unique(JSArray result) {\r
- return SelectorEngineImpl.unique(result.<JsArray<Element>>cast()).cast();\r
+ return GQUtils.unique(result.<JsArray<Element>>cast()).cast();\r
}\r
\r
/**\r
} else {\r
dataCache.delete(id);\r
}\r
- } \r
+ }\r
+ \r
}\r
package com.google.gwt.query.client;\r
\r
import com.google.gwt.core.client.GWT;\r
-import com.google.gwt.core.client.JavaScriptObject;\r
import com.google.gwt.dom.client.Document;\r
import com.google.gwt.dom.client.Element;\r
import com.google.gwt.dom.client.Node;\r
*/\r
public class SelectorEngine {\r
\r
- public static native boolean eq(String s1, String s2) /*-{\r
- return s1 == s2;\r
- }-*/;\r
-\r
public static native NodeList<Element> getElementsByClassName(String clazz,\r
Node ctx) /*-{\r
return ctx.getElementsByClassName(clazz);\r
return ctx.querySelectorAll(selector);\r
}-*/;\r
\r
- public static boolean truth(String a) {\r
- return GWT.isScript() ? truth0(a) : a != null && !"".equals(a);\r
- }\r
-\r
- public static boolean truth(JavaScriptObject a) {\r
- return GWT.isScript() ? truth0(a) : a != null;\r
- }\r
-\r
public static NodeList<Element> xpathEvaluate(String selector, Node ctx) {\r
return xpathEvaluate(selector, ctx, JSArray.create());\r
}\r
return r;\r
}-*/;\r
\r
- private static native boolean truth0(String a) /*-{\r
- return a;\r
- }-*/;\r
-\r
- private static native boolean truth0(JavaScriptObject a) /*-{\r
- return a;\r
- }-*/;\r
-\r
private SelectorEngineImpl impl;\r
\r
public SelectorEngine() {\r
package com.google.gwt.query.client.impl;
import com.google.gwt.dom.client.Element;
+import com.google.gwt.query.client.GQUtils;
/**
* A helper class to get computed CSS styles for elements.
*/
public String curCSS(Element elem, String name, boolean force) {
name = fixPropertyName(name);
- if (force) {
- return getComputedStyle(elem, hyphenize(name), name, null);
- } else {
+ if ("height".equalsIgnoreCase(name)) {
+ return String.valueOf(getHeight(elem));
+ }
+ if ("width".equalsIgnoreCase(name)) {
+ return String.valueOf(getWidth(elem));
+ }
+ if ("opacity".equalsIgnoreCase(name)) {
+ return String.valueOf(getOpacity(elem));
+ }
+ if (!force && elem.getStyle().getProperty(name) != null) {
String ret = elem.getStyle().getProperty(name);
return ret == null ? "" : ret;
+ } else {
+ return getComputedStyle(elem, hyphenize(name), name, null);
}
}
-
+
/**
* Fix style property names.
*/
return camelize(name);
}
+ public int getHeight(Element e) {
+ return (int) (e.getClientHeight() - num(curCSS(e, "paddingTop", true)) - num(curCSS(e, "paddingBottom", true)));
+ }
+
+ public double getOpacity(Element e) {
+ String o = e.getStyle().getOpacity();
+ return GQUtils.truth(o) ? num(o) : 1;
+ }
+
+ public int getWidth(Element e) {
+ return (int) (e.getClientWidth() - num(curCSS(e, "paddingLeft", true)) - num(curCSS(e, "paddingRight", true)));
+ }
+
+ /**
+ * Return whether the element is visible
+ */
+ public boolean isVisible(Element e) {
+ return !"none".equalsIgnoreCase(curCSS(e, "display", true));
+ }
+
+ public double num(String val) {
+ val = val.trim().replaceAll("[^\\d\\.\\-]+.*$", "");
+ return GQUtils.truth(val) ? Double.parseDouble(val) : 0;
+ }
+
/**
* Remove a style property from an element.
*/
public void removeStyleProperty(Element elem, String prop) {
elem.getStyle().setProperty(prop, "");
}
-
+
/**
* Set the value of a style property of an element.
*/
e.getStyle().setProperty(prop, val);
}
}
-
- /**
- * Return whether the element is visible
- */
- public boolean isVisible(Element e) {
- return !"none".equalsIgnoreCase(curCSS(e, "display", true));
- }
protected native String getComputedStyle(Element elem, String hyphenName,
String camelName, String pseudo) /*-{
var cStyle = $doc.defaultView.getComputedStyle(elem, pseudo);
return cStyle ? cStyle.getPropertyValue(hyphenName) : null;
}-*/;
-
+
}
* A helper class to get computed CSS styles for elements on IE6.
*/
public class DocumentStyleImplIE extends DocumentStyleImpl {
-
- /**
- * Return the string value of a css property of an element.
- * IE needs a special workaround to handle opacity.
- *
- * The parameter force has a special meaning here:
- * - When force is false, returns the value of the css property defined
- * in the style attribute of the element.
- * - Otherwise it returns the real computed value.
- *
- * For instance if you define 'display=none' not in the element style
- * but in the css stylesheet, it returns an empty string unless you
- * pass the parameter force=true.
- */
- @Override
- public String curCSS(Element elem, String name, boolean force) {
- if ("opacity".equalsIgnoreCase(name)) {
- Style s = elem.getStyle();
- String o = s.getProperty("filter");
- if (o != null) {
- return !o.matches(".*opacity=.*") ? "1" : ("" +
- (Double.valueOf(o.replaceAll("[^\\d]", "")) / 100));
- }
- o = s.getProperty("opacity");
- return o == null || o.length() == 0 ? "1" : o;
- }
- return super.curCSS(elem, name, force);
- }
-
+
/**
* Fix style property names.
*/
return name;
}
+ @Override
+ public int getHeight(Element e) {
+ return (int) (e.getOffsetHeight() - num(curCSS(e, "paddingTop", true)) - num(curCSS(e, "paddingBottom", true))
+ - num(curCSS(e, "borderTopWidth", true)) - num(curCSS(e, "borderBottomWidth", true)));
+ }
+
+ @Override
+ public double getOpacity(Element e) {
+ Style s = e.getStyle();
+ String o = s.getProperty("filter");
+ if (o != null) {
+ return !o.matches(".*opacity=.*") ? 1 : Double.valueOf(o.replaceAll("[^\\d]", "")) / 100;
+ }
+ return super.getOpacity(e);
+ }
+
+ @Override
+ public int getWidth(Element e) {
+ return (int) (e.getOffsetWidth() - num(curCSS(e, "paddingLeft", true)) - num(curCSS(e, "paddingRight", true))
+ - num(curCSS(e, "borderRightWidth", true)) - num(curCSS(e, "borderRightWidth", true)));
+ }
+
/**
* Remove a style property from an element.
*/
public native void removeStyleProperty(Element elem, String prop) /*-{
elem.style.removeAttribute(prop);
}-*/;
+
/**
* Set the value of a style property of an element.
@Override
public void setStyleProperty(Element e, String prop, String val) {
if ("opacity".equals(prop)) {
- e.getStyle().setProperty("zoom", "1");
- e.getStyle().setProperty("filter",
- "alpha(opacity=" + (int) (Double.valueOf(val) * 100) + ")");
+ setOpacity(e, val);
} else {
super.setStyleProperty(e, prop, val);
}
}
-
+
@Override
protected native String getComputedStyle(Element elem, String hyphenName,
String camelName, String pseudo) /*-{
// code lifted from jQuery
var style = elem.style;
var ret = elem.currentStyle[hyphenName] || elem.currentStyle[camelName];
- // From the awesome hack by Dean Edwards
- // http://erik.eae.net/archives/2007/07/27/18.54.15/#comment-102291
- // If we're not dealing with a regular pixel number
- // but a number that has a weird ending, we need to convert it to pixels
if ( !/^\d+(px)?$/i.test( ret ) && /^\d/.test( ret ) ) {
- // Remember the original values
- var left = style.left, rsLeft = elem.runtimeStyle.left;
- // Put in the new values to get a computed value out
- elem.runtimeStyle.left = elem.currentStyle.left;
- style.left = ret || 0;
- ret = style.pixelLeft + "px";
- // Revert the changed values
- style.left = left;
- elem.runtimeStyle.left = rsLeft;
+ // Remember the original values
+ var left = style.left, rsLeft = elem.runtimeStyle.left;
+ // Put in the new values to get a computed value out
+ elem.runtimeStyle.left = elem.currentStyle.left;
+ style.left = ret || 0;
+ ret = style.pixelLeft + "px";
+ // Revert the changed values
+ style.left = left;
+ elem.runtimeStyle.left = rsLeft;
}
return ret;
}-*/;
+
+ private void setOpacity(Element e, String val) {
+ if (val == null || val.trim().length() == 0) {
+ val = "1";
+ }
+ e.getStyle().setProperty("zoom", "1");
+ e.getStyle().setProperty("filter",
+ "alpha(opacity=" + (int) (Double.valueOf(val) * 100) + ")");
+ }
}
\ No newline at end of file
import com.google.gwt.dom.client.Element;\r
import com.google.gwt.dom.client.Node;\r
import com.google.gwt.dom.client.NodeList;\r
+import com.google.gwt.query.client.GQUtils;\r
import com.google.gwt.query.client.JSArray;\r
import com.google.gwt.query.client.Regexp;\r
import com.google.gwt.query.client.SelectorEngine;\r
sel = css2Xpath(sel);\r
}\r
SelectorEngine.xpathEvaluate(sel, ctx, elm);\r
- return unique(elm.<JsArray<Element>> cast()).cast();\r
+ return GQUtils.unique(elm.<JsArray<Element>> cast()).cast();\r
}\r
\r
}\r
* the License.\r
*/\r
package com.google.gwt.query.client.impl;\r
+import static com.google.gwt.query.client.GQUtils.eq;\r
+import static com.google.gwt.query.client.GQUtils.truth;\r
\r
-import java.util.HashSet;\r
\r
-import com.google.gwt.core.client.JavaScriptObject;\r
-import com.google.gwt.core.client.JsArray;\r
import com.google.gwt.dom.client.Element;\r
import com.google.gwt.dom.client.Node;\r
import com.google.gwt.dom.client.NodeList;\r
+import com.google.gwt.query.client.GQUtils;\r
import com.google.gwt.query.client.JSArray;\r
import com.google.gwt.query.client.Regexp;\r
-import com.google.gwt.query.client.SelectorEngine;\r
\r
/**\r
* Base/Utility class for runtime selector engine implementations.\r
Regexp expressionRegExp = new Regexp(\r
"^((odd|even)|([1-9]\\d*)|((([1-9]\\d*)?)n((\\+|\\-)(\\d+))?)|(\\-(([1-9]\\d*)?)n\\+(\\d+)))$");\r
JSArray pseudoValue = expressionRegExp.exec(expression);\r
- if (!SelectorEngine.truth(pseudoValue)) {\r
+ if (!truth(pseudoValue)) {\r
return null;\r
} else {\r
- if (SelectorEngine.truth(pseudoValue.getStr(2))) { // odd or even\r
- start = (SelectorEngine.eq(pseudoValue.getStr(2), "odd")) ? 1 : 2;\r
+ if (truth(pseudoValue.getStr(2))) { // odd or even\r
+ start = (eq(pseudoValue.getStr(2), "odd")) ? 1 : 2;\r
modVal = (start == 1) ? 1 : 0;\r
- } else if (SelectorEngine\r
+ } else if (GQUtils\r
.truth(pseudoValue.getStr(3))) { // single digit\r
start = Integer.parseInt(pseudoValue.getStr(3), 10);\r
add = 0;\r
max = start;\r
- } else if (SelectorEngine.truth(pseudoValue.getStr(4))) { // an+b\r
- add = SelectorEngine.truth(pseudoValue.getStr(6)) ? Integer\r
+ } else if (truth(pseudoValue.getStr(4))) { // an+b\r
+ add = truth(pseudoValue.getStr(6)) ? Integer\r
.parseInt(pseudoValue.getStr(6), 10) : 1;\r
- start = SelectorEngine.truth(pseudoValue.getStr(7)) ? Integer.parseInt(\r
+ start = truth(pseudoValue.getStr(7)) ? Integer.parseInt(\r
(pseudoValue.getStr(8).charAt(0) == '+' ? ""\r
: pseudoValue.getStr(8)) + pseudoValue.getStr(9), 10) : 0;\r
while (start < 1) {\r
}\r
modVal = (start > add) ? (start - add) % add\r
: ((start == add) ? 0 : start);\r
- } else if (SelectorEngine.truth(pseudoValue.getStr(10))) { // -an+b\r
- add = SelectorEngine.truth(pseudoValue.getStr(12)) ? Integer\r
+ } else if (truth(pseudoValue.getStr(10))) { // -an+b\r
+ add = truth(pseudoValue.getStr(12)) ? Integer\r
.parseInt(pseudoValue.getStr(12), 10) : 1;\r
start = max = Integer.parseInt(pseudoValue.getStr(13), 10);\r
while (start > add) {\r
* @return a list of matched nodes\r
*/\r
public abstract NodeList<Element> select(String selector, Node ctx);\r
-\r
- public static JsArray<Element> unique(JsArray<Element> a) {\r
- JsArray<Element> ret = JavaScriptObject.createArray().cast();\r
- HashSet<Integer> f = new HashSet<Integer>();\r
- for (int i = 0; i < a.length(); i++) {\r
- Element e = a.get(i);\r
- if (!f.contains(e.hashCode())) {\r
- f.add(e.hashCode());\r
- ret.push(e);\r
- }\r
- } \r
- return ret;\r
- }\r
}\r
import com.google.gwt.dom.client.Element;\r
import com.google.gwt.dom.client.Node;\r
import com.google.gwt.dom.client.NodeList;\r
+import com.google.gwt.query.client.GQUtils;\r
import com.google.gwt.query.client.JSArray;\r
import com.google.gwt.query.client.Regexp;\r
import com.google.gwt.query.client.SelectorEngine;\r
}-*/;\r
\r
private static String attrToRegExp(String attrVal, String op) {\r
- if (SelectorEngine.eq("^", op)) {\r
+ if (GQUtils.eq("^", op)) {\r
return "^" + attrVal;\r
}\r
- if (SelectorEngine.eq("$", op)) {\r
+ if (GQUtils.eq("$", op)) {\r
return attrVal + "$";\r
}\r
- if (SelectorEngine.eq("*", op)) {\r
+ if (GQUtils.eq("*", op)) {\r
return attrVal;\r
}\r
- if (SelectorEngine.eq("|", op)) {\r
+ if (GQUtils.eq("|", op)) {\r
return "(^" + attrVal + "(\\-\\w+)*$)";\r
}\r
- if (SelectorEngine.eq("~", op)) {\r
+ if (GQUtils.eq("~", op)) {\r
return "\\b" + attrVal + "\\b";\r
}\r
- return SelectorEngine.truth(attrVal) ? "^" + attrVal + "$" : null;\r
+ return GQUtils.truth(attrVal) ? "^" + attrVal + "$" : null;\r
}\r
\r
private static native boolean checked(Node previous) /*-{\r
private static void getGeneralSiblingNodes(JSArray matchingElms,\r
JSArray nextTag, Regexp nextRegExp, Node prevRef) {\r
while (\r
- SelectorEngine.truth((prevRef = SelectorEngine.getNextSibling(prevRef)))\r
+ GQUtils.truth((prevRef = SelectorEngine.getNextSibling(prevRef)))\r
&& !isAdded(prevRef)) {\r
- if (!SelectorEngine.truth(nextTag) || nextRegExp\r
+ if (!GQUtils.truth(nextTag) || nextRegExp\r
.test(prevRef.getNodeName())) {\r
setAdded(prevRef, true);\r
matchingElms.addNode(prevRef);\r
private static void getSiblingNodes(JSArray matchingElms, JSArray nextTag,\r
Regexp nextRegExp, Node prevRef) {\r
while (\r
- SelectorEngine.truth(prevRef = SelectorEngine.getNextSibling(prevRef))\r
+ GQUtils.truth(prevRef = SelectorEngine.getNextSibling(prevRef))\r
&& prevRef.getNodeType() != Node.ELEMENT_NODE) {\r
}\r
- if (SelectorEngine.truth(prevRef)) {\r
- if (!SelectorEngine.truth(nextTag) || nextRegExp\r
+ if (GQUtils.truth(prevRef)) {\r
+ if (!GQUtils.truth(nextTag) || nextRegExp\r
.test(prevRef.getNodeName())) {\r
matchingElms.addNode(prevRef);\r
}\r
if (a > 0) {\r
identical = false;\r
for (int b = 0, bl = a; b < bl; b++) {\r
- if (SelectorEngine.eq(selectors[a], selectors[b])) {\r
+ if (GQUtils.eq(selectors[a], selectors[b])) {\r
identical = true;\r
break;\r
}\r
String rule = cssSelectors.getStr(i);\r
if (i > 0 && childOrSiblingRefRegExp.test(rule)) {\r
JSArray childOrSiblingRef = childOrSiblingRefRegExp.exec(rule);\r
- if (SelectorEngine.truth(childOrSiblingRef)) {\r
+ if (GQUtils.truth(childOrSiblingRef)) {\r
JSArray nextTag = new Regexp("^\\w+")\r
.exec(cssSelectors.getStr(i + 1));\r
Regexp nextRegExp = null;\r
String nextTagStr = null;\r
- if (SelectorEngine.truth(nextTag)) {\r
+ if (GQUtils.truth(nextTag)) {\r
nextTagStr = nextTag.getStr(0);\r
nextRegExp = new Regexp("(^|\\s)" + nextTagStr + "(\\s|$)", "i");\r
}\r
for (int j = 0, jlen = prevElem.size(); j < jlen; j++) {\r
Node prevRef = prevElem.getNode(j);\r
String ref = childOrSiblingRef.getStr(0);\r
- if (SelectorEngine.eq(">", ref)) {\r
+ if (GQUtils.eq(">", ref)) {\r
getDescendantNodes(matchingElms, nextTagStr, prevRef);\r
- } else if (SelectorEngine.eq("+", ref)) {\r
+ } else if (GQUtils.eq("+", ref)) {\r
getSiblingNodes(matchingElms, nextTag, nextRegExp, prevRef);\r
- } else if (SelectorEngine.eq("~", ref)) {\r
+ } else if (GQUtils.eq("~", ref)) {\r
getGeneralSiblingNodes(matchingElms, nextTag, nextRegExp,\r
prevRef);\r
}\r
}\r
JSArray cssSelector = cssSelectorRegExp.exec(rule);\r
SplitRule splitRule = new SplitRule(\r
- !SelectorEngine.truth(cssSelector.getStr(1)) || SelectorEngine\r
+ !GQUtils.truth(cssSelector.getStr(1)) || GQUtils\r
.eq(cssSelector.getStr(3), "*") ? "*" : cssSelector.getStr(1),\r
- !SelectorEngine.eq(cssSelector.getStr(3), "*") ? cssSelector\r
+ !GQUtils.eq(cssSelector.getStr(3), "*") ? cssSelector\r
.getStr(2) : null, cssSelector.getStr(4), cssSelector.getStr(6),\r
cssSelector.getStr(10));\r
- if (SelectorEngine.truth(splitRule.id)) {\r
+ if (GQUtils.truth(splitRule.id)) {\r
Element domelem = Document.get()\r
.getElementById(splitRule.id.substring(1));\r
- if (SelectorEngine.truth(domelem)) {\r
+ if (GQUtils.truth(domelem)) {\r
matchingElms = JSArray.create(domelem);\r
}\r
prevElem = matchingElms;\r
- } else if (SelectorEngine.truth(splitRule.tag) && !isSkipped(\r
+ } else if (GQUtils.truth(splitRule.tag) && !isSkipped(\r
prevElem)) {\r
if (i == 0 && matchingElms.size() == 0 && prevElem.size() == 1) {\r
prevElem = matchingElms = JSArray.create(\r
break;\r
}\r
setSkipTag(prevElem, false);\r
- if (SelectorEngine.truth(splitRule.allClasses)) {\r
+ if (GQUtils.truth(splitRule.allClasses)) {\r
String[] allClasses = splitRule.allClasses.replaceFirst("^\\.", "")\r
.split("\\.");\r
Regexp[] regExpClassNames = new Regexp[allClasses.length];\r
Element current = prevElem.getElement(o);\r
String elmClass = current.getClassName();\r
boolean addElm = false;\r
- if (SelectorEngine.truth(elmClass) && !isAdded(current)) {\r
+ if (GQUtils.truth(elmClass) && !isAdded(current)) {\r
for (int p = 0, pl = regExpClassNames.length; p < pl; p++) {\r
addElm = regExpClassNames[p].test(elmClass);\r
if (!addElm) {\r
clearAdded(prevElem);\r
prevElem = matchingElms = matchingClassElms;\r
}\r
- if (SelectorEngine.truth(splitRule.allAttr)) {\r
+ if (GQUtils.truth(splitRule.allAttr)) {\r
JSArray allAttr = Regexp\r
.match("\\[[^\\]]+\\]", "g", splitRule.allAttr);\r
Regexp[] regExpAttributes = new Regexp[allAttr.size()];\r
JSArray attributeMatch = attributeMatchRegExp\r
.exec(allAttr.getStr(q));\r
String attributeValue =\r
- SelectorEngine.truth(attributeMatch.getStr(3))\r
+ GQUtils.truth(attributeMatch.getStr(3))\r
? attributeMatch.getStr(3).replaceAll("\\.", "\\.")\r
: null;\r
String attrVal = attrToRegExp(attributeValue,\r
(SelectorEngine.or(attributeMatch.getStr(2), null)));\r
- regExpAttributes[q] = (SelectorEngine.truth(attrVal) ? new Regexp(\r
+ regExpAttributes[q] = (GQUtils.truth(attrVal) ? new Regexp(\r
attrVal) : null);\r
regExpAttributesStr[q] = attributeMatch.getStr(1);\r
}\r
addElm = false;\r
Regexp attributeRegexp = regExpAttributes[s];\r
String currentAttr = getAttr(current, regExpAttributesStr[s]);\r
- if (SelectorEngine.truth(currentAttr)\r
+ if (GQUtils.truth(currentAttr)\r
&& currentAttr.length() != 0) {\r
if (attributeRegexp == null || attributeRegexp\r
.test(currentAttr)) {\r
}\r
prevElem = matchingElms = matchingAttributeElms;\r
}\r
- if (SelectorEngine.truth(splitRule.allPseudos)) {\r
+ if (GQUtils.truth(splitRule.allPseudos)) {\r
Regexp pseudoSplitRegExp = new Regexp(\r
":(\\w[\\w\\-]*)(\\(([^\\)]+)\\))?");\r
\r
splitRule.allPseudos);\r
for (int t = 0, tl = allPseudos.size(); t < tl; t++) {\r
JSArray pseudo = pseudoSplitRegExp.match(allPseudos.getStr(t));\r
- String pseudoClass = SelectorEngine.truth(pseudo.getStr(1))\r
+ String pseudoClass = GQUtils.truth(pseudo.getStr(1))\r
? pseudo.getStr(1).toLowerCase() : null;\r
- String pseudoValue = SelectorEngine.truth(pseudo.getStr(3))\r
+ String pseudoValue = GQUtils.truth(pseudo.getStr(3))\r
? pseudo.getStr(3) : null;\r
matchingElms = getElementsByPseudo(matchingElms, pseudoClass,\r
pseudoValue);\r
elm.pushAll(prevElem);\r
}\r
\r
- return unique(elm.<JsArray<Element>>cast()).cast();\r
+ return GQUtils.unique(elm.<JsArray<Element>>cast()).cast();\r
}\r
\r
protected String getAttr(Element current, String name) {\r
Node previous;\r
for (int w = 0, wlen = previousMatch.size(); w < wlen; w++) {\r
previous = previousMatch.getElement(w);\r
- if (SelectorEngine\r
+ if (GQUtils\r
.eq(((Element) previous).getAttribute(pseudoClass), pseudoValue)) {\r
matchingElms.addNode(previous);\r
}\r
JSArray prevParents = JSArray.create();\r
boolean previousDir = pseudoClass.startsWith("first") ? true : false;\r
JSArray matchingElms = JSArray.create();\r
- if (SelectorEngine.eq("first-child", pseudoClass) || SelectorEngine\r
+ if (GQUtils.eq("first-child", pseudoClass) || GQUtils\r
.eq("last-child", pseudoClass)) {\r
getFirstChildPseudo(previousMatch, previousDir, matchingElms);\r
- } else if (SelectorEngine.eq("only-child", pseudoClass)) {\r
+ } else if (GQUtils.eq("only-child", pseudoClass)) {\r
getOnlyChildPseudo(previousMatch, matchingElms);\r
- } else if (SelectorEngine.eq("nth-child", pseudoClass)) {\r
+ } else if (GQUtils.eq("nth-child", pseudoClass)) {\r
matchingElms = getNthChildPseudo(previousMatch, pseudoValue, prevParents,\r
matchingElms);\r
- } else if (SelectorEngine.eq("first-of-type", pseudoClass) || SelectorEngine\r
+ } else if (GQUtils.eq("first-of-type", pseudoClass) || GQUtils\r
.eq("last-of-type", pseudoClass)) {\r
getFirstOfTypePseudo(previousMatch, previousDir, matchingElms);\r
- } else if (SelectorEngine.eq("only-of-type", pseudoClass)) {\r
+ } else if (GQUtils.eq("only-of-type", pseudoClass)) {\r
getOnlyOfTypePseudo(previousMatch, matchingElms);\r
- } else if (SelectorEngine.eq("nth-of-type", pseudoClass)) {\r
+ } else if (GQUtils.eq("nth-of-type", pseudoClass)) {\r
matchingElms = getNthOfTypePseudo(previousMatch, pseudoValue, prevParents,\r
matchingElms);\r
- } else if (SelectorEngine.eq("empty", pseudoClass)) {\r
+ } else if (GQUtils.eq("empty", pseudoClass)) {\r
getEmptyPseudo(previousMatch, matchingElms);\r
- } else if (SelectorEngine.eq("enabled", pseudoClass)) {\r
+ } else if (GQUtils.eq("enabled", pseudoClass)) {\r
getEnabledPseudo(previousMatch, matchingElms);\r
- } else if (SelectorEngine.eq("disabled", pseudoClass)) {\r
+ } else if (GQUtils.eq("disabled", pseudoClass)) {\r
getDisabledPseudo(previousMatch, matchingElms);\r
- } else if (SelectorEngine.eq("checked", pseudoClass)) {\r
+ } else if (GQUtils.eq("checked", pseudoClass)) {\r
getCheckedPseudo(previousMatch, matchingElms);\r
- } else if (SelectorEngine.eq("contains", pseudoClass)) {\r
+ } else if (GQUtils.eq("contains", pseudoClass)) {\r
getContainsPseudo(previousMatch, pseudoValue, matchingElms);\r
- } else if (SelectorEngine.eq("not", pseudoClass)) {\r
+ } else if (GQUtils.eq("not", pseudoClass)) {\r
matchingElms = getNotPseudo(previousMatch, pseudoValue, matchingElms);\r
} else {\r
getDefaultPseudo(previousMatch, pseudoClass, pseudoValue, matchingElms);\r
for (int j = 0, jlen = previousMatch.size(); j < jlen; j++) {\r
previous = next = previousMatch.getElement(j);\r
if (previousDir) {\r
- while (SelectorEngine\r
+ while (GQUtils\r
.truth((next = SelectorEngine.getPreviousSibling(next)))\r
&& next.getNodeType() != Node.ELEMENT_NODE) {\r
}\r
} else {\r
while (\r
- SelectorEngine.truth((next = SelectorEngine.getNextSibling(next)))\r
+ GQUtils.truth((next = SelectorEngine.getNextSibling(next)))\r
&& next.getNodeType() != Node.ELEMENT_NODE) {\r
}\r
}\r
- if (!SelectorEngine.truth(next)) {\r
+ if (!GQUtils.truth(next)) {\r
matchingElms.addNode(previous);\r
}\r
}\r
\r
if (previousDir) {\r
while (\r
- SelectorEngine.truth(next = SelectorEngine.getPreviousSibling(next))\r
- && !SelectorEngine\r
+ GQUtils.truth(next = SelectorEngine.getPreviousSibling(next))\r
+ && !GQUtils\r
.eq(next.getNodeName(), previous.getNodeName())) {\r
}\r
} else {\r
- while (SelectorEngine.truth(next = SelectorEngine.getNextSibling(next))\r
- && !SelectorEngine.eq(next.getNodeName(), previous.getNodeName())) {\r
+ while (GQUtils.truth(next = SelectorEngine.getNextSibling(next))\r
+ && !GQUtils.eq(next.getNodeName(), previous.getNodeName())) {\r
}\r
}\r
\r
- if (!SelectorEngine.truth(next)) {\r
+ if (!GQUtils.truth(next)) {\r
matchingElms.addNode(previous);\r
}\r
}\r
"\\[(\\w+)(\\^|\\$|\\*|\\||~)?=?([\\w\\u00C0-\\uFFFF\\s\\-_\\.]+)?\\]")\r
.exec(pseudoValue);\r
Regexp notRegExp = new Regexp("(^|\\s)"\r
- + (SelectorEngine.truth(notTag) ? notTag.getStr(1)\r
- : SelectorEngine.truth(notClass) ? notClass.getStr(1) : "")\r
+ + (GQUtils.truth(notTag) ? notTag.getStr(1)\r
+ : GQUtils.truth(notClass) ? notClass.getStr(1) : "")\r
+ "(\\s|$)", "i");\r
- if (SelectorEngine.truth(notAttr)) {\r
- String notAttribute = SelectorEngine.truth(notAttr.getStr(3)) ? notAttr\r
+ if (GQUtils.truth(notAttr)) {\r
+ String notAttribute = GQUtils.truth(notAttr.getStr(3)) ? notAttr\r
.getStr(3).replace("\\.", "\\.") : null;\r
String notMatchingAttrVal = attrToRegExp(notAttribute,\r
notAttr.getStr(2));\r
for (int v = 0, vlen = previousMatch.size(); v < vlen; v++) {\r
Element notElm = previousMatch.getElement(v);\r
Element addElm = null;\r
- if (SelectorEngine.truth(notTag) && !notRegExp\r
+ if (GQUtils.truth(notTag) && !notRegExp\r
.test(notElm.getNodeName())) {\r
addElm = notElm;\r
- } else if (SelectorEngine.truth(notClass) && !notRegExp\r
+ } else if (GQUtils.truth(notClass) && !notRegExp\r
.test(notElm.getClassName())) {\r
addElm = notElm;\r
- } else if (SelectorEngine.truth(notAttr)) {\r
+ } else if (GQUtils.truth(notAttr)) {\r
String att = getAttr(notElm, notAttr.getStr(1));\r
- if (!SelectorEngine.truth(att) || !notRegExp.test(att)) {\r
+ if (!GQUtils.truth(att) || !notRegExp.test(att)) {\r
addElm = notElm;\r
}\r
}\r
- if (SelectorEngine.truth(addElm) && !isAdded(addElm)) {\r
+ if (GQUtils.truth(addElm) && !isAdded(addElm)) {\r
setAdded(addElm, true);\r
matchingElms.addNode(addElm);\r
}\r
private JSArray getNthChildPseudo(JSArray previousMatch, String pseudoValue,\r
JSArray prevParents, JSArray matchingElms) {\r
Node previous;\r
- if (SelectorEngine.eq(pseudoValue, "n")) {\r
+ if (GQUtils.eq(pseudoValue, "n")) {\r
matchingElms = previousMatch;\r
} else {\r
Sequence sequence = getSequence(pseudoValue);\r
|| iteratorNext <= sequence.max)) {\r
if (childElm.getNodeType() == Node.ELEMENT_NODE) {\r
if (++childCount == iteratorNext) {\r
- if (SelectorEngine\r
+ if (GQUtils\r
.eq(childElm.getNodeName(), previous.getNodeName())) {\r
matchingElms.addNode(childElm);\r
}\r
int iteratorNext = sequence.start;\r
int childCount = 0;\r
Node childElm = prevParent.getFirstChild();\r
- while (SelectorEngine.truth(childElm) && (sequence.max < 0\r
+ while (GQUtils.truth(childElm) && (sequence.max < 0\r
|| iteratorNext <= sequence.max)) {\r
- if (SelectorEngine\r
+ if (GQUtils\r
.eq(childElm.getNodeName(), previous.getNodeName())) {\r
if (++childCount == iteratorNext) {\r
matchingElms.addNode(childElm);\r
Node prevParent = previous.getParentNode();\r
if (prevParent != kParent) {\r
while (\r
- SelectorEngine.truth(prev = SelectorEngine.getPreviousSibling(prev))\r
+ GQUtils.truth(prev = SelectorEngine.getPreviousSibling(prev))\r
&& prev.getNodeType() != Node.ELEMENT_NODE) {\r
}\r
- while (SelectorEngine.truth(next = SelectorEngine.getNextSibling(next))\r
+ while (GQUtils.truth(next = SelectorEngine.getNextSibling(next))\r
&& next.getNodeType() != Node.ELEMENT_NODE) {\r
}\r
- if (!SelectorEngine.truth(prev) && !SelectorEngine.truth(next)) {\r
+ if (!GQUtils.truth(prev) && !GQUtils.truth(next)) {\r
matchingElms.addNode(previous);\r
}\r
kParent = prevParent;\r
Node prevParent = previous.getParentNode();\r
if (prevParent != oParent) {\r
while (\r
- SelectorEngine.truth(prev = SelectorEngine.getPreviousSibling(prev))\r
- && !SelectorEngine\r
+ GQUtils.truth(prev = SelectorEngine.getPreviousSibling(prev))\r
+ && !GQUtils\r
.eq(prev.getNodeName(), previous.getNodeName())) {\r
}\r
- while (SelectorEngine.truth(next = SelectorEngine.getNextSibling(next))\r
- && !SelectorEngine.eq(next.getNodeName(), previous.getNodeName())) {\r
+ while (GQUtils.truth(next = SelectorEngine.getNextSibling(next))\r
+ && !GQUtils.eq(next.getNodeName(), previous.getNodeName())) {\r
}\r
- if (!SelectorEngine.truth(prev) && !SelectorEngine.truth(next)) {\r
+ if (!GQUtils.truth(prev) && !GQUtils.truth(next)) {\r
matchingElms.addNode(previous);\r
}\r
oParent = prevParent;\r
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.query.client.GQUtils;
/**
* Pure Javascript Selector Engine Implementation based on
public NodeList<Element> select(String selector, Node context) {
JsArray<Element> results = JavaScriptObject.createArray().cast();
- return unique(select(selector, context, results, null)).cast();
+ return GQUtils.unique(select(selector, context, results, null)).cast();
}
}
*/\r
package com.google.gwt.query.client.impl;\r
\r
-import static com.google.gwt.query.client.SelectorEngine.eq;\r
-import static com.google.gwt.query.client.SelectorEngine.truth;\r
+import static com.google.gwt.query.client.GQUtils.eq;\r
+import static com.google.gwt.query.client.GQUtils.truth;\r
\r
import com.google.gwt.core.client.JsArray;\r
import com.google.gwt.dom.client.Element;\r
import com.google.gwt.dom.client.Node;\r
import com.google.gwt.dom.client.NodeList;\r
+import com.google.gwt.query.client.GQUtils;\r
import com.google.gwt.query.client.JSArray;\r
import com.google.gwt.query.client.Regexp;\r
import com.google.gwt.query.client.SelectorEngine;\r
\r
+\r
/**\r
* Runtime selector engine implementation which translates selectors to XPath\r
* and delegates to document.evaluate().\r
}\r
SelectorEngine.xpathEvaluate(xPathExpression, ctx, elm);\r
}\r
- return unique(elm.<JsArray<Element>>cast()).cast();\r
+ return GQUtils.unique(elm.<JsArray<Element>>cast()).cast();\r
}\r
\r
private void init() {\r
*/\r
package com.google.gwt.query.client.plugins;\r
\r
+import com.google.gwt.animation.client.Animation;\r
import com.google.gwt.dom.client.Element;\r
import com.google.gwt.dom.client.NodeList;\r
import com.google.gwt.query.client.Function;\r
*/\r
public class Effects extends GQueryQueue {\r
\r
+ /**\r
+ * Just a class to store predefined speed constant values.\r
+ */\r
public static class Speed {\r
public static final int DEFAULT = 400;\r
- public static final int SLOW = 600;\r
public static final int FAST = 200;\r
+ public static final int SLOW = 600;\r
}\r
- \r
+\r
public static final Class<Effects> Effects = Effects.class;\r
+\r
+ private static final String EFFECTS_RUNNNING = "EffectsRunnning";\r
\r
static {\r
GQuery.registerPlugin(Effects.class, new Plugin<Effects>() {\r
public Effects animate(final Properties p, final int duration,\r
final Easing easing, final Function... funcs) {\r
queue(new Function() {\r
+ public void cancel(Element e) {\r
+ Animation anim = (Animation) data(e, EFFECTS_RUNNNING, null);\r
+ if (anim != null) {\r
+ anim.cancel();\r
+ }\r
+ }\r
public void f(Element e) {\r
- new PropertiesAnimation(easing, e, p, funcs).run(duration);\r
+ Animation anim = new PropertiesAnimation(easing, e, p, funcs);\r
+ anim.run(duration);\r
+ data(e, EFFECTS_RUNNNING, anim);\r
}\r
});\r
return this;\r
}\r
-\r
+ \r
/**\r
* The animate() method allows us to create animation effects on any numeric CSS property. \r
* The only required parameter is a map of CSS properties. \r
* from the current value of the property.\r
*/ \r
public Effects animate(String prop, int duration, Function... funcs) {\r
- return animate($$(prop), duration, Easing.SWING, funcs);\r
+ return animate($$(prop), duration, Easing.LINEAR, funcs);\r
}\r
\r
/**\r
public Effects clip(final ClipAnimation.Action a, final ClipAnimation.Corner c, \r
final ClipAnimation.Direction d, final int duration, final Function... f) {\r
queue(new Function() {\r
+ public void cancel(Element e) {\r
+ Animation anim = (Animation) data(e, EFFECTS_RUNNNING, null);\r
+ if (anim != null) {\r
+ anim.cancel();\r
+ }\r
+ }\r
public void f(Element e) {\r
- new ClipAnimation(e, a, c, d, f).run(duration);\r
+ Animation anim = new ClipAnimation(e, a, c, d, f);\r
+ anim.run(duration);\r
+ data(e, EFFECTS_RUNNNING, a);\r
}\r
- });\r
+ }); \r
return this;\r
}\r
\r
*/
public abstract class GQueryQueue extends GQuery {
+ private static final String QUEUE_DATA_PREFIX = "GQueryQueue_";
+
public GQueryQueue(Element element) {
super(element);
}
return this;
}
-
/**
* Adds a new function, to be executed, onto the end of the queue of all
* matched elements.
}
return this;
}
+
+ /**
+ * Stop the function which is currently in execution, remove it
+ * from the queue an start the next one.
+ */
+ public GQueryQueue stop() {
+ for (Element e : elements()) {
+ stop(e);
+ }
+ return this;
+ }
protected String getQueueType() {
- return "GQueryQueue_" + this.getClass().getName();
+ return QUEUE_DATA_PREFIX + this.getClass().getName();
}
private void dequeue(Element elem) {
Object f = q.peek();
if (f != null) {
if (f instanceof Function) {
- ((Function)f).f(elem);
+ ((Function) f).f(elem);
}
}
}
}
-
+
@SuppressWarnings("unchecked")
private <S> Queue<S> queue(Element elem, S func) {
if (elem != null) {
}
if (q.size() == 1 && func != null) {
if (func instanceof Function) {
- ((Function)func).f(elem);
+ ((Function) func).f(elem);
}
}
return q;
}
return null;
}
-
+
private void replacequeue(Element elem, Queue<?> queue) {
if (elem != null) {
data(elem, getQueueType(), queue);
}
}
+
+ private void stop(Element elem) {
+ Queue<?> q = queue(elem, null);
+ if (q != null) {
+ Object f = q.peek();
+ if (f != null) {
+ if (f instanceof Function) {
+ ((Function) f).cancel(elem);
+ }
+ }
+ dequeue();
+ }
+ }
}
* the License.
*/
package com.google.gwt.query.client.plugins;
+import com.google.gwt.animation.client.Animation;
import com.google.gwt.dom.client.Element;
import com.google.gwt.dom.client.NodeList;
import com.google.gwt.query.client.Function;
import com.google.gwt.animation.client.Animation;\r
import com.google.gwt.dom.client.Element;\r
import com.google.gwt.query.client.Function;\r
+import com.google.gwt.query.client.GQUtils;\r
import com.google.gwt.query.client.GQuery;\r
import com.google.gwt.query.client.JSArray;\r
import com.google.gwt.query.client.Properties;\r
LINEAR, SWING\r
}\r
\r
- private static class Effect {\r
- private static Regexp nonPx = new Regexp(\r
- "z-?index|font-?weight|opacity|zoom|line-?height", "i");\r
-\r
+ /**\r
+ * A pojo to store effect values.\r
+ */\r
+ public static class Effect {\r
+
public String attr;\r
public double end;\r
public double start;\r
this.value = value;\r
this.start = start;\r
this.end = end;\r
- this.unit = nonPx.test(attr) ? "" : unit == null ? "px" : unit;\r
+ this.unit = unit;\r
}\r
\r
public String getVal(double progress) {\r
double ret = (start + ((end - start) * progress));\r
return ("px".equals(unit) ? ((int) ret) : ret) + unit;\r
}\r
+ \r
+ public String toString() {\r
+ return ("attr=" + attr + " value=" + value + " start=" + start + " end=" + end + " unit=" + unit).replaceAll("\\.0([^\\d])", "$1");\r
+ }\r
}\r
-\r
+ \r
private static final String[] attrsToSave = new String[] { "overflow",\r
"visibility", "white-space" };\r
+\r
+ private static Regexp nonPxRegExp = new Regexp(\r
+ "z-?index|font-?weight|opacity|zoom|line-?height", "i");\r
+ \r
+ public static Effect computeFxProp(Element e, String key, String val, boolean hidden) {\r
+ GQuery g = Effects.$(e);\r
+ if ("toggle".equals(val)) {\r
+ val = hidden ? "show" : "hide";\r
+ }\r
+ double start = GQUtils.cur(e, key, true), end = start;\r
+ if ("show".equals(val)) {\r
+ if (!hidden) {\r
+ return null;\r
+ }\r
+ g.saveCssAttrs(key);\r
+ start = 0;\r
+ } else if ("hide".equals(val)) {\r
+ if (hidden) {\r
+ return null;\r
+ }\r
+ g.saveCssAttrs(key);\r
+ end = 0;\r
+ } \r
+ JSArray parts = new Regexp("^([+-]=)?([0-9+-.]+)(.*)?$").match(val);\r
+ String unit = "";\r
+ if (parts != null) {\r
+ unit = nonPxRegExp.test(key) ? "" : parts.getStr(3) == null ? "px" : parts.getStr(3); \r
+ end = Double.parseDouble(parts.getStr(2));\r
+ if (!"px".equals(unit)) {\r
+ double to = end == 0 ? 1 : end;\r
+ g.css(key, to + unit);\r
+ start = to * start / GQUtils.cur(e, key, true);\r
+ g.css(key, start + unit);\r
+ }\r
+ if (parts.getStr(1) != null) {\r
+ end = (("-=".equals(parts.getStr(1)) ? -1 : 1) * end) + start;\r
+ }\r
+ } \r
+ Effect fx = new Effect(key, val, start, end, unit);\r
+ return fx;\r
+ }\r
private Element e;\r
private Easing easing = Easing.SWING;\r
private ArrayList<Effect> effects = new ArrayList<Effect>();\r
private Function[] funcs;\r
+\r
private Effects g;\r
\r
private Properties prps;\r
\r
@Override\r
public void onStart() {\r
- boolean hidden = !g.visible();\r
boolean resize = false;\r
+ boolean move = false;\r
+ boolean hidden = !g.visible();\r
+ Effect fx;\r
g.show();\r
for (String key : prps.keys()) {\r
String val = prps.get(key);\r
- if ("toggle".equals(val)) {\r
- val = hidden ? "show" : "hide";\r
- }\r
- if (("top".equals(key) || "left".equals(key))\r
- && !"absolute".equalsIgnoreCase(g.css("position", true))) {\r
- g.css("position", "relative");\r
+ if ((fx = computeFxProp(e, key, val, hidden)) != null) {\r
+ effects.add(fx);\r
+ resize = resize || "height".equals(key) || "width".equals(key);\r
+ move = move || "top".equals(key) || "left".equals(key);\r
}\r
-\r
- JSArray parts = new Regexp("^([+-]=)?([0-9+-.]+)(.*)?$").match(val);\r
- String unit = parts != null ? parts.getStr(3) : "";\r
- double start = GQuery.cur(e, key);\r
- double end = start;\r
- if (parts != null) {\r
- end = Double.parseDouble(parts.getStr(2));\r
- if (parts.getStr(1) != null) {\r
- end = (("-=".equals(parts.getStr(1)) ? -1 : 1) * end) + start;\r
- }\r
- } else if ("show".equals(val)) {\r
- if (!hidden) {\r
- return;\r
- }\r
- g.saveCssAttrs(key);\r
- start = 0;\r
- } else if ("hide".equals(val)) {\r
- if (hidden) {\r
- return;\r
- }\r
- g.saveCssAttrs(key);\r
- end = 0;\r
- }\r
- resize = resize || "height".equals(key) || "width".equals(key);\r
- effects.add(new Effect(key, val, start, end, unit));\r
}\r
g.saveCssAttrs(attrsToSave);\r
if (resize) {\r
g.css("overflow", "hidden");\r
}\r
+ if (move && !g.css("position", true).matches("absolute|relative")) {\r
+ g.css("position", "relative"); \r
+ }\r
g.css("visibility", "visible");\r
g.css("white-space", "nowrap");\r
super.onStart();\r
assertHtmlEquals(expected, $(e).html());
}
+ public void testOpacity() {
+ $(e)
+ .html(
+ "<p id='id1' style='opacity: 0.6; filter: alpha(opacity=60)'>Content 1</p>");
+ GQuery g = $("#id1");
+ assertEquals("0.6", g.css("opacity", false));
+ assertEquals("0.6", g.css("opacity", true));
+ g.css("opacity", "");
+ assertEquals("1.0", g.css("opacity", false));
+ assertEquals("1.0", g.css("opacity", true));
+ g.css("opacity", "0.4");
+ assertEquals("0.4", g.css("opacity", false));
+ assertEquals("0.4", g.css("opacity", true));
+ }
+
public void testProperties() {
Properties p = $$("border:'1px solid black'");
assertEquals(1, p.keys().length);
$(e).html(content);
assertHtmlEquals(expected, $("p", e).contains("test"));
}
+
+ public void testShowHide() {
+ $(e)
+ .html(
+ "<p id='id1' style='display: inline'>Content 1</p><p id='id2'>Content 2</p><p id='id3'>Content 3</p>");
+
+ final GQuery sectA = $("#id1");
+ final GQuery sectB = $("#id2");
+ final GQuery sectC = $("#id3");
+
+ // hide()
+ sectA.hide();
+ assertEquals("none", sectA.css("display"));
+ sectB.hide();
+ assertEquals("none", sectB.css("display"));
+
+ // show()
+ sectA.show();
+ assertEquals("inline", sectA.css("display"));
+ sectB.show();
+ assertEquals("", sectB.css("display"));
+
+ // toggle()
+ assertEquals("", sectC.css("display"));
+ sectC.toggle();
+ assertEquals("none", sectC.css("display"));
+ sectC.toggle();
+ assertEquals("block", sectC.css("display"));
+ }
public void testSliceMethods() {
String content = "<p>This is just a test.</p><p>So is this</p>";
assertEquals(2, $("p", e).slice(0, -1).size());
assertEquals(0, $("p", e).slice(3, 2).size());
}
-
+
public void testUnique() {
SelectorEngineImpl selSizz = new SelectorEngineSizzle();
GQuery g = $(e).html("<div><p></p><p></p><span></span><p></p>");
assertEquals(3, a.getLength());
}
+ public void testUtilsEq() {
+ assertTrue(GQUtils.eq("a", "a"));
+ assertTrue(GQUtils.eq(true, true));
+ assertTrue(GQUtils.eq(45, 45));
+ assertTrue(GQUtils.eq(45d, 45f));
+ assertTrue(GQUtils.eq("", ""));
+ assertTrue(GQUtils.eq(0.45, 0.45));
+ assertTrue(GQUtils.eq(0.45d, 0.45d));
+ assertTrue(GQUtils.eq(0.45f, 0.45f));
+
+ assertFalse(GQUtils.eq("a", ""));
+ assertFalse(GQUtils.eq(true, false));
+ assertFalse(GQUtils.eq(45, 42));
+ assertFalse(GQUtils.eq("", null));
+ assertFalse(GQUtils.eq(0.45, 0.451));
+ }
+
+ public void testUtilsTruth() {
+ assertTrue(GQUtils.truth("a"));
+ assertTrue(GQUtils.truth(this));
+ assertTrue(GQUtils.truth(45));
+ assertTrue(GQUtils.truth(0.33));
+ assertTrue(GQUtils.truth(45l));
+ assertTrue(GQUtils.truth(45d));
+ assertTrue(GQUtils.truth(45f));
+ assertTrue(GQUtils.truth(0.33f));
+
+ assertFalse(GQUtils.truth(0));
+ assertFalse(GQUtils.truth(0l));
+ assertFalse(GQUtils.truth(0d));
+ assertFalse(GQUtils.truth(00.00d));
+ assertFalse(GQUtils.truth(00.00f));
+ assertFalse(GQUtils.truth(null));
+ assertFalse(GQUtils.truth(""));
+ }
+
+ public void testWidthHeight() {
+ $(e)
+ .html(
+ "<div style='border: 1px solid red; padding: 10px; width: 100px; height: 100px'>Content 1</div>");
+ GQuery g = $("div", e);
+ assertEquals(120, g.width());
+ assertEquals(122, g.height());
+ assertEquals(120, g.clientWidth());
+ assertEquals(120, g.clientHeight());
+ assertEquals(100, (int)GQUtils.cur(g.get(0), "width", false));
+ assertEquals(100, (int)GQUtils.cur(g.get(0), "height", false));
+ assertEquals(100, (int)GQUtils.cur(g.get(0), "width", true));
+ assertEquals(100, (int)GQUtils.cur(g.get(0), "height", true));
+ assertEquals("100", g.css("width"));
+ assertEquals("100", g.css("height"));
+ assertEquals("100px", g.get(0).getStyle().getProperty("width"));
+ assertEquals("100px", g.get(0).getStyle().getProperty("height"));
+ }
+
public void testWrapMethod() {
String content = "<p>Test Paragraph.</p>";
String wrapper = "<div id=\"content\">Content</div>";
assertHtmlEquals(expected, $(e).html());
}
- public void testShowHide() {
- $(e)
- .html(
- "<p id='id1' style='display: inline'>Content 1</p><p id='id2'>Content 2</p><p id='id3'>Content 3</p>");
-
- final GQuery sectA = $("#id1");
- final GQuery sectB = $("#id2");
- final GQuery sectC = $("#id3");
-
- // hide()
- sectA.hide();
- assertEquals("none", sectA.css("display"));
- sectB.hide();
- assertEquals("none", sectB.css("display"));
-
- // show()
- sectA.show();
- assertEquals("inline", sectA.css("display"));
- sectB.show();
- assertEquals("", sectB.css("display"));
-
- // toggle()
- assertEquals("", sectC.css("display"));
- sectC.toggle();
- assertEquals("none", sectC.css("display"));
- sectC.toggle();
- assertEquals("block", sectC.css("display"));
- }
-
- public void testWidthHeight() {
- $(e)
- .html(
- "<div style='border: 1px solid red; padding: 10px; width: 100px; height: 100px'>Content 1</div>");
- GQuery g = $("div", e);
- assertEquals(120, g.width());
- assertEquals(122, g.height());
- assertEquals(120, g.clientWidth());
- assertEquals(120, g.clientHeight());
- assertEquals(100, (int)GQuery.cur(g.get(0), "width"));
- assertEquals(100, (int)GQuery.cur(g.get(0), "height"));
- assertEquals("100px", g.css("width"));
- assertEquals("100px", g.css("height"));
- }
}
import com.google.gwt.junit.client.GWTTestCase;
import com.google.gwt.query.client.GQuery.Offset;
import com.google.gwt.query.client.plugins.Effects;
-import com.google.gwt.query.client.plugins.ClipAnimation.Action;
+import com.google.gwt.query.client.plugins.PropertiesAnimation;
import com.google.gwt.query.client.plugins.PropertiesAnimation.Easing;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.ui.HTML;
}
}
- public void testFade() {
- $(e)
- .html(
- "<p id='id1' style='display: inline'>Content 1</p><p id='id2'>Content 2</p><p id='id3'>Content 3</p>");
-
- final GQuery sectA = $("#id1");
- final GQuery sectB = $("#id2");
+ public void testClipAnimation() {
+ $(e).html("<p id='idtest'>Content 1</p></p>");
- // fadeIn() & fadeOut() are tested with delayed assertions
- sectA.hide();
- sectA.fadeIn(2000);
- sectB.fadeOut(2000);
+ final GQuery g = $("#idtest");
+ final int duration = 800;
+
+ // Clip effect places a relative div in the position of the original element
+ // So check that there is not any div.
+ GQuery back = $("div", e);
+ assertEquals(0, back.size());
- // Configure the max duration for this test
- // If the test exceeds the timeout without calling finishTest() it will fail
- delayTestFinish(2500);
+ g.as(Effects.Effects).clipDisappear(duration);
- // Delayed assertions at different intervals
- Timer timerShortTime = new Timer() {
- public void run() {
- double o = Double.valueOf(sectA.css("opacity"));
- assertTrue(
- "'sectA' opacity must be in the interval 0-0.5 but is: " + o, o > 0
- && o < 0.5);
- o = Double.valueOf(sectB.css("opacity"));
- assertTrue(
- "'sectB' opacity must be in the interval 0.5-1 but is: " + o,
- o > 0.5 && o < 1);
- }
- };
- Timer timerMidTime = new Timer() {
+ // Check that the back div has been created
+ back = $("div", e);
+ assertEquals(1, back.size());
+
+ // Configure the max duration for this test
+ delayTestFinish(duration * 2);
+
+ // each timer calls the next one
+ final Timer timer1 = new Timer() {
public void run() {
- assertEquals("inline", sectA.css("display"));
- assertEquals("", sectB.css("display"));
- double o = Double.valueOf(sectA.css("opacity"));
- assertTrue(
- "'sectA' opacity must be in the interval 0.5-1 but is: " + o,
- o > 0.5 && o < 1);
- o = Double.valueOf(sectB.css("opacity"));
- assertTrue(
- "'sectB' opacity must be in the interval 0-0.5 but is: " + o, o > 0
- && o < 0.5);
+ // Check that the back div has been removed
+ GQuery back = $("div", e);
+ assertEquals(0, back.size());
+ // Check that the attribute clip has been removed
+ assertEquals("", g.css("clip"));
+ finishTest();
}
};
- Timer timerLongTime = new Timer() {
+ final Timer timer2 = new Timer() {
public void run() {
- assertEquals("inline", sectA.css("display"));
- assertEquals("none", sectB.css("display"));
- // Last delayed assertion has to stop the test to avoid a timeout
- // failure
- finishTest();
+ // Check that the attribute clip has been set
+ assertTrue(g.css("clip").matches("rect\\(\\d+px[, ]+\\d+px[, ]+\\d+px[, ]+\\d+px\\)"));
+ timer1.schedule(duration/2 + 1);
}
};
-
- // schedule the delayed assertions
- timerShortTime.schedule(200);
- timerMidTime.schedule(1200);
- timerLongTime.schedule(2200);
+
+ // Start the first timer
+ timer2.schedule(duration/2);
}
public void testEffectsShouldBeQueued() {
timer3.schedule(duration);
}
};
- // Starts the first timer
+ // Start the first timer
timer4.schedule(duration/2);
}
- public void testClipAnimation() {
- $(e).html("<p id='idtest'>Content 1</p></p>");
-
- final GQuery g = $("#idtest");
- final int duration = 800;
-
- // Clip effect paces a relative div in the position of the original element
- // So check that there is not any div.
- GQuery back = $("div", e);
- assertEquals(0, back.size());
-
- g.as(Effects.Effects).clipDisappear(duration);
+ public void testFade() {
+ $(e)
+ .html(
+ "<p id='id1' style='display: inline'>Content 1</p><p id='id2'>Content 2</p><p id='id3'>Content 3</p>");
- // Check that the back div has been created
- back = $("div", e);
- assertEquals(1, back.size());
+ final GQuery sectA = $("#id1");
+ final GQuery sectB = $("#id2");
+ // fadeIn() & fadeOut() are tested with delayed assertions
+ sectA.hide();
+ sectA.fadeIn(2000);
+ sectB.fadeOut(2000);
+
// Configure the max duration for this test
- delayTestFinish(duration * 2);
-
- // each timer calls the next one
- final Timer timer1 = new Timer() {
+ // If the test exceeds the timeout without calling finishTest() it will fail
+ delayTestFinish(2500);
+
+ // Delayed assertions at different intervals
+ Timer timerShortTime = new Timer() {
public void run() {
- // Check that the back div has been removed
- GQuery back = $("div", e);
- assertEquals(0, back.size());
- // Check that the attribute clip has been removed
- assertEquals("", g.css("clip"));
- finishTest();
+ double o = Double.valueOf(sectA.css("opacity"));
+ assertTrue(
+ "'sectA' opacity must be in the interval 0-0.5 but is: " + o, o > 0
+ && o < 0.5);
+ o = Double.valueOf(sectB.css("opacity"));
+ assertTrue(
+ "'sectB' opacity must be in the interval 0.5-1 but is: " + o,
+ o > 0.5 && o < 1);
}
};
- final Timer timer2 = new Timer() {
+ Timer timerMidTime = new Timer() {
public void run() {
- // Check that the attribute clip has been set
- assertTrue(g.css("clip").matches("rect\\(\\d+px[, ]+\\d+px[, ]+\\d+px[, ]+\\d+px\\)"));
- timer1.schedule(duration/2 + 1);
+ assertEquals("inline", sectA.css("display"));
+ assertEquals("", sectB.css("display"));
+ double o = Double.valueOf(sectA.css("opacity"));
+ assertTrue(
+ "'sectA' opacity must be in the interval 0.5-1 but is: " + o,
+ o > 0.5 && o < 1);
+ o = Double.valueOf(sectB.css("opacity"));
+ assertTrue(
+ "'sectB' opacity must be in the interval 0-0.5 but is: " + o, o > 0
+ && o < 0.5);
}
};
-
- // Start the first timer
- timer2.schedule(duration/2);
+ Timer timerLongTime = new Timer() {
+ public void run() {
+ assertEquals("inline", sectA.css("display"));
+ assertEquals("none", sectB.css("display"));
+ // Last delayed assertion has to stop the test to avoid a timeout
+ // failure
+ finishTest();
+ }
+ };
+
+ // schedule the delayed assertions
+ timerShortTime.schedule(200);
+ timerMidTime.schedule(1200);
+ timerLongTime.schedule(2200);
}
+ public void testPropertiesAnimationComputeEffects() {
+ $(e)
+ .html(
+ "<div id='parent' style='background-color: yellow; width: 100px; height: 200px; top:130px; position: absolute; left: 130px'><p id='child' style='background-color: pink; width: 100px; height: 100px; position: absolute; padding: 5px; margin: 0px'>Content 1</p></div>");
+ GQuery g = $("#child");
+ Properties prop1;
+
+ assertEquals("attr=marginTop value=-110px start=0 end=-110 unit=px",
+ PropertiesAnimation.computeFxProp(g.get(0), "marginTop", "-110px",
+ false).toString());
+ assertEquals("attr=marginLeft value=-110px start=0 end=-110 unit=px",
+ PropertiesAnimation.computeFxProp(g.get(0), "marginLeft", "-110px",
+ false).toString());
+ assertEquals("attr=top value=50% start=0 end=50 unit=%",
+ PropertiesAnimation.computeFxProp(g.get(0), "top", "50%", false)
+ .toString());
+ assertEquals("attr=left value=50% start=0 end=50 unit=%",
+ PropertiesAnimation.computeFxProp(g.get(0), "left", "50%", false)
+ .toString());
+ assertEquals("attr=width value=174px start=100 end=174 unit=px",
+ PropertiesAnimation.computeFxProp(g.get(0), "width", "174px", false)
+ .toString());
+ assertEquals("attr=height value=174px start=100 end=174 unit=px",
+ PropertiesAnimation.computeFxProp(g.get(0), "height", "174px", false)
+ .toString());
+ assertEquals("attr=padding value=20px start=5 end=20 unit=px",
+ PropertiesAnimation.computeFxProp(g.get(0), "padding", "20px", false)
+ .toString());
+
+ prop1 = GQuery.$$("marginTop: '-110px', marginLeft: '-110px', top: '50%', left: '50%', width: '174px', height: '174px', padding: '20px'");
+ PropertiesAnimation an = new PropertiesAnimation(Easing.SWING, g.get(0), prop1);
+ an.onStart();
+ an.onComplete();
+
+ assertEquals("attr=marginTop value=0 start=-110 end=0 unit=px",
+ PropertiesAnimation.computeFxProp(g.get(0), "marginTop", "0", false)
+ .toString());
+ assertEquals("attr=marginLeft value=0 start=-110 end=0 unit=px",
+ PropertiesAnimation.computeFxProp(g.get(0), "marginLeft", "0", false)
+ .toString());
+ assertEquals("attr=top value=0% start=50 end=0 unit=%", PropertiesAnimation
+ .computeFxProp(g.get(0), "top", "0%", false).toString());
+ assertEquals("attr=left value=0% start=50 end=0 unit=%",
+ PropertiesAnimation.computeFxProp(g.get(0), "left", "0%", false)
+ .toString());
+ assertEquals("attr=width value=100px start=174 end=100 unit=px",
+ PropertiesAnimation.computeFxProp(g.get(0), "width", "100px", false)
+ .toString());
+ assertEquals("attr=height value=100px start=174 end=100 unit=px",
+ PropertiesAnimation.computeFxProp(g.get(0), "height", "100px", false)
+ .toString());
+ assertEquals("attr=padding value=5px start=20 end=5 unit=px",
+ PropertiesAnimation.computeFxProp(g.get(0), "padding", "5px", false)
+ .toString());
+
+ prop1 = GQuery.$$("marginTop: '0', marginLeft: '0', top: '0%', left: '0%', width: '100px', height: '100px', padding: '5px'");
+ an = new PropertiesAnimation(Easing.SWING, g.get(0), prop1);
+ an.onStart();
+ an.onComplete();
+
+ assertEquals("attr=marginTop value=-110px start=0 end=-110 unit=px",
+ PropertiesAnimation.computeFxProp(g.get(0), "marginTop", "-110px",
+ false).toString());
+ assertEquals("attr=marginLeft value=-110px start=0 end=-110 unit=px",
+ PropertiesAnimation.computeFxProp(g.get(0), "marginLeft", "-110px",
+ false).toString());
+ assertEquals("attr=top value=50% start=0 end=50 unit=%",
+ PropertiesAnimation.computeFxProp(g.get(0), "top", "50%", false)
+ .toString());
+ assertEquals("attr=left value=50% start=0 end=50 unit=%",
+ PropertiesAnimation.computeFxProp(g.get(0), "left", "50%", false)
+ .toString());
+ assertEquals("attr=width value=174px start=100 end=174 unit=px",
+ PropertiesAnimation.computeFxProp(g.get(0), "width", "174px", false)
+ .toString());
+ assertEquals("attr=height value=174px start=100 end=174 unit=px",
+ PropertiesAnimation.computeFxProp(g.get(0), "height", "174px", false)
+ .toString());
+ assertEquals("attr=padding value=20px start=5 end=20 unit=px",
+ PropertiesAnimation.computeFxProp(g.get(0), "padding", "20px", false)
+ .toString());
+ }
+
private void assertPosition(GQuery g, Offset min, Offset max) {
int a = Math.min(min.top, max.top);
int b = Math.max(min.top, max.top);
a.push(a.get(i));
}
assertEquals(n * 2 , a.length());
- a = SelectorEngineImpl.unique(a);
+ a = GQUtils.unique(a);
assertEquals(n, a.length());
}
-
private void assertArrayContains(Object result, Object... array) {
assertArrayContains("", result, array);
}