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

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: 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 (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.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 // =============================================================================
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
6 // Debugging code
7 // =============================================================================
8
9 // Set to true to debug this page locally.
10 var DEBUG_MODE = false;
11
12 function addDebugSuggestions() {
13 function makeFakeNativeSuggestion(html, rid, ranking) {
14 var node = document.createElement('div');
15 node.innerHTML = html;
16 return {
17 combinedNode: node,
18 rid: rid,
19 rankingData: {
20 relevance: ranking
21 }
22 };
23 }
24 var n = [];
25 n.push(makeFakeNativeSuggestion('Suggestion <b>ONE</b>', 1, 100));
26 n.push(makeFakeNativeSuggestion('Suggestion <b>TWO</b>', 2, 50));
27 n.push(makeFakeNativeSuggestion('Suggestion <b>ZERO</b>', 3, 500));
28 window.cideb.nativeSuggestions = n;
29 window.cideb.onnativesuggestions();
30 }
31
32 function setUpDebugMockObject() {
33 window.cideb = {
34 setNonNativeDropdownHeight: function(h) {
35 window.console.log('setNonNativeDropdownHeight(' + h + ')');
36 },
37 setRestrictedValue: function(rid) {
38 window.console.log('setRestrictedValue(' + rid + ')');
39 },
40 navigateContentWindow: function(rid) {
41 window.console.log('navigateContentWindow(' + rid + ')');
42 },
43 deb_addSuggestions: addDebugSuggestions
44 };
45 }
46
47 // =============================================================================
48 // Util functions
49 // =============================================================================
50
51 function addSuggestionToBox(suggestion, box, suggestionRank) {
52 var suggestionDiv = document.createElement('div');
53 if (suggestionRank == 0) {
54 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.
55 } else {
56 suggestionDiv.className = 'unselected suggestion';
57 }
58
59 var clock = document.createElement('div');
60 clock.className = 'clock';
61 suggestionDiv.appendChild(clock);
62
63 var contentsContainer = document.createElement('div');
64 contentsContainer.className = 'contents';
65 var contents = suggestion.combinedNode;
66 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.
67 contentsContainer.appendChild(contents);
68 suggestionDiv.appendChild(contentsContainer);
69 suggestionDiv.onclick = function() {
70 handleSuggestionClick(rid);
71 }
72 // TODO: hover stuff
73 var ridDiv = document.createElement('div');
74 ridDiv.innerHTML = rid;
75 ridDiv.className = 'rid';
76 suggestionDiv.appendChild(ridDiv);
77
78 box.appendChild(suggestionDiv);
79 }
80
81 function renderSuggestions(nativeSuggestions) {
82 clearSuggestions();
83
84 var box = $('suggestionsBox');
85
86 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.
87 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.
88 addSuggestionToBox(suggestion, box, i - 1);
89 }
90 }
91
92 function clearSuggestions() {
93 $('suggestionsBox').innerHTML = '';
94 }
95
96 function getDropdownHeight() {
97 return $('suggestionsBox').offsetHeight;
98 }
99
100 function getSelectedSuggestionIndex() {
101 var suggestions = $('suggestionsBox').childNodes;
102 for (var i = 0, suggestion; suggestion = suggestions[i++];) {
103 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.
104 return i - 1;
105 }
106 }
107 return -1;
108 }
109
110 function selectSuggestionAtIndex(index, ridCallback) {
111 var suggestions = $('suggestionsBox').childNodes;
112 if (index < 0) {
113 index = 0;
114 }
115 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.
116 index = suggestions.length - 1;
117 }
118 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.
119 if (i - 1 == index) {
120 suggestion.className = 'selected suggestion';
121 var rid = getRid(suggestion);
122 ridCallback(rid);
123 }
124 else {
125 suggestion.className = 'unselected suggestion';
126 }
127 }
128 }
129
130 function getRid(suggestion) {
131 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.
132 if (childNode.className == 'rid') {
133 return parseInt(childNode.innerHTML);
134 }
135 }
136 return -1;
137 }
138
139 // =============================================================================
140 // Handlers / API stuff
141 // =============================================================================
142
143 function getAPIObjectHandle() {
144 if (window.cideb) {
145 return window.cideb;
146 }
147 if (window.navigator && window.navigator.searchBox) {
148 return window.navigator.searchBox;
149 }
150 if (window.chrome && window.chrome.searchBox) {
151 return window.chrome.searchBox;
152 }
153 return null;
154 }
155
156 function handleNativeSuggestions() {
157 var apiHandle = getAPIObjectHandle();
158
159 var nativeSuggestions = apiHandle.nativeSuggestions;
160 if (nativeSuggestions) {
161 nativeSuggestions.sort(function(a, b) {
162 return b.rankingData.relevance - a.rankingData.relevance;
163 });
164 renderSuggestions(nativeSuggestions);
165 } else {
166 clearSuggestions();
167 }
168
169 var height = getDropdownHeight();
170 apiHandle.show(2, height);
171
172 if (nativeSuggestions && nativeSuggestions.length > 0) {
173 apiHandle.setRestrictedAutocompleteText(
174 nativeSuggestions[getSelectedSuggestionIndex()].rid);
175 }
176 }
177
178 function handleSuggestionClick(rid) {
179 var apiHandle = getAPIObjectHandle();
180 clearSuggestions();
181 apiHandle.navigateContentWindow(rid);
182 }
183
184 function handleKeyPress(e) {
185 var apiHandle = getAPIObjectHandle();
186 function callback(rid) {
187 apiHandle.setRestrictedValue(rid);
188 }
189
190 switch (e.keyCode) {
191 case 38: // Up arrow
192 selectSuggestionAtIndex(getSelectedSuggestionIndex() - 1, callback);
193 break;
194 case 40: // Down arrow
195 selectSuggestionAtIndex(getSelectedSuggestionIndex() + 1, callback);
196 break;
197 }
198 }
199
200 function onSubmit(e) {
201 }
202
203 function setUpAPI() {
204 if (DEBUG_MODE) {
205 setUpDebugMockObject();
206 }
207
208 var apiHandle = getAPIObjectHandle();
209 apiHandle.onnativesuggestions = handleNativeSuggestions;
210 apiHandle.onkeypress = handleKeyPress;
211 apiHandle.onsubmit = onSubmit;
212 }
213
214 document.addEventListener('DOMContentLoaded', setUpAPI);
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698