From: Manolo Carrasco Date: Mon, 21 Jun 2010 09:10:09 +0000 (+0000) Subject: - Implemented the stop() method in queue X-Git-Tag: release-1.3.2~685 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=bc0672c61b193b477a5fadadf52fb9eb1e9c916b;p=gwtquery.git - Implemented the stop() method in queue - Fixed some issues in css (width, height, opacity) - Fix PropertiesAnimation to handle correctly non-px units - Fixed an NPE in getParent - Moved some useful static method into a new GQUtils class - More tests --- diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/Function.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/Function.java index 005b9d2a..b15fba2f 100644 --- a/gwtquery-core/src/main/java/com/google/gwt/query/client/Function.java +++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/Function.java @@ -22,6 +22,12 @@ import com.google.gwt.user.client.Event; * 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 diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/GQUtils.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/GQUtils.java new file mode 100644 index 00000000..6be21c5e --- /dev/null +++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/GQUtils.java @@ -0,0 +1,118 @@ +/* + * 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 unique(JsArray a) { + JsArray ret = JavaScriptObject.createArray().cast(); + HashSet f = new HashSet(); + 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; + } + +} 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 394e27eb..16e93d83 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 @@ -39,7 +39,6 @@ import com.google.gwt.query.client.css.Percentage; import com.google.gwt.query.client.css.TakesLength; import com.google.gwt.query.client.css.TakesPercentage; import com.google.gwt.query.client.impl.DocumentStyleImpl; -import com.google.gwt.query.client.impl.SelectorEngineImpl; import com.google.gwt.user.client.Event; import com.google.gwt.user.client.Window; @@ -268,45 +267,6 @@ public class GQuery implements Lazy { } } - /** - * Returns the numeric value of an element css property - */ - public static double cur(Element elem, String prop) { - GQuery g = $(elem); - String val = g.css(prop, true); - if ("height".equalsIgnoreCase(prop)) { - return elem.getClientHeight() - cur(elem, "paddingTop") - cur(elem, "paddingBottom"); - } - if ("width".equalsIgnoreCase(prop)) { - return elem.getClientWidth() - cur(elem, "paddingLeft") - cur(elem, "paddingRight"); - } - if ("absolute".equalsIgnoreCase(g.css("position", true))) { - if ("left".equalsIgnoreCase(prop)) { - return g.offset().left; - } - if ("top".equalsIgnoreCase(prop)) { - return g.offset().top; - } - } - if ("opacity".equalsIgnoreCase(prop)) { - return Double.parseDouble(val); - } - if (elem.getPropertyString(prop) != null - && (elem.getStyle() == null || elem.getStyle().getProperty(prop) == null)) { - return elem.getPropertyDouble(prop); - } - - if ("thick".equalsIgnoreCase(val)) { - return (5); - } else if ("medium".equalsIgnoreCase(val)) { - return (3); - } else if ("thin".equalsIgnoreCase(val)) { - return (1); - } - val = "0" + val.replaceAll("[^\\d\\.]+", ""); - return Double.parseDouble(val); - } - /** * Return a lazy version of the GQuery interface. Lazy function calls are * simply queued up and not executed immediately. @@ -353,6 +313,7 @@ public class GQuery implements Lazy { postWrap = ""; } // TODO: fix IE link tag serialization + // TODO: fix IE