]> source.dussan.org Git - gwtquery.git/commitdiff
moved style code from GQuery to appropriate implementation classes
authorManolo Carrasco <manolo@apache.org>
Fri, 11 Jun 2010 22:29:00 +0000 (22:29 +0000)
committerManolo Carrasco <manolo@apache.org>
Fri, 11 Jun 2010 22:29:00 +0000 (22:29 +0000)
gwtquery-core/src/main/java/com/google/gwt/query/client/GQuery.java
gwtquery-core/src/main/java/com/google/gwt/query/client/impl/DocumentStyleImpl.java
gwtquery-core/src/main/java/com/google/gwt/query/client/impl/DocumentStyleImplIE.java

index 94900185ffe152d5023ee28d1eba052f9bdb7a27..8f3113ac30d27c415bf833145e57269654abebc3 100644 (file)
@@ -158,7 +158,7 @@ public class GQuery implements Lazy<GQuery, LazyGQuery> {
   private static JsMap<Class<? extends GQuery>, Plugin<? extends GQuery>>\r
       plugins;\r
 \r
-  private static DocumentStyleImpl styleImpl;\r
+  private static DocumentStyleImpl styleImpl = GWT.create(DocumentStyleImpl.class);;\r
 \r
   private static Element windowData = null;\r
 \r
@@ -270,12 +270,6 @@ public class GQuery implements Lazy<GQuery, LazyGQuery> {
     }\r
   }\r
 \r
-  public static native String camelize(String s)/*-{\r
-     return s.replace(/\-(\w)/g, function(all, letter){\r
-                               return letter.toUpperCase();\r
-                       });\r
-   }-*/;\r
-\r
   /**\r
    * Returns the numeric value of a css propery of the element.\r
    */\r
@@ -317,32 +311,6 @@ public class GQuery implements Lazy<GQuery, LazyGQuery> {
     return 0.0;\r
   }\r
   \r
-  /**\r
-   * Returns the string value of a css propery of the element.\r
-   * TODO: use implementations\r
-   */\r
-  public static String curCSS(Element elem, String name, boolean force) {\r
-    name = fixAttributeName(name);\r
-    Style s = elem.getStyle();\r
-    if ("opacity".equals(name)) {\r
-      String o = s.getProperty("filter");\r
-      if (o != null) { \r
-        return !o.matches(".*opacity=.*") ? "1" : \r
-            ("" + (Double.valueOf(o.replaceAll("[^\\d]", "")) / 100));\r
-      }\r
-      o = s.getProperty("opacity");\r
-      return o == null || o.length() == 0 ? "1" : o; \r
-    }    \r
-    if (!force) {\r
-      if (SelectorEngine.truth(s.getProperty(name))) {\r
-        return s.getProperty(name);\r
-      }\r
-    } else {\r
-      return styleImpl.getCurrentStyle(elem, name);\r
-    }\r
-    return "";\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
@@ -431,23 +399,7 @@ public class GQuery implements Lazy<GQuery, LazyGQuery> {
       return result;\r
     }\r
   }\r
-\r
-  protected static void setStyleProperty(String prop, String val, Element e) {\r
-    // put in lower-case only if all letters are upper-case, to avoid modify already camelized properties\r
-    if (prop.matches("^[A-Z]+$")) {\r
-       prop = prop.toLowerCase();\r
-    }    \r
-    String property = camelize(prop);\r
-    e.getStyle().setProperty(property, val);\r
-    \r
-    // TODO: this is a workaround in IE which must be moved to IE implementation\r
-    if ("opacity".equals(property)) {\r
-      e.getStyle().setProperty("zoom", "1");\r
-      e.getStyle().setProperty("filter",\r
-          "alpha(opacity=" + (int) (Double.valueOf(val) * 100) + ")");\r
-    }\r
-  }\r
-\r
+  \r
   private static JSArray copyNodeList(NodeList n) {\r
     JSArray res = JSArray.create();\r
     for (int i = 0; i < n.getLength(); i++) {\r
@@ -457,18 +409,7 @@ public class GQuery implements Lazy<GQuery, LazyGQuery> {
   }\r
 \r
   private static String curCSS(Element elem, String name) {\r
-    return curCSS(elem, name, false);\r
-  }\r
-\r
-  private static void ensureStyleImpl() {\r
-    if (styleImpl == null) {\r
-      styleImpl = GWT.create(DocumentStyleImpl.class);\r
-    }\r
-  }\r
-\r
-  private static String fixAttributeName(String key) {\r
-    ensureStyleImpl();\r
-    return styleImpl.getPropertyName(key);\r
+    return styleImpl.curCSS(elem, name);\r
   }\r
 \r
   private static boolean hasClass(Element e, String clz) {\r
@@ -484,11 +425,11 @@ public class GQuery implements Lazy<GQuery, LazyGQuery> {
   }-*/;\r
 \r
   private static native <T extends Node> T[] reinterpretCast(NodeList<T> nl) /*-{\r
-        return nl;\r
-    }-*/;\r
+    return nl;\r
+  }-*/;\r
 \r
-  private static NodeList select(String selector, Node context) {\r
-    NodeList n = new SelectorEngine().select(selector, context);\r
+  private static NodeList<Element> select(String selector, Node context) {\r
+    NodeList<Element> n = new SelectorEngine().select(selector, context);\r
     JSArray res = copyNodeList(n);\r
     return res;\r
   }\r
@@ -648,7 +589,7 @@ public class GQuery implements Lazy<GQuery, LazyGQuery> {
   public GQuery attr(Properties properties) {\r
     for (Element e : elements()) {\r
       for (String name : properties.keys()) {\r
-        e.setAttribute(fixAttributeName(name), properties.get(name));\r
+        e.setAttribute(styleImpl.fixPropertyName(name), properties.get(name));\r
       }\r
     }\r
     return this;\r
@@ -661,7 +602,7 @@ public class GQuery implements Lazy<GQuery, LazyGQuery> {
    * Attributes include title, alt, src, href, width, style, etc.\r
    */\r
   public String attr(String name) {\r
-    return elements.getItem(0).getAttribute(fixAttributeName(name));\r
+    return elements.getItem(0).getAttribute(styleImpl.fixPropertyName(name));\r
   }\r
 \r
   /**\r
@@ -670,7 +611,7 @@ public class GQuery implements Lazy<GQuery, LazyGQuery> {
   public GQuery attr(String key, Function closure) {\r
     for (int i = 0; i < elements.getLength(); i++) {\r
       Element e = elements.getItem(i);\r
-      e.setAttribute(fixAttributeName(key), closure.f(e, i));\r
+      e.setAttribute(styleImpl.fixPropertyName(key), closure.f(e, i));\r
     }\r
     return this;\r
   }\r
@@ -679,7 +620,7 @@ public class GQuery implements Lazy<GQuery, LazyGQuery> {
    * Set a single property to a value, on all matched elements.\r
    */\r
   public GQuery attr(String key, String value) {\r
-    key = fixAttributeName(key);\r
+    key = styleImpl.fixPropertyName(key);\r
     for (Element e : elements()) {\r
       e.setAttribute(key, value);\r
     }\r
@@ -822,7 +763,7 @@ public class GQuery implements Lazy<GQuery, LazyGQuery> {
   public GQuery contents() {\r
     JSArray result = JSArray.create();\r
     for (Element e : elements()) {\r
-      NodeList children = e.getChildNodes();\r
+      NodeList<Node> children = e.getChildNodes();\r
       for (int i = 0; i < children.getLength(); i++) {\r
         Node n = children.getItem(i);\r
         if (IFrameElement.is(n)) {\r
@@ -835,7 +776,7 @@ public class GQuery implements Lazy<GQuery, LazyGQuery> {
     return new GQuery(unique(result));\r
   }\r
 \r
-  public LazyGQuery createLazy() {\r
+  public LazyGQuery<?> createLazy() {\r
     return GWT.create(GQuery.class);\r
   }\r
 \r
@@ -865,9 +806,8 @@ public class GQuery implements Lazy<GQuery, LazyGQuery> {
    * Set a single style property to a value, on all matched elements.\r
    */\r
   public GQuery css(String prop, String val) {\r
-    prop = fixAttributeName(prop);\r
     for (Element e : elements()) {\r
-      setStyleProperty(prop, val, e);\r
+      styleImpl.setStyleProperty(prop, val, e);\r
     }\r
     return this;\r
   }\r
@@ -916,6 +856,7 @@ public class GQuery implements Lazy<GQuery, LazyGQuery> {
    *\r
    * @param clz return type class literal\r
    */\r
+  @SuppressWarnings("unchecked")\r
   public <T> T data(String name, Class<T> clz) {\r
     return (T) data(elements.getItem(0), name, null);\r
   }\r
@@ -1782,7 +1723,7 @@ public class GQuery implements Lazy<GQuery, LazyGQuery> {
   public void restoreCssAttrs(String[] cssProps) {\r
     for (Element e : elements()) {\r
       for (String a : cssProps) {\r
-        setStyleProperty(a, (String) data(e, "old-" + a, null), e);\r
+        styleImpl.setStyleProperty(a, (String) data(e, "old-" + a, null), e);\r
       }\r
     }\r
   }\r
index 64c2c0f5e756c0e6946743088619fb59d641007c..7d87797c54d0c09570637a1ce3ca6dc03776a11a 100644 (file)
@@ -16,7 +16,7 @@
 package com.google.gwt.query.client.impl;
 
 import com.google.gwt.dom.client.Element;
-import com.google.gwt.query.client.GQuery;
+import com.google.gwt.dom.client.Style;
 import com.google.gwt.query.client.SelectorEngine;
 
 /**
@@ -24,31 +24,81 @@ import com.google.gwt.query.client.SelectorEngine;
  */
 public class DocumentStyleImpl {
 
-  public String getCurrentStyle(Element elem, String name) {
-    name = hyphenize(name);
-    String propVal = getComputedStyle(elem, name, null);
-    if ("opacity".equalsIgnoreCase(name)) {
-      propVal = SelectorEngine.or(propVal, "1");
+  /**
+   * Camelize style property names.
+   *  for instance:
+   *   font-name -> fontName 
+   */
+  public static native String camelize(String s)/*-{
+    return s.replace(/\-(\w)/g, function(all, letter) {
+    return letter.toUpperCase();
+    });
+  }-*/;
+
+  /**
+   * Hyphenize style property names.
+   *  for instance:
+   *   fontName -> font-name 
+   */
+  public static native String hyphenize(String name) /*-{
+    return name.replace(/([A-Z])/g, "-$1" ).toLowerCase();
+  }-*/;
+
+  /**
+   * Return the string value of a css property of an element. 
+   */
+  public String curCSS(Element elem, String prop) {
+    prop = fixPropertyName(prop);
+    Style s = elem.getStyle();
+    if (SelectorEngine.truth(s.getProperty(prop))) {
+      return s.getProperty(prop);
+    } else {
+      prop = hyphenize(prop);
+      return getComputedStyle(elem, prop, null);
     }
-    return propVal;
   }
 
-  public String getPropertyName(String name) {
+  /**
+   * Fix style property names.
+   */
+  public String fixPropertyName(String name) {
     if ("float".equalsIgnoreCase(name)) {
       return "cssFloat";
     } else if ("for".equalsIgnoreCase(name)) {
       return "htmlFor";
     }
-    return GQuery.camelize(name);
+    return camelize(name);
   }
 
-  protected native String hyphenize(String name) /*-{
-      return name.replace( /([A-Z])/g, "-$1" ).toLowerCase();
-  }-*/;
+  /**
+   * Remove a style property from an element.
+   */
+  public void removeStyleProperty(Element elem, String prop) {
+    elem.getStyle().setProperty(prop, "");
+  }
 
-  private native String getComputedStyle(Element elem, String name,
+  /**
+   * Set the value of a style property of an element.
+   */
+  public void setStyleProperty(String prop, String val, Element e) {
+    prop = fixPropertyName(prop);
+    // put it in lower-case only when all letters are upper-case, to avoid 
+    // modifying already camelized properties
+    if (prop.matches("^[A-Z]+$")) {
+      prop = prop.toLowerCase();
+    }
+    prop = camelize(prop);
+    if (val == null || val.trim().length() == 0) {
+      removeStyleProperty(e, prop);
+    } else {
+      e.getStyle().setProperty(prop, val);
+    }
+  }
+
+  protected native String getComputedStyle(Element elem, String name,
       String pseudo) /*-{
-      var cStyle = $doc.defaultView.getComputedStyle( elem, pseudo );
-      return cStyle ? cStyle.getPropertyValue( name ) : null;
+    var cStyle = $doc.defaultView.getComputedStyle( elem, pseudo );
+    return cStyle ? cStyle.getPropertyValue( name ) : null;
   }-*/;
+
 }
index a8bbb23fc9f8d3920b4096eeae97babdcc9e8714..e3148bc9c0007f2c991192a5f125e82c3fb1bf9d 100644 (file)
 package com.google.gwt.query.client.impl;
 
 import com.google.gwt.dom.client.Element;
-import com.google.gwt.query.client.SelectorEngine;
+import com.google.gwt.dom.client.Style;
 
 /**
  * A helper class to get computed CSS styles for elements on IE6.
  */
 public class DocumentStyleImplIE extends DocumentStyleImpl {
 
-  public String getCurrentStyle(Element elem, String name) {
-    name = hyphenize(name);
-    String propVal = getComputedStyle(elem, name, null);
+  /**
+   * Return the string value of a css property of an element. 
+   * IE needs a special workaround to handle opacity.
+   */
+  @Override
+  public String curCSS(Element elem, String name) {
     if ("opacity".equalsIgnoreCase(name)) {
-      propVal = SelectorEngine.or(propVal, "1");
+      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 propVal;
+    return super.curCSS(elem, name);
   }
 
-  public String getPropertyName(String name) {
-    name = super.getPropertyName(name);
+  /**
+   * Fix style property names.
+   */
+  @Override
+  public String fixPropertyName(String name) {
+    name = super.fixPropertyName(name);
     if ("cssFloat".equals(name)) {
       return "styleFloat";
     } else if ("class".equals(name)) {
@@ -41,12 +55,33 @@ public class DocumentStyleImplIE extends DocumentStyleImpl {
     }
     return name;
   }
-  
-  
 
-  // code lifted from jQuery
-  private native String getComputedStyle(Element elem, String name,
+  /**
+   * 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. 
+   * IE needs a special workaround to handle opacity
+   */
+  @Override
+  public void setStyleProperty(String prop, String val, Element e) {
+    if ("opacity".equals(prop)) {
+      e.getStyle().setProperty("zoom", "1");
+      e.getStyle().setProperty("filter",
+          "alpha(opacity=" + (int) (Double.valueOf(val) * 100) + ")");
+    } else {
+      super.setStyleProperty(prop, val, e);
+    }
+  }
+
+  @Override
+  protected native String getComputedStyle(Element elem, String name,
       String pseudo) /*-{
+    // code lifted from jQuery
     var style = elem.style;
     var camelCase = name.replace(/\-(\w)/g, function(all, letter){
         return letter.toUpperCase();
@@ -67,6 +102,6 @@ public class DocumentStyleImplIE extends DocumentStyleImpl {
     style.left = left;
     elem.runtimeStyle.left = rsLeft;
     }
-   return ret;
+    return ret;
   }-*/;
 }
\ No newline at end of file