From 406360bd9d492054a1d4e918f12587c571b23be1 Mon Sep 17 00:00:00 2001 From: Manolo Carrasco Date: Fri, 11 Jun 2010 22:29:00 +0000 Subject: [PATCH] moved style code from GQuery to appropriate implementation classes --- .../com/google/gwt/query/client/GQuery.java | 91 ++++--------------- .../query/client/impl/DocumentStyleImpl.java | 80 +++++++++++++--- .../client/impl/DocumentStyleImplIE.java | 61 ++++++++++--- 3 files changed, 129 insertions(+), 103 deletions(-) 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 94900185..8f3113ac 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 @@ -158,7 +158,7 @@ public class GQuery implements Lazy { private static JsMap, Plugin> plugins; - private static DocumentStyleImpl styleImpl; + private static DocumentStyleImpl styleImpl = GWT.create(DocumentStyleImpl.class);; private static Element windowData = null; @@ -270,12 +270,6 @@ public class GQuery implements Lazy { } } - public static native String camelize(String s)/*-{ - return s.replace(/\-(\w)/g, function(all, letter){ - return letter.toUpperCase(); - }); - }-*/; - /** * Returns the numeric value of a css propery of the element. */ @@ -317,32 +311,6 @@ public class GQuery implements Lazy { return 0.0; } - /** - * Returns the string value of a css propery of the element. - * TODO: use implementations - */ - public static String curCSS(Element elem, String name, boolean force) { - name = fixAttributeName(name); - Style s = elem.getStyle(); - if ("opacity".equals(name)) { - 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; - } - if (!force) { - if (SelectorEngine.truth(s.getProperty(name))) { - return s.getProperty(name); - } - } else { - return styleImpl.getCurrentStyle(elem, name); - } - return ""; - } - /** * Return a lazy version of the GQuery interface. Lazy function calls are * simply queued up and not executed immediately. @@ -431,23 +399,7 @@ public class GQuery implements Lazy { return result; } } - - protected static void setStyleProperty(String prop, String val, Element e) { - // put in lower-case only if all letters are upper-case, to avoid modify already camelized properties - if (prop.matches("^[A-Z]+$")) { - prop = prop.toLowerCase(); - } - String property = camelize(prop); - e.getStyle().setProperty(property, val); - - // TODO: this is a workaround in IE which must be moved to IE implementation - if ("opacity".equals(property)) { - e.getStyle().setProperty("zoom", "1"); - e.getStyle().setProperty("filter", - "alpha(opacity=" + (int) (Double.valueOf(val) * 100) + ")"); - } - } - + private static JSArray copyNodeList(NodeList n) { JSArray res = JSArray.create(); for (int i = 0; i < n.getLength(); i++) { @@ -457,18 +409,7 @@ public class GQuery implements Lazy { } private static String curCSS(Element elem, String name) { - return curCSS(elem, name, false); - } - - private static void ensureStyleImpl() { - if (styleImpl == null) { - styleImpl = GWT.create(DocumentStyleImpl.class); - } - } - - private static String fixAttributeName(String key) { - ensureStyleImpl(); - return styleImpl.getPropertyName(key); + return styleImpl.curCSS(elem, name); } private static boolean hasClass(Element e, String clz) { @@ -484,11 +425,11 @@ public class GQuery implements Lazy { }-*/; private static native T[] reinterpretCast(NodeList nl) /*-{ - return nl; - }-*/; + return nl; + }-*/; - private static NodeList select(String selector, Node context) { - NodeList n = new SelectorEngine().select(selector, context); + private static NodeList select(String selector, Node context) { + NodeList n = new SelectorEngine().select(selector, context); JSArray res = copyNodeList(n); return res; } @@ -648,7 +589,7 @@ public class GQuery implements Lazy { public GQuery attr(Properties properties) { for (Element e : elements()) { for (String name : properties.keys()) { - e.setAttribute(fixAttributeName(name), properties.get(name)); + e.setAttribute(styleImpl.fixPropertyName(name), properties.get(name)); } } return this; @@ -661,7 +602,7 @@ public class GQuery implements Lazy { * Attributes include title, alt, src, href, width, style, etc. */ public String attr(String name) { - return elements.getItem(0).getAttribute(fixAttributeName(name)); + return elements.getItem(0).getAttribute(styleImpl.fixPropertyName(name)); } /** @@ -670,7 +611,7 @@ public class GQuery implements Lazy { public GQuery attr(String key, Function closure) { for (int i = 0; i < elements.getLength(); i++) { Element e = elements.getItem(i); - e.setAttribute(fixAttributeName(key), closure.f(e, i)); + e.setAttribute(styleImpl.fixPropertyName(key), closure.f(e, i)); } return this; } @@ -679,7 +620,7 @@ public class GQuery implements Lazy { * Set a single property to a value, on all matched elements. */ public GQuery attr(String key, String value) { - key = fixAttributeName(key); + key = styleImpl.fixPropertyName(key); for (Element e : elements()) { e.setAttribute(key, value); } @@ -822,7 +763,7 @@ public class GQuery implements Lazy { public GQuery contents() { JSArray result = JSArray.create(); for (Element e : elements()) { - NodeList children = e.getChildNodes(); + NodeList children = e.getChildNodes(); for (int i = 0; i < children.getLength(); i++) { Node n = children.getItem(i); if (IFrameElement.is(n)) { @@ -835,7 +776,7 @@ public class GQuery implements Lazy { return new GQuery(unique(result)); } - public LazyGQuery createLazy() { + public LazyGQuery createLazy() { return GWT.create(GQuery.class); } @@ -865,9 +806,8 @@ public class GQuery implements Lazy { * Set a single style property to a value, on all matched elements. */ public GQuery css(String prop, String val) { - prop = fixAttributeName(prop); for (Element e : elements()) { - setStyleProperty(prop, val, e); + styleImpl.setStyleProperty(prop, val, e); } return this; } @@ -916,6 +856,7 @@ public class GQuery implements Lazy { * * @param clz return type class literal */ + @SuppressWarnings("unchecked") public T data(String name, Class clz) { return (T) data(elements.getItem(0), name, null); } @@ -1782,7 +1723,7 @@ public class GQuery implements Lazy { public void restoreCssAttrs(String[] cssProps) { for (Element e : elements()) { for (String a : cssProps) { - setStyleProperty(a, (String) data(e, "old-" + a, null), e); + styleImpl.setStyleProperty(a, (String) data(e, "old-" + a, null), e); } } } diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/impl/DocumentStyleImpl.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/impl/DocumentStyleImpl.java index 64c2c0f5..7d87797c 100644 --- a/gwtquery-core/src/main/java/com/google/gwt/query/client/impl/DocumentStyleImpl.java +++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/impl/DocumentStyleImpl.java @@ -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; }-*/; + } diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/impl/DocumentStyleImplIE.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/impl/DocumentStyleImplIE.java index a8bbb23f..e3148bc9 100644 --- a/gwtquery-core/src/main/java/com/google/gwt/query/client/impl/DocumentStyleImplIE.java +++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/impl/DocumentStyleImplIE.java @@ -16,24 +16,38 @@ 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 -- 2.39.5