I'm trying to get a grip on the jquery file tree plugin, and I have a problem with file paths. The thing is, the jquery call sets a root directory, and if it is set to "/" I want it to be a path in my server directory. So I set this in the server code that the jquery code interacts with.
Here's the jquery call:
<script type="text/javascript">
$(document).ready(function () {
root: "/", //Have made sure that the HomeController makes this correspond to the server application path or subfolder.
$('#result').fileTree({
script: 'Home/JqueryFileTree',
expandSpeed: 1000,
collapseSpeed: 1000,
multiFolder: false
}, function (file) {
alert(file); //This shows the name of the file if you click it
});
});
</script>
And here's how I set the root "/" to correspond to a location in my web application:
if (Request.Form["dir"] == null || Request.Form["dir"].Length <= 0 || Request.Form["dir"] == "/")
dir = Server.MapPath(Request.ApplicationPath); //Works but creates a strange mix of slashes and backslashes...
else
dir = Server.UrlDecode(Request.Form["dir"]);
This works fine as far as getting the file tree to show the correct file tree. But the problem is, when I click a file, and the alert function is called in the jquery, the file path shown by the alert box is a mixture of a windows path (the one specified as root above), and a url (the relative end part of the path). E.g. c:\my documents\visual studio\MvcApplication\FileArea/Public/file.txt.
If I had specified the root in the server code to be "/", as it was in the original script sample from jquery file tree, I would only get the last relative part in the alert box. Also, in the file tree generated I got the root of my c: drive, not the root of the web application... But now when I specify a path relative to my web application, I get this mixture of a whole absolute path.
Since I want to be able to grab this path and do stuff to the file, I anticipate this path will be a problem. So what's going on here, why does the path end up like that and how can I fix it? I have no idea how to specify the path relative to my web application in the jquery, so doing it in the server code was the only thing I could think of. In any case, I guess it's good I good a whole absolute path anyway, as long as I can fix it so that it uses one format. But can anyone tell me how?
EDIT: I thought I'd post the actual jquery fileTree code as well if that helps:
// jQuery File Tree Plugin
//
// Version 1.01
//
// Cory S.N. LaViska
// A Beautiful Site (http://abeautifulsite.net/)
// 24 March 2008
//
// Visit http://abeautifulsite.net/notebook.php?article=58 for more information
//
// Usage: $('.fileTreeDemo').fileTree( options, callback )
//
// Options: root - root folder to display; default = /
// script - location of the serverside AJAX file to use; default = jqueryFileTree.php
// folderEvent - event to trigger expand/collapse; default = click
// expandSpeed - default = 500 (ms); use -1 for no animation
// collapseSpeed - default = 500 (ms); use -1 for no animation
// expandEasing - easing function to use on expand (optional)
// collapseEasing - easing function to use on collapse (optional)
// multiFolder - whether or not to limit the browser to one subfolder at a time
// loadMessage - Message to display while initial tree loads (can be HTML)
//
// History:
//
// 1.01 - updated to work with foreign characters in directory/file names (12 April 2008)
// 1.00 - released (24 March 2008)
//
// TERMS OF USE
//
// This plugin is dual-licensed under the GNU General Public License and the MIT License and
// is copyright 2008 A Beautiful Site, LLC.
//
if (jQuery) (function ($) {
$.extend($.fn, {
fileTree: function (o, h) {
// Defaults
if (!o) var o = {};
if (o.root == undefined) o.root = '/';
if (o.script == undefined) o.script = 'jqueryFileTree.php';
if (o.folderEvent == undefined) o.folderEvent = 'click';
if (o.expandSpeed == undefined) o.expandSpeed = 500;
if (o.collapseSpeed == undefined) o.collapseSpeed = 500;
if (o.expandEasing == undefined) o.expandEasing = null;
if (o.collapseEasing == undefined) o.collapseEasing = null;
if (o.multiFolder == undefined) o.multiFolder = true;
if (o.loadMessage == undefined) o.loadMessage = 'Loading...';
$(this).each(function () {
function showTree(c, t) {
$(c).addClass('wait');
$(".jqueryFileTree.start").remove();
$.post(o.script, { dir: t }, function (data) {
$(c).find('.start').html('');
$(c).removeClass('wait').append(data);
if (o.root == t) $(c).find('UL:hidden').show(); else $(c).find('UL:hidden').slideDown({ duration: o.expandSpeed, easing: o.expandEasing });
bindTree(c);
});
}
function bindTree(t) {
$(t).find('LI A').bind(o.folderEvent, function () {
if ($(this).parent().hasClass('directory')) {
if ($(this).parent().hasClass('collapsed')) {
// Expand
if (!o.multiFolder) {
$(this).parent().parent().find('UL').slideUp({ duration: o.collapseSpeed, easing: o.collapseEasing });
$(this).parent().parent().find('LI.directory').removeClass('expanded').addClass('collapsed');
}
$(this).parent().find('UL').remove(); // cleanup
showTree($(this).parent(), escape($(this).attr('rel').match(/.*\//)));
$(this).parent().removeClass('collapsed').addClass('expanded');
} else {
// Collapse
$(this).parent().find('UL').slideUp({ duration: o.collapseSpeed, easing: o.collapseEasing });
$(this).parent().removeClass('expanded').addClass('collapsed');
}
h($(this).attr('rel')); //Testing how to get the folder name to display... Works fine.
} else {
h($(this).attr('rel')); //Calls the callback event in the calling method on the page, with the rel attr as parameter
}
return false;
});
// Prevent A from triggering the # on non-click events
if (o.folderEvent.toLowerCase != 'click') $(t).find('LI A').bind('click', function () { return false; });
}
//ASN: I think it starts here, the stuff before are just definitions that need to be called here.
// Loading message
$(this).html('<ul class="jqueryFileTree start"><li class="wait">' + o.loadMessage + '<li></ul>');
// Get the initial file list
showTree($(this), escape(o.root));
});
}
});
})(jQuery);
So, long story short: I don't get how the file paths work here. Just specifying "/" as the root seems to work as a relative path (since the alert box then shows only a relative path), but it gives me a file tree of the root (c:) of my computer. So how do I work with this to use the relative path of my web application instead, and still get proper paths that I can work with?
Any help appreciated!