toolkit/content/finddialog.js

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

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

mercurial