0

Files.app: Not use URL in sorting

In URL, some non-alphanumeric characters are represented as "%xx" format and it makes the sort incorrect. This patch creates the utility functions to sort and use them in sorting.

BUG=404061
TEST=manually tested

Review URL: https://codereview.chromium.org/479503002

Cr-Commit-Position: refs/heads/master@{#289914}
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@289914 0039d316-1c4b-4281-b951-d872f2087c98
This commit is contained in:
yoshiki@chromium.org
2014-08-15 16:34:38 +00:00
parent 913997ed6b
commit 6308bcebc9
4 changed files with 36 additions and 30 deletions

@ -1005,6 +1005,34 @@ util.isSameFileSystem = function(fileSystem1, fileSystem2) {
return util.isSameEntry(fileSystem1.root, fileSystem2.root);
};
/**
* Collator for sorting.
* @type {Intl.Collator}
*/
util.collator = new Intl.Collator([], {usage: 'sort',
numeric: true,
sensitivity: 'base'});
/**
* Compare by name. The 2 entries must be in same directory.
* @param {Entry} entry1 First entry.
* @param {Entry} entry2 Second entry.
* @return {number} Compare result.
*/
util.compareName = function(entry1, entry2) {
return util.collator.compare(entry1.name, entry2.name);
};
/**
* Compare by path.
* @param {Entry} entry1 First entry.
* @param {Entry} entry2 Second entry.
* @return {number} Compare result.
*/
util.comparePath = function(entry1, entry2) {
return util.collator.compare(entry1.fullPath, entry2.fullPath);
};
/**
* Checks if the child entry is a descendant of another entry. If the entries
* point to the same file or directory, then returns false.

@ -415,14 +415,8 @@ function FileListModel(metadataCache) {
*/
this.metadataCache_ = metadataCache;
/**
* Collator for sorting.
* @type {Intl.Collator}
*/
this.collator_ = new Intl.Collator([], {numeric: true, sensitivity: 'base'});
// Initialize compare functions.
this.setCompareFunction('name', this.compareName_.bind(this));
this.setCompareFunction('name', util.compareName);
this.setCompareFunction('modificationTime', this.compareMtime_.bind(this));
this.setCompareFunction('size', this.compareSize_.bind(this));
this.setCompareFunction('type', this.compareType_.bind(this));
@ -432,18 +426,6 @@ FileListModel.prototype = {
__proto__: cr.ui.ArrayDataModel.prototype
};
/**
* Compare by mtime first, then by name.
* @param {Entry} a First entry.
* @param {Entry} b Second entry.
* @return {number} Compare result.
* @private
*/
FileListModel.prototype.compareName_ = function(a, b) {
var result = this.collator_.compare(a.name, b.name);
return result !== 0 ? result : a.toURL().localeCompare(b.toURL());
};
/**
* Compare by mtime first, then by name.
* @param {Entry} a First entry.
@ -464,7 +446,7 @@ FileListModel.prototype.compareMtime_ = function(a, b) {
if (aTime < bTime)
return -1;
return this.compareName_(a, b);
return util.compareName(a, b);
};
/**
@ -481,7 +463,7 @@ FileListModel.prototype.compareSize_ = function(a, b) {
var bCachedFilesystem = this.metadataCache_.getCached(b, 'filesystem');
var bSize = bCachedFilesystem ? bCachedFilesystem.size : 0;
return aSize !== bSize ? aSize - bSize : this.compareName_(a, b);
return aSize !== bSize ? aSize - bSize : util.compareName(a, b);
};
/**
@ -499,8 +481,8 @@ FileListModel.prototype.compareType_ = function(a, b) {
var aType = FileType.typeToString(FileType.getType(a));
var bType = FileType.typeToString(FileType.getType(b));
var result = this.collator_.compare(aType, bType);
return result !== 0 ? result : this.compareName_(a, b);
var result = util.collator.compare(aType, bType);
return result !== 0 ? result : util.compareName(a, b);
};
/**

@ -279,9 +279,7 @@ DirectoryItem.prototype.updateSubDirectories = function(
}
var sortEntries = function(fileFilter, entries) {
entries.sort(function(a, b) {
return (a.name.toLowerCase() > b.name.toLowerCase()) ? 1 : -1;
});
entries.sort(util.compareName);
return entries.filter(fileFilter.filter.bind(fileFilter));
};
@ -514,6 +512,7 @@ VolumeItem.prototype.updateSubDirectories = function(recursive) {
for (var key in this.volumeInfo.fakeEntries)
entries.push(this.volumeInfo.fakeEntries[key]);
}
// This list is sorted by URL on purpose.
entries.sort(function(a, b) { return a.toURL() < b.toURL(); });
for (var i = 0; i < entries.length; i++) {

@ -275,10 +275,7 @@ FolderShortcutsDataModel.prototype = {
* Otherwise, returns 1.
*/
compare: function(a, b) {
return a.toURL().localeCompare(
b.toURL(),
undefined, // locale parameter, use default locale.
{usage: 'sort', numeric: true});
return util.comparePath(a, b);
},
/**