Chromium Code Reviews| Index: chrome/browser/resources/local_omnibox_popup/local_omnibox_popup.js |
| diff --git a/chrome/browser/resources/local_omnibox_popup/local_omnibox_popup.js b/chrome/browser/resources/local_omnibox_popup/local_omnibox_popup.js |
| index e07c9e9cf850fade2465c1e73092d0d95a59f83c..1b63bfd722b9b63ae4f15dafea8b07f06cb1d636 100644 |
| --- a/chrome/browser/resources/local_omnibox_popup/local_omnibox_popup.js |
| +++ b/chrome/browser/resources/local_omnibox_popup/local_omnibox_popup.js |
| @@ -39,6 +39,21 @@ var MIDDLE_MOUSE_BUTTON = 1; |
| var MAX_SUGGESTIONS_TO_SHOW = 5; |
| /** |
| + * Assume any native suggestion with a score higher than this value has been |
| + * inlined by the browser. |
| + * @type {number} |
| + * @const |
| + */ |
| +var INLINE_SUGGESTION_THRESHOLD = 1200; |
| + |
| +/** |
| + * Suggestion provider type corresponding to a verbatim URL suggestion. |
| + * @type {string} |
| + * @const |
| + */ |
| +var VERBATIM_URL_TYPE = 'url-what-you-typed'; |
| + |
| +/** |
| * The omnibox input value during the last onnativesuggestions event. |
| * @type {string} |
| */ |
| @@ -106,8 +121,7 @@ function renderSuggestions(nativeSuggestions) { |
| for (var i = 0, length = nativeSuggestions.length; |
| i < Math.min(MAX_SUGGESTIONS_TO_SHOW, length); ++i) { |
| - // Select the first suggestion. |
| - addSuggestionToBox(nativeSuggestions[i], box, i == 0); |
| + addSuggestionToBox(nativeSuggestions[i], box, i == selectedIndex); |
| } |
| } |
| @@ -128,6 +142,21 @@ function getDropdownHeight() { |
| } |
| /** |
| + * @param {Object} suggestion A suggestion. |
| + * @param {boolean} inVerbatimMode Are we in verbatim mode? |
| + * @return {boolean} True if the suggestion should be selected. |
| + */ |
| +function shouldSelectSuggestion(suggestion, inVerbatimMode) { |
| + var isVerbatimUrl = suggestion.type == VERBATIM_URL_TYPE; |
| + var inlinableSuggestion = suggestion.rankingData.relevance > |
| + INLINE_SUGGESTION_THRESHOLD; |
| + // Verbatim URLs should always be selected. Otherwise, select suggestions |
| + // with a high enough score unless we are in verbatim mode (e.g. backspacing |
| + // away). |
| + return isVerbatimUrl || (!inVerbatimMode && inlinableSuggestion); |
| +} |
| + |
| +/** |
| * Updates selectedIndex, bounding it between -1 and the total number of |
| * of suggestions - 1 (looping as necessary), and selects the corresponding |
| * suggestion. |
| @@ -178,18 +207,10 @@ function updateSelectedSuggestion(increment) { |
| } |
| /** |
| - * chrome.searchBox.onnativesuggestions implementation. |
| + * Updates suggestions in response to a onchange or onnativesuggestions call. |
| */ |
| -function handleNativeSuggestions() { |
| +function updateSuggestions() { |
| var apiHandle = getApiObjectHandle(); |
| - |
| - // Used to workaround repeated undesired asynchronous onnativesuggestions |
|
Jered
2013/03/15 17:05:06
Is it ok to remove this now?
samarth
2013/03/15 21:58:27
Talked to Jeremy who wrote this and this was just
|
| - // events and the fact that when a suggestion is clicked, the omnibox unfocus |
| - // can cause onnativesuggestions to fire, preventing the suggestion onclick |
| - // from registering. |
| - if (lastInputValue == apiHandle.value && $('suggestionsBox')) { |
| - return; |
| - } |
| lastInputValue = apiHandle.value; |
| clearSuggestions(); |
| @@ -198,10 +219,12 @@ function handleNativeSuggestions() { |
| nativeSuggestions.sort(function(a, b) { |
| return b.rankingData.relevance - a.rankingData.relevance; |
| }); |
| + if (shouldSelectSuggestion(nativeSuggestions[0], apiHandle.verbatim)) { |
|
Jered
2013/03/15 17:05:06
Reset the selectedIndex to -1 otherwise.
samarth
2013/03/15 21:58:27
Already being done in clearSuggestions above. Woul
Jered
2013/03/15 22:04:09
Oh, I missed that. This way is fine.
|
| + selectedIndex = 0; |
| + apiHandle.setRestrictedAutocompleteText( |
| + nativeSuggestions[selectedIndex].rid); |
| + } |
| renderSuggestions(nativeSuggestions); |
| - selectedIndex = 0; |
| - apiHandle.setRestrictedAutocompleteText( |
| - nativeSuggestions[selectedIndex].rid); |
| } |
| var height = getDropdownHeight(); |
| @@ -276,7 +299,8 @@ function onSubmit() { |
| */ |
| function setUpApi() { |
| var apiHandle = getApiObjectHandle(); |
| - apiHandle.onnativesuggestions = handleNativeSuggestions; |
| + apiHandle.onnativesuggestions = updateSuggestions; |
| + apiHandle.onchange = updateSuggestions; |
|
Jered
2013/03/15 17:05:06
Why does this also need to happen onchange?
samarth
2013/03/15 21:58:27
AFAICT, hitting backspace does not trigger onnativ
|
| apiHandle.onkeypress = handleKeyPress; |
| apiHandle.onsubmit = onSubmit; |
| appendSuggestionStyles(); |