From d9acaee92941a6d99a5af457d170368e595a3af6 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Manuel=20Carrasco=20Mo=C3=B1ino?= Date: Fri, 1 Nov 2013 12:09:08 +0100 Subject: [PATCH] Implementation of the jQuery.browser object, in a GWT way which allows developer dont have to modify .gwt.xml file to do defferred binding --- .../java/com/google/gwt/query/Query.gwt.xml | 5 ++ .../com/google/gwt/query/client/Browser.java | 89 +++++++++++++++++++ .../com/google/gwt/query/client/GQuery.java | 20 ++--- .../gwt/query/client/GQueryCoreTestGwt.java | 13 +++ 4 files changed, 115 insertions(+), 12 deletions(-) create mode 100644 gwtquery-core/src/main/java/com/google/gwt/query/client/Browser.java 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 @@ + + + + + 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: + *
+      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");
+      } 
+ * 
+ * + */ +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 { * 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.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 { * 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/test/java/com/google/gwt/query/client/GQueryCoreTestGwt.java b/gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryCoreTestGwt.java index 20809f2a..99dfec8e 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.webkit); + 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() { -- 2.39.5