Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(599)

Unified Diff: chrome/browser/resources/local_ntp/local_ntp.js

Issue 14562006: Handle Esc key press event in Local NTP. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: git cl upload Created 7 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | chrome/browser/ui/search/instant_controller.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: chrome/browser/resources/local_ntp/local_ntp.js
diff --git a/chrome/browser/resources/local_ntp/local_ntp.js b/chrome/browser/resources/local_ntp/local_ntp.js
index 4f95ee0afead3acc0c00a5b0991f5961f6129ead..940c15efb1fd436437eff20c455d202ef19a25fa 100644
--- a/chrome/browser/resources/local_ntp/local_ntp.js
+++ b/chrome/browser/resources/local_ntp/local_ntp.js
@@ -613,6 +613,16 @@ function hideNtp() {
/**
+ * Shows the NTP (destroys the activeBox if exists and reloads the custom
+ * theme).
+ */
+function showNtp() {
+ hideActiveSuggestions();
+ document.body.classList.remove(CLASSES.HIDE_NTP);
+ onThemeChange();
+}
+
+/**
* Clears the custom theme (if any).
*/
function clearCustomTheme() {
@@ -779,6 +789,13 @@ var KEY_DOWN_ARROW = 40;
/**
+ * "Esc" keycode.
+ * @type {number}
+ * @const
+ */
+var KEY_ESC = 27;
+
+/**
* Pixels of padding inside a suggestion div for displaying its icon.
* @type {number}
* @const
@@ -1205,6 +1222,24 @@ SuggestionsBox.prototype = {
},
/**
+ * @return {boolean} True if a suggestion is selected by default or because
+ * the user arrowed down to it.
+ */
+ hasSelectedSuggestion: function() {
+ return this.selectedIndex_ != -1;
+ },
+
+ /**
+ * Clears the selected suggestion.
+ */
+ clearSelection: function() {
+ this.selectedIndex_ = -1;
+ var oldSelection = this.container_.querySelector('.' + CLASSES.SELECTED);
+ if (oldSelection)
+ oldSelection.classList.remove(CLASSES.SELECTED);
+ },
+
+ /**
* Changes the current selected suggestion index.
* @param {number} index The new selection to suggest.
* @private
@@ -1225,7 +1260,7 @@ SuggestionsBox.prototype = {
var oldSelection = this.container_.querySelector('.' + CLASSES.SELECTED);
if (oldSelection)
oldSelection.classList.remove(CLASSES.SELECTED);
- if (this.selectedIndex_ == -1) {
+ if (!this.hasSelectedSuggestion()) {
searchboxApiHandle.setValue(this.inputValue_);
} else {
this.suggestions_[this.selectedIndex_].select(true);
@@ -1442,19 +1477,24 @@ function setSuggestionStyles() {
/**
- * Makes keys navigate through suggestions.
+ * Handles key press events.
* @param {Object} e The key being pressed.
*/
function handleKeyPress(e) {
- if (!activeBox)
- return;
-
switch (e.keyCode) {
case KEY_UP_ARROW:
- activeBox.selectPrevious();
+ if (activeBox)
+ activeBox.selectPrevious();
break;
case KEY_DOWN_ARROW:
- activeBox.selectNext();
+ if (activeBox)
+ activeBox.selectNext();
+ break;
+ case KEY_ESC:
+ if (activeBox && activeBox.hasSelectedSuggestion())
+ activeBox.clearSelection();
+ else
+ showNtp();
break;
}
}
@@ -1643,7 +1683,7 @@ function init() {
searchboxApiHandle.onsubmit = function() {
var value = searchboxApiHandle.value;
if (!value) {
- // Interpret onsubmit with an empty query as an ESC key press.
+ // Hide the drop down right away when the user hit enter key on a URL.
hideActiveSuggestions();
}
};
« no previous file with comments | « no previous file | chrome/browser/ui/search/instant_controller.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698