Backout a74bd5095902, Bug 959405 - Please update the Buri Moz-central, 1.3, 1.2 with...
[gecko.git] / toolkit / content / finddialog.js
blob57c1ead3357f492190a6a09bf7cd64f9cd3f62cd
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()
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   }
36 function fillDialog()
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);
41   
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;
54 function saveFindData()
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;
67 function onLoad()
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();
90 function onUnload()
92   window.opener.findDialog = 0;
95 function onAccept()
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;
111   
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;
127 function doEnabling()
129   dialog.find.disabled = !dialog.findKey.value;
132 function updateFormHistory()
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   });