michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this file, michael@0: * You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: "use strict"; michael@0: michael@0: var FindHelper = { michael@0: _finder: null, michael@0: _targetTab: null, michael@0: _initialViewport: null, michael@0: _viewportChanged: false, michael@0: michael@0: observe: function(aMessage, aTopic, aData) { michael@0: switch(aTopic) { michael@0: case "FindInPage:Find": michael@0: this.doFind(aData); michael@0: break; michael@0: michael@0: case "FindInPage:Prev": michael@0: this.findAgain(aData, true); michael@0: break; michael@0: michael@0: case "FindInPage:Next": michael@0: this.findAgain(aData, false); michael@0: break; michael@0: michael@0: case "Tab:Selected": michael@0: case "FindInPage:Closed": michael@0: this.findClosed(); michael@0: break; michael@0: } michael@0: }, michael@0: michael@0: doFind: function(aSearchString) { michael@0: if (!this._finder) { michael@0: this._targetTab = BrowserApp.selectedTab; michael@0: this._finder = this._targetTab.browser.finder; michael@0: this._finder.addResultListener(this); michael@0: this._initialViewport = JSON.stringify(this._targetTab.getViewport()); michael@0: this._viewportChanged = false; michael@0: } michael@0: michael@0: this._finder.fastFind(aSearchString, false); michael@0: }, michael@0: michael@0: findAgain: function(aString, aFindBackwards) { michael@0: // This can happen if the user taps next/previous after re-opening the search bar michael@0: if (!this._finder) { michael@0: this.doFind(aString); michael@0: return; michael@0: } michael@0: michael@0: this._finder.findAgain(aFindBackwards, false, false); michael@0: }, michael@0: michael@0: findClosed: function() { michael@0: // If there's no find in progress, there's nothing to clean up michael@0: if (!this._finder) michael@0: return; michael@0: michael@0: this._finder.removeSelection(); michael@0: this._finder.removeResultListener(this); michael@0: this._finder = null; michael@0: this._targetTab = null; michael@0: this._initialViewport = null; michael@0: this._viewportChanged = false; michael@0: }, michael@0: michael@0: onFindResult: function(aData) { michael@0: if (aData.result == Ci.nsITypeAheadFind.FIND_NOTFOUND) { michael@0: if (this._viewportChanged) { michael@0: if (this._targetTab != BrowserApp.selectedTab) { michael@0: // this should never happen michael@0: Cu.reportError("Warning: selected tab changed during find!"); michael@0: // fall through and restore viewport on the initial tab anyway michael@0: } michael@0: this._targetTab.setViewport(JSON.parse(this._initialViewport)); michael@0: this._targetTab.sendViewportUpdate(); michael@0: } michael@0: } else { michael@0: // Disabled until bug 1014113 is fixed michael@0: //ZoomHelper.zoomToRect(aData.rect, -1, false, true); michael@0: this._viewportChanged = true; michael@0: } michael@0: } michael@0: };