예제 #1
0
파일: A.java 프로젝트: femto/scooter
  /**
   * Returns an ajax-url link on a label. If the url is empty, simply the label is returned. The
   * result of ajax request is displayed at a place denoted by <tt>targetElementId</tt> which is an
   * id field in an html element.
   *
   * <p>The <tt>linkProperties</tt> map contains name and value pairs of options.
   *
   * <p>Supported linkProperties are html and css key attributes--see the <tt>linkKeys</tt> section
   * of the description of this class.
   *
   * <p><tt>responseHandlers</tt> must be of JSON's object format. According to
   * <tt>http://json.org</tt>, an object is an unordered set of name/value pairs. An object begins
   * with { (left brace) and ends with } (right brace). Each name is followed by : (colon) and the
   * name/value pairs are separated by , (comma).
   *
   * <p><tt>method</tt> must be either <tt>null</tt> or <tt>GET</tt> or <tt>POST</tt>. No other
   * value is allowed. Default value is <tt>GET</tt>.
   *
   * <p><tt>responseType</tt> must be either <tt>null</tt> or <tt>TEXT</tt> or <tt>XML</tt>. No
   * other value is allowed. Default value is <tt>TEXT</tt>.
   *
   * <pre>
   * Examples:
   *      labelLink("output", "show post #1", "/blog/posts/1")
   *      result link: <a href="#" onclick="ajax_link('output', '/blog/posts/1'); return false;">show post #1</a>
   * </pre>
   *
   * @param targetElementId a view element id
   * @param label link label
   * @param url url or uri string
   * @param linkProperties map of link related properties
   * @param method request http method, either GET(default) or POST
   * @param responseHandlers a string of json object format
   * @param responseType type of expected response
   * @return http link string
   */
  public static String labelLink(
      String targetElementId,
      String label,
      String url,
      Map linkProperties,
      String method,
      String responseHandlers,
      String responseType) {
    if (url == null || "".equals(url)) return label;

    if (method != null && !"GET".equalsIgnoreCase(method) && !"POST".equalsIgnoreCase(method)) {
      throw new IllegalArgumentException(
          "method should be either GET or POST, not '" + method + "'.");
    }

    if (responseType != null && !"TEXT".equals(responseType) && !"XML".equals(responseType)) {
      throw new IllegalArgumentException(
          "responseType should be either TEXT or XML, not '" + responseType + "'.");
    }

    if (responseType == null) responseType = "TEXT";

    String ajaxFunctionName = "ajax_link";
    if ("TEXT".equals(responseType)) ajaxFunctionName = "ajax_link4text";
    if ("XML".equals(responseType)) ajaxFunctionName = "ajax_link4xml";

    StringBuffer sb = new StringBuffer();
    sb.append("<a href=\"#\" onclick=\"" + ajaxFunctionName + "('");

    sb.append(targetElementId).append("', '").append(url).append("'");

    if (method != null) {
      sb.append(", '").append(method).append("'");
    } else {
      sb.append(", undefined");
    }

    if (responseHandlers != null) {
      if (!responseHandlers.startsWith("{") || !responseHandlers.endsWith("}"))
        throw new IllegalArgumentException("responseHandlers should be of JSON's object format.");
      sb.append(", ").append(responseHandlers).append("");
    } else {
      sb.append(", undefined");
    }
    sb.append(")");
    sb.append("; return false;\" ").append(W.convertLinkPropertiesToString(linkProperties));
    sb.append(">").append(label).append("</a>");
    return sb.toString();
  }