toolkit/content/findUtils.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/content/findUtils.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,111 @@
     1.4 +// -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
     1.5 +
     1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.9 +
    1.10 +Components.utils.import("resource://gre/modules/Services.jsm");
    1.11 +
    1.12 +var gFindBundle;
    1.13 +
    1.14 +function nsFindInstData() {}
    1.15 +nsFindInstData.prototype =
    1.16 +{
    1.17 +  // set the next three attributes on your object to override the defaults
    1.18 +  browser : null,
    1.19 +
    1.20 +  get rootSearchWindow() { return this._root || this.window.content; },
    1.21 +  set rootSearchWindow(val) { this._root = val; },
    1.22 +
    1.23 +  get currentSearchWindow() {
    1.24 +    if (this._current)
    1.25 +      return this._current;
    1.26 +
    1.27 +    var focusedWindow = this.window.document.commandDispatcher.focusedWindow;
    1.28 +    if (!focusedWindow || focusedWindow == this.window)
    1.29 +      focusedWindow = this.window.content;
    1.30 +
    1.31 +    return focusedWindow;
    1.32 +  },
    1.33 +  set currentSearchWindow(val) { this._current = val; },
    1.34 +
    1.35 +  get webBrowserFind() { return this.browser.webBrowserFind; },
    1.36 +
    1.37 +  init : function() {
    1.38 +    var findInst = this.webBrowserFind;
    1.39 +    // set up the find to search the focussedWindow, bounded by the content window.
    1.40 +    var findInFrames = findInst.QueryInterface(Components.interfaces.nsIWebBrowserFindInFrames);
    1.41 +    findInFrames.rootSearchFrame = this.rootSearchWindow;
    1.42 +    findInFrames.currentSearchFrame = this.currentSearchWindow;
    1.43 +  
    1.44 +    // always search in frames for now. We could add a checkbox to the dialog for this.
    1.45 +    findInst.searchFrames = true;
    1.46 +  },
    1.47 +
    1.48 +  window : window,
    1.49 +  _root : null,
    1.50 +  _current : null
    1.51 +}
    1.52 +
    1.53 +// browser is the <browser> element
    1.54 +// rootSearchWindow is the window to constrain the search to (normally window.content)
    1.55 +// currentSearchWindow is the frame to start searching (can be, and normally, rootSearchWindow)
    1.56 +function findInPage(findInstData)
    1.57 +{
    1.58 +  // is the dialog up already?
    1.59 +  if ("findDialog" in window && window.findDialog)
    1.60 +    window.findDialog.focus();
    1.61 +  else
    1.62 +  {
    1.63 +    findInstData.init();
    1.64 +    window.findDialog = window.openDialog("chrome://global/content/finddialog.xul", "_blank", "chrome,resizable=no,dependent=yes", findInstData);
    1.65 +  }
    1.66 +}
    1.67 +
    1.68 +function findAgainInPage(findInstData, reverse)
    1.69 +{
    1.70 +  if ("findDialog" in window && window.findDialog)
    1.71 +    window.findDialog.focus();
    1.72 +  else
    1.73 +  {
    1.74 +    // get the find service, which stores global find state, and init the
    1.75 +    // nsIWebBrowser find with it. We don't assume that there was a previous
    1.76 +    // Find that set this up.
    1.77 +    var findService = Components.classes["@mozilla.org/find/find_service;1"]
    1.78 +                           .getService(Components.interfaces.nsIFindService);
    1.79 +
    1.80 +    var searchString = findService.searchString;
    1.81 +    if (searchString.length == 0) {
    1.82 +      // no previous find text
    1.83 +      findInPage(findInstData);
    1.84 +      return;
    1.85 +    }
    1.86 +
    1.87 +    findInstData.init();
    1.88 +    var findInst = findInstData.webBrowserFind;
    1.89 +    findInst.searchString  = searchString;
    1.90 +    findInst.matchCase     = findService.matchCase;
    1.91 +    findInst.wrapFind      = findService.wrapFind;
    1.92 +    findInst.entireWord    = findService.entireWord;
    1.93 +    findInst.findBackwards = findService.findBackwards ^ reverse;
    1.94 +
    1.95 +    var found = findInst.findNext();
    1.96 +    if (!found) {
    1.97 +      if (!gFindBundle)
    1.98 +        gFindBundle = document.getElementById("findBundle");
    1.99 +
   1.100 +      Services.prompt.alert(window, gFindBundle.getString("notFoundTitle"), gFindBundle.getString("notFoundWarning"));
   1.101 +    }
   1.102 +
   1.103 +    // Reset to normal value, otherwise setting can get changed in find dialog
   1.104 +    findInst.findBackwards = findService.findBackwards; 
   1.105 +  }
   1.106 +}
   1.107 +
   1.108 +function canFindAgainInPage()
   1.109 +{
   1.110 +    var findService = Components.classes["@mozilla.org/find/find_service;1"]
   1.111 +                           .getService(Components.interfaces.nsIFindService);
   1.112 +    return (findService.searchString.length > 0);
   1.113 +}
   1.114 +

mercurial