From 4a9488aca04b5212f784e848cc9e2d1083fbe31c Mon Sep 17 00:00:00 2001 From: =?utf8?q?Manuel=20Carrasco=20Mo=C3=B1ino?= Date: Thu, 5 Dec 2013 17:46:28 +0100 Subject: [PATCH] Implementation of GQuery.console --- .../java/com/google/gwt/query/Query.gwt.xml | 5 + .../com/google/gwt/query/QueryMin.gwt.xml | 5 + .../com/google/gwt/query/client/Console.java | 96 ++++++++ .../com/google/gwt/query/client/GQuery.java | 12 +- .../gwt/query/client/impl/ConsoleBrowser.java | 205 ++++++++++++++++++ .../google/gwt/query/client/js/JsUtils.java | 12 - 6 files changed, 316 insertions(+), 19 deletions(-) create mode 100644 gwtquery-core/src/main/java/com/google/gwt/query/client/Console.java create mode 100644 gwtquery-core/src/main/java/com/google/gwt/query/client/impl/ConsoleBrowser.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 a902fd99..520ceaa0 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 @@ -22,6 +22,11 @@ + + + + + diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/QueryMin.gwt.xml b/gwtquery-core/src/main/java/com/google/gwt/query/QueryMin.gwt.xml index c67ec2af..6453aba3 100644 --- a/gwtquery-core/src/main/java/com/google/gwt/query/QueryMin.gwt.xml +++ b/gwtquery-core/src/main/java/com/google/gwt/query/QueryMin.gwt.xml @@ -52,6 +52,11 @@ + + + + + diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/Console.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/Console.java new file mode 100644 index 00000000..66709908 --- /dev/null +++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/Console.java @@ -0,0 +1,96 @@ +/* + * Copyright 2013, 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; + + +/** + * This interface represents the Window.console object. + */ +public interface Console { + + /** + * Displays an interactive listing of the properties of a specified JavaScript object. This + * listing lets you use disclosure triangles to examine the contents of child objects. + */ + void dir(Object arg); + + /** + * Outputs an error message. You may use string substitution and additional arguments with this method. + */ + void error(Object arg); + + /** + * Creates a new inline group, indenting all following output by another level. To move back out a + * level, call groupEnd(). + */ + void group(Object arg); + + /** + * Creates a new inline group, indenting all following output by another level; unlike group(), + * this starts with the inline group collapsed, requiring the use of a disclosure button to expand + * it. To move back out a level, call groupEnd(). + */ + void groupCollapsed(Object arg); + + /** + * Exits the current inline group. + */ + void groupEnd(); + + /** + * Informative logging information. You may use string substitution and additional arguments with + * this method. + */ + void info(Object arg); + + /** + * For general output of logging information. You may use string substitution and additional + * arguments with this method. + */ + void log(Object arg); + + /** + * Starts a JavaScript CPU profile with a name. To complete the profile, call console.profileEnd(). + */ + void profile(String title); + + /** + * Stops the current JavaScript CPU profiling session, if one is in progress, and prints the report. + */ + void profileEnd(String title); + + /** + * Starts a timer with a name specified as an input parameter. Up to 10,000 simultaneous timers + * can run on a given page. + */ + void time(String title); + + /** + * Stops the specified timer and logs the elapsed time in seconds since its start. + */ + void timeEnd(String title); + + /** + * Outputs a timestamp. + */ + void timeStamp(Object arg); + + /** + * Outputs a warning message. You may use string substitution and additional arguments with this + * method. + */ + void warn(Object arg); +} 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 75d0c18b..e4f76a35 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 @@ -122,6 +122,11 @@ public class GQuery implements Lazy { */ public static final Browser browser = GWT.isClient() ? GWT.create(Browser.class) : null; + /** + * An object with the same methods than window.console. + */ + public static final Console console = GWT.isClient() ? GWT.create(Console.class) : null; + /** * Object to store element data (public so as we can access to it from tests). */ @@ -702,13 +707,6 @@ public class GQuery implements Lazy { return $().createLazy(); } - /** - * Dump an object to the window.console.log when available - */ - public static void log(Object o) { - JsUtils.log(o); - } - /** * Perform an ajax request to the server using POST. */ diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/impl/ConsoleBrowser.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/impl/ConsoleBrowser.java new file mode 100644 index 00000000..6f67fd95 --- /dev/null +++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/impl/ConsoleBrowser.java @@ -0,0 +1,205 @@ +/* + * Copyright 2013, 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.impl; + +import com.google.gwt.core.client.JavaScriptObject; +import com.google.gwt.query.client.Console; +import com.google.gwt.query.client.GQuery; + +/** + * Implementation of the Console interface based on the + * browser console. + */ +public class ConsoleBrowser implements Console { + + /** + * http://whattheheadsaid.com/2011/04/internet-explorer-9s-problematic-console-object + */ + private static class ConsoleIe8 extends ConsoleIe9 { + @Override + protected native void init()/*-{ + Function.prototype.call.call($wnd.console.log, $wnd.console, Array.prototype.slice.call(arguments)); + }-*/; + } + + /** + * See: http://whattheheadsaid.com/2011/04/internet-explorer-9s-problematic-console-object + */ + private static class ConsoleIe9 extends ConsoleImpl { + public ConsoleIe9(){ + init(); + } + + protected native void init()/*-{ + [ "log", "info", "warn", "error", "dir", "clear", "profile", "profileEnd" ] + .forEach(function(method) { + $wnd.console[method] = this.call($wnd.console[method], $wnd.console); + }, Function.prototype.bind); + }-*/; + + @Override + public void group(Object arg) {} + @Override + public void groupCollapsed(Object arg) {} + @Override + public void groupEnd() {} + @Override + public void time(String title) {} + @Override + public void timeStamp(Object arg) {} + @Override + public void timeEnd(String title) {} + } + + /** + * Default implementation: webkit, opera, FF, ie10 + */ + private static class ConsoleImpl { + public native void dir(Object arg) /*-{ + $wnd.console.dir(arg); + }-*/; + + public native void error(Object arg) /*-{ + $wnd.console.error(arg); + }-*/; + + public native void group(Object arg) /*-{ + $wnd.console.group(arg); + }-*/; + + public native void groupCollapsed(Object arg) /*-{ + $wnd.console.groupCollapsed(arg); + }-*/; + + public native void groupEnd() /*-{ + $wnd.console.groupEnd(); + }-*/; + + public native void info(Object arg) /*-{ + $wnd.console.info(arg); + }-*/; + + public native void log(Object arg) /*-{ + $wnd.console.log(arg); + }-*/; + + public native void profile(String title) /*-{ + $wnd.console.profile(title); + }-*/; + + public native void profileEnd(String title) /*-{ + $wnd.console.profileEnd(title); + }-*/; + + public native void time(String title) /*-{ + $wnd.console.time(title); + }-*/; + + public native void timeEnd(String title) /*-{ + $wnd.console.timeEnd(title); + }-*/; + + public native void timeStamp(Object arg) /*-{ + $wnd.console.timeStamp(arg); + }-*/; + + public native void warn(Object arg) /*-{ + $wnd.console.warn(arg); + }-*/; + } + + private ConsoleImpl impl; + + public ConsoleBrowser() { + impl = GQuery.browser.ie8? new ConsoleIe8(): GQuery.browser.ie9? new ConsoleIe9(): new ConsoleImpl(); + } + + @Override + public void dir(Object arg) { + impl.dir(toJs(arg)); + } + + @Override + public void error(Object arg) { + impl.error(toJs(arg)); + } + + @Override + public void group(Object arg) { + impl.group(toJs(arg)); + } + + @Override + public void groupCollapsed(Object arg) { + impl.groupCollapsed(toJs(arg)); + } + + @Override + public void groupEnd() { + impl.groupEnd(); + } + + @Override + public void info(Object arg) { + impl.info(toJs(arg)); + } + + @Override + public void log(Object arg) { + impl.log(toJs(arg)); + } + + @Override + public void profile(String title) { + impl.profile(title); + } + + @Override + public void profileEnd(String title) { + impl.profileEnd(title); + } + + @Override + public void time(String title) { + impl.time(title); + } + + @Override + public void timeEnd(String title) { + impl.timeEnd(title); + } + + @Override + public void timeStamp(Object arg) { + impl.timeStamp(toJs(arg)); + } + + @Override + public void warn(Object arg) { + impl.warn(toJs(arg)); + } + + /** + * Don't pass GWT Objects to JS methods + */ + private Object toJs(Object arg) { + if (arg instanceof JavaScriptObject || arg instanceof String) { + return arg; + } else { + return String.valueOf(arg); + } + } +} diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/js/JsUtils.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/js/JsUtils.java index 9790b12f..ecd61d22 100644 --- a/gwtquery-core/src/main/java/com/google/gwt/query/client/js/JsUtils.java +++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/js/JsUtils.java @@ -26,7 +26,6 @@ import com.google.gwt.dom.client.NodeList; import com.google.gwt.query.client.Function; import com.google.gwt.query.client.GQuery; import com.google.gwt.query.client.Properties; -import com.google.gwt.query.client.plugins.ajax.Ajax; import com.google.gwt.user.client.Command; import com.google.gwt.user.client.DOM; @@ -520,15 +519,4 @@ public class JsUtils { } return ret; } - - /** - * Dump an object to the browser console. - * - * TODO: enable console in IE8 && IE9 - * http://snipplr.com/view/67830/ - */ - public static native void log(Object o) /*-{ - if ($wnd.console && typeof $wnd.console.log === 'function') $wnd.console.log('' + o); - }-*/; - } -- 2.39.5