aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorManuel Carrasco <manolo@apache.org>2013-11-01 04:34:43 -0700
committerManuel Carrasco <manolo@apache.org>2013-11-01 04:34:43 -0700
commit318623ff6031d61981d54a616fc9ed4de6b3a46b (patch)
treed982df253d6e1c2dcee49c512af86eeccc37c291
parent4fcace21bc70be9ae19724e47daba5aee103dd46 (diff)
parent883ffe6cd477ab52234203b48bef98d1a3475454 (diff)
downloadgwtquery-318623ff6031d61981d54a616fc9ed4de6b3a46b.tar.gz
gwtquery-318623ff6031d61981d54a616fc9ed4de6b3a46b.zip
Merge pull request #227 from manolo/master
jquery.browser api implementation
-rw-r--r--gwtquery-core/src/main/java/com/google/gwt/query/Query.gwt.xml5
-rw-r--r--gwtquery-core/src/main/java/com/google/gwt/query/client/Browser.java89
-rw-r--r--gwtquery-core/src/main/java/com/google/gwt/query/client/GQuery.java20
-rw-r--r--gwtquery-core/src/main/java/com/google/gwt/query/rebind/BrowserGenerator.java80
-rw-r--r--gwtquery-core/src/main/java/com/google/gwt/query/rebind/LazyGenerator.java2
-rw-r--r--gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryCoreTestGwt.java13
6 files changed, 197 insertions, 12 deletions
diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/Query.gwt.xml b/gwtquery-core/src/main/java/com/google/gwt/query/Query.gwt.xml
index c71a4361..873e09ab 100644
--- a/gwtquery-core/src/main/java/com/google/gwt/query/Query.gwt.xml
+++ b/gwtquery-core/src/main/java/com/google/gwt/query/Query.gwt.xml
@@ -16,6 +16,11 @@
<module>
<inherits name='com.google.gwt.user.User'/>
+
+ <!-- Browser flags -->
+ <generate-with class="com.google.gwt.query.rebind.BrowserGenerator">
+ <when-type-assignable class="com.google.gwt.query.client.Browser"/>
+ </generate-with>
<!-- Json and Xml builders -->
<generate-with class="com.google.gwt.query.rebind.JsonBuilderGenerator">
diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/Browser.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/Browser.java
new file mode 100644
index 00000000..437b6ae9
--- /dev/null
+++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/Browser.java
@@ -0,0 +1,89 @@
+/*
+ * Copyright 2011, The gwtquery team.
+ *
+ * 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 com.google.gwt.query.rebind.BrowserGenerator;
+
+/**
+ * This class is the equivalent to the jQuery.browser object in gQuery.
+ *
+ * The implementation is performend by the {@link BrowserGenerator}
+ *
+ * It can be used as a way of deferred-binding without modifying .gwt.xml files,
+ * taking advantage of compiler optimizations which will or will not include the
+ * code in a 'if' statement checking these conditions.
+ *
+ * Example:
+ * <pre>
+ if (GQuery.browser.ie6) {
+ // this code will be removed on non-ie6 permutations
+ Window.alert("IE6");
+ } else if (!browser.webkit) {
+ // this code will be only in the webkit permutation
+ Window.alert("NOT WEBKIT");
+ }
+ * </pre>
+ *
+ */
+public abstract class Browser {
+
+ /**
+ * Not in jquery, but useful in GWT for deferred bindings.
+ * @return true if ie6
+ */
+ public final boolean ie6 = isIe6();
+ /**
+ * Not in jquery, but useful in GWT for deferred bindings.
+ * @return true if ie8
+ */
+ public final boolean ie8 = isIe8();
+ /**
+ * Not in jquery, but useful in GWT for deferred bindings.
+ * @return true if ie9
+ */
+ public final boolean ie9 = isIe9();
+ /**
+ * @return true if Firefox
+ */
+ public final boolean mozilla = isMozilla();
+ /**
+ * @return true if any IE
+ */
+ public final boolean msie = isMsie();
+ /**
+ * @return true if opera
+ */
+ public final boolean opera = isOpera();
+ /**
+ * Maintained for jQuery compatibility
+ * @return true if webkit
+ * @deprecated use webkit() instead
+ */
+ @Deprecated
+ public final boolean safari = isWebkit();
+ /**
+ * @return true if webkit
+ */
+ public final boolean webkit = isWebkit();
+
+ protected abstract boolean isIe6();
+ protected abstract boolean isIe8();
+ protected abstract boolean isIe9();
+ protected abstract boolean isMozilla();
+ protected abstract boolean isMsie();
+ protected abstract boolean isOpera();
+ protected abstract boolean isWebkit();
+}
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 6c179aaf..1ef33fbc 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
@@ -13,7 +13,7 @@
*/
package com.google.gwt.query.client;
-import static com.google.gwt.query.client.plugins.QueuePlugin.Queue;
+import static com.google.gwt.query.client.plugins.QueuePlugin.*;
import java.util.ArrayList;
import java.util.Arrays;
@@ -25,18 +25,9 @@ import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.core.client.JsArray;
import com.google.gwt.core.client.JsArrayMixed;
import com.google.gwt.core.client.JsArrayString;
-import com.google.gwt.dom.client.BodyElement;
-import com.google.gwt.dom.client.ButtonElement;
-import com.google.gwt.dom.client.Document;
-import com.google.gwt.dom.client.Element;
-import com.google.gwt.dom.client.InputElement;
-import com.google.gwt.dom.client.Node;
-import com.google.gwt.dom.client.NodeList;
-import com.google.gwt.dom.client.OptionElement;
-import com.google.gwt.dom.client.SelectElement;
+import com.google.gwt.dom.client.*;
import com.google.gwt.dom.client.Style.Display;
import com.google.gwt.dom.client.Style.HasCssName;
-import com.google.gwt.dom.client.TextAreaElement;
import com.google.gwt.query.client.css.CSS;
import com.google.gwt.query.client.css.HasCssValue;
import com.google.gwt.query.client.css.TakesCssValue;
@@ -123,6 +114,11 @@ public class GQuery implements Lazy<GQuery, LazyGQuery> {
* The body element in the current page.
*/
public static final BodyElement body = GWT.isClient() ? Document.get().getBody() : null;
+
+ /**
+ * A Browser object with a set of browser-specific flags.
+ */
+ public static final Browser browser = GWT.isClient() ? GWT.<Browser>create(Browser.class) : null;
/**
* Object to store element data (public so as we can access to it from tests).
@@ -133,7 +129,7 @@ public class GQuery implements Lazy<GQuery, LazyGQuery> {
* The document element in the current page.
*/
public static final Document document = GWT.isClient() ? Document.get() : null;
-
+
/**
* Static reference Effects plugin
*/
diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/rebind/BrowserGenerator.java b/gwtquery-core/src/main/java/com/google/gwt/query/rebind/BrowserGenerator.java
new file mode 100644
index 00000000..075c4da4
--- /dev/null
+++ b/gwtquery-core/src/main/java/com/google/gwt/query/rebind/BrowserGenerator.java
@@ -0,0 +1,80 @@
+/*
+ * Copyright 2011, The gwtquery team.
+ *
+ * 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.rebind;
+
+import java.io.PrintWriter;
+
+import com.google.gwt.core.ext.BadPropertyValueException;
+import com.google.gwt.core.ext.Generator;
+import com.google.gwt.core.ext.GeneratorContext;
+import com.google.gwt.core.ext.PropertyOracle;
+import com.google.gwt.core.ext.TreeLogger;
+import com.google.gwt.core.ext.UnableToCompleteException;
+import com.google.gwt.core.ext.typeinfo.JClassType;
+import com.google.gwt.core.ext.typeinfo.TypeOracle;
+import com.google.gwt.user.rebind.ClassSourceFileComposerFactory;
+import com.google.gwt.user.rebind.SourceWriter;
+
+/**
+ * Creates an implementation for {@link Browser}
+ */
+public class BrowserGenerator extends Generator {
+ @Override
+ public String generate(TreeLogger logger, GeneratorContext context, String typeName) throws UnableToCompleteException {
+ TypeOracle oracle = context.getTypeOracle();
+ PropertyOracle propOracle = context.getPropertyOracle();
+
+ String ua = null;
+ try {
+ ua = propOracle.getSelectionProperty(logger, "user.agent").getCurrentValue();
+ } catch (BadPropertyValueException e) {
+ logger.log(TreeLogger.ERROR, "Can not resolve user.agent property", e);
+ throw new UnableToCompleteException();
+ }
+
+ JClassType clz = oracle.findType(typeName);
+ String pName = clz.getPackage().getName();
+ String cName = clz.getName() + "_" + ua;
+
+ PrintWriter pWriter = context.tryCreate(logger, pName, cName);
+
+ ClassSourceFileComposerFactory cFact = new ClassSourceFileComposerFactory(pName, cName);
+ cFact.setSuperclass(pName + "." + clz.getName());
+
+ SourceWriter writer = cFact.createSourceWriter(context, pWriter);
+
+ writer.println("protected boolean isWebkit() {return " + "safari".equals(ua) + ";}");
+ writer.println("protected boolean isSafari() {return " + "safari".equals(ua) + ";}");
+ writer.println("protected boolean isOpera() {return " + "opera".equals(ua) + ";}");
+ writer.println("protected boolean isMozilla() {return " + ua.contains("gecko") + ";}");
+ writer.println("protected boolean isMsie() {return " + ua.contains("ie") + ";}");
+ writer.println("protected boolean isIe6() {return " + "ie6".equals(ua) + ";}");
+ writer.println("protected boolean isIe8() {return " + "ie8".equals(ua) + ";}");
+ writer.println("protected boolean isIe9() {return " + "ie9".equals(ua) + ";}");
+ writer.println("public String toString() {return \"Browser:\"" +
+ " + \" webkit=\" + webkit" +
+ " + \" mozilla=\" + mozilla" +
+ " + \" opera=\" + opera" +
+ " + \" msie=\" + msie" +
+ " + \" ie6=\" + ie6" +
+ " + \" ie8=\" + ie8" +
+ " + \" ie9=\" + ie9" +
+ ";}");
+ writer.commit(logger);
+
+ return pName + "." + cName;
+ }
+}
diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/rebind/LazyGenerator.java b/gwtquery-core/src/main/java/com/google/gwt/query/rebind/LazyGenerator.java
index 504fb221..8063b79e 100644
--- a/gwtquery-core/src/main/java/com/google/gwt/query/rebind/LazyGenerator.java
+++ b/gwtquery-core/src/main/java/com/google/gwt/query/rebind/LazyGenerator.java
@@ -63,6 +63,8 @@ public class LazyGenerator extends Generator {
break;
}
}
+
+ if (targetType == null) return null;
assert targetType != null : "Parameter of Lazy<T> not found";
String genClass = targetType.getPackage().getName() + "."
diff --git a/gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryCoreTestGwt.java b/gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryCoreTestGwt.java
index 20809f2a..d6d824c2 100644
--- a/gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryCoreTestGwt.java
+++ b/gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryCoreTestGwt.java
@@ -50,6 +50,8 @@ import java.util.Collections;
import java.util.Comparator;
import java.util.List;
+import javax.validation.constraints.AssertFalse;
+
import static com.google.gwt.query.client.GQuery.$;
import static com.google.gwt.query.client.GQuery.$$;
import static com.google.gwt.query.client.GQuery.document;
@@ -100,6 +102,17 @@ public class GQueryCoreTestGwt extends GWTTestCase {
e.setInnerHTML("");
}
}
+
+ @DoNotRunWith({Platform.Prod})
+ public void testBrowser() {
+ assertTrue(GQuery.browser.mozilla);
+ assertFalse(GQuery.browser.webkit);
+ assertFalse(GQuery.browser.opera);
+ assertFalse(GQuery.browser.msie);
+ assertFalse(GQuery.browser.ie6);
+ assertFalse(GQuery.browser.ie8);
+ assertFalse(GQuery.browser.ie9);
+ }
public void testAttributeMethods() {