From 825d92d59c54a5274f94e7058c4458238f9586a3 Mon Sep 17 00:00:00 2001
From: Sam Tuke <samtuke@owncloud.com>
Date: Wed, 4 Jul 2012 14:28:08 +0100
Subject: [PATCH] Added undo functionality to delete user procedure on user
 settings page

---
 settings/js/users.js         | 96 ++++++++++++++++++++++++++++++++----
 settings/templates/users.php |  2 +
 2 files changed, 89 insertions(+), 9 deletions(-)

diff --git a/settings/js/users.js b/settings/js/users.js
index c04df81d82..dfa28e4c4c 100644
--- a/settings/js/users.js
+++ b/settings/js/users.js
@@ -4,6 +4,72 @@
  * See the COPYING-README file.
  */
 
+UserList={
+	useUndo:true,
+	
+	/**
+	 * @brief Initiate user deletion process in UI
+	 * @param string uid the user ID to be deleted
+	 *
+	 * Does not actually delete the user; it sets them for
+	 * deletion when the current page is unloaded, at which point 
+	 * finishDelete() completes the process. This allows for 'undo'.
+	 */
+	do_delete:function( uid ) {
+		
+		UserList.deleteUid = uid;
+		
+		// Set undo flag
+		UserList.deleteCanceled = false;
+		
+		// Hide user in table to reflect deletion
+		$(this).parent().parent().hide();
+		$('tr').filterAttr( 'data-uid', UserList.deleteUid ).hide();
+		
+		// Provide user with option to undo
+		$('#notification').text(t('files','undo delete user'));
+		$('#notification').data('deleteuser',true);
+		$('#notification').fadeIn();
+			
+	},
+	
+	/**
+	 * @brief Delete a user via ajax
+	 * @param bool ready whether to use ready() upon completion
+	 *
+	 * Executes deletion via ajax of user identified by property deleteUid 
+	 * if 'undo' has not been used.  Completes the user deletion procedure 
+	 * and reflects success in UI.
+	 */
+	finishDelete:function( ready ){
+		
+		// Check deletion has not been undone
+		if( !UserList.deleteCanceled && UserList.deleteUid ){
+			
+			// Delete user via ajax
+			$.post(
+				OC.filePath('settings','ajax','removeuser.php'),
+				{username:UserList.deleteUid},
+				function(result){
+					
+					// Remove undo option, & remove user from table
+					boolOperationFinished( 
+						data, function(){
+							$('#notification').fadeOut();
+							$('tr').filterAttr( 'data-uid', username ).remove();
+							UserList.deleteCanceled=true;
+							UserList.deleteFiles=null;
+							if( ready ){
+								ready();
+							}
+						}
+					);
+				}
+			);
+ 		}
+	}
+}
+
 $(document).ready(function(){
 	function setQuota(uid,quota,ready){
 		$.post(
@@ -61,15 +127,12 @@ $(document).ready(function(){
 	});
 	
 	$('td.remove>img').live('click',function(event){
-		var uid=$(this).parent().parent().data('uid');
-		$.post(
-			OC.filePath('settings','ajax','removeuser.php'),
-			{username:uid},
-			function(result){
-			
-			}
-		);
-		$(this).parent().parent().remove();
+		
+		var uid = $(this).parent().parent().data('uid');
+		
+		// Call function for handling delete/undo
+		UserList.do_delete( uid );
+		
 	});
 	
 	$('td.password>img').live('click',function(event){
@@ -222,4 +285,19 @@ $(document).ready(function(){
 			}
 		);
 	});
+	// Handle undo notifications
+	$('#notification').hide();
+	$('#notification').click(function(){
+		if($('#notification').data('deleteuser'))
+		{
+			$( 'tr' ).filterAttr( 'data-uid', UserList.deleteUid ).show();
+			UserList.deleteCanceled=true;
+			UserList.deleteFiles=null;
+		}
+		$('#notification').fadeOut();
+	});
+	UserList.useUndo=('onbeforeunload' in window)
+	$(window).bind('beforeunload', function (){
+		UserList.finishDelete(null);
+	});
 });
diff --git a/settings/templates/users.php b/settings/templates/users.php
index c042f2664e..5511242456 100644
--- a/settings/templates/users.php
+++ b/settings/templates/users.php
@@ -52,6 +52,8 @@ foreach($_["groups"] as $group) {
 	</div>
 </div>
 
+<div id='notification'></div>
+
 <table data-groups="<?php echo implode(', ',$allGroups);?>">
 	<thead>
 		<tr>
-- 
GitLab