* See: http://whattheheadsaid.com/2011/04/internet-explorer-9s-problematic-console-object
*/
private static class ConsoleIe9 extends ConsoleImpl {
--
++
+ private boolean initialized = false;
-
++
public ConsoleIe9(){
init();
}
--
++
protected native void init()/*-{
try {
[ "log", "info", "warn", "error", "dir", "clear", "profile", "profileEnd" ]
.forEach(function(method) {
$wnd.console[method] = this.call($wnd.console[method], $wnd.console);
}, Function.prototype.bind);
+ this.@com.google.gwt.query.client.impl.ConsoleBrowser.ConsoleIe9::initialized = true;
} catch(e) {
- this.@com.google.gwt.query.client.impl.ConsoleBrowser.ConsoleIe9::initFallBack()();
- }
- }-*/;
-
- /**
- * Dummy implementation of console if IE8 or IE9 fail using dev tools.
- */
- private native void initFallBack() /*-{
- if (!$wnd.console || !$wnd.console.log) {
- $wnd.console = {};
- [ "log", "info", "warn", "error", "dir", "clear", "profile", "profileEnd" ]
- .forEach(function(method) {
- $wnd.console[method] = function(){};
- });
}
}-*/;
-
+
+ @Override
+ public void clear() {
+ if (initialized) super.clear();
+ }
+ @Override
+ public void dir(Object arg) {
+ if (initialized) super.dir(arg);
+ }
+ @Override
+ public void error(Object arg) {
+ if (initialized) super.error(arg);
+ }
+ @Override
+ public void info(Object arg) {
+ if (initialized) super.info(arg);
+ }
+ @Override
+ public void profile(String title) {
+ if (initialized) super.profile(title);
+ }
+ @Override
+ public void profileEnd(String title) {
+ if (initialized) super.profileEnd(title);
+ }
+ @Override
+ public void warn(Object arg) {
+ if (initialized) super.warn(arg);
+ }
@Override
public void group(Object arg) {}
@Override
@Override
public void timeEnd(String title) {}
}
--
++
/**
* Default implementation: webkit, opera, FF, ie10
*/
$wnd.console.warn(arg);
}-*/;
}
--
++
private ConsoleImpl impl;
--
++
public ConsoleBrowser() {
impl = GQuery.browser.ie8? new ConsoleIe8(): GQuery.browser.ie9? new ConsoleIe9(): new ConsoleImpl();
}
public void warn(Object arg) {
impl.warn(toJs(arg));
}
--
++
/**
* Don't pass GWT Objects to JS methods
*/
* <pre>
* RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, "http://127.0.0.1:8888/whatever");
* PromiseRequest gettingResponse = new PromiseReqBuilder(builder);
-- *
++ *
* gettingResponse.fail(new Function() {
* public void f() {
* Throwable exception = arguments(0);
* </pre>
*/
public class PromiseReqBuilder extends DeferredPromiseImpl implements RequestCallback {
--
++
public PromiseReqBuilder(RequestBuilder builder) {
builder.setCallback(this);
try {
onError(null, e);
}
}
--
++
/**
* Using this constructor we access to some things in the xmlHttpRequest
* which are not available in GWT, like adding progress handles or sending
* javascript data (like forms in modern html5 file api)
*/
- public PromiseReqBuilder(Settings settings, String httpMethod, String url, Object data) {
+ public PromiseReqBuilder(Settings settings) {
+ String httpMethod = settings.getType();
+ String url = settings.getUrl();
+ Binder data = settings.getData();
+ String ctype = settings.getContentType();
+ Boolean isFormData = data != null && data.getBound() instanceof JavaScriptObject && JsUtils.isFormData(data.<JavaScriptObject>getBound());
-
++
XMLHttpRequest xmlHttpRequest = XMLHttpRequest.create();
try {
if (settings.getUsername() != null && settings.getPassword() != null) {
onError(null, e);
return;
}
--
++
JsUtils.prop(xmlHttpRequest, "onprogress", JsUtils.wrapFunction(new Function() {
public void f() {
JsCache p = arguments(0);
dfd.notify(total, loaded, percent, "download");
}
}));
--
++
JavaScriptObject upload = JsUtils.prop(xmlHttpRequest, "upload");
JsUtils.prop(upload, "onprogress", JsUtils.wrapFunction(new Function() {
public void f() {
dfd.notify(total, loaded, percent, "upload");
}
}));
--
- Properties headers = settings.getHeaders();
++
+ Binder headers = settings.getHeaders();
if (headers != null) {
- for (String headerKey : headers.keys()) {
- xmlHttpRequest.setRequestHeader(headerKey, headers.getStr(headerKey));
+ for (String headerKey : headers.getFieldNames()) {
+ xmlHttpRequest.setRequestHeader(headerKey, String.valueOf(headers.get(headerKey)));
}
}
--
- if (data != null && !"GET".equalsIgnoreCase(httpMethod)) {
- String ctype = settings.getContentType();
- if (data instanceof JavaScriptObject && JsUtils.isFormData((JavaScriptObject)data)) {
- ctype = FormPanel.ENCODING_MULTIPART;;
- } else if (ctype == null) {
- String type = settings.getDataType();
- if (type != null && type.toLowerCase().startsWith("json")) {
- ctype = "application/json; charset=utf-8";
- } else {
- ctype = FormPanel.ENCODING_URLENCODED;
- }
- }
++
+ if (data != null && !isFormData && !"GET".equalsIgnoreCase(httpMethod)) {
xmlHttpRequest.setRequestHeader("Content-Type", ctype);
}
// Using gQuery to set credentials since this method was added in 2.5.1
// xmlHttpRequest.setWithCredentials(true);
JsUtils.prop(xmlHttpRequest, "withCredentials", true);
--
++
final Request request = createRequestVltr(xmlHttpRequest, settings.getTimeout(), this);
--
++
xmlHttpRequest.setOnReadyStateChange(new ReadyStateChangeHandler() {
public void onReadyStateChange(XMLHttpRequest xhr) {
if (xhr.getReadyState() == XMLHttpRequest.DONE) {
dfd.resolve(response, request);
}
}
--
++
/**
* Using violator pattern to execute private method
*/
private native void fireOnResponseReceivedVltr(Request rq, RequestCallback cb) /*-{
rq.@com.google.gwt.http.client.Request::fireOnResponseReceived(Lcom/google/gwt/http/client/RequestCallback;)(cb);
}-*/;
--
++
/**
* Using violator pattern to use protected constructor
*/