Terry.Li-彬

虚其心,可解天下之问;专其心,可治天下之学;静其心,可悟天下之理;恒其心,可成天下之业。

  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  143 随笔 :: 344 文章 :: 130 评论 :: 0 Trackbacks
 

前面分析了在页面上增加一个portlet的服务器端的具体实现,现在分析其在浏览器端的实现,JavaScript要出场了。
前面提到在Add Content中点击某个portlet的"Add"按钮,其实就是调用了addPortlet(...)方法。

top_js.jspf
------------------

function addPortlet(plid, portletId, doAsUserId) {

  var refreshPortletList = getRefreshPortletList();

  if (refreshPortletList["_" + portletId]) {
    self.location = "<%= themeDisplay.getPathMain() %>/portal/update_layout?p_l_id=" + plid + "&p_p_id=" + portletId + "&doAsUserId=" + doAsUserId + "&<%=

Constants.CMD %>=<%= Constants.ADD %>&referer=<%= HttpUtil.encodeURL(currentURL) %>&refresh=1";
  }
 
  else {
    var loadingDiv = document.createElement("div");
    var container = document.getElementById("layout-column_column-1");

    if (container == null) {
      return;
    }

    loadingDiv.className = "loading-animation";

    container.appendChild(loadingDiv);

    var queryString = "<%= themeDisplay.getPathMain() %>/portal/update_layout?p_l_id=" + plid + "&p_p_id=" + portletId + "&doAsUserId=" + doAsUserId + "&<%=

Constants.CMD %>=<%= Constants.ADD %>&currentURL=<%= HttpUtil.encodeURL(currentURL) %>";

    /* queryString = /c/portal/update_layout?p_l_id=10102&p_p_id=reports&doAsUserId=&cmd=add&currentURL=%2Fweb%2Fguest%2Fhome */
    /* 这就是最终发送往服务器端的数据了,刚好可以与前面所分析的服务器端收到/c/portal/update_layout后的处理流程对应起来了。*/

    addPortletHTML(queryString, loadingDiv);
  }
}


portlet_js.jspf
--------------------

function getRefreshPortletList() {

  <%
  List allPortlets = PortletLocalServiceUtil.getPortlets(company.getCompanyId(), false, false);
 
  List nonAjaxPortlets = new ArrayList();

  for (int i = 0; i < allPortlets.size(); i++) {
    Portlet portlet = (Portlet)allPortlets.get(i);

    if (!portlet.isAjaxable()) {
      nonAjaxPortlets.add(portlet.getPortletId());
    }
  }

  StringMaker portletList = new StringMaker();

  for (int i = 0; i < nonAjaxPortlets.size(); i++) {
    portletList.append("_");
    portletList.append(nonAjaxPortlets.get(i));
    portletList.append(":true");
    portletList.append((i < nonAjaxPortlets.size() - 1) ? "," : "");
  }
 %>

  var refreshPortletList = { <%= portletList.toString() %> };
  return refreshPortletList;
}

function addPortletHTML(url, placeHolder) {

  var addPortletReturn = function(html) {

    var container = placeHolder.parentNode;

    var addDiv = document.createElement("div");

    addDiv.style.display = "none";

    addDiv.innerHTML = html;

    var portletBound = jQuery(".portlet-boundary:first", addDiv).get(0);

    portletBound.parentNode.removeChild(portletBound);

    var portletId = portletBound.id;

    portletId = portletId.replace(/^p_p_id_/,"");

    portletId = portletId.replace(/_$/,"");

    portletBound.portletId = portletId;

    Liferay.Portlet.flagAjax(portletId);

    container.replaceChild(portletBound, placeHolder);

    Liferay.Util.evalScripts(addDiv);

    if (!jQuery.browser.firefox) {
      Liferay.Util.evalScripts(portletBound);
    }

    <c:if test="<%= (layout != null) && (LayoutPermissionUtil.contains(permissionChecker, layout, ActionKeys.UPDATE)) %>">
      if (!portletBound.isStatic) {
        if (themeDisplay.isFreeformLayout()) {
          Liferay.Freeform.add(portletBound);
        }
        else {
          Liferay.Columns.add(portletBound);
        }
      }
    </c:if>

    if (jQuery.browser.firefox) {
      setTimeout("Liferay.Portlet.process(\"" + portletId + "\")", 0);
    }
    else {
      Liferay.Portlet.process(portletId);
    }

    Liferay.Util.addInputType(portletBound.id);
    Liferay.Util.addInputFocus(portletBound.id);

    if (window.location.hash) {
      window.location.hash = "p_" + portletId;
    }
    return portletId;
  };

  /* addPortletHTML()生成一个Ajax Request,收到Ajax Response后调用addPortletReturn。*/
  AjaxUtil.request(url, {
  onComplete: function(xmlHttpReq){
   addPortletReturn(xmlHttpReq.responseText);
  }
  });
}

  /* xmlHttpReq.responseText的内容就是该portlet要嵌入portal中的的HTML代码片段,如下图所示。在经过addPortletReturn()处理后完成portlet的添加工作*/

 

Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=2177414

posted on 2008-03-14 14:14 礼物 阅读(735) 评论(0)  编辑  收藏 所属分类: Liferay