author | hgs |
Fri, 15 Oct 2010 17:30:59 -0400 | |
changeset 16 | 3c88a81ff781 |
parent 14 | 6aeb7a756187 |
permissions | -rw-r--r-- |
3 | 1 |
/*! |
2 |
\file suggests.js This module contains the Suggests class. |
|
3 |
*/ |
|
4 |
||
5 |
/*! |
|
6 |
Class to handle displaying URL suggestions from history and bookmarks. |
|
7 |
*/ |
|
0
1450b09d0cfd
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff
changeset
|
8 |
function Suggests() |
1450b09d0cfd
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff
changeset
|
9 |
{ |
3 | 10 |
// "Private" member variables |
11 |
var suggestsXOffset = 20; |
|
12 |
var inputTimeoutId = null; |
|
13 |
var inputTimeoutDelay = _getTimeoutDelaySetting(); |
|
14 |
var maxHeight = 0; // maximum height of suggest list |
|
15 |
var urlSearchHeight = 0; |
|
12 | 16 |
var urlSnippetId; |
17 |
var urlHasFoucus = false; // URL edit field focus flag |
|
0
1450b09d0cfd
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff
changeset
|
18 |
|
12 | 19 |
// Orbit UI has a different URL snippet. |
20 |
if (app.layoutType() == "tenone") { |
|
21 |
urlSnippetId = "TitleUrlId"; |
|
22 |
} |
|
23 |
else { |
|
24 |
urlSnippetId = "UrlSearchChromeId"; |
|
25 |
} |
|
26 |
||
3 | 27 |
// "Private" methods |
0
1450b09d0cfd
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff
changeset
|
28 |
|
3 | 29 |
//! Calculates the maximum height for the suggest list. |
30 |
//! If not all suggest items can fit in list, only half an item should be |
|
31 |
//! visible the end when scrolled to the top. This is to indicate more items. |
|
32 |
function _setMaxHeight() |
|
33 |
{ |
|
34 |
// Calculate height of available space for suggest list. |
|
16 | 35 |
var statusbarHeight = (snippets.StatusBarChromeId != undefined && snippets.StatusBarChromeId.visible) ? snippets.StatusBarChromeId.getGeometry().height : 0; |
12 | 36 |
// The Orbit UI doesn't have a status bar. |
16 | 37 |
var statusbarHeight = ((app.ui() == "orbit_ui") || (app.ui() == "maemo5_ui")) ? 0 : statusbarSz.height; |
12 | 38 |
var urlSearchSz = snippets[urlSnippetId].getGeometry(); |
3 | 39 |
var toolbarSz = snippets.WebViewToolbarId.getGeometry(); |
40 |
// leave some space between suggest and toolbar (~10% of display height) |
|
41 |
var bufferHeight = Math.ceil(chrome.displaySize.height / 10); |
|
42 |
var availableHeight = chrome.displaySize.height - |
|
12 | 43 |
(statusbarHeight + urlSearchSz.height + toolbarSz.height + bufferHeight); |
3 | 44 |
// Calculate number of elements that can fit in available space. |
45 |
var elementHeight = 70; // set in suggests.css |
|
46 |
var numElements = Math.floor(availableHeight / elementHeight); |
|
0
1450b09d0cfd
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff
changeset
|
47 |
|
3 | 48 |
// in order to make room for 1/2 entry at end of list we may |
49 |
// need to sacrifice 1 full element. |
|
50 |
if ((availableHeight % elementHeight) < (elementHeight / 2)) { |
|
51 |
numElements -= 1; |
|
52 |
} |
|
53 |
numElements += 0.5; // half of final visible entry |
|
54 |
// max suggest list height in pixels |
|
55 |
maxHeight = Math.floor(numElements * elementHeight); |
|
56 |
} |
|
57 |
||
58 |
//! Temporary substitute for settings function to get the delay for |
|
59 |
//! displaying the URL suggests list. |
|
60 |
function _getTimeoutDelaySetting() |
|
61 |
{ |
|
62 |
// no setting stored for this currently |
|
63 |
return (1000); // 1 second |
|
0
1450b09d0cfd
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff
changeset
|
64 |
} |
1450b09d0cfd
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff
changeset
|
65 |
|
3 | 66 |
//! Displays and updates suggest list. |
67 |
function _showSuggests() |
|
68 |
{ |
|
69 |
// make sure the input is the latest |
|
12 | 70 |
var input = snippets[urlSnippetId].url; |
3 | 71 |
|
72 |
// only display suggestions if there is input |
|
73 |
if (input.length != 0) { |
|
74 |
_updateSuggestList(input); |
|
75 |
this.updateSuggestsSize(); |
|
76 |
||
12 | 77 |
if (!snippets.SuggestsChromeId.visible && pageController.editMode) { |
3 | 78 |
window.scrollTo(0, 0); |
79 |
// Disable the content view, leave the URL serach bar and status bar enabled. |
|
80 |
views.WebView.enabled = false; |
|
81 |
views.WebView.freeze(); |
|
82 |
snippets.SuggestsChromeId.show(false); |
|
83 |
} |
|
84 |
} else { |
|
85 |
// no user input so hide suggestions |
|
86 |
_hideSuggests(); |
|
87 |
} |
|
88 |
} |
|
89 |
||
90 |
//! Updates the suggestion list based on the specified input. |
|
91 |
/*! |
|
92 |
\param input the current URL box text |
|
93 |
*/ |
|
94 |
function _updateSuggestList(input) |
|
95 |
{ |
|
96 |
var recenturl; |
|
16 | 97 |
var recenttitle = window.localeDelegate.translateText("txt_browser_chrome_suggests_search_for"); |
3 | 98 |
var snippetId = document.getElementById('SuggestsId'); |
14 | 99 |
var suggests = window.bookmarksController.suggestSimilar(input); |
3 | 100 |
var suggestUL = document.createElement('ul'); |
101 |
var suggestLI = document.createElement('li'); |
|
102 |
var pattern = new RegExp(input, "ig"); |
|
103 |
||
104 |
snippetId.innerHTML = ""; // clear previous results |
|
105 |
suggestUL.id = 'suggestUlId'; |
|
106 |
suggestLI.id = "searchLiId"; |
|
107 |
||
108 |
// always provide an option to search for user entry |
|
109 |
recenttitle = recenttitle.replace(/%1/, "<b>" + input + "</b>"); |
|
110 |
||
111 |
suggestLI.innerHTML = '<a href="#" onclick="searchSuggests.searchUrlValue(\''+input+'\');'+ |
|
112 |
'return false;">'+ |
|
113 |
'<div class="SuggestElement">'+'<span class="aTitle">'+recenttitle+'</span>'+'</div></a>'; |
|
114 |
suggestUL.appendChild(suggestLI); |
|
115 |
||
116 |
// add each search suggestion to unordered list |
|
117 |
for (i=0; i < suggests.length; i++) { |
|
16 | 118 |
recenturl = suggests[i].url1; |
14 | 119 |
recenttitle = suggests[i].title1; |
3 | 120 |
suggestLI = document.createElement('li'); |
121 |
suggestLI.id = "suggestsLiId"; |
|
122 |
||
123 |
// bold search text |
|
124 |
recenttitle = recenttitle.replace(pattern, "<b>$&</b>"); |
|
125 |
recenturl = recenturl.replace(pattern, "<b>$&</b>"); |
|
126 |
||
14 | 127 |
suggestLI.innerHTML = '<a href="#" onclick="searchSuggests.gotoUrl(\''+suggests[i].url1+'\');' + |
3 | 128 |
' return false;">'+ |
129 |
'<div class="SuggestElement">'+ |
|
130 |
'<span class="aTitle">'+recenttitle+'</span><br/>'+ |
|
131 |
'<span class="aUrl">'+recenturl+'</span></div></a>'; |
|
132 |
suggestUL.appendChild(suggestLI); |
|
133 |
} |
|
134 |
||
135 |
snippetId.appendChild(suggestUL); |
|
0
1450b09d0cfd
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff
changeset
|
136 |
} |
1450b09d0cfd
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff
changeset
|
137 |
|
3 | 138 |
//! Hides suggests list and support items. |
139 |
function _hideSuggests() |
|
140 |
{ |
|
141 |
clearTimeout(inputTimeoutId); |
|
142 |
window.snippets.SuggestsChromeId.hide(false); |
|
143 |
views.WebView.enabled = true; |
|
144 |
views.WebView.unfreeze(); |
|
145 |
} |
|
146 |
||
147 |
// Public Functions |
|
148 |
||
149 |
//! Handler for onload javascript event. |
|
150 |
this.loadComplete = function() |
|
151 |
{ |
|
12 | 152 |
var urlSearchSz = snippets[urlSnippetId].getGeometry(); |
3 | 153 |
|
154 |
urlSearchHeight = urlSearchSz.height; |
|
12 | 155 |
snippets.SuggestsChromeId.anchorTo(urlSnippetId, suggestsXOffset, urlSearchHeight); |
3 | 156 |
snippets.SuggestsChromeId.zValue = 10; |
157 |
||
158 |
_setMaxHeight(); // calculate max suggest list height |
|
159 |
||
160 |
// hide suggests on external mouse events |
|
161 |
snippets.SuggestsChromeId.externalMouseEvent.connect(this.handleExternalMouseEvent); |
|
162 |
// need to resize suggest page snippet on geometry change |
|
163 |
chrome.prepareForGeometryChange.connect(createDelegate(this, this.handleGeometryChange)); |
|
0
1450b09d0cfd
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff
changeset
|
164 |
} |
1450b09d0cfd
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff
changeset
|
165 |
|
3 | 166 |
//! Updates the size of the suggests window depending on the size of the |
167 |
//! main DIV -- which changes as the number of items changes. |
|
168 |
this.updateSuggestsSize = function() |
|
169 |
{ |
|
170 |
if (snippets.SuggestsChromeId != undefined) { |
|
171 |
var totalW = chrome.displaySize.width; |
|
172 |
var divHeight = document.getElementById("SuggestsId").clientHeight + 2; |
|
173 |
var displayHeight = Math.min(maxHeight, divHeight); |
|
174 |
var displayWidth = totalW - (2 * suggestsXOffset); |
|
175 |
||
176 |
// reset snippet height |
|
177 |
snippets.SuggestsChromeId.setSize(displayWidth, displayHeight); |
|
178 |
} |
|
179 |
} |
|
180 |
||
181 |
//! Handles the geometry change signal. Need to re-calculate max height |
|
182 |
//! and then update suggest page height. |
|
183 |
this.handleGeometryChange = function() |
|
184 |
{ |
|
185 |
_setMaxHeight(); // max height is based on display height |
|
186 |
this.updateSuggestsSize(); |
|
187 |
} |
|
188 |
||
189 |
//! Loads google search page for search string. |
|
190 |
/*! |
|
191 |
\param input value to search for |
|
192 |
*/ |
|
193 |
this.searchUrlValue = function(input) |
|
194 |
{ |
|
195 |
var searchurl = window.pageController.searchUrl(input); |
|
196 |
||
197 |
_hideSuggests(); |
|
198 |
||
199 |
window.pageController.currentLoad(searchurl); |
|
200 |
window.pageController.urlTextChanged(searchurl); |
|
0
1450b09d0cfd
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff
changeset
|
201 |
} |
1450b09d0cfd
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff
changeset
|
202 |
|
3 | 203 |
//! Hides suggests list and loads specified page. |
204 |
/*! |
|
205 |
\param url address of page to load |
|
206 |
*/ |
|
207 |
this.gotoUrl = function(url) |
|
208 |
{ |
|
209 |
_hideSuggests(); |
|
210 |
url = window.pageController.guessUrlFromString(url); |
|
211 |
window.pageController.currentLoad(url); |
|
212 |
window.pageController.urlTextChanged(url); |
|
213 |
} |
|
214 |
||
215 |
//! Handles external mouse events - dismisses suggests list. |
|
216 |
/*! |
|
217 |
\param type the type of event |
|
218 |
\param name the name of event |
|
219 |
\param description event description |
|
220 |
*/ |
|
221 |
this.handleExternalMouseEvent = function(type, name, description) |
|
222 |
{ |
|
12 | 223 |
// external mouse event received on VKB mouse clicks and |
224 |
// suggest list mouse clicks both of which should be ignored |
|
225 |
if ((name == "MouseClick") && !urlHasFoucus |
|
226 |
&& !snippets.SuggestsChromeId.hasFocus) { |
|
3 | 227 |
_hideSuggests(); |
0
1450b09d0cfd
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff
changeset
|
228 |
} |
1450b09d0cfd
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff
changeset
|
229 |
} |
1450b09d0cfd
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff
changeset
|
230 |
|
3 | 231 |
//! Updates the user input for suggestion list. |
232 |
/*! |
|
233 |
\param input the current user input |
|
234 |
*/ |
|
235 |
this.updateUserInput = function() |
|
236 |
{ |
|
237 |
// user still editing URL - don't show/update suggests yet |
|
238 |
clearTimeout(inputTimeoutId); |
|
239 |
inputTimeoutId = setTimeout(_showSuggests.bind(this), inputTimeoutDelay); |
|
240 |
} |
|
241 |
||
242 |
//! Called when load state changes. Suggests should only be called when |
|
243 |
//! the load state is editing. |
|
244 |
this.updateLoadState = function() |
|
245 |
{ |
|
12 | 246 |
if (!pageController.editMode) { |
247 |
// not in editing mode - suggests not allowed |
|
3 | 248 |
_hideSuggests(); // ensure suggests are hidden |
249 |
} |
|
250 |
} |
|
251 |
||
12 | 252 |
//! Called when URL search bar focus changes. The external mouse event |
3 | 253 |
//! handler deals with most cases where the suggestion list should be |
254 |
//! dismissed but we don't get those events when the list isn't visible |
|
255 |
//! so this handler is needed to cancel the timer in those cases. |
|
12 | 256 |
this.urlSearchFocusChanged = function(focusIn) |
3 | 257 |
{ |
12 | 258 |
urlHasFoucus = focusIn; |
3 | 259 |
// if visible user may be scrolling suggestion page so ignore focus change |
12 | 260 |
if (!focusIn && !snippets.SuggestsChromeId.visible) { |
3 | 261 |
// prevent suggestion list from being displayed until URL edited again |
262 |
clearTimeout(inputTimeoutId); |
|
263 |
} |
|
264 |
} |
|
265 |
||
266 |
//! Sets the user input URL suggest delay. |
|
267 |
/*! |
|
268 |
\param to the new delay time. |
|
269 |
*/ |
|
270 |
this.setSuggestTimeout = function(to) |
|
271 |
{ |
|
272 |
inputTimeoutDelay = to; |
|
0
1450b09d0cfd
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff
changeset
|
273 |
} |
16 | 274 |
|
275 |
//! Hides suggests list and support items. |
|
276 |
this.hideSuggests = function() |
|
277 |
{ |
|
278 |
_hideSuggests(); |
|
279 |
} |
|
0
1450b09d0cfd
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff
changeset
|
280 |
} |
1450b09d0cfd
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff
changeset
|
281 |