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

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: 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 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 // 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 // The maximum number of suggestions to show.
52 var MAX_SUGGESTIONS_TO_SHOW = 5;
53
54 function addSuggestionToBox(suggestion, box, suggestionRank) {
55 var suggestionDiv = document.createElement('div');
56 suggestionDiv.classList.add('suggestion');
57 if (suggestionRank == 0)
58 suggestionDiv.classList.add('selected');
59
60 var clock = document.createElement('div');
61 clock.className = 'clock';
62 suggestionDiv.appendChild(clock);
63
64 var contentsContainer = document.createElement('div');
65 contentsContainer.className = 'contents';
66 var contents = suggestion.combinedNode;
67 var restrictedId = suggestion.rid;
68 contentsContainer.appendChild(contents);
69 suggestionDiv.appendChild(contentsContainer);
70 suggestionDiv.onclick = function() {
71 handleSuggestionClick(restrictedId);
72 }
73 // TODO: hover stuff
74 var restrictedIdDiv = document.createElement('div');
75 restrictedIdDiv.innerHTML = restrictedId;
76 restrictedIdDiv.className = 'rid';
77 suggestionDiv.appendChild(restrictedIdDiv);
78
79 box.appendChild(suggestionDiv);
80 }
81
82 function renderSuggestions(nativeSuggestions) {
83 clearSuggestions();
84
85 var box = $('suggestionsBox');
86 for (var i = 0, suggestion;
87 (suggestion = nativeSuggestions[i]) && i <= MAX_SUGGESTIONS_TO_SHOW;
88 i++) {
89 addSuggestionToBox(suggestion, box, i);
90 }
91 }
92
93 function clearSuggestions() {
94 $('suggestionsBox').innerHTML = '';
95 }
96
97 function getDropdownHeight() {
98 return $('suggestionsBox').offsetHeight;
99 }
100
101 function getSelectedSuggestionIndex() {
102 var suggestions = $('suggestionsBox').childNodes;
103 for (var i = 0, suggestion; suggestion = suggestions[i]; i++) {
104 if (suggestion.classList.contains('selected'))
105 return i;
106 }
107 return -1;
108 }
109
110 function selectSuggestionAtIndex(index, ridCallback) {
111 var oldSelection = $('suggestionsBox').querySelector('.selected');
112 oldSelection.classList.remove('selected');
113 var restrictedId = getRid(oldSelection);
114 ridCallback(restrictedId);
115
116 var selection = index + 1;
117 var newSelection = $('suggestionsBox').querySelector(
118 '.suggestion:nth-of-type(' + selection + ')');
119 newSelection.classList.add('selected');
120 }
121
122 function getRid(suggestion) {
123 for (var i = 0, childNode; childNode = suggestion.childNodes[i]; i++) {
124 if (childNode.className == 'rid')
125 return parseInt(childNode.innerHTML);
126 }
127 return -1;
128 }
129
130 // =============================================================================
131 // Handlers / API stuff
132 // =============================================================================
133
134 function getAPIObjectHandle() {
135 if (window.cideb)
136 return window.cideb;
137 if (window.navigator && window.navigator.searchBox)
138 return window.navigator.searchBox;
139 if (window.chrome && window.chrome.searchBox)
140 return window.chrome.searchBox;
141 return null;
142 }
143
144 function handleNativeSuggestions() {
145 var apiHandle = getAPIObjectHandle();
146
147 var nativeSuggestions = apiHandle.nativeSuggestions;
148 if (nativeSuggestions) {
149 nativeSuggestions.sort(function(a, b) {
150 return b.rankingData.relevance - a.rankingData.relevance;
151 });
152 renderSuggestions(nativeSuggestions);
153 } else {
154 clearSuggestions();
155 }
156
157 var height = getDropdownHeight();
158 apiHandle.show(2, height);
159
160 if (nativeSuggestions && nativeSuggestions.length > 0) {
161 apiHandle.setRestrictedAutocompleteText(
162 nativeSuggestions[getSelectedSuggestionIndex()].rid);
163 }
164 }
165
166 function handleSuggestionClick(rid) {
167 var apiHandle = getAPIObjectHandle();
168 clearSuggestions();
169 apiHandle.navigateContentWindow(rid);
170 }
171
172 function handleKeyPress(e) {
173 var apiHandle = getAPIObjectHandle();
174 function callback(rid) {
175 apiHandle.setRestrictedValue(rid);
176 }
177
178 switch (e.keyCode) {
179 case 38: // Up arrow
180 selectSuggestionAtIndex(getSelectedSuggestionIndex() - 1, callback);
181 break;
182 case 40: // Down arrow
183 selectSuggestionAtIndex(getSelectedSuggestionIndex() + 1, callback);
184 break;
185 }
186 }
187
188 function onSubmit(e) {
189 }
190
191 function setUpAPI() {
192 if (DEBUG_MODE)
193 setUpDebugMockObject();
194
195 var apiHandle = getAPIObjectHandle();
196 apiHandle.onnativesuggestions = handleNativeSuggestions;
197 apiHandle.onkeypress = handleKeyPress;
198 apiHandle.onsubmit = onSubmit;
199 }
200
201 document.addEventListener('DOMContentLoaded', setUpAPI);
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698