Skip to content
Snippets Groups Projects
Commit dfae77de authored by Michael Gapczynski's avatar Michael Gapczynski
Browse files

Add notifications and undo support for replacing files when renaming

parent b465fc84
Branches
No related tags found
No related merge requests found
...@@ -89,4 +89,4 @@ a.action>img { max-height:16px; max-width:16px; vertical-align:text-bottom; } ...@@ -89,4 +89,4 @@ a.action>img { max-height:16px; max-width:16px; vertical-align:text-bottom; }
#navigation>ul>li:first-child+li { padding-top:2.9em; } #navigation>ul>li:first-child+li { padding-top:2.9em; }
#scanning-message{ top:40%; left:40%; position:absolute; display:none; } #scanning-message{ top:40%; left:40%; position:absolute; display:none; }
#notification .undo { cursor:pointer; font-weight:bold; margin-left:1em; } #notification span { cursor:pointer; font-weight:bold; margin-left:1em; }
\ No newline at end of file \ No newline at end of file
...@@ -136,24 +136,39 @@ FileList={ ...@@ -136,24 +136,39 @@ FileList={
event.stopPropagation(); event.stopPropagation();
event.preventDefault(); event.preventDefault();
var newname=input.val(); var newname=input.val();
if (newname != name) {
if ($('tr').filterAttr('data-file', newname).length > 0) {
$('#notification').html(newname+' '+t('files', 'already exists')+'<span class="replace">'+t('files', 'replace')+'</span><span class="cancel">'+t('files', 'cancel')+'</span>');
$('#notification').data('oldName', name);
$('#notification').data('newName', newname);
$('#notification').fadeIn();
newname = name;
} else {
$.get(OC.filePath('files','ajax','rename.php'), { dir : $('#dir').val(), newname: newname, file: name },function(result) {
if (!result || result.status == 'error') {
OC.dialogs.alert(result.data.message, 'Error moving file');
newname = name;
}
});
}
}
tr.attr('data-file', newname); tr.attr('data-file', newname);
td.children('a.name').empty();
var path = td.children('a.name').attr('href'); var path = td.children('a.name').attr('href');
td.children('a.name').attr('href', path.replace(encodeURIComponent(name), encodeURIComponent(newname))); td.children('a.name').attr('href', path.replace(encodeURIComponent(name), encodeURIComponent(newname)));
if (newname.indexOf('.') > 0) { if (newname.indexOf('.') > 0) {
basename=newname.substr(0,newname.lastIndexOf('.')); var basename=newname.substr(0,newname.lastIndexOf('.'));
} else { } else {
basename=newname; var basename=newname;
} }
td.children('a.name').empty();
var span=$('<span class="nametext"></span>'); var span=$('<span class="nametext"></span>');
span.text(basename); span.text(basename);
td.children('a.name').append(span); td.children('a.name').append(span);
if (newname.indexOf('.') > 0) { if (newname.indexOf('.') > 0) {
span.append($('<span class="extension">'+newname.substr(newname.lastIndexOf('.'))+'</span>')); span.append($('<span class="extension">'+newname.substr(newname.lastIndexOf('.'))+'</span>'));
} }
$.get(OC.filePath('files','ajax','rename.php'), { dir : $('#dir').val(), newname: newname, file: name },function(){
tr.data('renaming',false); tr.data('renaming',false);
});
return false; return false;
}); });
input.click(function(event){ input.click(function(event){
...@@ -164,12 +179,64 @@ FileList={ ...@@ -164,12 +179,64 @@ FileList={
form.trigger('submit'); form.trigger('submit');
}); });
}, },
do_delete:function(files){ replace:function(oldName, newName) {
if(FileList.deleteFiles || !FileList.useUndo){//finish any ongoing deletes first // Finish any existing actions
if (FileList.lastAction || !FileList.useUndo) {
FileList.lastAction();
}
var tr = $('tr').filterAttr('data-file', oldName);
tr.hide();
FileList.replaceCanceled = false;
FileList.replaceOldName = oldName;
FileList.replaceNewName = newName;
FileList.lastAction = function() {
FileList.finishReplace();
};
$('#notification').html(t('files', 'replaced')+' '+newName+' '+t('files', 'with')+' '+oldName+'<span class="undo">'+t('files', 'undo')+'</span>');
$('#notification').fadeIn();
},
finishReplace:function() {
if (!FileList.replaceCanceled && FileList.replaceOldName && FileList.replaceNewName) {
// Delete the file being replaced and rename the replacement
FileList.deleteCanceled = false;
FileList.deleteFiles = [FileList.replaceNewName];
FileList.finishDelete(function() { FileList.finishDelete(function() {
FileList.do_delete(files); $.ajax({url: OC.filePath('files', 'ajax', 'rename.php'), async: false, data: { dir: $('#dir').val(), newname: FileList.replaceNewName, file: FileList.replaceOldName }, success: function(result) {
}); if (result && result.status == 'success') {
return; var tr = $('tr').filterAttr('data-file', FileList.replaceOldName);
tr.attr('data-file', FileList.replaceNewName);
var td = tr.children('td.filename');
td.children('a.name .span').text(FileList.replaceNewName);
var path = td.children('a.name').attr('href');
td.children('a.name').attr('href', path.replace(encodeURIComponent(FileList.replaceOldName), encodeURIComponent(FileList.replaceNewName)));
if (FileList.replaceNewName.indexOf('.') > 0) {
var basename = FileList.replaceNewName.substr(0, FileList.replaceNewName.lastIndexOf('.'));
} else {
var basename = FileList.replaceNewName;
}
td.children('a.name').empty();
var span = $('<span class="nametext"></span>');
span.text(basename);
td.children('a.name').append(span);
if (FileList.replaceNewName.indexOf('.') > 0) {
span.append($('<span class="extension">'+FileList.replaceNewName.substr(FileList.replaceNewName.lastIndexOf('.'))+'</span>'));
}
tr.show();
} else {
OC.dialogs.alert(result.data.message, 'Error moving file');
}
FileList.replaceCanceled = true;
FileList.replaceOldName = null;
FileList.replaceNewName = null;
FileList.lastAction = null;
}});
}, true);
}
},
do_delete:function(files){
// Finish any existing actions
if (FileList.lastAction || !FileList.useUndo) {
FileList.lastAction();
} }
if(files.substr){ if(files.substr){
files=[files]; files=[files];
...@@ -183,8 +250,10 @@ FileList={ ...@@ -183,8 +250,10 @@ FileList={
procesSelection(); procesSelection();
FileList.deleteCanceled=false; FileList.deleteCanceled=false;
FileList.deleteFiles=files; FileList.deleteFiles=files;
FileList.lastAction = function() {
FileList.finishDelete(null, true);
};
$('#notification').html(t('files', 'deleted')+' '+files+'<span class="undo">'+t('files', 'undo')+'</span>'); $('#notification').html(t('files', 'deleted')+' '+files+'<span class="undo">'+t('files', 'undo')+'</span>');
$('#notification').data('deletefile',true);
$('#notification').fadeIn(); $('#notification').fadeIn();
}, },
finishDelete:function(ready,sync){ finishDelete:function(ready,sync){
...@@ -202,6 +271,7 @@ FileList={ ...@@ -202,6 +271,7 @@ FileList={
}); });
FileList.deleteCanceled=true; FileList.deleteCanceled=true;
FileList.deleteFiles=null; FileList.deleteFiles=null;
FileList.lastAction = null;
if(ready){ if(ready){
ready(); ready();
} }
...@@ -215,18 +285,32 @@ FileList={ ...@@ -215,18 +285,32 @@ FileList={
$(document).ready(function(){ $(document).ready(function(){
$('#notification').hide(); $('#notification').hide();
$('#notification .undo').live('click', function(){ $('#notification .undo').live('click', function(){
if($('#notification').data('deletefile')) if (FileList.deleteFiles) {
{
$.each(FileList.deleteFiles,function(index,file){ $.each(FileList.deleteFiles,function(index,file){
$('tr').filterAttr('data-file',file).show(); $('tr').filterAttr('data-file',file).show();
}); });
FileList.deleteCanceled=true; FileList.deleteCanceled=true;
FileList.deleteFiles=null; FileList.deleteFiles=null;
} else if (FileList.replaceOldName && FileList.replaceNewName) {
$('tr').filterAttr('data-file', FileList.replaceOldName).show();
FileList.replaceCanceled = true;
FileList.replaceOldName = null;
FileList.replaceNewName = null;
} }
$('#notification').fadeOut(); $('#notification').fadeOut();
}); });
$('#notification .replace').live('click', function() {
$('#notification').fadeOut('400', function() {
FileList.replace($('#notification').data('oldName'), $('#notification').data('newName'));
});
});
$('#notification .cancel').live('click', function() {
$('#notification').fadeOut();
});
FileList.useUndo=('onbeforeunload' in window) FileList.useUndo=('onbeforeunload' in window)
$(window).bind('beforeunload', function (){ $(window).bind('beforeunload', function (){
FileList.finishDelete(null,true); if (FileList.lastAction) {
FileList.lastAction();
}
}); });
}); });
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment