Allow UiActions to perform redirects without JavaScript
This is a simple approach to enable a GET based UiAction to send a redirect to the browser to either replace Gerrit or open a new window without writing JavaScript. Change-Id: I38ca4bbad1ccf4a9ff376d17243b9d445abed537
This commit is contained in:
		@@ -0,0 +1,59 @@
 | 
			
		||||
// Copyright (C) 2014 The Android Open Source Project
 | 
			
		||||
//
 | 
			
		||||
// 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.gerrit.extensions.webui;
 | 
			
		||||
 | 
			
		||||
import java.net.URI;
 | 
			
		||||
 | 
			
		||||
/** Default result for {@link UiAction}s with no JavaScript. */
 | 
			
		||||
public class UiResult {
 | 
			
		||||
  /** Display an alert message to the user. */
 | 
			
		||||
  public static UiResult alert(String message) {
 | 
			
		||||
    UiResult r = new UiResult();
 | 
			
		||||
    r.alert = message;
 | 
			
		||||
    return r;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  /** Launch URL in a new window. */
 | 
			
		||||
  public static UiResult openUrl(URI uri) {
 | 
			
		||||
    return openUrl(uri.toString());
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  /** Launch URL in a new window. */
 | 
			
		||||
  public static UiResult openUrl(String url) {
 | 
			
		||||
    UiResult r = new UiResult();
 | 
			
		||||
    r.url = url;
 | 
			
		||||
    r.openWindow = true;
 | 
			
		||||
    return r;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  /** Redirect the browser to a new URL. */
 | 
			
		||||
  public static UiResult redirectUrl(String url) {
 | 
			
		||||
    UiResult r = new UiResult();
 | 
			
		||||
    r.url = url;
 | 
			
		||||
    return r;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  /** Alert the user with a message. */
 | 
			
		||||
  protected String alert;
 | 
			
		||||
 | 
			
		||||
  /** If present redirect browser to this URL. */
 | 
			
		||||
  protected String url;
 | 
			
		||||
 | 
			
		||||
  /** When true open {@link #url} in a new tab/window. */
 | 
			
		||||
  protected Boolean openWindow;
 | 
			
		||||
 | 
			
		||||
  private UiResult() {
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
@@ -24,6 +24,7 @@ import com.google.gerrit.common.PageLinks;
 | 
			
		||||
import com.google.gerrit.reviewdb.client.Project;
 | 
			
		||||
import com.google.gwt.core.client.JavaScriptObject;
 | 
			
		||||
import com.google.gwt.user.client.Window;
 | 
			
		||||
import com.google.gwt.user.client.Window.Location;
 | 
			
		||||
import com.google.gwt.user.client.rpc.AsyncCallback;
 | 
			
		||||
 | 
			
		||||
class DefaultActions {
 | 
			
		||||
@@ -38,14 +39,27 @@ class DefaultActions {
 | 
			
		||||
  private static AsyncCallback<JavaScriptObject> callback(final String target) {
 | 
			
		||||
    return new GerritCallback<JavaScriptObject>() {
 | 
			
		||||
      @Override
 | 
			
		||||
      public void onSuccess(JavaScriptObject msg) {
 | 
			
		||||
        if (NativeString.is(msg)) {
 | 
			
		||||
          NativeString str = (NativeString) msg;
 | 
			
		||||
          if (!str.asString().isEmpty()) {
 | 
			
		||||
            Window.alert(str.asString());
 | 
			
		||||
          }
 | 
			
		||||
      public void onSuccess(JavaScriptObject in) {
 | 
			
		||||
        UiResult result = asUiResult(in);
 | 
			
		||||
        if (result.alert() != null) {
 | 
			
		||||
          Window.alert(result.alert());
 | 
			
		||||
        }
 | 
			
		||||
        Gerrit.display(target);
 | 
			
		||||
 | 
			
		||||
        if (result.redirectUrl() != null && result.openWindow()) {
 | 
			
		||||
          Window.open(result.redirectUrl(), "_blank", null);
 | 
			
		||||
        } else if (result.redirectUrl() != null) {
 | 
			
		||||
          Location.assign(result.redirectUrl());
 | 
			
		||||
        } else {
 | 
			
		||||
          Gerrit.display(target);
 | 
			
		||||
        }
 | 
			
		||||
      }
 | 
			
		||||
 | 
			
		||||
      private UiResult asUiResult(JavaScriptObject in) {
 | 
			
		||||
        if (NativeString.is(in)) {
 | 
			
		||||
          String str = ((NativeString) in).asString();
 | 
			
		||||
          return str.isEmpty() ? UiResult.none() : UiResult.alert(str);
 | 
			
		||||
        }
 | 
			
		||||
        return in.cast();
 | 
			
		||||
      }
 | 
			
		||||
    };
 | 
			
		||||
  }
 | 
			
		||||
@@ -65,4 +79,16 @@ class DefaultActions {
 | 
			
		||||
 | 
			
		||||
  private DefaultActions() {
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  private static class UiResult extends JavaScriptObject {
 | 
			
		||||
    static native UiResult alert(String m) /*-{ return {'alert':m} }-*/;
 | 
			
		||||
    static native UiResult none() /*-{ return {} }-*/;
 | 
			
		||||
 | 
			
		||||
    final native String alert() /*-{ return this.alert }-*/;
 | 
			
		||||
    final native String redirectUrl() /*-{ return this.url }-*/;
 | 
			
		||||
    final native boolean openWindow() /*-{ return this.open_window || false }-*/;
 | 
			
		||||
 | 
			
		||||
    protected UiResult() {
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user