|
1 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
2 * License, v. 2.0. If a copy of the MPL was not distributed with this file, |
|
3 * You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
4 "use strict"; |
|
5 |
|
6 var FindHelper = { |
|
7 _finder: null, |
|
8 _targetTab: null, |
|
9 _initialViewport: null, |
|
10 _viewportChanged: false, |
|
11 |
|
12 observe: function(aMessage, aTopic, aData) { |
|
13 switch(aTopic) { |
|
14 case "FindInPage:Find": |
|
15 this.doFind(aData); |
|
16 break; |
|
17 |
|
18 case "FindInPage:Prev": |
|
19 this.findAgain(aData, true); |
|
20 break; |
|
21 |
|
22 case "FindInPage:Next": |
|
23 this.findAgain(aData, false); |
|
24 break; |
|
25 |
|
26 case "Tab:Selected": |
|
27 case "FindInPage:Closed": |
|
28 this.findClosed(); |
|
29 break; |
|
30 } |
|
31 }, |
|
32 |
|
33 doFind: function(aSearchString) { |
|
34 if (!this._finder) { |
|
35 this._targetTab = BrowserApp.selectedTab; |
|
36 this._finder = this._targetTab.browser.finder; |
|
37 this._finder.addResultListener(this); |
|
38 this._initialViewport = JSON.stringify(this._targetTab.getViewport()); |
|
39 this._viewportChanged = false; |
|
40 } |
|
41 |
|
42 this._finder.fastFind(aSearchString, false); |
|
43 }, |
|
44 |
|
45 findAgain: function(aString, aFindBackwards) { |
|
46 // This can happen if the user taps next/previous after re-opening the search bar |
|
47 if (!this._finder) { |
|
48 this.doFind(aString); |
|
49 return; |
|
50 } |
|
51 |
|
52 this._finder.findAgain(aFindBackwards, false, false); |
|
53 }, |
|
54 |
|
55 findClosed: function() { |
|
56 // If there's no find in progress, there's nothing to clean up |
|
57 if (!this._finder) |
|
58 return; |
|
59 |
|
60 this._finder.removeSelection(); |
|
61 this._finder.removeResultListener(this); |
|
62 this._finder = null; |
|
63 this._targetTab = null; |
|
64 this._initialViewport = null; |
|
65 this._viewportChanged = false; |
|
66 }, |
|
67 |
|
68 onFindResult: function(aData) { |
|
69 if (aData.result == Ci.nsITypeAheadFind.FIND_NOTFOUND) { |
|
70 if (this._viewportChanged) { |
|
71 if (this._targetTab != BrowserApp.selectedTab) { |
|
72 // this should never happen |
|
73 Cu.reportError("Warning: selected tab changed during find!"); |
|
74 // fall through and restore viewport on the initial tab anyway |
|
75 } |
|
76 this._targetTab.setViewport(JSON.parse(this._initialViewport)); |
|
77 this._targetTab.sendViewportUpdate(); |
|
78 } |
|
79 } else { |
|
80 // Disabled until bug 1014113 is fixed |
|
81 //ZoomHelper.zoomToRect(aData.rect, -1, false, true); |
|
82 this._viewportChanged = true; |
|
83 } |
|
84 } |
|
85 }; |