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

Last change on this file since 4951 was 4951, checked in by Nicklas Nordborg, 13 years ago

Merged patch release 2.11.1 to the trunk.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Date Id
File size: 10.8 KB
Line 
1<%-- $Id: list_directories.jsp 4951 2009-05-27 10:07:18Z 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 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 ItemContext cc = sc.getCurrentContext(itemType);
99int directoryId = cc.getId();
100try
101{
102  final User user = User.getById(dc, sc.getLoggedInUserId());
103  final Directory userHome = user.getHomeDirectory();
104  final boolean readDirectories = sc.hasPermission(Permission.READ, Item.DIRECTORY);
105  final Directory root = Directory.getById(dc, SystemItems.getId(readDirectories ? Directory.ROOT : Directory.HOME));
106 
107  Directory current = null;
108  // Try to get current/last visited directory. If not successful, use default directory.
109  try
110  {
111    current = directoryId != 0 ? Directory.getById(dc, directoryId) : null;
112  }
113  catch (Throwable ex){} 
114  if (current == null) current = userHome;
115  if (current == null) current = root;
116 
117  final String rootIcon;
118  final String rootTitle;
119  if (root.isRootDirectory())
120  {
121    rootIcon = "Root";
122    rootTitle = "Root";
123  }
124  else
125  {
126    rootIcon = "Home";
127    rootTitle = "Other users";
128  }
129  Map<Directory, List<Directory>> tree = FileUtil.loadMinimalDirectoryTree(dc, root, userHome, current);
130  Set<Directory> ignore = new HashSet<Directory>();
131  %>
132  <base:page title="<%=requestTitle != null ? requestTitle : ""%>" type="popup">
133  <base:head scripts="newjoust.js,table.js,ajax.js" styles="newjoust.css,toolbar.css">
134  <script language="JavaScript">
135  var isInitialised = false;
136  function initialise()
137  {
138    if (parent && parent.parent && parent.parent.adjustIFrameSize) parent.parent.adjustIFrameSize();
139    IconStore.init();
140    <%
141    // Root entry for the user's Home directory
142    if (userHome != null)
143    {
144      %>
145      var userHome = JoustMenu.addMenuItem(-1, 'Home', 'My home', 'directoryOnClick(<%=userHome.getId()%>, "<%=HTML.javaScriptEncode(userHome.getPath().toString())%>")', '', 'D<%=userHome.getId()%>');
146      JoustMenu.menuItems[userHome].noOutlineIcon = true;
147      JoustMenu.menuItems[userHome].isOpen = true;
148      <%=generateSubTree(tree, ignore, userHome, ID, "userHome")%>
149      <%
150    }
151    // Root entry for entire directory tree
152    %>
153    var root = JoustMenu.addMenuItem(-1, '<%=rootIcon%>', '<%=HTML.javaScriptEncode(rootTitle)%>', 'directoryOnClick(<%=root.getId()%>, "<%=HTML.javaScriptEncode(root.getPath().toString())%>")', '', 'D<%=root.getId()%>');
154    JoustMenu.menuItems[root].noOutlineIcon = true;
155    JoustMenu.menuItems[root].isOpen = true;
156    <%=generateSubTree(tree, ignore, root, ID, "root")%>
157
158    <%
159    // Add 'current' as root entry if we have not seen it before
160    if (!ignore.contains(current))
161    {
162      // but... move up as far as the user has 'read' permission...
163      Directory currentRoot = current;
164      try
165      {
166        while (currentRoot != null && !ignore.contains(currentRoot.getParent()))
167        {
168          currentRoot = currentRoot.getParent();
169        }
170      }
171      catch (Throwable t)
172      {}
173      if (currentRoot == null) currentRoot = current;
174      %>
175      var currentRoot = JoustMenu.addMenuItem(-1, 'Folder', '<%=HTML.javaScriptEncode(currentRoot.getPath().toString())%>', 'directoryOnClick(<%=currentRoot.getId()%>, "<%=HTML.javaScriptEncode(currentRoot.getPath().toString())%>")', '', 'D<%=currentRoot.getId()%>');
176      JoustMenu.menuItems[currentRoot].noOutlineIcon = true;
177      JoustMenu.menuItems[currentRoot].isOpen = true;
178      <%=generateSubTree(tree, ignore, currentRoot, ID, "currentRoot")%>
179      <%
180    }
181    %>
182    JoustMenu.draw('joust');
183    var lastDirectory = JoustMenu.menuItems['D<%=current.getId()%>'];
184    JoustMenu.select(lastDirectory.index);
185    JoustMenu.alwaysSendOnClickToSelected = true;
186    isInitialised = true;
187  }
188 
189  function lazyInitSubdir(directoryId)
190  {
191    var request = Ajax.getXmlHttpRequest();
192    var url = 'ajax.jsp?ID=<%=ID%>&cmd=GetSubdirectories&item_id=' + directoryId;
193    request.open("GET", url, false);
194    // NOTE! 'false' causes code to wait for the response. aka. 'Synchronous AJAX' or SJAX.
195    request.send(null);
196    var response = request.responseText.split('\n');
197   
198    var kv = new Array();
199    var numAdded = 0;
200    var parent = JoustMenu.menuItems['D'+directoryId];
201    for (var i = 0; i < response.length; i++)
202    {
203      var txt = Main.trimString(response[i]);
204      //alert(txt.length+':'+txt);
205      if (txt == 'end')
206      {
207        var id = kv['id'];
208        var folderIcon = 'Folder';
209        if (kv['home'] == 1)
210        {
211          folderIcon = 'UserHome';
212        }
213        else if (kv['removed'] == 1)
214        {
215          folderIcon = 'FolderDeleted';
216        }
217        else if (kv['auto-compress'] == 1)
218        {
219          folderIcon = 'FolderCompressed';
220        }
221        numAdded++;
222        JoustMenu.addLazyChildItem(parent.index, folderIcon, kv['name'], 'directoryOnClick('+id+',\''+kv['path']+'\')', '', 'D'+id, 'lazyInitSubdir('+id+')');
223      }
224      else
225      {
226        var tmp = txt.split(':', 2);
227        kv[tmp[0]] = tmp[1];
228      }
229    }
230    //alert(numAdded);
231    return numAdded > 0;
232  }
233  <%
234  if ("selectonedirectory".equals(mode))
235  {
236    %>
237    var lastDirectoryId;
238    function directoryOnClick(directoryId, path)
239    {
240      var frm = document.forms['directory'];
241      frm.directory_id.value = directoryId;
242      frm.path.value = path;
243      lastDirectoryId = directoryId;
244    }
245    function returnSelected()
246    {
247      var frm = document.forms['directory'];
248      top.window.opener.<%=callback%>(frm.directory_id.value, frm.path.value);
249      top.window.close();
250    }
251    function newDirectory()
252    {
253      Main.viewOrEditItem('<%=ID%>', '<%=itemType.name()%>', 0, true, '&parent_id='+lastDirectoryId);
254    }
255    <%
256  }
257  else
258  {
259    %>
260    function directoryOnClick(directoryId, path)
261    {
262      if (!isInitialised && parent.frames.files.location.href.match('jsp')) return;
263      parent.lastOpenDirectoryId = directoryId;
264      parent.frames.files.location.href = '../files/index.jsp?ID=<%=ID%>&cmd=List&directory_id='+directoryId+'&mode=<%=mode%>';
265      if (window.top.setSelected)
266      {
267        window.top.setSelected('', path);
268      }
269    }
270    <%
271  }
272  %>
273  function refresh()
274  {
275    location.reload();
276  }
277  </script>
278  </base:head>
279 
280  <%
281  if ("selectonedirectory".equals(mode))
282  {
283    %>
284    <base:body onload="initialise()"
285      style="padding-left: 0px; padding-right: 0px;"
286      >
287      <div id="main" class="joust" style="width:100%;">
288        <h3 class="docked"><%=requestTitle == null ? "Select one directory" : requestTitle%></h3>
289        <tbl:toolbar>
290          <tbl:button 
291            image="directory_new.gif" 
292            onclick="newDirectory()" 
293            title="New&hellip;" 
294            tooltip="Create a new directory" 
295          />
296        </tbl:toolbar>
297        <div id="joust" class="boxedbottom" style="height: <%=(int)(scale*340)%>px; overflow:auto; background: #E0E0E0;">
298        </div>
299      </div>
300     
301      <form name="directory">
302      <br>
303      <input type="hidden" name="directory_id" value="">
304      <b><%=requestTitle == null ? "Path" : requestTitle%></b> <input type="text" class="text" readonly name="path" size="40">
305      <p>
306      <table align="center">
307      <tr>
308        <td width="50%"><base:button onclick="returnSelected()" title="Ok" /></td>
309        <td width="50%"><base:button onclick="window.close()" title="Cancel" /></td>
310      </tr>
311      </table>
312      </form>
313    </base:body>
314    <%
315  }
316  else
317  {
318    %>
319    <base:body onload="initialise()" 
320      style="border: 1px solid #999999; border-bottom: 0px; border-right: 0px; padding-left: 0px; padding-right: 0px;" >
321    <div id="main" class="joust" style="width:100%;">
322      <div id="joust" style="overflow: auto;">
323      </div>
324      <div style="width:99%;">
325      <table align="center">
326      <tr>
327        <td><base:button onclick="refresh()" title="Refresh" image="refresh.gif" /></td>
328      </tr>
329      </table>
330      </div>
331    </div>
332    </base:body>
333    <%
334  }
335  %>
336  </base:page>
337  <%
338}
339finally
340{
341  if (dc != null) dc.close();
342}
343%>
Note: See TracBrowser for help on using the repository browser.