-
Notifications
You must be signed in to change notification settings - Fork 7.6k
[OPEN] Fix #4332 and #4409: Sort issues fixed on the Working Set and Project Tree #4463
Changes from 2 commits
a81d8c3
9331daf
f65acfe
49c48ba
121ab4c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -485,14 +485,18 @@ define(function (require, exports, module) { | |
//(note: our actual jsTree theme CSS lives in brackets.less; we specify an empty .css | ||
// file because jsTree insists on loading one itself) | ||
sort : function (a, b) { | ||
var a1, b1, a2, b2; | ||
if (brackets.platform === "win") { | ||
// Windows: prepend folder names with a '0' and file names with a '1' so folders are listed first | ||
var a1 = ($(a).hasClass("jstree-leaf") ? "1" : "0") + this.get_text(a).toLowerCase(), | ||
b1 = ($(b).hasClass("jstree-leaf") ? "1" : "0") + this.get_text(b).toLowerCase(); | ||
return (a1 > b1) ? 1 : -1; | ||
a1 = ($(a).hasClass("jstree-leaf") ? "1" : "0") + $(a).text(); | ||
b1 = ($(b).hasClass("jstree-leaf") ? "1" : "0") + $(b).text(); | ||
a2 = FileUtils.getFilenameAndExtension(a1).name; | ||
b2 = FileUtils.getFilenameAndExtension(b1).name; | ||
} else { | ||
return this.get_text(a).toLowerCase() > this.get_text(b).toLowerCase() ? 1 : -1; | ||
a2 = $(a).text(); | ||
b2 = $(b).text(); | ||
} | ||
return a2.toLocaleLowerCase().localeCompare(b2.toLocaleLowerCase()); | ||
} | ||
}).bind( | ||
"before.jstree", | ||
|
@@ -764,6 +768,15 @@ define(function (require, exports, module) { | |
|
||
} | ||
|
||
/** | ||
* Forces createNewItem() to complete by removing focus from the rename field which causes | ||
* the new file to be written to disk | ||
*/ | ||
function forceFinishRename() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Moved due to a JSLint issue (used before defined) from a previous commit. |
||
$(".jstree-rename-input").blur(); | ||
} | ||
|
||
|
||
/** Returns the full path to the welcome project, which we open on first launch. | ||
* @private | ||
* @return {!string} fullPath reference | ||
|
@@ -824,7 +837,6 @@ define(function (require, exports, module) { | |
* project is loaded and tree is rendered, or rejected if the project path | ||
* fails to load. | ||
*/ | ||
|
||
function _loadProject(rootPath, isUpdating) { | ||
forceFinishRename(); // in case we're in the middle of renaming a file in the project | ||
|
||
|
@@ -1514,13 +1526,6 @@ define(function (require, exports, module) { | |
return result.promise(); | ||
} | ||
|
||
/** | ||
* Forces createNewItem() to complete by removing focus from the rename field which causes | ||
* the new file to be written to disk | ||
*/ | ||
function forceFinishRename() { | ||
$(".jstree-rename-input").blur(); | ||
} | ||
|
||
// Initialize variables and listeners that depend on the HTML DOM | ||
AppInit.htmlReady(function () { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,7 +23,7 @@ | |
|
||
|
||
/*jslint vars: true, plusplus: true, devel: true, nomen: true, indent: 4, maxerr: 50 */ | ||
/*global define, $, window */ | ||
/*global define, $, window, brackets */ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You don't need |
||
|
||
/** | ||
* Manages the workingSet sort methods. | ||
|
@@ -35,6 +35,7 @@ define(function (require, exports, module) { | |
CommandManager = require("command/CommandManager"), | ||
DocumentManager = require("document/DocumentManager"), | ||
PreferencesManager = require("preferences/PreferencesManager"), | ||
FileUtils = require("file/FileUtils"), | ||
AppInit = require("utils/AppInit"), | ||
Strings = require("strings"); | ||
|
||
|
@@ -292,28 +293,40 @@ define(function (require, exports, module) { | |
function (file1, file2) { | ||
var index1 = DocumentManager.findInWorkingSetAddedOrder(file1.fullPath), | ||
index2 = DocumentManager.findInWorkingSetAddedOrder(file2.fullPath); | ||
|
||
return index1 - index2; | ||
}, | ||
"workingSetAdd workingSetAddList" | ||
); | ||
register( | ||
Commands.SORT_WORKINGSET_BY_NAME, | ||
function (file1, file2) { | ||
return file1.name.toLocaleLowerCase().localeCompare(file2.name.toLocaleLowerCase()); | ||
var name1, name2; | ||
if (brackets.platform === "win") { | ||
name1 = FileUtils.getFilenameAndExtension(file1.name).name; | ||
name2 = FileUtils.getFilenameAndExtension(file2.name).name; | ||
} else { | ||
name1 = file1.name; | ||
name2 = file2.name; | ||
} | ||
return name1.toLocaleLowerCase().localeCompare(name2.toLocaleLowerCase()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I know these three sorting functions have some differences, but I still think we can at least refactor the code here to a function as follows and share it here and the other one below. How about moving your code here to a local function
And then replace the anonymous function here with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, I am working on doing something like this. But passing just the name and not the files, and making it a utils function in |
||
}, | ||
"workingSetAdd workingSetAddList" | ||
); | ||
register( | ||
Commands.SORT_WORKINGSET_BY_TYPE, | ||
function (file1, file2) { | ||
var ext1 = file1.name.split('.').pop(), | ||
ext2 = file2.name.split('.').pop(), | ||
cmp = ext1.localeCompare(ext2); | ||
var name1 = FileUtils.getFilenameAndExtension(file1.name), | ||
name2 = FileUtils.getFilenameAndExtension(file2.name), | ||
cmpExt = name1.extension.localeCompare(name2.extension); | ||
|
||
name1 = brackets.platform === "win" ? name1.name : file1.name; | ||
name2 = brackets.platform === "win" ? name2.name : file2.name; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since you need to conditionally assign name1 and name2, I think it is better to avoid using ternary assignments. Instead, just wrap these two assignments with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe the reuse of the variables I can change it to an if, but I still need to assign the names in both branches. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry, I just notice that you need to compare extensions first and therefore you need to call FileUtils.getFilenameAndExtension() in name1 and name2 initialization. So no need to make any changes. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To share the refactored code, change the anonymous function here as follows:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Correction: Don't use the above |
||
|
||
if (cmp === 0) { | ||
return file1.name.toLocaleLowerCase().localeCompare(file2.name.toLocaleLowerCase()); | ||
if (cmpExt === 0) { | ||
return name1.toLocaleLowerCase().localeCompare(name2.toLocaleLowerCase()); | ||
} else { | ||
return cmp; | ||
return cmpExt; | ||
} | ||
}, | ||
"workingSetAdd workingSetAddList" | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since this is only used for sorting, it might be a good idea to implement it in-place as a special string treatment instead of FileUtils method. Just a thought.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right now is only used for sorting since I created it for it, but it could have more uses. Lots of the other functions here actually deal with string too, but they know is a path or file name and treat them as one. Moving it to StringUtils or some other place like doesn't sound good.
Maybe it would be better to rename this one as
getFilenameName
or something like that, that only returns the file name without the extension to make it less specific.