mobile/android/chrome/content/FindHelper.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/mobile/android/chrome/content/FindHelper.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,85 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this file,
     1.6 + * You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +"use strict";
     1.8 +
     1.9 +var FindHelper = {
    1.10 +  _finder: null,
    1.11 +  _targetTab: null,
    1.12 +  _initialViewport: null,
    1.13 +  _viewportChanged: false,
    1.14 +
    1.15 +  observe: function(aMessage, aTopic, aData) {
    1.16 +    switch(aTopic) {
    1.17 +      case "FindInPage:Find":
    1.18 +        this.doFind(aData);
    1.19 +        break;
    1.20 +
    1.21 +      case "FindInPage:Prev":
    1.22 +        this.findAgain(aData, true);
    1.23 +        break;
    1.24 +
    1.25 +      case "FindInPage:Next":
    1.26 +        this.findAgain(aData, false);
    1.27 +        break;
    1.28 +
    1.29 +      case "Tab:Selected":
    1.30 +      case "FindInPage:Closed":
    1.31 +        this.findClosed();
    1.32 +        break;
    1.33 +    }
    1.34 +  },
    1.35 +
    1.36 +  doFind: function(aSearchString) {
    1.37 +    if (!this._finder) {
    1.38 +      this._targetTab = BrowserApp.selectedTab;
    1.39 +      this._finder = this._targetTab.browser.finder;
    1.40 +      this._finder.addResultListener(this);
    1.41 +      this._initialViewport = JSON.stringify(this._targetTab.getViewport());
    1.42 +      this._viewportChanged = false;
    1.43 +    }
    1.44 +
    1.45 +    this._finder.fastFind(aSearchString, false);
    1.46 +  },
    1.47 +
    1.48 +  findAgain: function(aString, aFindBackwards) {
    1.49 +    // This can happen if the user taps next/previous after re-opening the search bar
    1.50 +    if (!this._finder) {
    1.51 +      this.doFind(aString);
    1.52 +      return;
    1.53 +    }
    1.54 +
    1.55 +    this._finder.findAgain(aFindBackwards, false, false);
    1.56 +  },
    1.57 +
    1.58 +  findClosed: function() {
    1.59 +    // If there's no find in progress, there's nothing to clean up
    1.60 +    if (!this._finder)
    1.61 +      return;
    1.62 +
    1.63 +    this._finder.removeSelection();
    1.64 +    this._finder.removeResultListener(this);
    1.65 +    this._finder = null;
    1.66 +    this._targetTab = null;
    1.67 +    this._initialViewport = null;
    1.68 +    this._viewportChanged = false;
    1.69 +  },
    1.70 +
    1.71 +  onFindResult: function(aData) {
    1.72 +    if (aData.result == Ci.nsITypeAheadFind.FIND_NOTFOUND) {
    1.73 +      if (this._viewportChanged) {
    1.74 +        if (this._targetTab != BrowserApp.selectedTab) {
    1.75 +          // this should never happen
    1.76 +          Cu.reportError("Warning: selected tab changed during find!");
    1.77 +          // fall through and restore viewport on the initial tab anyway
    1.78 +        }
    1.79 +        this._targetTab.setViewport(JSON.parse(this._initialViewport));
    1.80 +        this._targetTab.sendViewportUpdate();
    1.81 +      }
    1.82 +    } else {
    1.83 +      // Disabled until bug 1014113 is fixed
    1.84 +      //ZoomHelper.zoomToRect(aData.rect, -1, false, true);
    1.85 +      this._viewportChanged = true;
    1.86 +    }
    1.87 +  }
    1.88 +};

mercurial