From dfae77dec1650f171d09d4bde88ab74029f6e8c7 Mon Sep 17 00:00:00 2001 From: Michael Gapczynski Date: Mon, 30 Jul 2012 12:21:58 -0400 Subject: [PATCH] Add notifications and undo support for replacing files when renaming --- apps/files/css/files.css | 2 +- apps/files/js/filelist.js | 122 ++++++++++++++++++++++++++++++++------ 2 files changed, 104 insertions(+), 20 deletions(-) diff --git a/apps/files/css/files.css b/apps/files/css/files.css index e08d69f08d..317c0b8a1e 100644 --- a/apps/files/css/files.css +++ b/apps/files/css/files.css @@ -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; } #scanning-message{ top:40%; left:40%; position:absolute; display:none; } -#notification .undo { cursor:pointer; font-weight:bold; margin-left:1em; } \ No newline at end of file +#notification span { cursor:pointer; font-weight:bold; margin-left:1em; } \ No newline at end of file diff --git a/apps/files/js/filelist.js b/apps/files/js/filelist.js index aaf2e681ab..a414c5f830 100644 --- a/apps/files/js/filelist.js +++ b/apps/files/js/filelist.js @@ -136,24 +136,39 @@ FileList={ event.stopPropagation(); event.preventDefault(); var newname=input.val(); - tr.attr('data-file',newname); - td.children('a.name').empty(); + if (newname != name) { + if ($('tr').filterAttr('data-file', newname).length > 0) { + $('#notification').html(newname+' '+t('files', 'already exists')+''+t('files', 'replace')+''+t('files', 'cancel')+''); + $('#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); var path = td.children('a.name').attr('href'); td.children('a.name').attr('href', path.replace(encodeURIComponent(name), encodeURIComponent(newname))); - if(newname.indexOf('.')>0){ - basename=newname.substr(0,newname.lastIndexOf('.')); - }else{ - basename=newname; + if (newname.indexOf('.') > 0) { + var basename=newname.substr(0,newname.lastIndexOf('.')); + } else { + var basename=newname; } + td.children('a.name').empty(); var span=$(''); span.text(basename); td.children('a.name').append(span); - if(newname.indexOf('.')>0){ + if (newname.indexOf('.') > 0) { span.append($(''+newname.substr(newname.lastIndexOf('.'))+'')); } - $.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; }); input.click(function(event){ @@ -164,12 +179,64 @@ FileList={ form.trigger('submit'); }); }, + replace:function(oldName, newName) { + // 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+''+t('files', 'undo')+''); + $('#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() { + $.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') { + 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.text(basename); + td.children('a.name').append(span); + if (FileList.replaceNewName.indexOf('.') > 0) { + span.append($(''+FileList.replaceNewName.substr(FileList.replaceNewName.lastIndexOf('.'))+'')); + } + 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){ - if(FileList.deleteFiles || !FileList.useUndo){//finish any ongoing deletes first - FileList.finishDelete(function(){ - FileList.do_delete(files); - }); - return; + // Finish any existing actions + if (FileList.lastAction || !FileList.useUndo) { + FileList.lastAction(); } if(files.substr){ files=[files]; @@ -183,8 +250,10 @@ FileList={ procesSelection(); FileList.deleteCanceled=false; FileList.deleteFiles=files; + FileList.lastAction = function() { + FileList.finishDelete(null, true); + }; $('#notification').html(t('files', 'deleted')+' '+files+''+t('files', 'undo')+''); - $('#notification').data('deletefile',true); $('#notification').fadeIn(); }, finishDelete:function(ready,sync){ @@ -202,6 +271,7 @@ FileList={ }); FileList.deleteCanceled=true; FileList.deleteFiles=null; + FileList.lastAction = null; if(ready){ ready(); } @@ -215,18 +285,32 @@ FileList={ $(document).ready(function(){ $('#notification').hide(); $('#notification .undo').live('click', function(){ - if($('#notification').data('deletefile')) - { + if (FileList.deleteFiles) { $.each(FileList.deleteFiles,function(index,file){ $('tr').filterAttr('data-file',file).show(); }); FileList.deleteCanceled=true; 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 .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) $(window).bind('beforeunload', function (){ - FileList.finishDelete(null,true); + if (FileList.lastAction) { + FileList.lastAction(); + } }); });