diff options
author | Julius Härtl <jus@bitgrid.net> | 2018-11-08 13:30:39 +0100 |
---|---|---|
committer | Julius Härtl <jus@bitgrid.net> | 2018-11-16 14:12:41 +0100 |
commit | 3e11337b3b4c36df20d5565d60d05995b0314363 (patch) | |
tree | 560eb9976da313fa391c83ad62a52d2e52b2bffc /core/js | |
parent | 09efdfb4e640a748d9f7af1d120a4315028d969a (diff) | |
download | nextcloud-server-3e11337b3b4c36df20d5565d60d05995b0314363.tar.gz nextcloud-server-3e11337b3b4c36df20d5565d60d05995b0314363.zip |
Allow sorting in the file picker
Signed-off-by: Julius Härtl <jus@bitgrid.net>
Diffstat (limited to 'core/js')
-rw-r--r-- | core/js/oc-dialogs.js | 52 |
1 files changed, 46 insertions, 6 deletions
diff --git a/core/js/oc-dialogs.js b/core/js/oc-dialogs.js index dee74502a6f..e46dd9a57ea 100644 --- a/core/js/oc-dialogs.js +++ b/core/js/oc-dialogs.js @@ -192,6 +192,9 @@ var OCdialogs = { */ filepicker:function(title, callback, multiselect, mimetypeFilter, modal, type) { var self = this; + + this.filepicker.sortField = 'name'; + this.filepicker.sortOrder = 'asc'; // avoid opening the picker twice if (this.filepicker.loading) { return; @@ -252,6 +255,7 @@ var OCdialogs = { } self.$filePicker.ready(function() { + self.$fileListHeader = self.$filePicker.find('.filelist thead tr'); self.$filelist = self.$filePicker.find('.filelist tbody'); self.$dirTree = self.$filePicker.find('.dirtree'); self.$dirTree.on('click', 'div:not(:last-child)', self, function (event) { @@ -260,6 +264,12 @@ var OCdialogs = { self.$filelist.on('click', 'tr', function(event) { self._handlePickerClick(event, $(this), type); }); + self.$fileListHeader.on('click', 'a', function(event) { + var dir = self.$filePicker.data('path'); + self.filepicker.sortField = $(event.currentTarget).data('sort'); + self.filepicker.sortOrder = self.filepicker.sortOrder === 'asc' ? 'desc' : 'asc'; + self._fillFilePicker(dir); + }); self._fillFilePicker(''); }); @@ -824,7 +834,7 @@ var OCdialogs = { var self = this; $.get(OC.filePath('core', 'templates', 'filepicker.html'), function(tmpl) { self.$filePickerTemplate = $(tmpl); - self.$listTmpl = self.$filePickerTemplate.find('.filelist tr:first-child').detach(); + self.$listTmpl = self.$filePickerTemplate.find('.filelist tbody tr:first-child').detach(); defer.resolve(self.$filePickerTemplate); }) .fail(function(jqXHR, textStatus, errorThrown) { @@ -892,20 +902,50 @@ var OCdialogs = { if (typeof(filter) === "string") { filter = [filter]; } + self.$fileListHeader.find('.sort-indicator').addClass('hidden').removeClass('icon-triangle-n').removeClass('icon-triangle-s'); + self.$fileListHeader.find('[data-sort=' + self.filepicker.sortField + '] .sort-indicator').removeClass('hidden'); + if (self.filepicker.sortOrder === 'asc') { + self.$fileListHeader.find('[data-sort=' + self.filepicker.sortField + '] .sort-indicator').addClass('icon-triangle-s'); + } else { + self.$fileListHeader.find('[data-sort=' + self.filepicker.sortField + '] .sort-indicator').addClass('icon-triangle-n'); + } self.filepicker.filesClient.getFolderContents(dir).then(function(status, files) { if (filter && filter.length > 0 && filter.indexOf('*') === -1) { files = files.filter(function (file) { return file.type === 'dir' || filter.indexOf(file.mimetype) !== -1; }); } - files = files.sort(function(a, b) { - if (a.type === 'dir' && b.type !== 'dir') { + + var Comparators = { + name: function(fileInfo1, fileInfo2) { + if (fileInfo1.type === 'dir' && fileInfo2.type !== 'dir') { + return -1; + } + if (fileInfo1.type !== 'dir' && fileInfo2.type === 'dir') { + return 1; + } + return OC.Util.naturalSortCompare(fileInfo1.name, fileInfo2.name); + }, + size: function(fileInfo1, fileInfo2) { + return fileInfo1.size - fileInfo2.size; + }, + mtime: function(fileInfo1, fileInfo2) { + return fileInfo1.mtime - fileInfo2.mtime; + } + }; + var comparator = Comparators[self.filepicker.sortField] || Comparators.name; + files = files.sort(function(file1, file2) { + var isFavorite = function(fileInfo) { + return fileInfo.tags && fileInfo.tags.indexOf(OC.TAG_FAVORITE) >= 0; + }; + + if (isFavorite(file1) && !isFavorite(file2)) { return -1; - } else if(a.type !== 'dir' && b.type === 'dir') { + } else if (!isFavorite(file1) && isFavorite(file2)) { return 1; - } else { - return a.name.localeCompare(b.name, undefined, {numeric: true}); } + + return self.filepicker.sortOrder === 'asc' ? comparator(file1, file2) : -comparator(file1, file2); }); self._fillSlug(); |