From d265c814e1bed730de7d765ec69ae288b3e17319 Mon Sep 17 00:00:00 2001 From: Michael Jumper Date: Fri, 3 Jul 2015 11:44:51 -0700 Subject: [PATCH] GUAC-1172: Sort files lexicographically with directories first. --- .../app/client/directives/guacFileBrowser.js | 47 +++++++++++++++++-- 1 file changed, 43 insertions(+), 4 deletions(-) diff --git a/guacamole/src/main/webapp/app/client/directives/guacFileBrowser.js b/guacamole/src/main/webapp/app/client/directives/guacFileBrowser.js index 1f86fb62d..55323f690 100644 --- a/guacamole/src/main/webapp/app/client/directives/guacFileBrowser.js +++ b/guacamole/src/main/webapp/app/client/directives/guacFileBrowser.js @@ -157,16 +157,55 @@ angular.module('client').directive('guacFileBrowser', [function guacFileBrowser( }; + /** + * Sorts the given map of files, returning an array of those files + * grouped by file type (directories first, followed by non- + * directories) and sorted lexicographically. + * + * @param {Object.} files + * The map of files to sort. + * + * @returns {ManagedFilesystem.File[]} + * An array of all files in the given map, sorted + * lexicographically with directories first, followed by non- + * directories. + */ + var sortFiles = function sortFiles(files) { + + // Get all given files as an array + var unsortedFiles = []; + for (var name in files) + unsortedFiles.push(files[name]); + + // Sort files - directories first, followed by all other files + // sorted by name + return unsortedFiles.sort(function fileComparator(a, b) { + + // Directories come before non-directories + if ($scope.isDirectory(a) && !$scope.isDirectory(b)) + return -1; + + // Non-directories come after directories + if (!$scope.isDirectory(a) && $scope.isDirectory(b)) + return 1; + + // All other combinations are sorted by name + return a.name.localeCompare(b.name); + + }); + + }; + // Update the contents of the file browser whenever the current directory (or its contents) changes $scope.$watch('filesystem.currentDirectory.files', function currentDirectoryChanged(files) { // Clear current content currentDirectoryContents.html(''); - // Display all files within current directory - for (var name in files) { - currentDirectoryContents.append(createFileElement(files[name])); - } + // Display all files within current directory, sorted + angular.forEach(sortFiles(files), function displayFile(file) { + currentDirectoryContents.append(createFileElement(file)); + }); });