author | hgs |
Thu, 23 Sep 2010 15:32:11 -0400 | |
changeset 14 | 6aeb7a756187 |
parent 12 | afcd8e6d025b |
child 16 | 3c88a81ff781 |
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. |
|
35 |
var statusbarSz = snippets.StatusBarChromeId.getGeometry(); |
|
12 | 36 |
// The Orbit UI doesn't have a status bar. |
37 |
var statusbarHeight = (app.ui() == "orbit_ui") ? 0 : statusbarSz.height; |
|
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; |
|
97 |
var recenttitle = window.localeDelegate.translateText( |
|
98 |
"txt_browser_chrome_suggests_search_for"); |
|
99 |
var snippetId = document.getElementById('SuggestsId'); |
|
14 | 100 |
var suggests = window.bookmarksController.suggestSimilar(input); |
3 | 101 |
var suggestUL = document.createElement('ul'); |
102 |
var suggestLI = document.createElement('li'); |
|
103 |
var pattern = new RegExp(input, "ig"); |
|
104 |
||
105 |
snippetId.innerHTML = ""; // clear previous results |
|
106 |
suggestUL.id = 'suggestUlId'; |
|
107 |
suggestLI.id = "searchLiId"; |
|
108 |
||
109 |
// always provide an option to search for user entry |
|
110 |
recenttitle = recenttitle.replace(/%1/, "<b>" + input + "</b>"); |
|
111 |
||
112 |
suggestLI.innerHTML = '<a href="#" onclick="searchSuggests.searchUrlValue(\''+input+'\');'+ |
|
113 |
'return false;">'+ |
|
114 |
'<div class="SuggestElement">'+'<span class="aTitle">'+recenttitle+'</span>'+'</div></a>'; |
|
115 |
suggestUL.appendChild(suggestLI); |
|
116 |
||
117 |
// add each search suggestion to unordered list |
|
118 |
for (i=0; i < suggests.length; i++) { |
|
14 | 119 |
recenturl = suggests[i].url1; |
120 |
recenttitle = suggests[i].title1; |
|
3 | 121 |
suggestLI = document.createElement('li'); |
122 |
suggestLI.id = "suggestsLiId"; |
|
123 |
||
124 |
// bold search text |
|
125 |
recenttitle = recenttitle.replace(pattern, "<b>$&</b>"); |
|
126 |
recenturl = recenturl.replace(pattern, "<b>$&</b>"); |
|
127 |
||
14 | 128 |
suggestLI.innerHTML = '<a href="#" onclick="searchSuggests.gotoUrl(\''+suggests[i].url1+'\');' + |
3 | 129 |
' return false;">'+ |
130 |
'<div class="SuggestElement">'+ |
|
131 |
'<span class="aTitle">'+recenttitle+'</span><br/>'+ |
|
132 |
'<span class="aUrl">'+recenturl+'</span></div></a>'; |
|
133 |
suggestUL.appendChild(suggestLI); |
|
134 |
} |
|
135 |
||
136 |
snippetId.appendChild(suggestUL); |
|
0
1450b09d0cfd
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff
changeset
|
137 |
} |
1450b09d0cfd
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff
changeset
|
138 |
|
3 | 139 |
//! Hides suggests list and support items. |
140 |
function _hideSuggests() |
|
141 |
{ |
|
142 |
clearTimeout(inputTimeoutId); |
|
143 |
window.snippets.SuggestsChromeId.hide(false); |
|
144 |
views.WebView.enabled = true; |
|
145 |
views.WebView.unfreeze(); |
|
146 |
} |
|
147 |
||
148 |
// Public Functions |
|
149 |
||
150 |
//! Handler for onload javascript event. |
|
151 |
this.loadComplete = function() |
|
152 |
{ |
|
12 | 153 |
var urlSearchSz = snippets[urlSnippetId].getGeometry(); |
3 | 154 |
|
155 |
urlSearchHeight = urlSearchSz.height; |
|
12 | 156 |
snippets.SuggestsChromeId.anchorTo(urlSnippetId, suggestsXOffset, urlSearchHeight); |
3 | 157 |
snippets.SuggestsChromeId.zValue = 10; |
158 |
||
159 |
_setMaxHeight(); // calculate max suggest list height |
|
160 |
||
161 |
// hide suggests on external mouse events |
|
162 |
snippets.SuggestsChromeId.externalMouseEvent.connect(this.handleExternalMouseEvent); |
|
163 |
// need to resize suggest page snippet on geometry change |
|
164 |
chrome.prepareForGeometryChange.connect(createDelegate(this, this.handleGeometryChange)); |
|
0
1450b09d0cfd
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff
changeset
|
165 |
} |
1450b09d0cfd
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff
changeset
|
166 |
|
3 | 167 |
//! Updates the size of the suggests window depending on the size of the |
168 |
//! main DIV -- which changes as the number of items changes. |
|
169 |
this.updateSuggestsSize = function() |
|
170 |
{ |
|
171 |
if (snippets.SuggestsChromeId != undefined) { |
|
172 |
var totalW = chrome.displaySize.width; |
|
173 |
var divHeight = document.getElementById("SuggestsId").clientHeight + 2; |
|
174 |
var displayHeight = Math.min(maxHeight, divHeight); |
|
175 |
var displayWidth = totalW - (2 * suggestsXOffset); |
|
176 |
||
177 |
// reset snippet height |
|
178 |
snippets.SuggestsChromeId.setSize(displayWidth, displayHeight); |
|
179 |
} |
|
180 |
} |
|
181 |
||
182 |
//! Handles the geometry change signal. Need to re-calculate max height |
|
183 |
//! and then update suggest page height. |
|
184 |
this.handleGeometryChange = function() |
|
185 |
{ |
|
186 |
_setMaxHeight(); // max height is based on display height |
|
187 |
this.updateSuggestsSize(); |
|
188 |
} |
|
189 |
||
190 |
//! Loads google search page for search string. |
|
191 |
/*! |
|
192 |
\param input value to search for |
|
193 |
*/ |
|
194 |
this.searchUrlValue = function(input) |
|
195 |
{ |
|
196 |
var searchurl = window.pageController.searchUrl(input); |
|
197 |
||
198 |
_hideSuggests(); |
|
199 |
||
200 |
window.pageController.currentLoad(searchurl); |
|
201 |
window.pageController.urlTextChanged(searchurl); |
|
0
1450b09d0cfd
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff
changeset
|
202 |
} |
1450b09d0cfd
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff
changeset
|
203 |
|
3 | 204 |
//! Hides suggests list and loads specified page. |
205 |
/*! |
|
206 |
\param url address of page to load |
|
207 |
*/ |
|
208 |
this.gotoUrl = function(url) |
|
209 |
{ |
|
210 |
_hideSuggests(); |
|
211 |
url = window.pageController.guessUrlFromString(url); |
|
212 |
window.pageController.currentLoad(url); |
|
213 |
window.pageController.urlTextChanged(url); |
|
214 |
} |
|
215 |
||
216 |
//! Handles external mouse events - dismisses suggests list. |
|
217 |
/*! |
|
218 |
\param type the type of event |
|
219 |
\param name the name of event |
|
220 |
\param description event description |
|
221 |
*/ |
|
222 |
this.handleExternalMouseEvent = function(type, name, description) |
|
223 |
{ |
|
12 | 224 |
// external mouse event received on VKB mouse clicks and |
225 |
// suggest list mouse clicks both of which should be ignored |
|
226 |
if ((name == "MouseClick") && !urlHasFoucus |
|
227 |
&& !snippets.SuggestsChromeId.hasFocus) { |
|
3 | 228 |
_hideSuggests(); |
0
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 |
} |
1450b09d0cfd
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff
changeset
|
231 |
|
3 | 232 |
//! Updates the user input for suggestion list. |
233 |
/*! |
|
234 |
\param input the current user input |
|
235 |
*/ |
|
236 |
this.updateUserInput = function() |
|
237 |
{ |
|
238 |
// user still editing URL - don't show/update suggests yet |
|
239 |
clearTimeout(inputTimeoutId); |
|
240 |
inputTimeoutId = setTimeout(_showSuggests.bind(this), inputTimeoutDelay); |
|
241 |
} |
|
242 |
||
243 |
//! Called when load state changes. Suggests should only be called when |
|
244 |
//! the load state is editing. |
|
245 |
this.updateLoadState = function() |
|
246 |
{ |
|
12 | 247 |
if (!pageController.editMode) { |
248 |
// not in editing mode - suggests not allowed |
|
3 | 249 |
_hideSuggests(); // ensure suggests are hidden |
250 |
} |
|
251 |
} |
|
252 |
||
12 | 253 |
//! Called when URL search bar focus changes. The external mouse event |
3 | 254 |
//! handler deals with most cases where the suggestion list should be |
255 |
//! dismissed but we don't get those events when the list isn't visible |
|
256 |
//! so this handler is needed to cancel the timer in those cases. |
|
12 | 257 |
this.urlSearchFocusChanged = function(focusIn) |
3 | 258 |
{ |
12 | 259 |
urlHasFoucus = focusIn; |
3 | 260 |
// if visible user may be scrolling suggestion page so ignore focus change |
12 | 261 |
if (!focusIn && !snippets.SuggestsChromeId.visible) { |
3 | 262 |
// prevent suggestion list from being displayed until URL edited again |
263 |
clearTimeout(inputTimeoutId); |
|
264 |
} |
|
265 |
} |
|
266 |
||
267 |
//! Sets the user input URL suggest delay. |
|
268 |
/*! |
|
269 |
\param to the new delay time. |
|
270 |
*/ |
|
271 |
this.setSuggestTimeout = function(to) |
|
272 |
{ |
|
273 |
inputTimeoutDelay = to; |
|
0
1450b09d0cfd
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff
changeset
|
274 |
} |
1450b09d0cfd
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff
changeset
|
275 |
} |
1450b09d0cfd
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff
changeset
|
276 |