11 var suggestsXOffset = 20; |
11 var suggestsXOffset = 20; |
12 var inputTimeoutId = null; |
12 var inputTimeoutId = null; |
13 var inputTimeoutDelay = _getTimeoutDelaySetting(); |
13 var inputTimeoutDelay = _getTimeoutDelaySetting(); |
14 var maxHeight = 0; // maximum height of suggest list |
14 var maxHeight = 0; // maximum height of suggest list |
15 var urlSearchHeight = 0; |
15 var urlSearchHeight = 0; |
16 |
16 var urlSnippetId; |
|
17 var urlHasFoucus = false; // URL edit field focus flag |
|
18 |
|
19 // Orbit UI has a different URL snippet. |
|
20 if (app.layoutType() == "tenone") { |
|
21 urlSnippetId = "TitleUrlId"; |
|
22 } |
|
23 else { |
|
24 urlSnippetId = "UrlSearchChromeId"; |
|
25 } |
|
26 |
17 // "Private" methods |
27 // "Private" methods |
18 |
28 |
19 //! Calculates the maximum height for the suggest list. |
29 //! Calculates the maximum height for the suggest list. |
20 //! If not all suggest items can fit in list, only half an item should be |
30 //! If not all suggest items can fit in list, only half an item should be |
21 //! visible the end when scrolled to the top. This is to indicate more items. |
31 //! visible the end when scrolled to the top. This is to indicate more items. |
22 function _setMaxHeight() |
32 function _setMaxHeight() |
23 { |
33 { |
24 // Calculate height of available space for suggest list. |
34 // Calculate height of available space for suggest list. |
25 var statusbarSz = snippets.StatusBarChromeId.getGeometry(); |
35 var statusbarSz = snippets.StatusBarChromeId.getGeometry(); |
26 var urlSearchSz = snippets.UrlSearchChromeId.getGeometry(); |
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(); |
27 var toolbarSz = snippets.WebViewToolbarId.getGeometry(); |
39 var toolbarSz = snippets.WebViewToolbarId.getGeometry(); |
28 // leave some space between suggest and toolbar (~10% of display height) |
40 // leave some space between suggest and toolbar (~10% of display height) |
29 var bufferHeight = Math.ceil(chrome.displaySize.height / 10); |
41 var bufferHeight = Math.ceil(chrome.displaySize.height / 10); |
30 var availableHeight = chrome.displaySize.height - |
42 var availableHeight = chrome.displaySize.height - |
31 (statusbarSz.height + urlSearchSz.height + toolbarSz.height + bufferHeight); |
43 (statusbarHeight + urlSearchSz.height + toolbarSz.height + bufferHeight); |
32 // Calculate number of elements that can fit in available space. |
44 // Calculate number of elements that can fit in available space. |
33 var elementHeight = 70; // set in suggests.css |
45 var elementHeight = 70; // set in suggests.css |
34 var numElements = Math.floor(availableHeight / elementHeight); |
46 var numElements = Math.floor(availableHeight / elementHeight); |
35 |
47 |
36 // in order to make room for 1/2 entry at end of list we may |
48 // in order to make room for 1/2 entry at end of list we may |
53 |
65 |
54 //! Displays and updates suggest list. |
66 //! Displays and updates suggest list. |
55 function _showSuggests() |
67 function _showSuggests() |
56 { |
68 { |
57 // make sure the input is the latest |
69 // make sure the input is the latest |
58 var input = window.snippets.UrlSearchChromeId.url; |
70 var input = snippets[urlSnippetId].url; |
59 |
71 |
60 // only display suggestions if there is input |
72 // only display suggestions if there is input |
61 if (input.length != 0) { |
73 if (input.length != 0) { |
62 _updateSuggestList(input); |
74 _updateSuggestList(input); |
63 this.updateSuggestsSize(); |
75 this.updateSuggestsSize(); |
64 |
76 |
65 if (!snippets.SuggestsChromeId.visible |
77 if (!snippets.SuggestsChromeId.visible && pageController.editMode) { |
66 && (pageController.loadState == Suggests.GotoModeEditing)) { |
|
67 window.scrollTo(0, 0); |
78 window.scrollTo(0, 0); |
68 // Disable the content view, leave the URL serach bar and status bar enabled. |
79 // Disable the content view, leave the URL serach bar and status bar enabled. |
69 views.WebView.enabled = false; |
80 views.WebView.enabled = false; |
70 views.WebView.freeze(); |
81 views.WebView.freeze(); |
71 snippets.SuggestsChromeId.show(false); |
82 snippets.SuggestsChromeId.show(false); |
84 { |
95 { |
85 var recenturl; |
96 var recenturl; |
86 var recenttitle = window.localeDelegate.translateText( |
97 var recenttitle = window.localeDelegate.translateText( |
87 "txt_browser_chrome_suggests_search_for"); |
98 "txt_browser_chrome_suggests_search_for"); |
88 var snippetId = document.getElementById('SuggestsId'); |
99 var snippetId = document.getElementById('SuggestsId'); |
89 var suggests = window.pageController.fetchSuggestions(input); |
100 var suggests = window.bookmarksController.suggestSimilar(input); |
90 var suggestUL = document.createElement('ul'); |
101 var suggestUL = document.createElement('ul'); |
91 var suggestLI = document.createElement('li'); |
102 var suggestLI = document.createElement('li'); |
92 var pattern = new RegExp(input, "ig"); |
103 var pattern = new RegExp(input, "ig"); |
93 |
104 |
94 snippetId.innerHTML = ""; // clear previous results |
105 snippetId.innerHTML = ""; // clear previous results |
103 '<div class="SuggestElement">'+'<span class="aTitle">'+recenttitle+'</span>'+'</div></a>'; |
114 '<div class="SuggestElement">'+'<span class="aTitle">'+recenttitle+'</span>'+'</div></a>'; |
104 suggestUL.appendChild(suggestLI); |
115 suggestUL.appendChild(suggestLI); |
105 |
116 |
106 // add each search suggestion to unordered list |
117 // add each search suggestion to unordered list |
107 for (i=0; i < suggests.length; i++) { |
118 for (i=0; i < suggests.length; i++) { |
108 recenturl = suggests[i].url; |
119 recenturl = suggests[i].url1; |
109 recenttitle = suggests[i].title; |
120 recenttitle = suggests[i].title1; |
110 suggestLI = document.createElement('li'); |
121 suggestLI = document.createElement('li'); |
111 suggestLI.id = "suggestsLiId"; |
122 suggestLI.id = "suggestsLiId"; |
112 |
123 |
113 // bold search text |
124 // bold search text |
114 recenttitle = recenttitle.replace(pattern, "<b>$&</b>"); |
125 recenttitle = recenttitle.replace(pattern, "<b>$&</b>"); |
115 recenturl = recenturl.replace(pattern, "<b>$&</b>"); |
126 recenturl = recenturl.replace(pattern, "<b>$&</b>"); |
116 |
127 |
117 suggestLI.innerHTML = '<a href="#" onclick="searchSuggests.gotoUrl(\''+suggests[i].url+'\');' + |
128 suggestLI.innerHTML = '<a href="#" onclick="searchSuggests.gotoUrl(\''+suggests[i].url1+'\');' + |
118 ' return false;">'+ |
129 ' return false;">'+ |
119 '<div class="SuggestElement">'+ |
130 '<div class="SuggestElement">'+ |
120 '<span class="aTitle">'+recenttitle+'</span><br/>'+ |
131 '<span class="aTitle">'+recenttitle+'</span><br/>'+ |
121 '<span class="aUrl">'+recenturl+'</span></div></a>'; |
132 '<span class="aUrl">'+recenturl+'</span></div></a>'; |
122 suggestUL.appendChild(suggestLI); |
133 suggestUL.appendChild(suggestLI); |
137 // Public Functions |
148 // Public Functions |
138 |
149 |
139 //! Handler for onload javascript event. |
150 //! Handler for onload javascript event. |
140 this.loadComplete = function() |
151 this.loadComplete = function() |
141 { |
152 { |
142 var urlSearchSz = snippets.UrlSearchChromeId.getGeometry(); |
153 var urlSearchSz = snippets[urlSnippetId].getGeometry(); |
143 |
154 |
144 urlSearchHeight = urlSearchSz.height; |
155 urlSearchHeight = urlSearchSz.height; |
145 snippets.SuggestsChromeId.anchorTo("UrlSearchChromeId", suggestsXOffset, urlSearchHeight); |
156 snippets.SuggestsChromeId.anchorTo(urlSnippetId, suggestsXOffset, urlSearchHeight); |
146 snippets.SuggestsChromeId.zValue = 10; |
157 snippets.SuggestsChromeId.zValue = 10; |
147 |
158 |
148 _setMaxHeight(); // calculate max suggest list height |
159 _setMaxHeight(); // calculate max suggest list height |
149 |
160 |
150 // hide suggests on external mouse events |
161 // hide suggests on external mouse events |
208 \param name the name of event |
219 \param name the name of event |
209 \param description event description |
220 \param description event description |
210 */ |
221 */ |
211 this.handleExternalMouseEvent = function(type, name, description) |
222 this.handleExternalMouseEvent = function(type, name, description) |
212 { |
223 { |
213 if (name == "MouseClick") { |
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) { |
214 _hideSuggests(); |
228 _hideSuggests(); |
215 } |
229 } |
216 } |
230 } |
217 |
231 |
218 //! Updates the user input for suggestion list. |
232 //! Updates the user input for suggestion list. |
228 |
242 |
229 //! Called when load state changes. Suggests should only be called when |
243 //! Called when load state changes. Suggests should only be called when |
230 //! the load state is editing. |
244 //! the load state is editing. |
231 this.updateLoadState = function() |
245 this.updateLoadState = function() |
232 { |
246 { |
233 if (pageController.loadState != Suggests.GotoModeEditing) { |
247 if (!pageController.editMode) { |
234 // loading or reloadable - suggests not ok |
248 // not in editing mode - suggests not allowed |
235 _hideSuggests(); // ensure suggests are hidden |
249 _hideSuggests(); // ensure suggests are hidden |
236 } |
250 } |
237 } |
251 } |
238 |
252 |
239 //! Called when URL search bar looses focus. The external mouse event |
253 //! Called when URL search bar focus changes. The external mouse event |
240 //! handler deals with most cases where the suggestion list should be |
254 //! handler deals with most cases where the suggestion list should be |
241 //! dismissed but we don't get those events when the list isn't visible |
255 //! dismissed but we don't get those events when the list isn't visible |
242 //! so this handler is needed to cancel the timer in those cases. |
256 //! so this handler is needed to cancel the timer in those cases. |
243 this.urlSearchLostFocus = function() |
257 this.urlSearchFocusChanged = function(focusIn) |
244 { |
258 { |
|
259 urlHasFoucus = focusIn; |
245 // if visible user may be scrolling suggestion page so ignore focus change |
260 // if visible user may be scrolling suggestion page so ignore focus change |
246 if (!snippets.SuggestsChromeId.visible) { |
261 if (!focusIn && !snippets.SuggestsChromeId.visible) { |
247 // prevent suggestion list from being displayed until URL edited again |
262 // prevent suggestion list from being displayed until URL edited again |
248 clearTimeout(inputTimeoutId); |
263 clearTimeout(inputTimeoutId); |
249 } |
264 } |
250 } |
265 } |
251 |
266 |