source: trunk/www/filemanager/directories/list_directories.jsp @ 5938

Last change on this file since 5938 was 5938, checked in by Nicklas Nordborg, 10 years ago

References #1655: GUI improvements

  • Trashcan and file manager
  • Property svn:eol-style set to native
  • Property svn:keywords set to Date Id
File size: 12.5 KB
Line 
1<%-- $Id: list_directories.jsp 5938 2012-01-27 14:17:51Z nicklas $
2  ------------------------------------------------------------------
3  Copyright (C) 2006 Jari Häkkinen, Nicklas Nordborg
4
5  This file is part of BASE - BioArray Software Environment.
6  Available at http://base.thep.lu.se/
7
8  BASE is free software; you can redistribute it and/or
9  modify it under the terms of the GNU General Public License
10  as published by the Free Software Foundation; either version 3
11  of the License, or (at your option) any later version.
12
13  BASE is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  GNU General Public License for more details.
17
18  You should have received a copy of the GNU General Public License
19  along with BASE. If not, see <http://www.gnu.org/licenses/>.
20  ------------------------------------------------------------------
21
22  @author Nicklas
23  @version 2.0
24--%>
25<%@ page pageEncoding="UTF-8" session="false"
26  import="net.sf.basedb.core.SessionControl"
27  import="net.sf.basedb.core.DbControl"
28  import="net.sf.basedb.core.SystemItems"
29  import="net.sf.basedb.core.Item"
30  import="net.sf.basedb.core.ItemContext"
31  import="net.sf.basedb.core.Directory"
32  import="net.sf.basedb.core.Experiment"
33  import="net.sf.basedb.core.User"
34  import="net.sf.basedb.core.ItemQuery"
35  import="net.sf.basedb.core.ItemResultIterator"
36  import="net.sf.basedb.core.Permission"
37  import="net.sf.basedb.core.query.Restrictions"
38  import="net.sf.basedb.core.query.Expressions"
39  import="net.sf.basedb.core.query.Orders"
40  import="net.sf.basedb.core.query.Hql"
41  import="net.sf.basedb.util.FileUtil"
42  import="net.sf.basedb.clients.web.Base"
43  import="net.sf.basedb.clients.web.util.HTML"
44  import="net.sf.basedb.util.Values"
45  import="java.util.Map"
46  import="java.util.Set"
47  import="java.util.HashSet"
48  import="java.util.List"
49%>
50<%@ taglib prefix="base" uri="/WEB-INF/base.tld" %>
51<%@ taglib prefix="tbl" uri="/WEB-INF/table.tld" %>
52<%!
53String generateSubTree(Map<Directory, List<Directory>> tree, Set<Directory> ignore, Directory parent, String ID, String parentId)
54{
55  if (ignore.contains(parent)) return "";
56  ignore.add(parent);
57  StringBuilder sb = new StringBuilder();
58  if (parentId == null) parentId = "dir"+parent.getId();
59  List<Directory> children = tree.get(parent);
60  if (children == null) return "";
61  for (Directory child : children)
62  {
63    if (ignore.contains(child)) continue;
64    String folderIcon = "Folder";
65    if (child.isHomeDirectory())
66    {
67      folderIcon = "UserHome";
68    }
69    else if (child.isRemoved())
70    {
71      folderIcon = "FolderDeleted";
72    }
73    else if (child.getAutoCompress())
74    {
75      folderIcon = "FolderCompressed";
76    }
77    sb.append("var dir").append(child.getId()).append(" = JoustMenu.addLazyChildItem(").append(parentId);
78    sb.append(",'").append(folderIcon).append("'");
79    sb.append(",'").append(HTML.javaScriptEncode(child.getName())).append("'");
80    sb.append(",'directoryOnClick(").append(child.getId()).append(",");
81    sb.append(" \"").append(HTML.javaScriptEncode(child.getPath().toString())).append("\")'");
82    sb.append(",'', 'D").append(child.getId()).append("',");
83    sb.append("'lazyInitSubdir(").append(child.getId()).append(")')\n");
84    sb.append(generateSubTree(tree, ignore, child, ID, null));
85  }
86  return sb.toString();
87}
88%>
89<%
90final Item itemType = Item.DIRECTORY;
91final SessionControl sc = Base.getExistingSessionControl(pageContext, Permission.DENIED, itemType);
92final String ID = sc.getId();
93final DbControl dc = sc.newDbControl();
94final String mode = request.getParameter("mode");
95final String callback = request.getParameter("callback");
96final float scale = Base.getScale(sc);
97final String requestTitle = request.getParameter("title");
98final boolean syncWithDirectory = Values.getBoolean(request.getParameter("sync"));
99final ItemContext cc = sc.getCurrentContext(itemType);
100int directoryId = cc.getId();
101try
102{
103  final User user = User.getById(dc, sc.getLoggedInUserId());
104  final Directory userHome = user.getHomeDirectory();
105  final boolean readDirectories = sc.hasPermission(Permission.READ, Item.DIRECTORY);
106  final Directory root = Directory.getById(dc, SystemItems.getId(readDirectories ? Directory.ROOT : Directory.HOME));
107 
108  Directory current = null;
109  // Try to get current/last visited directory. If not successful, use default directory.
110  try
111  {
112    current = directoryId != 0 ? Directory.getById(dc, directoryId) : null;
113  }
114  catch (Throwable ex){} 
115  if (current == null) current = userHome;
116  if (current == null) current = root;
117 
118  final String rootIcon;
119  final String rootTitle;
120  if (root.isRootDirectory())
121  {
122    rootIcon = "Root";
123    rootTitle = "Root";
124  }
125  else
126  {
127    rootIcon = "Home";
128    rootTitle = "Other users";
129  }
130  Map<Directory, List<Directory>> tree = FileUtil.loadMinimalDirectoryTree(dc, root, userHome, current);
131  Set<Directory> ignore = new HashSet<Directory>();
132  %>
133  <base:page title="<%=requestTitle != null ? requestTitle : ""%>" type="popup">
134  <base:head scripts="newjoust.js,table.js,ajax.js,json2.js" styles="newjoust.css,toolbar.css">
135  <script language="JavaScript">
136  var isInitialised = false;
137  function initialise()
138  {
139    IconStore.init();
140    var path = getRoot()+'images/joust/';
141    IconStore.addIcon('Search', path + 'search.png', 18, 16);
142    IconStore.addIcon('SearchSelected', path + 'searchselected.png', 18, 16);
143    <%
144    // Root entry for the user's Home directory
145    if (userHome != null)
146    {
147      %>
148      var userHome = JoustMenu.addMenuItem(-1, 'Home', 'My home', 'directoryOnClick(<%=userHome.getId()%>, "<%=HTML.javaScriptEncode(userHome.getPath().toString())%>")', '', 'D<%=userHome.getId()%>');
149      JoustMenu.menuItems[userHome].noOutlineIcon = true;
150      JoustMenu.menuItems[userHome].isOpen = true;
151      <%=generateSubTree(tree, ignore, userHome, ID, "userHome")%>
152      <%
153    }
154    // Root entry for entire directory tree
155    %>
156    var search = JoustMenu.addMenuItem(-1, 'Search', 'Search results', 'searchOnClick()', 'The latest search results', 'search');
157    JoustMenu.menuItems[search].noOutlineIcon = true;
158    JoustMenu.menuItems[search].style = 'display:none; font-style: italic; border-bottom: 1px solid #cccccc; margin-bottom: 2px; padding-bottom: 2px;';
159    var root = JoustMenu.addMenuItem(-1, '<%=rootIcon%>', '<%=HTML.javaScriptEncode(rootTitle)%>', 'directoryOnClick(<%=root.getId()%>, "<%=HTML.javaScriptEncode(root.getPath().toString())%>")', '', 'D<%=root.getId()%>');
160    JoustMenu.menuItems[root].noOutlineIcon = true;
161    JoustMenu.menuItems[root].isOpen = true;
162    <%=generateSubTree(tree, ignore, root, ID, "root")%>
163
164    <%
165    // Add 'current' as root entry if we have not seen it before
166    if (!ignore.contains(current))
167    {
168      // but... move up as far as the user has 'read' permission...
169      Directory currentRoot = current;
170      try
171      {
172        while (currentRoot != null && !ignore.contains(currentRoot.getParent()))
173        {
174          currentRoot = currentRoot.getParent();
175        }
176      }
177      catch (Throwable t)
178      {}
179      if (currentRoot == null) currentRoot = current;
180      %>
181      var currentRoot = JoustMenu.addMenuItem(-1, 'Folder', '<%=HTML.javaScriptEncode(currentRoot.getPath().toString())%>', 'directoryOnClick(<%=currentRoot.getId()%>, "<%=HTML.javaScriptEncode(currentRoot.getPath().toString())%>")', '', 'D<%=currentRoot.getId()%>');
182      JoustMenu.menuItems[currentRoot].noOutlineIcon = true;
183      JoustMenu.menuItems[currentRoot].isOpen = true;
184      <%=generateSubTree(tree, ignore, currentRoot, ID, "currentRoot")%>
185      <%
186    }
187    %>
188    JoustMenu.draw('joust');
189    var lastDirectory = JoustMenu.menuItems['D<%=current.getId()%>'];
190    JoustMenu.select(lastDirectory.index);
191    JoustMenu.alwaysSendOnClickToSelected = true;
192    isInitialised = true;
193  }
194 
195  function lazyInitSubdir(directoryId)
196  {
197    var request = Ajax.getXmlHttpRequest();
198    var url = 'ajax.jsp?ID=<%=ID%>&cmd=GetSubdirectories&item_id=' + directoryId;
199    request.open("GET", url, false);
200    // NOTE! 'false' causes code to wait for the response. aka. 'Synchronous AJAX' or SJAX.
201    request.send(null);
202    var response = Ajax.parseJsonResponse(request.responseText);
203    if (response.status != 'ok')
204    {
205      alert(response.message);
206      return false;
207    }
208   
209    var subdirs = response.subdirs;
210    var numAdded = 0;
211    var parent = JoustMenu.menuItems['D'+directoryId];
212    for (var i = 0; i < subdirs.length; i++)
213    {
214      var subdir = subdirs[i];
215      var id = subdir.id;
216      var folderIcon = 'Folder';
217      if (subdir.isHome)
218      {
219        folderIcon = 'UserHome';
220      }
221      else if (subdir.isRemoved)
222      {
223        folderIcon = 'FolderDeleted';
224      }
225      else if (subdir.isAutoCompress == 1)
226      {
227        folderIcon = 'FolderCompressed';
228      }
229      numAdded++;
230      JoustMenu.addLazyChildItem(parent.index, folderIcon, subdir.name, 'directoryOnClick('+id+',\''+subdir.path+'\')', '', 'D'+id, 'lazyInitSubdir('+id+')');
231    }
232    return numAdded > 0;
233  }
234  <%
235  if ("selectonedirectory".equals(mode))
236  {
237    %>
238    var lastDirectoryId;
239    function directoryOnClick(directoryId, path)
240    {
241      var frm = document.forms['directory'];
242      frm.directory_id.value = directoryId;
243      frm.path.value = path;
244      lastDirectoryId = directoryId;
245    }
246    function returnSelected()
247    {
248      var frm = document.forms['directory'];
249      top.window.opener.<%=callback%>(frm.directory_id.value, frm.path.value);
250      top.window.close();
251    }
252    function newDirectory()
253    {
254      Main.viewOrEditItem('<%=ID%>', '<%=itemType.name()%>', 0, true, '&parent_id='+lastDirectoryId);
255    }
256    <%
257  }
258  else
259  {
260    %>
261    function directoryOnClick(directoryId, path)
262    {
263      if (!isInitialised && <%=!syncWithDirectory%>) return;
264      parent.lastOpenDirectoryId = directoryId;
265      parent.frames.files.location.href = '../files/index.jsp?ID=<%=ID%>&cmd=List&directory_id='+directoryId+'&mode=<%=mode%>';
266      if (window.top.setSelected)
267      {
268        window.top.setSelected('', path);
269      }
270    }
271    <%
272  }
273  %>
274  function searchOnClick()
275  {
276    parent.frames.files.location.href = '../files/index.jsp?ID=<%=ID%>&cmd=Search&subcontext=search&mode=<%=mode%>';
277  }
278  function searchButtonOnClick()
279  {
280    var searchResults = JoustMenu.menuItems['search'];
281    Main.show('menuItem'+searchResults.index);
282    JoustMenu.select(searchResults.index, null, false);
283  }
284  function updateSearchResults(files, directories)
285  {
286    var searchResults = JoustMenu.menuItems['search'];
287    if (searchResults)
288    {
289      var text = document.getElementById('text'+searchResults.index);
290      text.innerHTML = 'Search results (' + (files + directories) + ')';
291    }
292  }
293  function synchronizeTo(directoryId, parentId)
294  {
295    var item = JoustMenu.menuItems['D'+directoryId];
296    if (!item)
297    {
298      if (!parentId) parentId = parent.lastOpenDirectoryId;
299      if (parentId)
300      {
301        var parentDir = JoustMenu.menuItems['D'+parentId];
302        if (parentDir.lazyInit)
303        {
304          JoustMenu.toggle(parentDir.index);
305          item = JoustMenu.menuItems['D'+directoryId];
306        }
307      }
308    }
309    if (item)
310    {
311      JoustMenu.select(item.index);
312    }
313    else
314    {
315      parent.parent.location.href = '../directories/index.jsp?ID=<%=ID%>&cmd=ViewItem&item_id=' + directoryId+'&mode=<%=mode%>';
316    }
317  }
318  function refresh()
319  {
320    location.reload();
321  }
322  </script>
323  </base:head>
324 
325  <%
326  if ("selectonedirectory".equals(mode))
327  {
328    %>
329    <base:body onload="initialise()"
330      style="padding-left: 0px; padding-right: 0px;"
331      >
332      <div id="main" class="joust" style="width:100%;">
333        <h3 class="docked"><%=requestTitle == null ? "Select one directory" : requestTitle%></h3>
334        <tbl:toolbar>
335          <tbl:button 
336            image="directory_new.gif" 
337            onclick="newDirectory()" 
338            title="New&hellip;" 
339            tooltip="Create a new directory" 
340          />
341        </tbl:toolbar>
342        <div id="joust" class="boxedbottom" style="height: <%=(int)(scale*340)%>px; overflow:auto; background: #E0E0E0;">
343        </div>
344      </div>
345     
346      <form name="directory">
347      <br>
348      <input type="hidden" name="directory_id" value="">
349      <b><%=requestTitle == null ? "Path" : requestTitle%></b> <input type="text" class="text" readonly name="path" size="40">
350      <p>
351      <table align="center">
352      <tr>
353        <td width="50%"><base:button onclick="returnSelected()" title="Ok" /></td>
354        <td width="50%"><base:button onclick="window.close()" title="Cancel" /></td>
355      </tr>
356      </table>
357      </form>
358    </base:body>
359    <%
360  }
361  else
362  {
363    %>
364    <base:body onload="initialise()">
365    <div id="main" class="absolutefull joust" 
366      onmousemove="parent.DragDrop.drag(event)" onmouseup="parent.DragDrop.endDrag(event)">
367      <tbl:toolbar subclass="bottomborder">
368        <tbl:button 
369          image="refresh.gif" 
370          onclick="refresh()" 
371          title="Refresh" 
372          tooltip="Refresh the directory tree" 
373        />
374        <tbl:button 
375          image="search.png" 
376          onclick="searchButtonOnClick()" 
377          title="Search" 
378          tooltip="Search for files and directories" 
379        />
380      </tbl:toolbar>
381      <div id="joust" style="overflow: auto;"></div>
382    </div>
383    </base:body>
384    <%
385  }
386  %>
387  </base:page>
388  <%
389}
390finally
391{
392  if (dc != null) dc.close();
393}
394%>
Note: See TracBrowser for help on using the repository browser.