nextcloud/apps/files_trashbin/js/trash.js

288 lines
7.6 KiB
JavaScript
Raw Normal View History

/*
* Copyright (c) 2014
*
* This file is licensed under the Affero General Public License version 3
* or later.
*
* See the COPYING-README file.
*
*/
/* global OC, t, BreadCrumb, FileActions, FileList, Files */
2013-01-18 16:11:29 +04:00
$(document).ready(function() {
var deletedRegExp = new RegExp(/^(.+)\.d[0-9]+$/);
/**
* Convert a file name in the format filename.d12345 to the real file name.
* This will use basename.
* The name will not be changed if it has no ".d12345" suffix.
* @param name file name
* @return converted file name
*/
function getDeletedFileName(name) {
name = OC.basename(name);
var match = deletedRegExp.exec(name);
if (match && match.length > 1) {
name = match[1];
}
return name;
}
function removeCallback(result) {
if (result.status !== 'success') {
OC.dialogs.alert(result.data.message, t('core', 'Error'));
}
var files = result.data.success;
for (var i = 0; i < files.length; i++) {
FileList.remove(OC.basename(files[i].filename), {updateSummary: false});
}
FileList.updateFileSummary();
FileList.updateEmptyContent();
enableActions();
}
2013-01-18 16:11:29 +04:00
Files.updateStorageStatistics = function() {
// no op because the trashbin doesn't have
// storage info like free space / used space
};
2013-01-18 16:11:29 +04:00
if (typeof FileActions !== 'undefined') {
FileActions.register('all', 'Restore', OC.PERMISSION_READ, OC.imagePath('core', 'actions/history'), function(filename) {
var tr = FileList.findFileEl(filename);
var deleteAction = tr.children("td.date").children(".action.delete");
2013-10-09 14:01:25 +04:00
deleteAction.removeClass('delete-icon').addClass('progress-icon');
disableActions();
$.post(OC.filePath('files_trashbin', 'ajax', 'undelete.php'), {
files: JSON.stringify([filename]),
dir: FileList.getCurrentDirectory()
},
removeCallback
2013-10-09 14:43:56 +04:00
);
});
};
2013-02-22 20:21:57 +04:00
2013-10-09 14:43:56 +04:00
FileActions.register('all', 'Delete', OC.PERMISSION_READ, function() {
return OC.imagePath('core', 'actions/delete');
}, function(filename) {
$('.tipsy').remove();
var tr = FileList.findFileEl(filename);
var deleteAction = tr.children("td.date").children(".action.delete");
2013-10-09 14:43:56 +04:00
deleteAction.removeClass('delete-icon').addClass('progress-icon');
disableActions();
$.post(OC.filePath('files_trashbin', 'ajax', 'delete.php'), {
files: JSON.stringify([filename]),
dir: FileList.getCurrentDirectory()
},
removeCallback
2013-10-09 14:43:56 +04:00
);
});
2013-02-22 20:21:57 +04:00
2013-10-09 14:43:56 +04:00
// Sets the select_all checkbox behaviour :
$('#select_all').click(function() {
if ($(this).attr('checked')) {
// Check all
$('td.filename input:checkbox').attr('checked', true);
$('td.filename input:checkbox').parent().parent().addClass('selected');
} else {
// Uncheck all
$('td.filename input:checkbox').attr('checked', false);
$('td.filename input:checkbox').parent().parent().removeClass('selected');
}
2013-10-09 15:15:53 +04:00
procesSelection();
2013-10-09 14:43:56 +04:00
});
$('.undelete').click('click', function(event) {
event.preventDefault();
var allFiles = $('#select_all').is(':checked');
var files = [];
var params = {};
2013-10-09 14:43:56 +04:00
disableActions();
if (allFiles) {
FileList.showMask();
params = {
allfiles: true,
dir: FileList.getCurrentDirectory()
};
}
else {
files = Files.getSelectedFiles('name');
for (var i = 0; i < files.length; i++) {
var deleteAction = FileList.findFileEl(files[i]).children("td.date").children(".action.delete");
deleteAction.removeClass('delete-icon').addClass('progress-icon');
}
params = {
files: JSON.stringify(files),
dir: FileList.getCurrentDirectory()
};
2013-10-09 14:43:56 +04:00
}
2013-10-09 14:43:56 +04:00
$.post(OC.filePath('files_trashbin', 'ajax', 'undelete.php'),
params,
function(result) {
if (allFiles) {
2013-10-09 14:01:25 +04:00
if (result.status !== 'success') {
OC.dialogs.alert(result.data.message, t('core', 'Error'));
}
FileList.hideMask();
// simply remove all files
FileList.update('');
2013-10-09 14:01:25 +04:00
enableActions();
}
else {
removeCallback(result);
}
}
2013-10-09 14:43:56 +04:00
);
});
2013-01-18 20:50:44 +04:00
2013-10-09 14:43:56 +04:00
$('.delete').click('click', function(event) {
event.preventDefault();
var allFiles = $('#select_all').is(':checked');
var files = [];
var params = {};
if (allFiles) {
params = {
allfiles: true,
dir: FileList.getCurrentDirectory()
};
}
else {
files = Files.getSelectedFiles('name');
params = {
files: JSON.stringify(files),
dir: FileList.getCurrentDirectory()
};
}
2013-02-22 20:21:57 +04:00
2013-10-09 14:43:56 +04:00
disableActions();
if (allFiles) {
FileList.showMask();
}
else {
for (var i = 0; i < files.length; i++) {
var deleteAction = FileList.findFileEl(files[i]).children("td.date").children(".action.delete");
deleteAction.removeClass('delete-icon').addClass('progress-icon');
}
2013-10-09 14:43:56 +04:00
}
2013-02-22 20:21:57 +04:00
2013-10-09 14:43:56 +04:00
$.post(OC.filePath('files_trashbin', 'ajax', 'delete.php'),
params,
2013-10-09 14:43:56 +04:00
function(result) {
if (allFiles) {
if (result.status !== 'success') {
OC.dialogs.alert(result.data.message, t('core', 'Error'));
}
FileList.hideMask();
// simply remove all files
FileList.setFiles([]);
enableActions();
}
else {
removeCallback(result);
2013-10-09 14:35:15 +04:00
}
}
2013-10-09 14:43:56 +04:00
);
2013-10-09 14:35:15 +04:00
2013-10-09 14:43:56 +04:00
});
2013-01-18 20:50:44 +04:00
2013-12-02 18:27:40 +04:00
$('#fileList').on('click', 'td.filename input', function() {
var checkbox = $(this).parent().children('input:checkbox');
$(checkbox).parent().parent().toggleClass('selected');
2013-12-12 19:45:45 +04:00
if ($(checkbox).is(':checked')) {
var selectedCount = $('td.filename input:checkbox:checked').length;
if (selectedCount === $('td.filename input:checkbox').length) {
$('#select_all').prop('checked', true);
}
} else {
$('#select_all').prop('checked',false);
}
2013-12-02 18:27:40 +04:00
procesSelection();
});
$('#fileList').on('click', 'td.filename a', function(event) {
2013-02-28 16:32:08 +04:00
var mime = $(this).parent().parent().data('mime');
if (mime !== 'httpd/unix-directory') {
event.preventDefault();
}
var filename = $(this).parent().parent().attr('data-file');
var tr = FileList.findFileEl(filename);
var renaming = tr.data('renaming');
if(!renaming){
if(mime.substr(0, 5) === 'text/'){ //no texteditor for now
return;
}
var type = $(this).parent().parent().data('type');
var permissions = $(this).parent().parent().data('permissions');
var action = FileActions.getDefault(mime, type, permissions);
if(action){
event.preventDefault();
action(filename);
}
}
});
2013-01-31 22:19:58 +04:00
/**
* Override crumb URL maker (hacky!)
*/
FileList.breadcrumb.getCrumbUrl = function(part, index) {
if (index === 0) {
return OC.linkTo('files', 'index.php');
}
return OC.linkTo('files_trashbin', 'index.php')+"?dir=" + encodeURIComponent(part.dir);
};
2013-01-18 16:11:29 +04:00
Files.generatePreviewUrl = function(urlSpec) {
return OC.generateUrl('/apps/files_trashbin/ajax/preview.php?') + $.param(urlSpec);
};
Files.getDownloadUrl = function(action, params) {
// no downloads
return '#';
};
Files.getAjaxUrl = function(action, params) {
var q = '';
if (params) {
q = '?' + OC.buildQueryString(params);
}
return OC.filePath('files_trashbin', 'ajax', action + '.php') + q;
};
/**
* Override crumb making to add "Deleted Files" entry
* and convert files with ".d" extensions to a more
* user friendly name.
*/
var oldMakeCrumbs = BreadCrumb.prototype._makeCrumbs;
BreadCrumb.prototype._makeCrumbs = function() {
var parts = oldMakeCrumbs.apply(this, arguments);
// duplicate first part
parts.unshift(parts[0]);
parts[1] = {
dir: '/',
name: t('files_trashbin', 'Deleted Files')
2013-01-18 20:50:44 +04:00
};
for (var i = 2; i < parts.length; i++) {
parts[i].name = getDeletedFileName(parts[i].name);
2013-01-18 20:50:44 +04:00
}
return parts;
};
FileActions.actions.dir = {
// only keep 'Open' action for navigation
'Open': FileActions.actions.dir.Open
};
});
function enableActions() {
$(".action").css("display", "inline");
$(":input:checkbox").css("display", "inline");
}
function disableActions() {
$(".action").css("display", "none");
$(":input:checkbox").css("display", "none");
}