Wed, 31 Dec 2014 06:55:50 +0100
Added tag UPSTREAM_283F7C6 for changeset ca08bd8f51b2
1 // -*- Mode: js2; tab-width: 2; indent-tabs-mode: nil; js2-basic-offset: 2; js2-skip-preprocessor-directives: t; -*-
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 /* We don't support zooming yet, disable Animated zoom by clamping it to the default zoom. */
7 const kBrowserFindZoomLevelMin = 1;
8 const kBrowserFindZoomLevelMax = 1;
10 var FindHelperUI = {
11 type: "find",
12 commands: {
13 next: "cmd_findNext",
14 previous: "cmd_findPrevious",
15 close: "cmd_findClose"
16 },
18 _finder: null,
19 _open: false,
20 _status: null,
21 _searchString: "",
23 /*
24 * Properties
25 */
27 get isActive() {
28 return this._open;
29 },
31 get status() {
32 return this._status;
33 },
35 set status(val) {
36 if (val != this._status) {
37 this._status = val;
38 if (!val)
39 this._textbox.removeAttribute("status");
40 else
41 this._textbox.setAttribute("status", val);
42 this.updateCommands(this._textbox.value);
43 }
44 },
46 init: function findHelperInit() {
47 this._textbox = document.getElementById("findbar-textbox");
48 this._container = Elements.findbar;
50 this._cmdPrevious = document.getElementById(this.commands.previous);
51 this._cmdNext = document.getElementById(this.commands.next);
53 this._textbox.addEventListener("keydown", this);
55 // Listen for events where form assistant should be closed
56 Elements.tabList.addEventListener("TabSelect", this, true);
57 Elements.browsers.addEventListener("URLChanged", this, true);
58 window.addEventListener("MozAppbarShowing", this);
59 window.addEventListener("MozFlyoutPanelShowing", this, false);
60 },
62 handleEvent: function findHelperHandleEvent(aEvent) {
63 switch (aEvent.type) {
64 case "TabSelect":
65 this.hide();
66 break;
68 case "URLChanged":
69 if (aEvent.detail && aEvent.target == getBrowser())
70 this.hide();
71 break;
73 case "keydown":
74 if (aEvent.keyCode == Ci.nsIDOMKeyEvent.DOM_VK_RETURN) {
75 let backwardsSearch = aEvent.shiftKey;
76 this.searchAgain(this._searchString, backwardsSearch);
77 }
78 break;
80 case "MozAppbarShowing":
81 case "MozFlyoutPanelShowing":
82 if (aEvent.target != this._container) {
83 this.hide();
84 }
85 break;
86 }
87 },
89 show: function findHelperShow() {
90 if (BrowserUI.isStartTabVisible) {
91 return;
92 }
93 if (this._open) {
94 setTimeout(() => {
95 this._textbox.select();
96 this._textbox.focus();
97 }, 0);
98 return;
99 }
101 // Hide any menus
102 ContextUI.dismiss();
104 // Shutdown selection related ui
105 SelectionHelperUI.closeEditSession();
107 let findbar = this._container;
108 setTimeout(() => {
109 findbar.show();
110 this.search(this._textbox.value);
111 this._textbox.select();
112 this._textbox.focus();
114 this._open = true;
115 }, 0);
117 // Prevent the view to scroll automatically while searching
118 Browser.selectedBrowser.scrollSync = false;
119 },
121 hide: function findHelperHide() {
122 if (!this._open)
123 return;
125 ContentAreaObserver.shiftBrowserDeck(0);
127 let onTransitionEnd = () => {
128 this._container.removeEventListener("transitionend", onTransitionEnd, true);
129 this._textbox.value = "";
130 this.status = null;
131 this._open = false;
132 if (this._finder) {
133 this._finder.removeResultListener(this);
134 this._finder = null
135 }
136 // Restore the scroll synchronisation
137 Browser.selectedBrowser.scrollSync = true;
138 };
140 this._textbox.blur();
141 this._container.addEventListener("transitionend", onTransitionEnd, true);
142 this._container.dismiss();
143 },
145 search: function findHelperSearch(aValue) {
146 if (!this._finder) {
147 this._finder = Browser.selectedBrowser.finder;
148 this._finder.addResultListener(this);
149 }
150 this._searchString = aValue;
151 if (aValue != "") {
152 this._finder.fastFind(aValue, false, false);
153 } else {
154 this.updateCommands();
155 }
156 },
158 searchAgain: function findHelperSearchAgain(aValue, aFindBackwards) {
159 // This can happen if the user taps next/previous after re-opening the search bar
160 if (!this._finder) {
161 this.search(aValue);
162 return;
163 }
165 this._finder.findAgain(aFindBackwards, false, false);
166 },
168 goToPrevious: function findHelperGoToPrevious() {
169 this.searchAgain(this._searchString, true);
170 },
172 goToNext: function findHelperGoToNext() {
173 this.searchAgain(this._searchString, false);
174 },
176 onFindResult: function(aData) {
177 this._status = aData.result;
178 if (aData.rect) {
179 this._zoom(aData.rect, Browser.selectedBrowser.contentDocumentHeight);
180 }
181 this.updateCommands();
182 },
184 updateCommands: function findHelperUpdateCommands() {
185 let disabled = (this._status == Ci.nsITypeAheadFind.FIND_NOTFOUND) || (this._searchString == "");
186 this._cmdPrevious.setAttribute("disabled", disabled);
187 this._cmdNext.setAttribute("disabled", disabled);
188 },
190 _zoom: function _findHelperZoom(aElementRect, aContentHeight) {
191 // The rect we get here is the content rect including scroll offset
192 // in the page.
194 // If the text falls below the find bar and keyboard shift content up.
195 let browserShift = 0;
196 // aElementRect.y is the top left origin of the selection rect.
197 if ((aElementRect.y + aElementRect.height) >
198 (aContentHeight - this._container.boxObject.height)) {
199 browserShift += this._container.boxObject.height;
200 }
201 browserShift += Services.metro.keyboardHeight;
203 // If the rect top of the selection is above the view, don't shift content
204 // (or if it's already shifted, shift it back down).
205 if (aElementRect.y < browserShift) {
206 browserShift = 0;
207 }
209 // Shift the deck so that the selection is within the visible view.
210 ContentAreaObserver.shiftBrowserDeck(browserShift);
212 // Adjust for keyboad display and position the text selection rect in
213 // the middle of the viewable area.
214 let xPos = aElementRect.x;
215 let yPos = aElementRect.y;
216 let scrollAdjust = ((ContentAreaObserver.height - Services.metro.keyboardHeight) * .5) +
217 Services.metro.keyboardHeight;
218 yPos -= scrollAdjust;
219 if (yPos < 0) {
220 yPos = 0;
221 }
223 // TODO zoom via apzc, right now all we support is scroll
224 // positioning.
225 Browser.selectedBrowser.contentWindow.scrollTo(xPos, yPos);
226 }
227 };