home *** CD-ROM | disk | FTP | other *** search
/ PC go! 2006 April / PCgo_0406_DVD.iso / lib / chrome / embed.jar / content / global / finddialog.js < prev    next >
Encoding:
JavaScript  |  2002-09-18  |  5.1 KB  |  144 lines

  1. /* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2.  *
  3.  * The contents of this file are subject to the Netscape Public
  4.  * License Version 1.1 (the "License"); you may not use this file
  5.  * except in compliance with the License. You may obtain a copy of
  6.  * the License at http://www.mozilla.org/NPL/
  7.  *
  8.  * Software distributed under the License is distributed on an "AS
  9.  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
  10.  * implied. See the License for the specific language governing
  11.  * rights and limitations under the License.
  12.  *
  13.  * The Original Code is Mozilla Communicator client code, released March
  14.  * 31, 1998.
  15.  *
  16.  * The Initial Developer of the Original Code is Netscape Communications
  17.  * Corporation. Portions created by Netscape are
  18.  * Copyright (C) 1998 Netscape Communications Corporation. All
  19.  * Rights Reserved.
  20.  *
  21.  * Contributor(s): Alec Flett       <alecf@netscape.com>
  22.  *                 Bill Law         <law@netscape.com>
  23.  *                 Blake Ross       <blakeross@telocity.com>
  24.  *                 Dean Tessman     <dean_tessman@hotmail.com>
  25.  *                 Matt Fisher      <matt@netscape.com>
  26.  *                 Simon Fraser     <sfraser@netscape.com>
  27.  *                 Stuart Parmenter <pavlov@netscape.com>
  28.  */
  29.  
  30. var dialog;     // Quick access to document/form elements.
  31. var gFindInst;   // nsIWebBrowserFind that we're going to use
  32.  
  33. function initDialogObject()
  34. {
  35.   // Create dialog object and initialize.
  36.   dialog = new Object;
  37.   dialog.findKey         = document.getElementById("dialog.findKey");
  38.   dialog.caseSensitive   = document.getElementById("dialog.caseSensitive");
  39.   dialog.wrap            = document.getElementById("dialog.wrap");
  40.   dialog.searchBackwards = document.getElementById("dialog.searchBackwards");
  41.   dialog.find            = document.getElementById("findDialog").getButton("accept");
  42.   dialog.bundle          = null;
  43.  
  44.   // Move dialog to center, if it not been shown before
  45.   var windowElement = document.getElementById("findDialog");
  46.   if (!windowElement.hasAttribute("screenX") || !windowElement.hasAttribute("screenY"))
  47.   {
  48.     sizeToContent();
  49.     moveToAlertPosition();
  50.   }
  51. }
  52.  
  53. function fillDialog()
  54. {
  55.   // get the find service, which stores global find state
  56.   var findService = Components.classes["@mozilla.org/find/find_service;1"]
  57.                          .getService(Components.interfaces.nsIFindService);
  58.   
  59.   // Set initial dialog field contents. Use the gFindInst attributes first,
  60.   // this is necessary for window.find()
  61.   dialog.findKey.value           = gFindInst.searchString ? gFindInst.searchString : findService.searchString;
  62.   dialog.caseSensitive.checked   = gFindInst.matchCase ? gFindInst.matchCase : findService.matchCase;
  63.   dialog.wrap.checked            = gFindInst.wrapFind ? gFindInst.wrapFind : findService.wrapFind;
  64.   dialog.searchBackwards.checked = gFindInst.findBackwards ? gFindInst.findBackwards : findService.findBackwards;
  65. }
  66.  
  67. function saveFindData()
  68. {
  69.   // get the find service, which stores global find state
  70.   var findService = Components.classes["@mozilla.org/find/find_service;1"]
  71.                          .getService(Components.interfaces.nsIFindService);
  72.  
  73.   // Set data attributes per user input.
  74.   findService.searchString  = dialog.findKey.value;
  75.   findService.matchCase     = dialog.caseSensitive.checked;
  76.   findService.wrapFind      = dialog.wrap.checked;
  77.   findService.findBackwards = dialog.searchBackwards.checked;
  78. }
  79.  
  80. function onLoad()
  81. {
  82.   initDialogObject();
  83.  
  84.   // Change "OK" to "Find".
  85.   dialog.find.label = document.getElementById("fBLT").getAttribute("label");
  86.   dialog.find.accessKey = document.getElementById("fBLT").getAttribute("accesskey");
  87.  
  88.   // get the find instance
  89.   var finder = window.arguments[0];
  90.   // If the dialog was opened from window.find(), findInst will be an
  91.   // nsISupports interface, so QueryInterface anyway to nsIWebBrowserFind.
  92.   gFindInst = finder.QueryInterface(Components.interfaces.nsIWebBrowserFind);
  93.  
  94.   fillDialog();
  95.   doEnabling();
  96.  
  97.   if (dialog.findKey.value)
  98.     dialog.findKey.select();
  99.   dialog.findKey.focus();
  100. }
  101.  
  102. function onUnload()
  103. {
  104.     window.opener.findDialog = 0;
  105. }
  106.  
  107. function onAccept()
  108. {
  109.   // Transfer dialog contents to the find service.
  110.   saveFindData();
  111.  
  112.   // set up the find instance
  113.   gFindInst.searchString  = dialog.findKey.value;
  114.   gFindInst.matchCase     = dialog.caseSensitive.checked;
  115.   gFindInst.wrapFind      = dialog.wrap.checked;
  116.   gFindInst.findBackwards = dialog.searchBackwards.checked;
  117.   
  118.   // Search.
  119.   var result = gFindInst.findNext();
  120.  
  121.   if (!result)
  122.   {
  123.     if (!dialog.bundle)
  124.       dialog.bundle = document.getElementById("findBundle");
  125.  
  126.     try {
  127.       var promptService = Components.classes["@mozilla.org/embedcomp/prompt-service;1"].getService();
  128.       promptService = promptService.QueryInterface(Components.interfaces.nsIPromptService);
  129.       promptService.alert(window, null,
  130.                           dialog.bundle.getString("notFoundWarning"));
  131.     }
  132.     catch (e) { dump("The text you entered was not found."); }
  133.  
  134.     dialog.findKey.select();
  135.     dialog.findKey.focus();
  136.   } 
  137.   return false;
  138. }
  139.  
  140. function doEnabling()
  141. {
  142.   dialog.find.disabled = !dialog.findKey.value;
  143. }
  144.