<%
  /**
   * Created by The eXo Platform SARL
   * Author : Nguyen Quang Trung
   *          trung.nguyen@exoplatform.com
   * October 20, 2009
   */
%>
<%
import java.net.URLEncoder;

import java.util.List;
import java.util.ArrayList;
import java.text.SimpleDateFormat;

import org.exoplatform.ecm.webui.utils.Utils ;
import org.exoplatform.web.application.Parameter ;
import org.exoplatform.webui.core.UIRightClickPopupMenu ;
import org.exoplatform.ecm.webui.component.explorer.UIWorkingArea;
import org.exoplatform.ecm.webui.component.explorer.UIJCRExplorer;
import org.exoplatform.services.jcr.util.Text;
import org.exoplatform.ecm.jcr.model.Preference;
import org.exoplatform.services.jcr.RepositoryService;

  def uiWorkingArea = uicomponent.getAncestorOfType(UIWorkingArea.class);
  def uiExplorer = uicomponent.getAncestorOfType(UIJCRExplorer.class);
  def pref = uiExplorer.getPreference();
  def enableDragAndDrop = pref.isEnableDragAndDrop();
  String componentId = uicomponent.getId();
  String serverUrl= uicomponent.getWebDAVServerPrefix();
  String portalName = uicomponent.getPortalName();
  RepositoryService rService = uicomponent.getApplicationComponent(RepositoryService.class);
  String repository = rService.getCurrentRepository().getConfiguration().getName();
  def viewComponentId  = new Date().getTime();
  def rcontext = _ctx.getRequestContext() ;
  UIRightClickPopupMenu contextMenu = uicomponent.getContextMenu();
  def managersMultiItemContext =  uiWorkingArea.getMultiActionsExtensionList();
  def managersGroundContext = uiWorkingArea.getGroundActionsExtensionList();
  rcontext.getJavascriptManager().importJavascript('eXo.ecm.UITimelineView','/ecmexplorer/javascript/');
  rcontext.getJavascriptManager().importJavascript('eXo.ecm.DMSBrowser','/ecm-wcm-extension/javascript/');
  rcontext.getJavascriptManager().addJavascript("eXo.ecm.UITimelineView.initAllEvent('${componentId}-$viewComponentId', '$enableDragAndDrop');");
  uicomponent.updateNodeLists();
  List<String> labels = new ArrayList<String>();
  labels.add("UIDocumentInfo.label.Today");
  labels.add("UIDocumentInfo.label.Yesterday");
  labels.add("UIDocumentInfo.label.EarlierThisWeek");
  labels.add("UIDocumentInfo.label.EarlierThisMonth");
  labels.add("UIDocumentInfo.label.EarlierThisYear");
  
  List<List<Node>> nodeLists = new ArrayList<List<Node>>();
  nodeLists.add(uicomponent.getTodayNodes());
  nodeLists.add(uicomponent.getYesterdayNodes());
  nodeLists.add(uicomponent.getEarlierThisWeekNodes());
  nodeLists.add(uicomponent.getEarlierThisMonthNodes());
  nodeLists.add(uicomponent.getEarlierThisYearNodes());
  
  SimpleDateFormat formatDateTime = uicomponent.getSimpleDateFormat();
  
  int listCount = 5;

%>
<div id="$componentId">
<div id="${componentId}-$viewComponentId" class="UIListView" style="">
  <div class="UIListGrid UITimelineView">
    <div style="padding:4px 3px 0px 3px;">
      <div class="TitleTable">
        <div style="width: 40px;" class="LeftColumn Split">
          <span></span>
        </div>
        <div style="width: 26px;" class="LeftColumn Split">
          <% if (uicomponent.getTimeLineSortByFavourite().equals(Preference.BLUE_DOWN_ARROW)) { %>
            <div  class="StarLight"
                  style="margin-top: 5px;"
                  onclick="<%=uicomponent.event("SortTimelineDESC", Utils.formatNodeName("favourite"))%>"
                  title="<%=_ctx.appRes("UIDocumentInfo.tooltip.timelineFavouriteDESC")%>">
              <span></span>
            </div>
          <% } else {%>
          <div  class="StarLight"
                style="margin-top: 5px;"
                onclick="<%=uicomponent.event("SortTimelineASC", Utils.formatNodeName("favourite"))%>"
                title="<%=_ctx.appRes("UIDocumentInfo.tooltip.timelineFavouriteASC")%>">
            <span></span>
          </div>
          <% } %>
        </div>
        <div style="border: medium none; width: 130px; padding: 0px 3px;" class="RightColumn">
          <% if (uicomponent.getTimeLineSortByDate().equals(Preference.BLUE_UP_ARROW)) {%>
              <div onclick="<%=uicomponent.event("SortTimelineASC", Utils.formatNodeName("dateTime"))%>"
                   title="<%=_ctx.appRes("UIDocumentInfo.tooltip.timelineDateTimeASC")%>">
                <div class="BlueUpArrow16x16Icon"> <%=_ctx.appRes("UIDocumentInfo.header.dateTime")%></div>
            
              </div>
          <% } else if (uicomponent.getTimeLineSortByDate().equals(Preference.BLUE_DOWN_ARROW)) { %>
              <div onclick="<%=uicomponent.event("SortTimelineDESC", Utils.formatNodeName("dateTime"))%>"
                   title="<%=_ctx.appRes("UIDocumentInfo.tooltip.timelineDateTimeDESC")%>">
                <div class="BlueDownArrow16x16Icon"><%=_ctx.appRes("UIDocumentInfo.header.dateTime")%></div>
              
              </div>
          <% } else { %>
              <div onclick="<%=uicomponent.event("SortTimelineASC", Utils.formatNodeName("dateTime"))%>"
                   title="<%=_ctx.appRes("UIDocumentInfo.tooltip.timelineDateTimeASC")%>">
                <%=_ctx.appRes("UIDocumentInfo.header.dateTime")%>
              </div>
          <% } %>
        </div>
        <div class="ResizeColumn Split" style="margin-left: 163px;" >
          <% if (uicomponent.getTimeLineSortByName().equals(Preference.BLUE_UP_ARROW)) {%>
              <div style="padding: 6px 5px 0px;" onclick="<%=uicomponent.event("SortTimelineASC", Utils.formatNodeName("name"))%>"
                   title="<%=_ctx.appRes("UIDocumentInfo.tooltip.timelineNameASC")%>">
                <div class="BlueUpArrow16x16Icon" style="margin: 0px;"><%=_ctx.appRes("UIDocumentInfo.header.name")%></div>
                
              </div>
          <% } else if (uicomponent.getTimeLineSortByName().equals(Preference.BLUE_DOWN_ARROW)) { %>
              <div style="padding: 6px 5px 0px;" onclick="<%=uicomponent.event("SortTimelineDESC", Utils.formatNodeName("name"))%>"
                   title="<%=_ctx.appRes("UIDocumentInfo.tooltip.timelineNameDESC")%>">
                <div class="BlueDownArrow16x16Icon" style="margin: 0px;"><%=_ctx.appRes("UIDocumentInfo.header.name")%></div>
                
              </div>
          <% } else { %>
              <div onclick="<%=uicomponent.event("SortTimelineASC", Utils.formatNodeName("name"))%>"
                     title="<%=_ctx.appRes("UIDocumentInfo.tooltip.timelineNameASC")%>">
                  <%=_ctx.appRes("UIDocumentInfo.header.name")%>
              </div>
          <% } %>
        </div>
        <div style="clear: both;"><span></span></div>
      </div>
      
      <div style="position: relative; top: 0px; left: 0px; width: 0px; height: 0px">
        <div class="Mask" style="position: absolute; top: 0px; left: 0px; width: 0px; height: 0px"></div>
        
        <div class="JCRMoveAction" 
             style="display: none;" 
             request="<%=uiWorkingArea.getJCRMoveAction().event(UIWorkingArea.MOVE_NODE)%>" 
             symlink="<%=uiWorkingArea.getCreateLinkAction().event(UIWorkingArea.CREATE_LINK)%>">
        </div>
        <div class="ItemContextMenu" style="position: absolute; top: 0px; left: 0px; display: none;">
          <div class="UIRightClickPopupMenu" style="display: block;">
            <div class="UIRightPopupMenuContainer" onmousedown="event.cancelBubble = true;">                  
            <%
              for(itemContext in managersMultiItemContext) {
                action = itemContext.getUIExtensionName();
            %>
                <a class="MenuItem" style="display: block;" onclick=eXo.ecm.UIListView.postGroupAction("<%=itemContext.event(action)%>")>   
                  <div class="ItemIcon ${action}16x16Icon"><%=_ctx.appRes("ECMContextMenu.event." + action)%></div>
                </a>
            <%}%>            
           </div> 
         </div> 
       </div>
       <div class="GroundContextMenu" style="position: absolute;top: 0px; left: 0px; display: none;">
          <div class="UIRightClickPopupMenu" style="display: block;">
            <div class="UIRightPopupMenuContainer" onmousedown="event.cancelBubble = true;">          
              <%
                for(itemGroundContext in managersGroundContext) {
                action = itemGroundContext.getUIExtensionName();
              %>
                <a class="MenuItem" href="<%=itemGroundContext.event(action)%>" style="display: block;">  
                  <div class="ItemIcon ${action}16x16Icon"><%=_ctx.appRes("ECMContextMenu.event." + action)%></div>
                </a>
              <%}%>     
             </div> 
           </div>
         </div>
       </div>
       
      <% for(int i = 0; i < 5; i++) {
            String label = labels.get(i);
            List<Node> nodeList = nodeLists.get(i);
            if (nodeList.size() == 0) continue;
      %>    
      <div class="TextTitleDay">
        <%=_ctx.appRes(label)%>
      </div>  
      <div class="BorderRow">
          <% for (node in nodeList) {
            def data = node;
            def rightClickMenu = "" ;
            def isPreferenceNode = uicomponent.isPreferenceNode(data);
            def preferenceWS = data.getSession().getWorkspace().getName();
            String nodePath = data.getPath();
            String path = node.getPath() + "&workspaceName=" + preferenceWS ;
            String name = nodePath.substring(nodePath.lastIndexOf("/") + 1, nodePath.length());   
            String title = uiWorkingArea.getTitle(data);            
            String subName;
            String hiddenStyle = (node.isNodeType("exo:hiddenable"))?"color: #A0A0A0;":"";
            String actionLink = uicomponent.event("ChangeNode",Utils.formatNodeName(data.path), new Parameter("workspaceName", preferenceWS));
            permLinkComponent =  uiWorkingArea.getPermlink(data);
            String isFavourite = "false";
            String isInTrash = "false";
            String isMediaType = "false";
            String isTrashHome = "false";
            Boolean isLocked = false;
            String lockedLabel = "";
            if (data.isLocked()) {
              isLocked = true;
              lockedLabel  = "("+_ctx.appRes("UIDocumentWorkspace.tooltip.LockedBy")+" "+data.getLock().getLockOwner()+")";
            }
            if (uicomponent.isFavouriter(data)) isFavourite = "true";
            if (data.isNodeType("exo:restoreLocation")) isInTrash = "true";
            if (uicomponent.isMediaType(data)) isMediaType = "true";
            if (Utils.isTrashHomeNode(data)) isTrashHome = "true"
            if (!uicomponent.isSystemWorkspace()) rightClickMenu = contextMenu.getJSOnclickShowPopup(preferenceWS + ":" + Utils.formatNodeName(nodePath), uiWorkingArea.getActionsExtensionList(data)) ;
            
            %>
            <div <%=rightClickMenu%> class="RowView Normal" trashHome="$isTrashHome" locked="$isLocked" mediaType="$isMediaType" removeFavourite="$isFavourite" inTrash="$isInTrash" workspacename="$preferenceWS" objectId="<%=URLEncoder.encode(Utils.formatNodeName(nodePath), "utf-8")%>" ondblclick="$actionLink">
            <div style="width: 31px;" class="LeftColumn">
              <span></span>
            </div>                
            <div style="width: 26px; padding-top: 5px;" class="LeftColumn">
              <% if (uicomponent.isFavouriteNode(node.getSession().getUserID(), node)) { %>
                <div class="StarLight" onclick="<%=uicomponent.event("StarClick",node.getSession().getWorkspace().getName() + ":" +  Utils.formatNodeName(node.getPath()))%>"
                     title="<%=_ctx.appRes("UIDocumentInfo.tooltip.RemoveFromFavourite")%>"/>
                  <span></span>
                </div>
              <% } else { %>
                <div class="StarOff" onclick="<%=uicomponent.event("StarClick",node.getSession().getWorkspace().getName() + ":" +  Utils.formatNodeName(node.getPath()))%>"
                     title="<%=_ctx.appRes("UIDocumentInfo.tooltip.AddToFavourite")%>"/>
                  <span></span>
                </div>
              <% }%>
            </div>
            <div class="LeftColumn default16x16Icon ItemIcon <%=Utils.getNodeTypeIcon(node, "16x16Icon")%>" style="margin-left:10px; width: 26px; padding-top: 5px;">
              <%if(Utils.isSymLink(node)) {%>
                <div class="LinkSmall"></div>
              <%}%>
              <%if(isLocked) {%>
              <div class="IconLocked"></div>
              <%}%>
              <span></span>
            </div>
            <div style="border: medium none; width: 120px" class="RightColumn">
              <%=formatDateTime.format(node.getProperty(Utils.EXO_MODIFIED_DATE).getValue().getDate().getTime())%>
            </div>
            <div class="ResizeColumn" style="width:495px; *width: 425px;">
              <div class="PositionIconDocument">
                <div class="NodeLabel">
                  <div style="cursor: pointer; -moz-outline: none; -moz-user-select: none;$hiddenStyle;" title="<%=title%> $lockedLabel"><%=title%></div>
                </div>                
              </div>
            </div>
            <div style="clear: both;"><span></span></div>
            </div>
          <% } %>
        </div>
      <% } %>
    </div>
  </div>
</div>
</div>