toolkit/content/finddialog.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     1 // -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
     3 /* This Source Code Form is subject to the terms of the Mozilla Public
     4  * License, v. 2.0. If a copy of the MPL was not distributed with this
     5  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     7 Components.utils.import("resource://gre/modules/Services.jsm");
     8 Components.utils.import("resource://gre/modules/FormHistory.jsm");
    10 var dialog;     // Quick access to document/form elements.
    11 var gFindInst;   // nsIWebBrowserFind that we're going to use
    12 var gFindInstData; // use this to update the find inst data
    14 function initDialogObject()
    15 {
    16   // Create dialog object and initialize.
    17   dialog = new Object;
    18   dialog.findKey         = document.getElementById("dialog.findKey");
    19   dialog.caseSensitive   = document.getElementById("dialog.caseSensitive");
    20   dialog.wrap            = document.getElementById("dialog.wrap");
    21   dialog.find            = document.getElementById("btnFind");
    22   dialog.up              = document.getElementById("radioUp");
    23   dialog.down            = document.getElementById("radioDown");
    24   dialog.rg              = dialog.up.radioGroup;
    25   dialog.bundle          = null;
    27   // Move dialog to center, if it not been shown before
    28   var windowElement = document.getElementById("findDialog");
    29   if (!windowElement.hasAttribute("screenX") || !windowElement.hasAttribute("screenY"))
    30   {
    31     sizeToContent();
    32     moveToAlertPosition();
    33   }
    34 }
    36 function fillDialog()
    37 {
    38   // get the find service, which stores global find state
    39   var findService = Components.classes["@mozilla.org/find/find_service;1"]
    40                               .getService(Components.interfaces.nsIFindService);
    42   // Set initial dialog field contents. Use the gFindInst attributes first,
    43   // this is necessary for window.find()
    44   dialog.findKey.value           = gFindInst.searchString ? gFindInst.searchString : findService.searchString;
    45   dialog.caseSensitive.checked   = gFindInst.matchCase ? gFindInst.matchCase : findService.matchCase;
    46   dialog.wrap.checked            = gFindInst.wrapFind ? gFindInst.wrapFind : findService.wrapFind;
    47   var findBackwards              = gFindInst.findBackwards ? gFindInst.findBackwards : findService.findBackwards;
    48   if (findBackwards)
    49     dialog.rg.selectedItem = dialog.up;
    50   else
    51     dialog.rg.selectedItem = dialog.down;
    52 }
    54 function saveFindData()
    55 {
    56   // get the find service, which stores global find state
    57   var findService = Components.classes["@mozilla.org/find/find_service;1"]
    58                          .getService(Components.interfaces.nsIFindService);
    60   // Set data attributes per user input.
    61   findService.searchString  = dialog.findKey.value;
    62   findService.matchCase     = dialog.caseSensitive.checked;
    63   findService.wrapFind      = dialog.wrap.checked;
    64   findService.findBackwards = dialog.up.selected;
    65 }
    67 function onLoad()
    68 {
    69   initDialogObject();
    71   // get the find instance
    72   var arg0 = window.arguments[0];                                               
    73   // If the dialog was opened from window.find(),
    74   // arg0 will be an instance of nsIWebBrowserFind
    75   if (arg0 instanceof Components.interfaces.nsIWebBrowserFind) {
    76     gFindInst = arg0;
    77   } else {  
    78     gFindInstData = arg0;                                                       
    79     gFindInst = gFindInstData.webBrowserFind;                                   
    80   }                                                                             
    82   fillDialog();
    83   doEnabling();
    85   if (dialog.findKey.value)
    86     dialog.findKey.select();
    87   dialog.findKey.focus();
    88 }
    90 function onUnload()
    91 {
    92   window.opener.findDialog = 0;
    93 }
    95 function onAccept()
    96 {
    97   if (gFindInstData && gFindInst != gFindInstData.webBrowserFind) {
    98     gFindInstData.init();             
    99     gFindInst = gFindInstData.webBrowserFind;
   100   }
   102   // Transfer dialog contents to the find service.
   103   saveFindData();
   104   updateFormHistory();
   106   // set up the find instance
   107   gFindInst.searchString  = dialog.findKey.value;
   108   gFindInst.matchCase     = dialog.caseSensitive.checked;
   109   gFindInst.wrapFind      = dialog.wrap.checked;
   110   gFindInst.findBackwards = dialog.up.selected;
   112   // Search.
   113   var result = gFindInst.findNext();
   115   if (!result)
   116   {
   117     if (!dialog.bundle)
   118       dialog.bundle = document.getElementById("findBundle");
   119     Services.prompt.alert(window, dialog.bundle.getString("notFoundTitle"),
   120                                   dialog.bundle.getString("notFoundWarning"));
   121     dialog.findKey.select();
   122     dialog.findKey.focus();
   123   } 
   124   return false;
   125 }
   127 function doEnabling()
   128 {
   129   dialog.find.disabled = !dialog.findKey.value;
   130 }
   132 function updateFormHistory()
   133 {
   134   if (window.opener.PrivateBrowsingUtils &&
   135       window.opener.PrivateBrowsingUtils.isWindowPrivate(window.opener) ||
   136       !dialog.findKey.value)
   137     return;
   139   FormHistory.update({
   140     op: "bump",
   141     fieldname: "find-dialog",
   142     value: dialog.findKey.value
   143   }, {
   144     handleError: function(aError) {
   145       Components.utils.reportError("Saving find to form history failed: " +
   146                                    aError.message);
   147     }
   148   });
   149 }

mercurial