-
Notifications
You must be signed in to change notification settings - Fork 7.6k
Change dialog label for Quick Open based on mode #2298
Changes from 5 commits
9568935
2a62d64
a63ee72
a581152
84b210e
8206f6b
3bd7dda
59993eb
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 |
---|---|---|
|
@@ -79,6 +79,11 @@ define(function (require, exports, module) { | |
|
||
/** @type {boolean} */ | ||
var dialogOpen = false; | ||
|
||
/** | ||
* The currently open quick open dialog. | ||
*/ | ||
var _curDialog; | ||
|
||
/** Object representing a search result with associated metadata (added as extra ad hoc fields) */ | ||
function SearchResult(label) { | ||
|
@@ -151,6 +156,8 @@ define(function (require, exports, module) { | |
*/ | ||
function QuickNavigateDialog() { | ||
this.$searchField = undefined; // defined when showDialog() is called | ||
this._handleFilter = this._handleFilter.bind(this); | ||
this._handleResultsFormatter = this._handleResultsFormatter.bind(this); | ||
} | ||
|
||
/** | ||
|
@@ -699,7 +706,13 @@ define(function (require, exports, module) { | |
return filteredList; | ||
} | ||
|
||
function _handleFilter(query) { | ||
/** | ||
* Handles changes to the current query in the search field. | ||
* @param {string} query The new query. | ||
*/ | ||
QuickNavigateDialog.prototype._handleFilter = function (query) { | ||
this._updateDialogLabel(query); | ||
|
||
var curDoc = DocumentManager.getCurrentDocument(); | ||
if (curDoc) { | ||
var filename = _filenameFromPath(curDoc.file.fullPath, true); | ||
|
@@ -719,8 +732,7 @@ define(function (require, exports, module) { | |
// No plugin: use default file search mode | ||
currentPlugin = null; | ||
return searchFileList(query); | ||
} | ||
|
||
}; | ||
|
||
/** | ||
* Formats item's label as properly escaped HTML text, highlighting sections that match 'query'. | ||
|
@@ -786,8 +798,12 @@ define(function (require, exports, module) { | |
return "<li>" + displayName + "<br /><span class='quick-open-path'>" + displayPath + "</span></li>"; | ||
} | ||
|
||
function _handleResultsFormatter(item) { | ||
var query = $("input#quickOpenSearch").val(); | ||
/** | ||
* Formats the entry for the given item to be displayed in the dropdown. | ||
* @param {Object} item The item to be displayed. | ||
*/ | ||
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. Missing return JSDoc. |
||
QuickNavigateDialog.prototype._handleResultsFormatter = function (item) { | ||
var query = this.$searchField.val(); | ||
|
||
var formatter; | ||
|
||
|
@@ -799,23 +815,49 @@ define(function (require, exports, module) { | |
formatter = _filenameResultsFormatter; | ||
} | ||
return formatter(item, query); | ||
} | ||
}; | ||
|
||
function setSearchFieldValue(prefix, initialString) { | ||
/** | ||
* Sets the value in the search field, updating the current mode and label based on the | ||
* given prefix. | ||
* @param {string} prefix The prefix that determines which mode we're in: must be empty (for file search), | ||
* "@" for go to definition, or ":" for go to line. | ||
* @param {string} initialString The query string to search for (without the prefix). | ||
*/ | ||
QuickNavigateDialog.prototype.setSearchFieldValue = function (prefix, initialString) { | ||
prefix = prefix || ""; | ||
initialString = initialString || ""; | ||
initialString = prefix + initialString; | ||
|
||
var $field = $("input#quickOpenSearch"); | ||
if ($field) { | ||
$field.val(initialString); | ||
$field.get(0).setSelectionRange(prefix.length, initialString.length); | ||
|
||
// Kick smart-autocomplete to update (it only listens for keyboard events) | ||
// (due to #1855, this will only pop up results list; it won't auto-"focus" the first result) | ||
$field.trigger("keyIn", [initialString]); | ||
} | ||
} | ||
var $field = this.$searchField; | ||
$field.val(initialString); | ||
$field.get(0).setSelectionRange(prefix.length, initialString.length); | ||
|
||
// Kick smart-autocomplete to update (it only listens for keyboard events) | ||
// (due to #1855, this will only pop up results list; it won't auto-"focus" the first result) | ||
$field.trigger("keyIn", [initialString]); | ||
|
||
this._updateDialogLabel(initialString); | ||
}; | ||
|
||
QuickNavigateDialog.prototype._updateDialogLabel = function (query) { | ||
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. JSDoc |
||
var prefix = (query.length > 0 ? query.charAt(0) : ""); | ||
|
||
// Update the dialog label based on the current prefix. | ||
var dialogLabel = ""; | ||
switch (prefix) { | ||
case ":": | ||
dialogLabel = Strings.CMD_GOTO_LINE; | ||
break; | ||
case "@": | ||
dialogLabel = Strings.CMD_GOTO_DEFINITION; | ||
break; | ||
default: | ||
dialogLabel = Strings.CMD_QUICK_OPEN; | ||
break; | ||
} | ||
$(".find-dialog-label", this.dialog).text(dialogLabel); | ||
}; | ||
|
||
/** | ||
* Close the dialog when the user clicks outside of it. Smart-autocomplete listens for this and automatically closes its popup, | ||
|
@@ -863,13 +905,12 @@ define(function (require, exports, module) { | |
} else { | ||
origSelection = null; | ||
} | ||
|
||
// Show the search bar ("dialog") | ||
var dialogHTML = Strings.CMD_QUICK_OPEN + ": <input type='text' autocomplete='off' id='quickOpenSearch' style='width: 30em'>"; | ||
var dialogHTML = "<span class='find-dialog-label'></span>: <input type='text' autocomplete='off' id='quickOpenSearch' style='width: 30em'>"; | ||
that._createDialogDiv(dialogHTML); | ||
that.$searchField = $("input#quickOpenSearch"); | ||
|
||
|
||
that.$searchField.smartAutoComplete({ | ||
source: [], | ||
maxResults: 20, | ||
|
@@ -878,8 +919,8 @@ define(function (require, exports, module) { | |
forceSelect: false, | ||
typeAhead: false, // won't work right now because smart auto complete | ||
// using internal raw results instead of filtered results for matching | ||
filter: _handleFilter, | ||
resultFormatter: _handleResultsFormatter | ||
filter: this._handleFilter, | ||
resultFormatter: this._handleResultsFormatter | ||
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. This use of Also, just for consistency, I noticed _handleFilter and _handleResultsFormatter aren't wrapped in a wrapper function like the event handlers are. I'd actually prefer if we remove all the wrapper event handler functions (line 927-932) and change the signatures instead. It might be worth renaming _handleFilter and _handleResultsFormatter too since they aren't actually event handlers. This is probably more refactoring than necessary, but I think at least we should fix the first issue with this/that and bind. |
||
}); | ||
|
||
that.$searchField.bind({ | ||
|
@@ -893,7 +934,7 @@ define(function (require, exports, module) { | |
// triggers lostFocus | ||
}); | ||
|
||
setSearchFieldValue(prefix, initialString); | ||
this.setSearchFieldValue(prefix, initialString); | ||
|
||
// Start fetching the file list, which will be needed the first time the user enters an un-prefixed query. If FileIndexManager's | ||
// caches are out of date, this list might take some time to asynchronously build. See searchFileList() for how this is handled. | ||
|
@@ -918,10 +959,10 @@ define(function (require, exports, module) { | |
*/ | ||
function beginSearch(prefix, initialString) { | ||
if (dialogOpen) { | ||
setSearchFieldValue(prefix, initialString); | ||
_curDialog.setSearchFieldValue(prefix, initialString); | ||
} else { | ||
var dialog = new QuickNavigateDialog(); | ||
dialog.showDialog(prefix, initialString); | ||
_curDialog = new QuickNavigateDialog(); | ||
_curDialog.showDialog(prefix, initialString); | ||
} | ||
} | ||
|
||
|
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.
Missing return JSDoc