PageRenderTime 26ms CodeModel.GetById 13ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 1ms

/toolkit/content/finddialog.js

http://github.com/zpao/v8monkey
JavaScript | 165 lines | 134 code | 18 blank | 13 comment | 12 complexity | 0b9607bb6cfd2b6bc716dd32a81a33ad MD5 | raw file
  1# -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2#
  3# ***** BEGIN LICENSE BLOCK *****
  4# Version: MPL 1.1/GPL 2.0/LGPL 2.1
  5#
  6# The contents of this file are subject to the Mozilla Public License Version
  7# 1.1 (the "License"); you may not use this file except in compliance with
  8# the License. You may obtain a copy of the License at
  9# http://www.mozilla.org/MPL/
 10#
 11# Software distributed under the License is distributed on an "AS IS" basis,
 12# WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 13# for the specific language governing rights and limitations under the
 14# License.
 15#
 16# The Original Code is Mozilla Communicator client code, released
 17# March 31, 1998.
 18#
 19# The Initial Developer of the Original Code is
 20# Netscape Communications Corporation.
 21# Portions created by the Initial Developer are Copyright (C) 1998
 22# the Initial Developer. All Rights Reserved.
 23#
 24# Contributor(s):
 25#   Alec Flett       <alecf@netscape.com>
 26#   Bill Law         <law@netscape.com>
 27#   Blake Ross       <blakeross@telocity.com>
 28#   Dean Tessman     <dean_tessman@hotmail.com>
 29#   Matt Fisher      <matt@netscape.com>
 30#   Simon Fraser     <sfraser@netscape.com>
 31#   Stuart Parmenter <pavlov@netscape.com>
 32#
 33# Alternatively, the contents of this file may be used under the terms of
 34# either the GNU General Public License Version 2 or later (the "GPL"), or
 35# the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 36# in which case the provisions of the GPL or the LGPL are applicable instead
 37# of those above. If you wish to allow use of your version of this file only
 38# under the terms of either the GPL or the LGPL, and not to allow others to
 39# use your version of this file under the terms of the MPL, indicate your
 40# decision by deleting the provisions above and replace them with the notice
 41# and other provisions required by the GPL or the LGPL. If you do not delete
 42# the provisions above, a recipient may use your version of this file under
 43# the terms of any one of the MPL, the GPL or the LGPL.
 44#
 45# ***** END LICENSE BLOCK *****
 46
 47var dialog;     // Quick access to document/form elements.
 48var gFindInst;   // nsIWebBrowserFind that we're going to use
 49var gFindInstData; // use this to update the find inst data
 50
 51function initDialogObject()
 52{
 53  // Create dialog object and initialize.
 54  dialog = new Object;
 55  dialog.findKey         = document.getElementById("dialog.findKey");
 56  dialog.caseSensitive   = document.getElementById("dialog.caseSensitive");
 57  dialog.wrap            = document.getElementById("dialog.wrap");
 58  dialog.find            = document.getElementById("btnFind");
 59  dialog.up              = document.getElementById("radioUp");
 60  dialog.down            = document.getElementById("radioDown");
 61  dialog.rg              = dialog.up.radioGroup;
 62  dialog.bundle          = null;
 63
 64  // Move dialog to center, if it not been shown before
 65  var windowElement = document.getElementById("findDialog");
 66  if (!windowElement.hasAttribute("screenX") || !windowElement.hasAttribute("screenY"))
 67  {
 68    sizeToContent();
 69    moveToAlertPosition();
 70  }
 71}
 72
 73function fillDialog()
 74{
 75  // get the find service, which stores global find state
 76  var findService = Components.classes["@mozilla.org/find/find_service;1"]
 77                              .getService(Components.interfaces.nsIFindService);
 78  
 79  // Set initial dialog field contents. Use the gFindInst attributes first,
 80  // this is necessary for window.find()
 81  dialog.findKey.value           = gFindInst.searchString ? gFindInst.searchString : findService.searchString;
 82  dialog.caseSensitive.checked   = gFindInst.matchCase ? gFindInst.matchCase : findService.matchCase;
 83  dialog.wrap.checked            = gFindInst.wrapFind ? gFindInst.wrapFind : findService.wrapFind;
 84  var findBackwards              = gFindInst.findBackwards ? gFindInst.findBackwards : findService.findBackwards;
 85  if (findBackwards)
 86    dialog.rg.selectedItem = dialog.up;
 87  else
 88    dialog.rg.selectedItem = dialog.down;
 89}
 90
 91function saveFindData()
 92{
 93  // get the find service, which stores global find state
 94  var findService = Components.classes["@mozilla.org/find/find_service;1"]
 95                         .getService(Components.interfaces.nsIFindService);
 96
 97  // Set data attributes per user input.
 98  findService.searchString  = dialog.findKey.value;
 99  findService.matchCase     = dialog.caseSensitive.checked;
100  findService.wrapFind      = dialog.wrap.checked;
101  findService.findBackwards = dialog.up.selected;
102}
103
104function onLoad()
105{
106  initDialogObject();
107
108  // get the find instance
109  var arg0 = window.arguments[0];                                               
110  // If the dialog was opened from window.find(),
111  // arg0 will be an instance of nsIWebBrowserFind
112  if (arg0 instanceof Components.interfaces.nsIWebBrowserFind) {
113    gFindInst = arg0;
114  } else {  
115    gFindInstData = arg0;                                                       
116    gFindInst = gFindInstData.webBrowserFind;                                   
117  }                                                                             
118
119  fillDialog();
120  doEnabling();
121
122  if (dialog.findKey.value)
123    dialog.findKey.select();
124  dialog.findKey.focus();
125}
126
127function onUnload()
128{
129  window.opener.findDialog = 0;
130}
131
132function onAccept()
133{
134  if (gFindInstData && gFindInst != gFindInstData.webBrowserFind) {
135    gFindInstData.init();             
136    gFindInst = gFindInstData.webBrowserFind;
137  }
138
139  // Transfer dialog contents to the find service.
140  saveFindData();
141
142  // set up the find instance
143  gFindInst.searchString  = dialog.findKey.value;
144  gFindInst.matchCase     = dialog.caseSensitive.checked;
145  gFindInst.wrapFind      = dialog.wrap.checked;
146  gFindInst.findBackwards = dialog.up.selected;
147  
148  // Search.
149  var result = gFindInst.findNext();
150
151  if (!result)
152  {
153    if (!dialog.bundle)
154      dialog.bundle = document.getElementById("findBundle");
155    window.alert(dialog.bundle.getString("notFoundWarning"));
156    dialog.findKey.select();
157    dialog.findKey.focus();
158  } 
159  return false;
160}
161
162function doEnabling()
163{
164  dialog.find.disabled = !dialog.findKey.value;
165}