Adding project search history

This commit is contained in:
Brian Smith 2015-11-04 23:59:35 -07:00
parent 5e396a7f78
commit 7ff9d54366

View file

@ -17,8 +17,8 @@ define([
var self = this;
this.element = document.find(".searchbar");
this.input = this.element.find(".search-box");
this.maxMatches = Settings.get("user").maxSearchMatches || 50;
this.maxMatches = Settings.get("user").maxSearchMatches || 50;
command.on("init:restart", function() {
self.maxMatches = Settings.get("user").maxSearchMatches || 50;
});
@ -28,6 +28,12 @@ define([
running: false
};
this.searchHistory = {
history: [],
currentIndex: 0,
temporaryQuery: ''
};
this.bindInput();
this.bindButtons();
};
@ -35,6 +41,7 @@ define([
Searchbar.prototype = {
bindInput: function() {
var input = this.input;
var hist = this.searchHistory;
var self = this;
input.on("keydown", function(e) {
@ -51,6 +58,33 @@ define([
self.deactivate();
return;
}
//up/down
if (e.keyCode == 38 || e.keyCode == 40) {
e.preventDefault();
e.stopImmediatePropagation();
if (e.keyCode == 38) { // up
// show previous search query
if (hist.currentIndex == hist.history.length) {
hist.temporaryQuery = input.value;
// skip previous search if we're already showing the same value
if (hist.temporaryQuery == hist.history[hist.currentIndex-1]) {
hist.currentIndex--;
}
}
if (hist.currentIndex - 1 >= 0) {
input.value = hist.history[--hist.currentIndex];
}
} else { // down
//show next search query
if (hist.currentIndex + 1 < hist.history.length) {
input.value = hist.history[++hist.currentIndex];
} else if (hist.currentIndex + 1 == hist.history.length) {
hist.currentIndex++;
input.value = hist.temporaryQuery;
}
}
return;
}
});
},
@ -64,7 +98,6 @@ define([
},
// todo add regex support
// todo add search history
// we don't have to worry about the files blacklist because they are already removed from the project structure
search: function() {
if (this.currentSearch.running) {
@ -75,6 +108,14 @@ define([
var isCaseSensitive = this.element.find("#search-case-check").checked;
var displayQuery = this.input.value;
// add query to search history
var hist = this.searchHistory;
if (displayQuery != hist.history[hist.history.length - 1]) { // deduplicate
hist.history.push(displayQuery);
}
hist.currentIndex = hist.history.length;
hist.temporaryQuery = '';
var resultsTab = this.createResultsTab(displayQuery);
this.currentSearch = {