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

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

Issue 11555033: Adding local html page used in Instant Extended mode when instant is disabled or unavailable. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Addressing comments from estade, dhollowa and samarth. Created 8 years 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
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) {
+ 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
+ var restrictedIdDiv = document.createElement('div');
+ restrictedIdDiv.innerHTML = restrictedId;
+ restrictedIdDiv.className = 'rid';
+ suggestionDiv.appendChild(restrictedIdDiv);
+
+ box.appendChild(suggestionDiv);
+}
+
+function renderSuggestions(nativeSuggestions) {
+ clearSuggestions();
+
+ var box = $('suggestionsBox');
+ for (var i = 0, suggestion;
+ (suggestion = nativeSuggestions[i]) && i <= MAX_SUGGESTIONS_TO_SHOW;
+ i++) {
+ 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++) {
+ 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) {
+ 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() {
+ 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);

Powered by Google App Engine
This is Rietveld 408576698