toolkit/content/finddialog.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/content/finddialog.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,149 @@
     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 +Components.utils.import("resource://gre/modules/FormHistory.jsm");
    1.12 +
    1.13 +var dialog;     // Quick access to document/form elements.
    1.14 +var gFindInst;   // nsIWebBrowserFind that we're going to use
    1.15 +var gFindInstData; // use this to update the find inst data
    1.16 +
    1.17 +function initDialogObject()
    1.18 +{
    1.19 +  // Create dialog object and initialize.
    1.20 +  dialog = new Object;
    1.21 +  dialog.findKey         = document.getElementById("dialog.findKey");
    1.22 +  dialog.caseSensitive   = document.getElementById("dialog.caseSensitive");
    1.23 +  dialog.wrap            = document.getElementById("dialog.wrap");
    1.24 +  dialog.find            = document.getElementById("btnFind");
    1.25 +  dialog.up              = document.getElementById("radioUp");
    1.26 +  dialog.down            = document.getElementById("radioDown");
    1.27 +  dialog.rg              = dialog.up.radioGroup;
    1.28 +  dialog.bundle          = null;
    1.29 +
    1.30 +  // Move dialog to center, if it not been shown before
    1.31 +  var windowElement = document.getElementById("findDialog");
    1.32 +  if (!windowElement.hasAttribute("screenX") || !windowElement.hasAttribute("screenY"))
    1.33 +  {
    1.34 +    sizeToContent();
    1.35 +    moveToAlertPosition();
    1.36 +  }
    1.37 +}
    1.38 +
    1.39 +function fillDialog()
    1.40 +{
    1.41 +  // get the find service, which stores global find state
    1.42 +  var findService = Components.classes["@mozilla.org/find/find_service;1"]
    1.43 +                              .getService(Components.interfaces.nsIFindService);
    1.44 +  
    1.45 +  // Set initial dialog field contents. Use the gFindInst attributes first,
    1.46 +  // this is necessary for window.find()
    1.47 +  dialog.findKey.value           = gFindInst.searchString ? gFindInst.searchString : findService.searchString;
    1.48 +  dialog.caseSensitive.checked   = gFindInst.matchCase ? gFindInst.matchCase : findService.matchCase;
    1.49 +  dialog.wrap.checked            = gFindInst.wrapFind ? gFindInst.wrapFind : findService.wrapFind;
    1.50 +  var findBackwards              = gFindInst.findBackwards ? gFindInst.findBackwards : findService.findBackwards;
    1.51 +  if (findBackwards)
    1.52 +    dialog.rg.selectedItem = dialog.up;
    1.53 +  else
    1.54 +    dialog.rg.selectedItem = dialog.down;
    1.55 +}
    1.56 +
    1.57 +function saveFindData()
    1.58 +{
    1.59 +  // get the find service, which stores global find state
    1.60 +  var findService = Components.classes["@mozilla.org/find/find_service;1"]
    1.61 +                         .getService(Components.interfaces.nsIFindService);
    1.62 +
    1.63 +  // Set data attributes per user input.
    1.64 +  findService.searchString  = dialog.findKey.value;
    1.65 +  findService.matchCase     = dialog.caseSensitive.checked;
    1.66 +  findService.wrapFind      = dialog.wrap.checked;
    1.67 +  findService.findBackwards = dialog.up.selected;
    1.68 +}
    1.69 +
    1.70 +function onLoad()
    1.71 +{
    1.72 +  initDialogObject();
    1.73 +
    1.74 +  // get the find instance
    1.75 +  var arg0 = window.arguments[0];                                               
    1.76 +  // If the dialog was opened from window.find(),
    1.77 +  // arg0 will be an instance of nsIWebBrowserFind
    1.78 +  if (arg0 instanceof Components.interfaces.nsIWebBrowserFind) {
    1.79 +    gFindInst = arg0;
    1.80 +  } else {  
    1.81 +    gFindInstData = arg0;                                                       
    1.82 +    gFindInst = gFindInstData.webBrowserFind;                                   
    1.83 +  }                                                                             
    1.84 +
    1.85 +  fillDialog();
    1.86 +  doEnabling();
    1.87 +
    1.88 +  if (dialog.findKey.value)
    1.89 +    dialog.findKey.select();
    1.90 +  dialog.findKey.focus();
    1.91 +}
    1.92 +
    1.93 +function onUnload()
    1.94 +{
    1.95 +  window.opener.findDialog = 0;
    1.96 +}
    1.97 +
    1.98 +function onAccept()
    1.99 +{
   1.100 +  if (gFindInstData && gFindInst != gFindInstData.webBrowserFind) {
   1.101 +    gFindInstData.init();             
   1.102 +    gFindInst = gFindInstData.webBrowserFind;
   1.103 +  }
   1.104 +
   1.105 +  // Transfer dialog contents to the find service.
   1.106 +  saveFindData();
   1.107 +  updateFormHistory();
   1.108 +
   1.109 +  // set up the find instance
   1.110 +  gFindInst.searchString  = dialog.findKey.value;
   1.111 +  gFindInst.matchCase     = dialog.caseSensitive.checked;
   1.112 +  gFindInst.wrapFind      = dialog.wrap.checked;
   1.113 +  gFindInst.findBackwards = dialog.up.selected;
   1.114 +  
   1.115 +  // Search.
   1.116 +  var result = gFindInst.findNext();
   1.117 +
   1.118 +  if (!result)
   1.119 +  {
   1.120 +    if (!dialog.bundle)
   1.121 +      dialog.bundle = document.getElementById("findBundle");
   1.122 +    Services.prompt.alert(window, dialog.bundle.getString("notFoundTitle"),
   1.123 +                                  dialog.bundle.getString("notFoundWarning"));
   1.124 +    dialog.findKey.select();
   1.125 +    dialog.findKey.focus();
   1.126 +  } 
   1.127 +  return false;
   1.128 +}
   1.129 +
   1.130 +function doEnabling()
   1.131 +{
   1.132 +  dialog.find.disabled = !dialog.findKey.value;
   1.133 +}
   1.134 +
   1.135 +function updateFormHistory()
   1.136 +{
   1.137 +  if (window.opener.PrivateBrowsingUtils &&
   1.138 +      window.opener.PrivateBrowsingUtils.isWindowPrivate(window.opener) ||
   1.139 +      !dialog.findKey.value)
   1.140 +    return;
   1.141 +
   1.142 +  FormHistory.update({
   1.143 +    op: "bump",
   1.144 +    fieldname: "find-dialog",
   1.145 +    value: dialog.findKey.value
   1.146 +  }, {
   1.147 +    handleError: function(aError) {
   1.148 +      Components.utils.reportError("Saving find to form history failed: " +
   1.149 +                                   aError.message);
   1.150 +    }
   1.151 +  });
   1.152 +}

mercurial