From 7ff9d54366c1128b438e2969e6b33df8addb8dc6 Mon Sep 17 00:00:00 2001 From: Brian Smith Date: Wed, 4 Nov 2015 23:59:35 -0700 Subject: [PATCH] Adding project search history --- js/ui/searchbar.js | 45 +++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 43 insertions(+), 2 deletions(-) diff --git a/js/ui/searchbar.js b/js/ui/searchbar.js index 5ae38bd..5ef6f7c 100644 --- a/js/ui/searchbar.js +++ b/js/ui/searchbar.js @@ -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 = {