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

Side by Side 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: Fix uninitialized variable. 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 unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 // =============================================================================
6 // Util functions
7 // =============================================================================
8
9 // The maximum number of suggestions to show.
10 var MAX_SUGGESTIONS_TO_SHOW = 5;
11
12 /**
13 * Displays a suggestion.
14 * @param {Object} suggestion The suggestion to render.
15 * @param {HTMLElement} box The html element to add the suggestion to.
16 * @param {integer} suggestionRank The rank of the suggestion (0 based).
17 */
18 function addSuggestionToBox(suggestion, box, suggestionRank) {
19 var suggestionDiv = document.createElement('div');
20 suggestionDiv.classList.add('suggestion');
21 if (suggestionRank == 0)
22 suggestionDiv.classList.add('selected');
23
24 var clock = document.createElement('div');
25 clock.className = 'clock';
26 suggestionDiv.appendChild(clock);
27
28 var contentsContainer = document.createElement('div');
29 contentsContainer.className = 'contents';
30 var contents = suggestion.combinedNode;
31 var restrictedId = suggestion.rid;
32 contentsContainer.appendChild(contents);
33 suggestionDiv.appendChild(contentsContainer);
34 suggestionDiv.onclick = function() {
35 handleSuggestionClick(restrictedId);
36 }
37
38 // TODO(shishir): Support hover over suggestions.
39 var restrictedIdDiv = document.createElement('div');
40 restrictedIdDiv.innerHTML = restrictedId;
41 restrictedIdDiv.className = 'restricted-id';
42 suggestionDiv.appendChild(restrictedIdDiv);
43
44 box.appendChild(suggestionDiv);
45 }
46
47 /**
48 * Renders the input suggestions.
49 * @param {Array} nativeSuggestions An array of native suggestions to render.
50 */
51 function renderSuggestions(nativeSuggestions) {
52 clearSuggestions();
53
54 var box = $('suggestionsBox');
55 for (var i = 0; i < MAX_SUGGESTIONS_TO_SHOW && i < nativeSuggestions.length;
56 ++i) {
57 addSuggestionToBox(nativeSuggestions[i], box, i);
58 }
59 }
60
61 /**
62 * Clears the suggestions being displayed.
63 */
64 function clearSuggestions() {
65 $('suggestionsBox').innerHTML = '';
66 }
67
68 /**
69 * @return {integer} The height of the dropdown.
70 */
71 function getDropdownHeight() {
72 return $('suggestionsBox').offsetHeight;
73 }
74
75 /**
76 * @return {integer} the index of the suggestion currently selected.
77 */
78 function getSelectedSuggestionIndex() {
79 var suggestions = $('suggestionsBox').childNodes;
80 for (var i = 0; i < suggestions.length; ++i) {
81 if (suggestions[i].classList.contains('selected'))
82 return i;
83 }
84 return -1;
85 }
86
87 /**
88 * Changes the selected suggestion.
89 * @param {integer} index The index of the suggestion to select.
90 * @param {function} restrictedIdCallback Callback to call on old selection.
91 */
92 function selectSuggestionAtIndex(index, restrictedIdCallback) {
93 var oldSelection = $('suggestionsBox').querySelector('.selected');
94 oldSelection.classList.remove('selected');
95
96 var numSuggestions = $('suggestionsBox').childNodes.length;
97 var sanitizedIndex = Math.min(Math.max(0, index), numSuggestions - 1);
98 var selection = sanitizedIndex + 1;
99 var newSelection = $('suggestionsBox').querySelector(
100 '.suggestion:nth-of-type(' + selection + ')');
101 newSelection.classList.add('selected');
102 var restrictedId = getRestrictedId(newSelection);
103 restrictedIdCallback(restrictedId);
104 }
105
106 /**
107 * Returns the restricted id for the input suggestion. Since the suggestions
108 * contain the user's personal data (browser history) the searchBox API embedds
109 * the content of the suggestion in a shadow dom, and assigns a random id to
110 * each suggestion called restricted id which is accessible to the js.
111 *
112 * @param {HTMLElement} suggestion The node representing the suggestion.
113 * @return {integer} The restricted id of the suggestion.
114 */
115 function getRestrictedId(suggestion) {
116 for (var i = 0; i < suggestion.childNodes.length; ++i) {
117 if (suggestion.childNodes[i].classList.contains('restricted-id'))
118 return parseInt(suggestion.childNodes[i].innerHTML);
119 }
120 return -1;
121 }
122
123 // =============================================================================
124 // Handlers / API stuff
125 // =============================================================================
126
127 /**
128 * @return {Object} the handle to the searchBox API.
129 */
130 function getApiObjectHandle() {
131 if (window.cideb)
132 return window.cideb;
133 if (window.navigator && window.navigator.searchBox)
134 return window.navigator.searchBox;
135 if (window.chrome && window.chrome.searchBox)
136 return window.chrome.searchBox;
137 return null;
138 }
139
140 /**
141 * chrome.searchBox.onnativesuggestions implementation.
142 */
143 function handleNativeSuggestions() {
144 var apiHandle = getApiObjectHandle();
145
146 var nativeSuggestions = apiHandle.nativeSuggestions;
147 if (nativeSuggestions) {
148 nativeSuggestions.sort(function(a, b) {
149 return b.rankingData.relevance - a.rankingData.relevance;
150 });
151 renderSuggestions(nativeSuggestions);
152 } else {
153 clearSuggestions();
154 }
155
156 var height = getDropdownHeight();
157 apiHandle.show(2, height);
158
159 if (nativeSuggestions && nativeSuggestions.length > 0) {
160 apiHandle.setRestrictedAutocompleteText(
161 nativeSuggestions[getSelectedSuggestionIndex()].rid);
162 }
163 }
164
165 /**
166 * Handles suggestion clicks.
167 * @param {integer} restrictedId The restricted id of the suggestion being
168 * clicked.
169 */
170 function handleSuggestionClick(restrictedId) {
171 var apiHandle = getApiObjectHandle();
172 clearSuggestions();
173 apiHandle.navigateContentWindow(restrictedId);
174 }
175
176 /**
177 * chrome.searchBox.onkeypress implementation.
178 * @param {Object} e The key being pressed.
179 */
180 function handleKeyPress(e) {
181 var apiHandle = getApiObjectHandle();
182 function callback(restrictedId) {
183 apiHandle.setRestrictedValue(restrictedId);
184 }
185
186 switch (e.keyCode) {
187 case 38: // Up arrow
188 selectSuggestionAtIndex(getSelectedSuggestionIndex() - 1, callback);
189 break;
190 case 40: // Down arrow
191 selectSuggestionAtIndex(getSelectedSuggestionIndex() + 1, callback);
192 break;
193 }
194 }
195
196 /**
197 * chrome.searchBox.onsubmit implementation.
198 */
199 function onSubmit() {
200 }
201
202 /**
203 * Sets up the searchBox API.
204 */
205 function setUpApi() {
206 var apiHandle = getApiObjectHandle();
207 apiHandle.onnativesuggestions = handleNativeSuggestions;
208 apiHandle.onkeypress = handleKeyPress;
209 apiHandle.onsubmit = onSubmit;
210 }
211
212 document.addEventListener('DOMContentLoaded', setUpApi);
OLDNEW
« no previous file with comments | « chrome/browser/resources/local_omnibox_popup/local_omnibox_popup.html ('k') | chrome/browser/ui/browser_instant_controller.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698