From ff729608e18760e1870b5357acad0d279377e354 Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Fri, 7 Jul 2017 10:58:51 -0400 Subject: [PATCH] delete attachments --- src/app/services/apiService.js | 3 +- src/app/services/cipherService.js | 2 +- src/app/vault/vaultAttachmentsController.js | 36 ++++++++++++++++++--- src/app/vault/vaultController.js | 6 ++-- 4 files changed, 38 insertions(+), 9 deletions(-) diff --git a/src/app/services/apiService.js b/src/app/services/apiService.js index 7178ac84e8..6c2c58da20 100644 --- a/src/app/services/apiService.js +++ b/src/app/services/apiService.js @@ -46,7 +46,8 @@ method: 'POST', headers: { 'Content-Type': undefined }, params: { id: '@id' } - } + }, + delAttachment: { url: _apiUri + '/ciphers/:id/attachment/:attachmentId/delete', method: 'POST', params: { id: '@id', attachmentId: '@attachmentId' } } }); _service.organizations = $resource(_apiUri + '/organizations/:id', {}, { diff --git a/src/app/services/cipherService.js b/src/app/services/cipherService.js index d357044cbc..a2c1da6715 100644 --- a/src/app/services/cipherService.js +++ b/src/app/services/cipherService.js @@ -70,7 +70,7 @@ angular name: _service.decryptProperty(encryptedCipher.Data.Name, key, false), username: _service.decryptProperty(encryptedCipher.Data.Username, key, true), password: _service.decryptProperty(encryptedCipher.Data.Password, key, true), - hasAttachments: !!encryptedCipher.Attachments + hasAttachments: !!encryptedCipher.Attachments && encryptedCipher.Attachments.length > 0 }; return login; diff --git a/src/app/vault/vaultAttachmentsController.js b/src/app/vault/vaultAttachmentsController.js index 77879b155e..5ac53ba77a 100644 --- a/src/app/vault/vaultAttachmentsController.js +++ b/src/app/vault/vaultAttachmentsController.js @@ -7,6 +7,7 @@ $scope.login = {}; $scope.readOnly = false; $scope.loading = true; + var closing = false; apiService.logins.get({ id: loginId }, function (login) { $scope.login = cipherService.decryptLogin(login); @@ -46,10 +47,8 @@ }).then(function (response) { $analytics.eventTrack('Added Attachment'); toastr.success('The attachment has been added.'); - $uibModalInstance.close({ - action: 'attach', - data: $scope.login - }); + closing = true; + $uibModalInstance.close(true); }); }; reader.onerror = function (evt) { @@ -104,7 +103,36 @@ return null; } + $scope.remove = function (attachment) { + if (!confirm('Are you sure you want to delete this attachment (' + attachment.fileName + ')?')) { + return; + } + + attachment.loading = true; + apiService.ciphers.delAttachment({ id: loginId, attachmentId: attachment.id }).$promise.then(function () { + attachment.loading = false; + $analytics.eventTrack('Deleted Attachment'); + var index = $scope.login.attachments.indexOf(attachment); + if (index > -1) { + $scope.login.attachments.splice(index, 1); + } + }, function () { + toastr.error('Cannot delete attachment.'); + attachment.loading = false; + }); + }; + $scope.close = function () { $uibModalInstance.dismiss('cancel'); }; + + $scope.$on('modal.closing', function (e, reason, closed) { + if (closing) { + return; + } + + e.preventDefault(); + closing = true; + $uibModalInstance.close(!!$scope.login.attachments && $scope.login.attachments.length > 0); + }); }); diff --git a/src/app/vault/vaultController.js b/src/app/vault/vaultController.js index dde646c4d4..dad36be735 100644 --- a/src/app/vault/vaultController.js +++ b/src/app/vault/vaultController.js @@ -217,7 +217,7 @@ return; } - var addModel = $uibModal.open({ + var attachmentModel = $uibModal.open({ animation: true, templateUrl: 'app/vault/views/vaultAttachments.html', controller: 'vaultAttachmentsController', @@ -226,8 +226,8 @@ } }); - addModel.result.then(function (data) { - + attachmentModel.result.then(function (hasAttachments) { + login.hasAttachments = hasAttachments; }); }); };