/*---------------------------------------------------------
Setup, Layout, and Status Functions
---------------------------------------------------------*/
//var lang = 'jsp';
//var fileRoot = 'C:/Documents and Settings/m.gena/Bureau/';
// Sets paths to connectors based on language selection.
var treeConnector = 'scripts/jquery.filetree/connectors/jqueryFileTree.' + lang;
var fileConnector = 'connectors/' + lang + '/filemanager.' + lang;
// Options for alert, prompt, and confirm dialogues.
$.SetImpromptuDefaults({
overlayspeed: 'fast',
show: 'fadeIn',
opacity: 0.4
});
// Forces columns to fill the layout vertically.
// Called on initial page load and on resize.
var setDimensions = function(){
var newH = $(window).height() - 50;
$('#splitter, #filetree, #fileinfo, .vsplitbar').height(newH);
}
// Sets the folder status, upload, and new folder functions
// to the path specified. Called on initial page load and
// whenever a new directory is selected.
var setUploader = function(path){
$('#currentpath').val(path);
//$('#uploader h1').text('Current Folder: ' + path);
path = path.substring(0, path.length-1);
path = path.replace(new RegExp("\\\\", 'g'),"/");
index = path.lastIndexOf("/");
if(index == -1){
index = path.lastIndexOf("\\");
}
relativePath = path.substring(index);
$('#path').text(relativePath+ "/");
flder = relativePath.replace(new RegExp("/", 'g'),"");
//flder = flder.replace(new RegExp("\\\\", 'g'),"");
$('#actualpath').val(flder);
// $('#newfolder').unbind().click(function(){
// // var foldername = prompt('Enter the name of the new folder:', 'My Folder');
// var foldername = 'My Folder';
// var msg = 'Enter the name of the new folder: ';
//
// var getFolderName = function(v, m){
// if(v != 1) return false;
// var fname = m.children('#fname').val();
//
// if(fname != ''){
// foldername = fname;
//
// $.getJSON(fileConnector + '?mode=addfolder&path=' + $('#currentpath').val() + '&name=' + foldername, function(result){
// if(result['Code'] == 0){
// addFolder(result['Parent'], result['Name']);
// getFolderInfo(result['Parent']);
// } else {
// $.prompt(result['Error']);
// }
// });
// } else {
// $.prompt('No folder name was provided.');
// }
// }
//
// $.prompt(msg, {
// callback: getFolderName,
// buttons: { 'Create Folder': 1, 'Cancel': 0 }
// });
// });
}
// Binds specific actions to the toolbar in detail views.
// Called when detail views are loaded.
var bindToolbar = function(data){
// this little bit is purely cosmetic
$('#fileinfo').find('button').wrapInner('');
$('#fileinfo').find('button#select').click(function(){
selectItem(data);
});
$('#fileinfo').find('button#rename').click(function(){
var newName = renameItem(data);
if(newName.length) $('#fileinfo > h1').text(newName);
});
$('#fileinfo').find('button#delete').click(function(){
if(deleteItem(data)) $('#fileinfo').html('
Select an item from the left.
');
});
$('#fileinfo').find('button#download').click(function(){
window.location = fileConnector + '?mode=download&path=' + data['Path'];
});
}
// Converts bytes to kb, mb, or gb as needed for display.
var formatBytes = function(bytes){
var n = parseFloat(bytes);
var d = parseFloat(1024);
var c = 0;
var u = [' bytes','kb','mb','gb'];
while(true){
if(n < d){
n = Math.round(n * 100) / 100;
return n + u[c];
} else {
n /= d;
c += 1;
}
}
}
// function to retrieve GET params
$.urlParam = function(name){
var results = new RegExp('[\\?&]' + name + '=([^]*)').exec(window.location.href);
return results[1] || 0;
}
/*---------------------------------------------------------
Item Actions
---------------------------------------------------------*/
// Calls the SetUrl function for FCKEditor compatibility,
// passes file path, dimensions, and alt text back to the
// opening window. Triggered by clicking the "Select"
// button in detail views or choosing the "Select"
// contextual menu option in list views.
// NOTE: closes the window when finished.
var selectItem = function(data){
if(window.opener){
if($.urlParam('CKEditor')){
// use CKEditor 3.0 integration method
window.opener.CKEDITOR.tools.callFunction($.urlParam('CKEditorFuncNum'), data['Path']);
} else {
// use FCKEditor 2.0 integration method
if(data['Properties']['Width'] != ''){
var p = data['Path'];
var w = data['Properties']['Width'];
var h = data['Properties']['Height'];
window.opener.SetUrl(p,w,h);
} else {
window.opener.SetUrl(data['Path']);
}
}
window.close();
} else {
$.prompt('The "Select" function is only used for integration with FCKEditor.');
}
}
// Renames the current item and returns the new name.
// Called by clicking the "Rename" button in detail views
// or choosing the "Rename" contextual menu option in
// list views.
var renameItem = function(data){
var finalName = '';
var msg = 'Enter a new name for the file: ';
var getNewName = function(v, m){
if(v != 1) return false;
rname = m.children('#rname').val();
if(rname != ''){
var givenName = rname;
var oldPath = data['Path'];
var connectString = fileConnector + '?mode=rename&old=' + data['Path'] + '&new=' + givenName;
$.ajax({
type: 'GET',
url: connectString,
dataType: 'json',
async: false,
success: function(result){
if(result['Code'] == 0){
var newPath = result['New Path'];
var newName = result['New Name'];
updateNode(oldPath, newPath, newName);
if($('#fileinfo').data('view') == 'grid'){
$('#fileinfo img[alt="' + oldPath + '"]').next('p').text(newName);
$('#fileinfo img[alt="' + oldPath + '"]').attr('alt', newPath);
} else {
$('#fileinfo td[title="' + oldPath + '"]').text(newName);
$('#fileinfo td[title="' + oldPath + '"]').attr('title', newPath);
}
$.prompt('Rename successful.');
} else {
$.prompt(result['Error']);
}
finalName = result['New Name'];
}
});
}
}
$.prompt(msg, {
callback: getNewName,
buttons: { 'Rename': 1, 'Cancel': 0 }
});
return finalName;
}
// Prompts for confirmation, then deletes the current item.
// Called by clicking the "Delete" button in detail views
// or choosing the "Delete contextual menu item in list views.
var deleteItem = function(data){
var isDeleted = false;
var msg = 'Are you sure you wish to delete this file?';
var doDelete = function(v, m){
if(v != 1) return false;
var connectString = fileConnector + '?mode=delete&path=' + data['Path'];
$.ajax({
type: 'GET',
url: connectString,
dataType: 'json',
async: false,
success: function(result){
if(result['Code'] == 0){
removeNode(result['Path']);
isDeleted = true;
$.prompt('Delete successful.');
} else {
isDeleted = false;
$.prompt(result['Error']);
}
}
});
}
$.prompt(msg, {
callback: doDelete,
buttons: { 'Yes': 1, 'No': 0 }
});
return isDeleted;
}
/*---------------------------------------------------------
Functions to Update the File Tree
---------------------------------------------------------*/
// Adds a new node as the first item beneath the specified
// parent node. Called after a successful file upload.
var addNode = function(path, name){
var ext = name.substr(name.lastIndexOf('.') + 1);
var thisNode = $('#filetree').find('a[rel="' + path + '"]');
var parentNode = thisNode.parent();
var newNode = '
');
parentNode.find('ul').prepend(newNode);
thisNode.click().click();
getFolderInfo(path);
$.prompt('New file added successfully.');
}
// Updates the specified node with a new name. Called after
// a successful rename operation.
var updateNode = function(oldPath, newPath, newName){
var thisNode = $('#filetree').find('a[rel="' + oldPath + '"]');
var parentNode = thisNode.parent().parent().prev('a');
thisNode.attr('rel', newPath).text(newName);
parentNode.click().click();
}
// Removes the specified node. Called after a successful
// delete operation.
var removeNode = function(path){
$('#filetree')
.find('a[rel="' + path + '"]')
.parent()
.fadeOut('slow', function(){
$(this).remove();
});
}
// Adds a new folder as the first item beneath the
// specified parent node. Called after a new folder is
// successfully created.
var addFolder = function(parent, name){
var newNode = '
';
var parentNode = $('#filetree').find('a[rel="' + parent + '"]');
if(parent != fileRoot){
parentNode.next('ul').prepend(newNode).prev('a').click().click();
} else {
$('#filetree > ul').append(newNode);
}
$.prompt('New folder added successfully.');
}
/*---------------------------------------------------------
Functions to Retrieve File and Folder Details
---------------------------------------------------------*/
// Decides whether to retrieve file or folder info based on
// the path provided.
var getDetailView = function(path){
if(path.lastIndexOf('/') == path.length - 1){
getFolderInfo(path);
$('#filetree').find('a[rel="' + path + '"]').click();
} else {
getFileInfo(path);
}
}
// Binds contextual menus to items in list and grid views.
var setMenus = function(action, path){
$.getJSON(fileConnector + '?mode=getinfo&path=' + path, function(data){
if($('#fileinfo').data('view') == 'grid'){
var item = $('#fileinfo').find('img[alt="' + data['Path'] + '"]').parent();
} else {
var item = $('#fileinfo').find('td[title="' + data['Path'] + '"]').parent();
}
switch(action){
case 'select':
selectItem(data);
break;
case 'download':
window.location = fileConnector + '?mode=download&path=' + data['Path'];
break;
case 'rename':
var newName = renameItem(data);
break;
case 'delete':
// TODO: When selected, the file is deleted and the
// file tree is updated, but the grid/list view is not.
if(deleteItem(data)) item.fadeOut('slow', function(){ $(this).remove(); });
break;
}
});
}
// Retrieves information about the specified file as a JSON
// object and uses that data to populate a template for
// detail views. Binds the toolbar for that detail view to
// enable specific actions. Called whenever an item is
// clicked in the file tree or list views.
var getFileInfo = function(file){
// Update location for status, upload, & new folder functions.
var currentpath = file.substr(0, file.lastIndexOf('/') + 1);
setUploader(currentpath);
// Include the template.
var template = '
';
template += '';
$('#fileinfo').html(template);
// Retrieve the data & populate the template.
$.getJSON(fileConnector + '?mode=getinfo&path=' + file, function(data){
if(data['Code'] == 0){
$('#fileinfo').find('h1').text(data['Filename']);
$('#fileinfo').find('img').attr('src',data['Preview']);
var properties = '';
if(data['Properties']['Width'] && data['Properties']['Width'] != '') properties += '
';
$('#fileinfo').find('dl').html(properties);
// Bind toolbar functions.
bindToolbar(data);
} else {
$.prompt(data['Error']);
}
});
}
// Retrieves data for all items within the given folder and
// creates a list view. Binds contextual menu options.
// TODO: consider stylesheet switching to switch between grid
// and list views with sorting options.
var getFolderInfo = function(path){
// Update location for status, upload, & new folder functions.
setUploader(path);
// Display an activity indicator.
$('#fileinfo').html('');
// Retrieve the data and generate the markup.
$.getJSON(fileConnector + '?path=' + path + '&mode=getfolder&showThumbs=' + showThumbs, function(data){
var result = '';
if(data){
if($('#fileinfo').data('view') == 'grid'){
result += '
';
for(key in data){
var props = data[key]['Properties'];
var scaledWidth = 64;
var actualWidth = props['Width'];
if(actualWidth > 1 && actualWidth < scaledWidth) scaledWidth = actualWidth;
result += '