toolkit/content/finddialog.js

branch
TOR_BUG_3246
changeset 7
129ffea94266
equal deleted inserted replaced
-1:000000000000 0:56e7012f4036
1 // -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
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/. */
6
7 Components.utils.import("resource://gre/modules/Services.jsm");
8 Components.utils.import("resource://gre/modules/FormHistory.jsm");
9
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
13
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;
26
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 }
35
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);
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;
52 }
53
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);
59
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 }
66
67 function onLoad()
68 {
69 initDialogObject();
70
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 }
81
82 fillDialog();
83 doEnabling();
84
85 if (dialog.findKey.value)
86 dialog.findKey.select();
87 dialog.findKey.focus();
88 }
89
90 function onUnload()
91 {
92 window.opener.findDialog = 0;
93 }
94
95 function onAccept()
96 {
97 if (gFindInstData && gFindInst != gFindInstData.webBrowserFind) {
98 gFindInstData.init();
99 gFindInst = gFindInstData.webBrowserFind;
100 }
101
102 // Transfer dialog contents to the find service.
103 saveFindData();
104 updateFormHistory();
105
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();
114
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 }
126
127 function doEnabling()
128 {
129 dialog.find.disabled = !dialog.findKey.value;
130 }
131
132 function updateFormHistory()
133 {
134 if (window.opener.PrivateBrowsingUtils &&
135 window.opener.PrivateBrowsingUtils.isWindowPrivate(window.opener) ||
136 !dialog.findKey.value)
137 return;
138
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