home *** CD-ROM | disk | FTP | other *** search
/ PC World 2005 March / PCWorld_2005-03_cd.bin / komunikace / kmeleon / kmeleon09.exe / comm.jar / content / communicator / findUtils.js < prev    next >
Text File  |  2003-04-18  |  6KB  |  147 lines

  1. /* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* ***** BEGIN LICENSE BLOCK *****
  3.  * Version: NPL 1.1/GPL 2.0/LGPL 2.1
  4.  *
  5.  * The contents of this file are subject to the Netscape Public License
  6.  * Version 1.1 (the "License"); you may not use this file except in
  7.  * compliance with the License. You may obtain a copy of the License at
  8.  * http://www.mozilla.org/NPL/
  9.  *
  10.  * Software distributed under the License is distributed on an "AS IS" basis,
  11.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  12.  * for the specific language governing rights and limitations under the
  13.  * License.
  14.  *
  15.  * The Original Code is mozilla.org code.
  16.  *
  17.  * The Initial Developer of the Original Code is 
  18.  * Netscape Communications Corporation.
  19.  * Portions created by the Initial Developer are Copyright (C) 1998
  20.  * the Initial Developer. All Rights Reserved.
  21.  *
  22.  * Contributor(s):
  23.  *   Simon Fraser <sfraser@netscape.com>
  24.  *   Dean Tessman <dean_tessman@hotmail.com>
  25.  *
  26.  * Alternatively, the contents of this file may be used under the terms of
  27.  * either the GNU General Public License Version 2 or later (the "GPL"), or 
  28.  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  29.  * in which case the provisions of the GPL or the LGPL are applicable instead
  30.  * of those above. If you wish to allow use of your version of this file only
  31.  * under the terms of either the GPL or the LGPL, and not to allow others to
  32.  * use your version of this file under the terms of the NPL, indicate your
  33.  * decision by deleting the provisions above and replace them with the notice
  34.  * and other provisions required by the GPL or the LGPL. If you do not delete
  35.  * the provisions above, a recipient may use your version of this file under
  36.  * the terms of any one of the NPL, the GPL or the LGPL.
  37.  *
  38.  * ***** END LICENSE BLOCK ***** */
  39.  
  40. var gPromptService;
  41. var gFindBundle;
  42.  
  43. function nsFindInstData() {}
  44. nsFindInstData.prototype =
  45. {
  46.   // set the next three attributes on your object to override the defaults
  47.   browser : null,
  48.  
  49.   get rootSearchWindow() { return this._root || this.window.content; },
  50.   set rootSearchWindow(val) { this._root = val; },
  51.  
  52.   get currentSearchWindow() {
  53.     if (this._current)
  54.       return this._current;
  55.  
  56.     var focusedWindow = this.window.document.commandDispatcher.focusedWindow;
  57.     if (!focusedWindow || focusedWindow == this.window)
  58.       focusedWindow = this.window.content;
  59.  
  60.     return focusedWindow;
  61.   },
  62.   set currentSearchWindow(val) { this._current = val; },
  63.  
  64.   get webBrowserFind() { return this.browser.webBrowserFind; },
  65.  
  66.   init : function() {
  67.     var findInst = this.webBrowserFind;
  68.     // set up the find to search the focussedWindow, bounded by the content window.
  69.     var findInFrames = findInst.QueryInterface(Components.interfaces.nsIWebBrowserFindInFrames);
  70.     findInFrames.rootSearchFrame = this.rootSearchWindow;
  71.     findInFrames.currentSearchFrame = this.currentSearchWindow;
  72.   
  73.     // always search in frames for now. We could add a checkbox to the dialog for this.
  74.     findInst.searchFrames = true;
  75.   },
  76.  
  77.   window : window,
  78.   _root : null,
  79.   _current : null
  80. }
  81.  
  82. // browser is the <browser> element
  83. // rootSearchWindow is the window to constrain the search to (normally window._content)
  84. // currentSearchWindow is the frame to start searching (can be, and normally, rootSearchWindow)
  85. function findInPage(findInstData)
  86. {
  87.   // is the dialog up already?
  88.   if ("findDialog" in window && window.findDialog)
  89.     window.findDialog.focus();
  90.   else
  91.   {
  92.     findInstData.init();
  93.     window.findDialog = window.openDialog("chrome://global/content/finddialog.xul", "_blank", "chrome,resizable=no,dependent=yes", findInstData);
  94.   }
  95. }
  96.  
  97. function findAgainInPage(findInstData, reverse)
  98. {
  99.   if ("findDialog" in window && window.findDialog)
  100.     window.findDialog.focus();
  101.   else
  102.   {
  103.     // get the find service, which stores global find state, and init the
  104.     // nsIWebBrowser find with it. We don't assume that there was a previous
  105.     // Find that set this up.
  106.     var findService = Components.classes["@mozilla.org/find/find_service;1"]
  107.                            .getService(Components.interfaces.nsIFindService);
  108.  
  109.     var searchString = findService.searchString;
  110.     if (searchString.length == 0) {
  111.       // no previous find text
  112.       findInPage(findInstData);
  113.       return;
  114.     }
  115.  
  116.     findInstData.init();
  117.     var findInst = findInstData.webBrowserFind;
  118.     findInst.searchString  = searchString;
  119.     findInst.matchCase     = findService.matchCase;
  120.     findInst.wrapFind      = findService.wrapFind;
  121.     findInst.entireWord    = findService.entireWord;
  122.     findInst.findBackwards = findService.findBackwards ^ reverse;
  123.  
  124.     var found = findInst.findNext();
  125.     if (!found) {
  126.       if (!gPromptService)
  127.         gPromptService = Components.classes["@mozilla.org/embedcomp/prompt-service;1"].getService()
  128.                                    .QueryInterface(Components.interfaces.nsIPromptService);                                     
  129.       if (!gFindBundle)
  130.         gFindBundle = document.getElementById("findBundle");
  131.           
  132.       gPromptService.alert(window, gFindBundle.getString("notFoundTitle"), gFindBundle.getString("notFoundWarning"));
  133.     }      
  134.  
  135.     // Reset to normal value, otherwise setting can get changed in find dialog
  136.     findInst.findBackwards = findService.findBackwards; 
  137.   }
  138. }
  139.  
  140. function canFindAgainInPage()
  141. {
  142.     var findService = Components.classes["@mozilla.org/find/find_service;1"]
  143.                            .getService(Components.interfaces.nsIFindService);
  144.     return (findService.searchString.length > 0);
  145. }
  146.  
  147.