
<%
/*
<!--

    Copyright (C) 2003-2007 eXo Platform SAS.

    This program is free software; you can redistribute it and/or
    modify it under the terms of the GNU Affero General Public License
    as published by the Free Software Foundation; either version 3
    of the License, or (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, see<http://www.gnu.org/licenses/>.

-->
*/
%>
<%
  import org.exoplatform.portal.mop.user.UserNavigation;
  import org.exoplatform.portal.mop.user.UserNode;
  import org.exoplatform.portal.webui.util.Util;
  import org.exoplatform.portal.mop.Visibility;
  import org.exoplatform.webui.organization.OrganizationUtils;
  import org.exoplatform.platform.webui.NavigationURLUtils;
  
  import java.util.Collection;

  import org.exoplatform.web.application.JavascriptManager;
  import org.gatein.common.text.EntityEncoder;

  void renderPageNode(UserNode node, boolean flag) {
    if(!node.getVisibility().equals(Visibility.DISPLAYED)) {
      return;
    }
    UserNode selectedNode = uicomponent.getSelectedPageNode();
    def tabStyleNavigation = "";
    if(selectedNode != null && node.getURI() == selectedNode.getURI()) {
      tabStyleNavigation = "SelectedItem";
    }
    def hasChild = node.getChildrenCount() > 0;
    def clazz = "";
    if(hasChild) clazz = "ArrowIcon";
    def href = NavigationURLUtils.getURL(node);
    def icon = node.getIcon();
    if(icon == null) icon = "DefaultPageIcon";
    def toolong = (node.resolvedLabel.length() > 60);
    def label = (toolong ? node.resolvedLabel.substring(0, 57) + "..." : node.resolvedLabel);
    def title = "";
    if(toolong == true) {
      title = "title='$node.resolvedLabel'";
    } else {
      title = "";
    }
    print """
      <li class="MenuItem $clazz $tabStyleNavigation">
    """;
    if(node.pageRef != null) {
      print """<a class="ItemIcon $icon" href="$href" $title>$label</a>""";
    } else {
      print """<a class="ItemIcon $icon" href="#" $title>$label</a>""";
    }

    if(hasChild) {
      print """
        <ul class="MenuItemContainer" style="position: absolute; display:none">
      """;
		int j = 0;
        for(UserNode childNode : node.getChildren()) {
          /** PLF-971 : we do not separate between group and space navigation, these navigations are rendered in the same level
          /* this test is no more needed
          */
          //def isRender = uicomponent.isRender(node, childNode);
          //if (isRender) 
          renderPageNode(childNode, j++ %2 == 0);
        }
      print """
        </ul>
      """;
    }
    print """
      </li>
    """;
  }

    void renderGroupPageNavigation(UserNavigation navigation) {
    	def nodes = uicomponent.getUserNodes(navigation);
    	if(nodes == null || nodes.isEmpty()) return ;
    	def ownerId = navigation.getKey().getName();
    	String navTitle = _ctx.appRes("UIPageNavigation.label.titleBar") ;
    	navTitle = navTitle.replace("{0}", OrganizationUtils.getGroupLabel(ownerId));
        String spacesNavigations= navigation.getKey().getName();
        if(spacesNavigations.indexOf("spaces") > 0) {
                int idx =0;
                for(UserNode node : nodes) {
                    renderPageNodeGroup(node, idx++ %2 == 0 ) ;
                }
        }
    }
	void renderPageNodeGroup(UserNode node, boolean flag) {
	    if(!node.getVisibility().equals(Visibility.DISPLAYED)) {
	      return;
	    }
		UserNode selectedNode = uicomponent.getSelectedNode();
		String tabStyleNavigation = "";
		if(selectedNode != null && node.getURI() == selectedNode.getURI()) {
				tabStyleNavigation = "SelectedItem";
		}
		boolean hasChild = node.getChildrenCount() > 0;
		String clazz = "";
		if(hasChild) clazz = "ArrowIcon";
		String	href = NavigationURLUtils.getURL(node);

		String icon = node.getIcon();
		if(icon == null) icon = "DefaultPageIcon";
		boolean toolong = (node.resolvedLabel.length() > 60);
		String label = ( toolong ? node.resolvedLabel.substring(0, 57) + "..." : node.resolvedLabel);
		String title = "";
		if(toolong) title = "title='$node.resolvedLabel'";
		else title = "";
        EntityEncoder entityEncoder = EntityEncoder.FULL;
		label = entityEncoder.encode(label);
		print """
			<li class="MenuItem $clazz $tabStyleNavigation">
		""";
						if(node.pageRef != null) {
								print """<a class="ItemIcon $icon" href="$href" $title>$label</a>""";

						} else {
								print """<a class="ItemIcon $icon" href="#" $title>$label</a>""";
						}
		if(hasChild) {
			print """
				<ul class="MenuItemContainer" style="position: absolute; display:none">
			""" ;
				int idx = 0;
				for(UserNode child : node.getChildren()) {
		            if(!node.getVisibility().equals(Visibility.DISPLAYED)) {
		              continue;
		            }
					renderPageNode(child, idx++ % 2 == 0);
				}
			print """
				</ul>
			""" ;

		}
		print """
			</li>
		""" ;
	}

	if (uicomponent.renderSpacesLink()) {
    
    def spacesLink = NavigationURLUtils.getURLInCurrentPortal("spaces");
    def findSpacesLink = NavigationURLUtils.getURLInCurrentPortal("all-spaces");
%>

<ul class="UIUserToolBarGroupPortlet UIHorizontalTabs" id="$uicomponent.id">
  <li class="UITab NormalToolbarTab"><a class="EditorIcon TBIcon" href="<%= spacesLink %>"><%=_ctx.appRes("UIMySpacePlatformToolBarPortlet.label")%></a> <!-- Merge exoadmin:  navigation-->

  <ul style="display: none" class="MenuItemContainer">
    <% 
    
    def rcontext = _ctx.getRequestContext();
    def jsManager = rcontext.getJavascriptManager();
    jsManager.importJavascript('eXo.portal.UIPortalNavigation');
    jsManager.importJavascript('eXo.portal.UIAdminToolbar');
    jsManager.addCustomizedOnLoadScript('eXo.portal.UIAdminToolbar.onLoad("' + uicomponent.id + '");');
  
    def groupNavigations = uicomponent.getGroupNavigations();
    if (groupNavigations!= null && !groupNavigations.isEmpty()) {
    

      def MY_SPACES_MAX_NUMBER = 10;
      def mySpacesDisplayed = 0;
      for(navigation in groupNavigations) {
      	if (navigation.getKey().getName().indexOf("spaces") <0 || mySpacesDisplayed == -1){  
      		break;
      	}
      	if(mySpacesDisplayed < MY_SPACES_MAX_NUMBER){
      		mySpacesDisplayed += 1;
      		renderGroupPageNavigation(navigation);
      	}else{
      		mySpacesDisplayed = -1;
      		def moreSpacesLabel = _ctx.appRes("UIMySpacePlatformToolBarPortlet.more");
      		def moreSpacesLink = spacesLink;
      		print """
      			<li class="MenuItem">
      				""" ;
      				print """
      				<a href="$moreSpacesLink">$moreSpacesLabel</a>
      				""" ;
      				print """
      			</li>
      		""" ;
      	}
      } %>

    <% } %>
    <!-- FIN -->
    <li class="MenuItem"><a class="FindPeopleIcon" href="<%= findSpacesLink %>"><%=_ctx.appRes("UIMySpacePlatformToolBarPortlet.find")%></a></li>
  </ul>
  </li>
</ul>
<% } %>
