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 |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..659db0a4d9a4ea7c7bc975b312760d34874588a8 |
| --- /dev/null |
| +++ b/chrome/browser/resources/local_omnibox_popup/local_omnibox_popup.js |
| @@ -0,0 +1,214 @@ |
| +// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
|
dhollowa
2012/12/13 01:20:15
nit: no (c)
Shishir
2012/12/13 22:57:58
Done.
|
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +// ============================================================================= |
|
dhollowa
2012/12/13 01:20:15
Remove debugging code.
samarth
2012/12/13 01:32:09
Actually, unless you have a strong objection, it'l
dhollowa
2012/12/13 01:48:19
I predict estade@ will veto this. I personally ke
Shishir
2012/12/13 22:57:58
I agree with david that we should remove the debug
Evan Stade
2012/12/14 01:09:25
I do prefer to keep debugging code out of the prod
|
| +// Debugging code |
| +// ============================================================================= |
| + |
| +// Set to true to debug this page locally. |
| +var DEBUG_MODE = false; |
| + |
| +function addDebugSuggestions() { |
| + function makeFakeNativeSuggestion(html, rid, ranking) { |
| + var node = document.createElement('div'); |
| + node.innerHTML = html; |
| + return { |
| + combinedNode: node, |
| + rid: rid, |
| + rankingData: { |
| + relevance: ranking |
| + } |
| + }; |
| + } |
| + var n = []; |
| + n.push(makeFakeNativeSuggestion('Suggestion <b>ONE</b>', 1, 100)); |
| + n.push(makeFakeNativeSuggestion('Suggestion <b>TWO</b>', 2, 50)); |
| + n.push(makeFakeNativeSuggestion('Suggestion <b>ZERO</b>', 3, 500)); |
| + window.cideb.nativeSuggestions = n; |
| + window.cideb.onnativesuggestions(); |
| +} |
| + |
| +function setUpDebugMockObject() { |
| + window.cideb = { |
| + setNonNativeDropdownHeight: function(h) { |
| + window.console.log('setNonNativeDropdownHeight(' + h + ')'); |
| + }, |
| + setRestrictedValue: function(rid) { |
| + window.console.log('setRestrictedValue(' + rid + ')'); |
| + }, |
| + navigateContentWindow: function(rid) { |
| + window.console.log('navigateContentWindow(' + rid + ')'); |
| + }, |
| + deb_addSuggestions: addDebugSuggestions |
| + }; |
| +} |
| + |
| +// ============================================================================= |
| +// Util functions |
| +// ============================================================================= |
| + |
| +function addSuggestionToBox(suggestion, box, suggestionRank) { |
| + var suggestionDiv = document.createElement('div'); |
| + if (suggestionRank == 0) { |
| + suggestionDiv.className = 'selected suggestion'; |
|
Evan Stade
2012/12/13 02:20:54
you shouldn't need a 'selected' and 'unselected'.
Shishir
2012/12/13 22:57:58
Done.
|
| + } else { |
| + suggestionDiv.className = 'unselected suggestion'; |
| + } |
| + |
| + var clock = document.createElement('div'); |
| + clock.className = 'clock'; |
| + suggestionDiv.appendChild(clock); |
| + |
| + var contentsContainer = document.createElement('div'); |
| + contentsContainer.className = 'contents'; |
| + var contents = suggestion.combinedNode; |
| + var rid = suggestion.rid; |
|
Evan Stade
2012/12/13 02:20:54
please come up with a better variable name than ri
Shishir
2012/12/13 22:57:58
Done.
|
| + contentsContainer.appendChild(contents); |
| + suggestionDiv.appendChild(contentsContainer); |
| + suggestionDiv.onclick = function() { |
| + handleSuggestionClick(rid); |
| + } |
| + // TODO: hover stuff |
| + var ridDiv = document.createElement('div'); |
| + ridDiv.innerHTML = rid; |
| + ridDiv.className = 'rid'; |
| + suggestionDiv.appendChild(ridDiv); |
| + |
| + box.appendChild(suggestionDiv); |
| +} |
| + |
| +function renderSuggestions(nativeSuggestions) { |
| + clearSuggestions(); |
| + |
| + var box = $('suggestionsBox'); |
| + |
| + for (var i = 0, suggestion; suggestion = nativeSuggestions[i++];) { |
|
samarth
2012/12/13 00:58:21
Check for i < 5 here.
samarth
2012/12/13 00:58:21
Put i++ in the last section, so you can just use i
Shishir
2012/12/13 22:57:58
Done.
Shishir
2012/12/13 22:57:58
Done.
|
| + if (i > 5) break; |
|
samarth
2012/12/13 00:58:21
Please make this a constant in this file.
Shishir
2012/12/13 22:57:58
Done.
|
| + addSuggestionToBox(suggestion, box, i - 1); |
| + } |
| +} |
| + |
| +function clearSuggestions() { |
| + $('suggestionsBox').innerHTML = ''; |
| +} |
| + |
| +function getDropdownHeight() { |
| + return $('suggestionsBox').offsetHeight; |
| +} |
| + |
| +function getSelectedSuggestionIndex() { |
| + var suggestions = $('suggestionsBox').childNodes; |
| + for (var i = 0, suggestion; suggestion = suggestions[i++];) { |
| + if (suggestion.className == 'selected suggestion') { |
|
Evan Stade
2012/12/13 02:20:54
no curlies for single line if statements (imo)
Shishir
2012/12/13 22:57:58
Done.
|
| + return i - 1; |
| + } |
| + } |
| + return -1; |
| +} |
| + |
| +function selectSuggestionAtIndex(index, ridCallback) { |
| + var suggestions = $('suggestionsBox').childNodes; |
| + if (index < 0) { |
| + index = 0; |
| + } |
| + if (index >= suggestions.length) { |
|
Evan Stade
2012/12/13 02:20:54
consider
index = max(index, suggesions.length - 1
Shishir
2012/12/13 22:57:58
Done.
|
| + index = suggestions.length - 1; |
| + } |
| + for (var i = 0, suggestion; suggestion = suggestions[i++];) { |
|
Evan Stade
2012/12/13 02:20:54
consider
$('suggestionsBox').querySelector('.sele
Shishir
2012/12/13 22:57:58
Done.
|
| + if (i - 1 == index) { |
| + suggestion.className = 'selected suggestion'; |
| + var rid = getRid(suggestion); |
| + ridCallback(rid); |
| + } |
| + else { |
| + suggestion.className = 'unselected suggestion'; |
| + } |
| + } |
| +} |
| + |
| +function getRid(suggestion) { |
| + for (var i = 0, childNode; childNode = suggestion.childNodes[i++];) { |
|
Evan Stade
2012/12/13 02:20:54
why not put i++ in the traditional location? imo t
Shishir
2012/12/13 22:57:58
Done.
|
| + if (childNode.className == 'rid') { |
| + return parseInt(childNode.innerHTML); |
| + } |
| + } |
| + return -1; |
| +} |
| + |
| +// ============================================================================= |
| +// Handlers / API stuff |
| +// ============================================================================= |
| + |
| +function getAPIObjectHandle() { |
| + if (window.cideb) { |
| + return window.cideb; |
| + } |
| + if (window.navigator && window.navigator.searchBox) { |
| + return window.navigator.searchBox; |
| + } |
| + if (window.chrome && window.chrome.searchBox) { |
| + return window.chrome.searchBox; |
| + } |
| + return null; |
| +} |
| + |
| +function handleNativeSuggestions() { |
| + var apiHandle = getAPIObjectHandle(); |
| + |
| + var nativeSuggestions = apiHandle.nativeSuggestions; |
| + if (nativeSuggestions) { |
| + nativeSuggestions.sort(function(a, b) { |
| + return b.rankingData.relevance - a.rankingData.relevance; |
| + }); |
| + renderSuggestions(nativeSuggestions); |
| + } else { |
| + clearSuggestions(); |
| + } |
| + |
| + var height = getDropdownHeight(); |
| + apiHandle.show(2, height); |
| + |
| + if (nativeSuggestions && nativeSuggestions.length > 0) { |
| + apiHandle.setRestrictedAutocompleteText( |
| + nativeSuggestions[getSelectedSuggestionIndex()].rid); |
| + } |
| +} |
| + |
| +function handleSuggestionClick(rid) { |
| + var apiHandle = getAPIObjectHandle(); |
| + clearSuggestions(); |
| + apiHandle.navigateContentWindow(rid); |
| +} |
| + |
| +function handleKeyPress(e) { |
| + var apiHandle = getAPIObjectHandle(); |
| + function callback(rid) { |
| + apiHandle.setRestrictedValue(rid); |
| + } |
| + |
| + switch (e.keyCode) { |
| + case 38: // Up arrow |
| + selectSuggestionAtIndex(getSelectedSuggestionIndex() - 1, callback); |
| + break; |
| + case 40: // Down arrow |
| + selectSuggestionAtIndex(getSelectedSuggestionIndex() + 1, callback); |
| + break; |
| + } |
| +} |
| + |
| +function onSubmit(e) { |
| +} |
| + |
| +function setUpAPI() { |
| + if (DEBUG_MODE) { |
| + setUpDebugMockObject(); |
| + } |
| + |
| + var apiHandle = getAPIObjectHandle(); |
| + apiHandle.onnativesuggestions = handleNativeSuggestions; |
| + apiHandle.onkeypress = handleKeyPress; |
| + apiHandle.onsubmit = onSubmit; |
| +} |
| + |
| +document.addEventListener('DOMContentLoaded', setUpAPI); |