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
}\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
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
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
}\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
}-*/;\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
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
* 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
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
* 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
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
return new GQuery(unique(result));\r
}\r
\r
- public LazyGQuery createLazy() {\r
+ public LazyGQuery<?> createLazy() {\r
return GWT.create(GQuery.class);\r
}\r
\r
* 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
*\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
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
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;
/**
*/
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;
}-*/;
+
}
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)) {
}
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();
style.left = left;
elem.runtimeStyle.left = rsLeft;
}
- return ret;
+ return ret;
}-*/;
}
\ No newline at end of file