From d4ff6d18f7700686ffdafa9a450dda238a4263c1 Mon Sep 17 00:00:00 2001 From: Fortier Date: Tue, 16 Jan 2018 12:07:16 -0800 Subject: [PATCH] modified directory-scanning to short-circuit if match found --- .../directives/documents.manager.directive.js | 16 +++++++--------- .../documents.manager.rename.directive.js | 14 ++++++-------- 2 files changed, 13 insertions(+), 17 deletions(-) diff --git a/modules/documents/client/directives/documents.manager.directive.js b/modules/documents/client/directives/documents.manager.directive.js index 635959525..4efe85db6 100644 --- a/modules/documents/client/directives/documents.manager.directive.js +++ b/modules/documents/client/directives/documents.manager.directive.js @@ -655,17 +655,15 @@ angular.module('documents') return Promise.reject('Destination required for moving files and folders.'); } else { //Check destination directory for folders of identical name to selected folder(s). - var repeat = false; - destination.children.forEach(function(destFolder) { - self.moveSelected.moveableFolders.forEach(function(srcFolder) { - if (destFolder.model.name === srcFolder.model.name) { - repeat = true; - AlertService.error('Folder name ' + destFolder.model.name + ' already exists in ' + destination.model.name); - } - + var repeat = _.find(self.moveSelected.moveableFolders, function(srcFolder) { + return _.find(destination.children, function(destFolder) { + return destFolder.model.name === srcFolder.model.name; }); }); - if (!repeat) { + //If repeat name found, throw error. Otherwise, continue with move. + if (repeat) { + AlertService.error('Folder name ' + repeat.model.name + ' already exists in ' + destination.model.name); + } else { var dirs = _.size(self.checkedDirs); var files = _.size(self.checkedFiles); if (dirs === 0 && files === 0) { diff --git a/modules/documents/client/directives/documents.manager.rename.directive.js b/modules/documents/client/directives/documents.manager.rename.directive.js index 93b3df272..7d3810de0 100644 --- a/modules/documents/client/directives/documents.manager.rename.directive.js +++ b/modules/documents/client/directives/documents.manager.rename.directive.js @@ -34,15 +34,13 @@ angular.module('documents') self.ok = function () { //Check if there is already a folder of name ${entryText} in current directory. - var repeat = false; - $scope.node.parent.children.forEach(function(element) { - if (self.entryText === element.model.name) { - repeat = true; - AlertService.error(self.entryText + " already exists in this directory."); - } + var repeat = _.find($scope.node.parent.children, function(element) { + return element.model.name === self.entryText; }); - //If ${entryText} is a unique name for this directory, rename the folder. - if (!repeat) { + //If ${entryText} is a unique name for this directory, rename the folder, otherwise throw an error. + if (repeat) { + AlertService.error(self.entryText + " already exists in this directory."); + } else { DocumentMgrService.renameDirectory($scope.project, $scope.node, self.entryText) .then( function (result) {