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..74dd1e32e9669be59855af2f2bdf723150d05faa |
| --- /dev/null |
| +++ b/chrome/browser/resources/local_omnibox_popup/local_omnibox_popup.js |
| @@ -0,0 +1,201 @@ |
| +// Copyright 2012 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +// ============================================================================= |
| +// 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 |
| +// ============================================================================= |
| + |
| +// The maximum number of suggestions to show. |
| +var MAX_SUGGESTIONS_TO_SHOW = 5; |
| + |
| +function addSuggestionToBox(suggestion, box, suggestionRank) { |
|
Evan Stade
2012/12/14 01:09:25
docs for every one of these functions and paramete
Shishir
2012/12/14 03:46:52
Done.
|
| + var suggestionDiv = document.createElement('div'); |
| + suggestionDiv.classList.add('suggestion'); |
| + if (suggestionRank == 0) |
| + suggestionDiv.classList.add('selected'); |
| + |
| + var clock = document.createElement('div'); |
| + clock.className = 'clock'; |
| + suggestionDiv.appendChild(clock); |
| + |
| + var contentsContainer = document.createElement('div'); |
| + contentsContainer.className = 'contents'; |
| + var contents = suggestion.combinedNode; |
| + var restrictedId = suggestion.rid; |
| + contentsContainer.appendChild(contents); |
| + suggestionDiv.appendChild(contentsContainer); |
| + suggestionDiv.onclick = function() { |
| + handleSuggestionClick(restrictedId); |
| + } |
| + // TODO: hover stuff |
|
Evan Stade
2012/12/14 01:09:25
TODO(shouldAlwaysHaveANameHere): and the comment s
Shishir
2012/12/14 03:46:52
Done.
|
| + var restrictedIdDiv = document.createElement('div'); |
| + restrictedIdDiv.innerHTML = restrictedId; |
| + restrictedIdDiv.className = 'rid'; |
|
Evan Stade
2012/12/14 01:09:25
better name than rid here too.
Shishir
2012/12/14 03:46:52
Done.
|
| + suggestionDiv.appendChild(restrictedIdDiv); |
| + |
| + box.appendChild(suggestionDiv); |
| +} |
| + |
| +function renderSuggestions(nativeSuggestions) { |
| + clearSuggestions(); |
| + |
| + var box = $('suggestionsBox'); |
| + for (var i = 0, suggestion; |
|
Evan Stade
2012/12/14 01:09:25
no need for suggestion to be inside the loop, or s
Shishir
2012/12/14 03:46:52
Done.
|
| + (suggestion = nativeSuggestions[i]) && i <= MAX_SUGGESTIONS_TO_SHOW; |
| + i++) { |
|
Evan Stade
2012/12/14 01:09:25
++i
Shishir
2012/12/14 03:46:52
Done.
|
| + addSuggestionToBox(suggestion, box, i); |
| + } |
| +} |
| + |
| +function clearSuggestions() { |
| + $('suggestionsBox').innerHTML = ''; |
| +} |
| + |
| +function getDropdownHeight() { |
| + return $('suggestionsBox').offsetHeight; |
| +} |
| + |
| +function getSelectedSuggestionIndex() { |
| + var suggestions = $('suggestionsBox').childNodes; |
| + for (var i = 0, suggestion; suggestion = suggestions[i]; i++) { |
|
Evan Stade
2012/12/14 01:09:25
++i
Shishir
2012/12/14 03:46:52
Done.
|
| + if (suggestion.classList.contains('selected')) |
| + return i; |
| + } |
| + return -1; |
| +} |
| + |
| +function selectSuggestionAtIndex(index, ridCallback) { |
| + var oldSelection = $('suggestionsBox').querySelector('.selected'); |
| + oldSelection.classList.remove('selected'); |
| + var restrictedId = getRid(oldSelection); |
| + ridCallback(restrictedId); |
| + |
| + var selection = index + 1; |
| + var newSelection = $('suggestionsBox').querySelector( |
| + '.suggestion:nth-of-type(' + selection + ')'); |
| + newSelection.classList.add('selected'); |
| +} |
| + |
| +function getRid(suggestion) { |
|
Evan Stade
2012/12/14 01:09:25
perfect example of why you should not abbreviate t
Shishir
2012/12/14 03:46:52
Done.
|
| + for (var i = 0, childNode; childNode = suggestion.childNodes[i]; i++) { |
| + if (childNode.className == 'rid') |
| + return parseInt(childNode.innerHTML); |
| + } |
| + return -1; |
| +} |
| + |
| +// ============================================================================= |
| +// Handlers / API stuff |
| +// ============================================================================= |
| + |
| +function getAPIObjectHandle() { |
|
Evan Stade
2012/12/14 01:09:25
getApiObjectHandle
Shishir
2012/12/14 03:46:52
Done.
|
| + 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); |
|
Evan Stade
2012/12/14 01:09:25
does this handle overflow or underflow correctly?
Shishir
2012/12/14 03:46:52
Done.
|
| + 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); |