<%
  import org.exoplatform.wiki.mow.api.Page;
  import org.exoplatform.container.PortalContainer;
  import org.exoplatform.web.application.JavascriptManager;
  import org.apache.commons.lang.StringEscapeUtils;
  
  def rcontext = _ctx.getRequestContext() ;
  def userName = rcontext.getRemoteUser();
  String viewLink = uicomponent.event(uicomponent.VIEW_PAGE_ACTION);
  String actionId = uicomponent.WIKI_PORTLET_ACTION_PREFIX + uicomponent.VIEW_PAGE_ACTION;
  String restContextName = PortalContainer.getInstance().getRestContextName();
  
  String changeModeLink= uicomponent.event(uicomponent.CHANGE_MODE_ACTION);
  String changeModeLinkId = uicomponent.WIKI_PORTLET_ACTION_PREFIX + uicomponent.CHANGE_MODE_ACTION; 
  JavascriptManager jsManager = rcontext.getJavascriptManager();
  def requireJs = jsManager.getRequireJS();
  
  requireJs.require("SHARED/jquery", "jQuery").addScripts("jQuery.noConflict();");
  requireJs.require("SHARED/WikiLayout", "WikiLayout");  
  requireJs.require("SHARED/UIWikiAjaxRequest", "UIWikiAjaxRequest"); 
  requireJs.require("SHARED/UIWikiPortlet", "UIWikiPortlet");
  requireJs.require("SHARED/UITreeExplorer", "UITreeExplorer");
  requireJs.require("SHARED/UIRelated", "UIRelated");
  
  if(uicomponent.isKeepSessionAlive()){
    requireJs.require("SHARED/UIWikiPageEditForm", "UIWikiPageEditForm");
    requireJs.require("SHARED/UIConfirmBox", "UIConfirmBox");
    requireJs.require("SHARED/UIWikiSavePage", "UIWikiSavePage");
  }  
  
  requireJs.addScripts("WikiLayout.init('" + uicomponent.id + "', '" + userName + "') ;") ; 
  requireJs.addScripts("UIWikiAjaxRequest.init('" + uicomponent.WIKI_PORTLET_ACTION_PREFIX + "', '" + uicomponent.VIEW_PAGE_ACTION + "');");
  requireJs.addScripts("UIWikiPortlet.init('"+uicomponent.id+ "', '" + changeModeLinkId + "');");
  requireJs.addScripts("UIWikiPortlet.keepSessionAlive(" + uicomponent.isKeepSessionAlive() + ");");
  requireJs.addScripts("UITreeExplorer.loading ='" + StringEscapeUtils.escapeJavaScript(_ctx.appRes("UITreeExplorer.label.loading")) +"' ;");
  
%>
<script type="text/javascript"> 
  if(!eXo.env.rest){
    eXo.env.rest = {};
  }
  eXo.env.rest.context = '$restContextName';
</script>

<% if(!rcontext.useAjax()) { %>
<!-- This script must to put here to ensure xwe.onScriptLoad is alway called. -->
<script type="text/javascript" src="/wiki/javascript/eXo/wiki/UIWikiWysiwygLoader.js"></script>
<!-- This script must to put here to avoid impact submit plugin -->
<script type="text/javascript" src="/wiki/javascript/prototype/prototype.js"></script>
<script type="text/javascript" src="/wiki/resources/js/xwiki/wysiwyg/xwe/sc/initsc.js"></script>
<script type="text/javascript" src="/wiki/resources/js/xwiki/wysiwyg/xwe/sc/modules/ISC_Core.js"></script>
<script type="text/javascript" src="/wiki/resources/js/xwiki/wysiwyg/xwe/sc/modules/ISC_Foundation.js"></script>
<script type="text/javascript" src="/wiki/resources/js/xwiki/wysiwyg/xwe/sc/modules/ISC_Containers.js"></script>
<script type="text/javascript" src="/wiki/resources/js/xwiki/wysiwyg/xwe/sc/modules/ISC_Grids.js"></script>
<script type="text/javascript" src="/wiki/resources/js/xwiki/wysiwyg/xwe/sc/modules/ISC_Forms.js"></script>
<script type="text/javascript" src="/wiki/resources/js/xwiki/wysiwyg/xwe/sc/modules/ISC_DataBinding.js"></script>
<script type="text/javascript" src="/wiki/javascript/xwiki/xwiki.js"></script>
<% } %>

<style type="text/css">
.wiki-body {
  overflow-x: hidden;
}
</style>

<!--  -->
<div class="uiWikiPortlet" id="$uicomponent.id">
  <div class="ParentMaskLayer">
    <div id="KSMaskLayer" class="KSMaskLayer"><span></span></div>
  </div>
  <% Page page = _ctx.getRequestContext().getAttribute("wikiPage");%>
  <%
    List children = uicomponent.getChildren() ;
    for(component in children) {
      if(component.isRendered()){
        uicomponent.renderChild(component) ;
      }
    }
  %> 
  <a onclick="$viewLink" id="$actionId" style="display:none;">&nbsp;</a>
  <a href="$changeModeLink" id="$changeModeLinkId" style="display:none;">&nbsp;</a>
</div>
