michael@0: // -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- michael@0: michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: Components.utils.import("resource://gre/modules/Services.jsm"); michael@0: Components.utils.import("resource://gre/modules/FormHistory.jsm"); michael@0: michael@0: var dialog; // Quick access to document/form elements. michael@0: var gFindInst; // nsIWebBrowserFind that we're going to use michael@0: var gFindInstData; // use this to update the find inst data michael@0: michael@0: function initDialogObject() michael@0: { michael@0: // Create dialog object and initialize. michael@0: dialog = new Object; michael@0: dialog.findKey = document.getElementById("dialog.findKey"); michael@0: dialog.caseSensitive = document.getElementById("dialog.caseSensitive"); michael@0: dialog.wrap = document.getElementById("dialog.wrap"); michael@0: dialog.find = document.getElementById("btnFind"); michael@0: dialog.up = document.getElementById("radioUp"); michael@0: dialog.down = document.getElementById("radioDown"); michael@0: dialog.rg = dialog.up.radioGroup; michael@0: dialog.bundle = null; michael@0: michael@0: // Move dialog to center, if it not been shown before michael@0: var windowElement = document.getElementById("findDialog"); michael@0: if (!windowElement.hasAttribute("screenX") || !windowElement.hasAttribute("screenY")) michael@0: { michael@0: sizeToContent(); michael@0: moveToAlertPosition(); michael@0: } michael@0: } michael@0: michael@0: function fillDialog() michael@0: { michael@0: // get the find service, which stores global find state michael@0: var findService = Components.classes["@mozilla.org/find/find_service;1"] michael@0: .getService(Components.interfaces.nsIFindService); michael@0: michael@0: // Set initial dialog field contents. Use the gFindInst attributes first, michael@0: // this is necessary for window.find() michael@0: dialog.findKey.value = gFindInst.searchString ? gFindInst.searchString : findService.searchString; michael@0: dialog.caseSensitive.checked = gFindInst.matchCase ? gFindInst.matchCase : findService.matchCase; michael@0: dialog.wrap.checked = gFindInst.wrapFind ? gFindInst.wrapFind : findService.wrapFind; michael@0: var findBackwards = gFindInst.findBackwards ? gFindInst.findBackwards : findService.findBackwards; michael@0: if (findBackwards) michael@0: dialog.rg.selectedItem = dialog.up; michael@0: else michael@0: dialog.rg.selectedItem = dialog.down; michael@0: } michael@0: michael@0: function saveFindData() michael@0: { michael@0: // get the find service, which stores global find state michael@0: var findService = Components.classes["@mozilla.org/find/find_service;1"] michael@0: .getService(Components.interfaces.nsIFindService); michael@0: michael@0: // Set data attributes per user input. michael@0: findService.searchString = dialog.findKey.value; michael@0: findService.matchCase = dialog.caseSensitive.checked; michael@0: findService.wrapFind = dialog.wrap.checked; michael@0: findService.findBackwards = dialog.up.selected; michael@0: } michael@0: michael@0: function onLoad() michael@0: { michael@0: initDialogObject(); michael@0: michael@0: // get the find instance michael@0: var arg0 = window.arguments[0]; michael@0: // If the dialog was opened from window.find(), michael@0: // arg0 will be an instance of nsIWebBrowserFind michael@0: if (arg0 instanceof Components.interfaces.nsIWebBrowserFind) { michael@0: gFindInst = arg0; michael@0: } else { michael@0: gFindInstData = arg0; michael@0: gFindInst = gFindInstData.webBrowserFind; michael@0: } michael@0: michael@0: fillDialog(); michael@0: doEnabling(); michael@0: michael@0: if (dialog.findKey.value) michael@0: dialog.findKey.select(); michael@0: dialog.findKey.focus(); michael@0: } michael@0: michael@0: function onUnload() michael@0: { michael@0: window.opener.findDialog = 0; michael@0: } michael@0: michael@0: function onAccept() michael@0: { michael@0: if (gFindInstData && gFindInst != gFindInstData.webBrowserFind) { michael@0: gFindInstData.init(); michael@0: gFindInst = gFindInstData.webBrowserFind; michael@0: } michael@0: michael@0: // Transfer dialog contents to the find service. michael@0: saveFindData(); michael@0: updateFormHistory(); michael@0: michael@0: // set up the find instance michael@0: gFindInst.searchString = dialog.findKey.value; michael@0: gFindInst.matchCase = dialog.caseSensitive.checked; michael@0: gFindInst.wrapFind = dialog.wrap.checked; michael@0: gFindInst.findBackwards = dialog.up.selected; michael@0: michael@0: // Search. michael@0: var result = gFindInst.findNext(); michael@0: michael@0: if (!result) michael@0: { michael@0: if (!dialog.bundle) michael@0: dialog.bundle = document.getElementById("findBundle"); michael@0: Services.prompt.alert(window, dialog.bundle.getString("notFoundTitle"), michael@0: dialog.bundle.getString("notFoundWarning")); michael@0: dialog.findKey.select(); michael@0: dialog.findKey.focus(); michael@0: } michael@0: return false; michael@0: } michael@0: michael@0: function doEnabling() michael@0: { michael@0: dialog.find.disabled = !dialog.findKey.value; michael@0: } michael@0: michael@0: function updateFormHistory() michael@0: { michael@0: if (window.opener.PrivateBrowsingUtils && michael@0: window.opener.PrivateBrowsingUtils.isWindowPrivate(window.opener) || michael@0: !dialog.findKey.value) michael@0: return; michael@0: michael@0: FormHistory.update({ michael@0: op: "bump", michael@0: fieldname: "find-dialog", michael@0: value: dialog.findKey.value michael@0: }, { michael@0: handleError: function(aError) { michael@0: Components.utils.reportError("Saving find to form history failed: " + michael@0: aError.message); michael@0: } michael@0: }); michael@0: }