/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. var dialog; // Quick access to document/form elements.
  47. var gFindInst; // nsIWebBrowserFind that we're going to use
  48. var gFindInstData; // use this to update the find inst data
  49. function initDialogObject()
  50. {
  51. // Create dialog object and initialize.
  52. dialog = new Object;
  53. dialog.findKey = document.getElementById("dialog.findKey");
  54. dialog.caseSensitive = document.getElementById("dialog.caseSensitive");
  55. dialog.wrap = document.getElementById("dialog.wrap");
  56. dialog.find = document.getElementById("btnFind");
  57. dialog.up = document.getElementById("radioUp");
  58. dialog.down = document.getElementById("radioDown");
  59. dialog.rg = dialog.up.radioGroup;
  60. dialog.bundle = null;
  61. // Move dialog to center, if it not been shown before
  62. var windowElement = document.getElementById("findDialog");
  63. if (!windowElement.hasAttribute("screenX") || !windowElement.hasAttribute("screenY"))
  64. {
  65. sizeToContent();
  66. moveToAlertPosition();
  67. }
  68. }
  69. function fillDialog()
  70. {
  71. // get the find service, which stores global find state
  72. var findService = Components.classes["@mozilla.org/find/find_service;1"]
  73. .getService(Components.interfaces.nsIFindService);
  74. // Set initial dialog field contents. Use the gFindInst attributes first,
  75. // this is necessary for window.find()
  76. dialog.findKey.value = gFindInst.searchString ? gFindInst.searchString : findService.searchString;
  77. dialog.caseSensitive.checked = gFindInst.matchCase ? gFindInst.matchCase : findService.matchCase;
  78. dialog.wrap.checked = gFindInst.wrapFind ? gFindInst.wrapFind : findService.wrapFind;
  79. var findBackwards = gFindInst.findBackwards ? gFindInst.findBackwards : findService.findBackwards;
  80. if (findBackwards)
  81. dialog.rg.selectedItem = dialog.up;
  82. else
  83. dialog.rg.selectedItem = dialog.down;
  84. }
  85. function saveFindData()
  86. {
  87. // get the find service, which stores global find state
  88. var findService = Components.classes["@mozilla.org/find/find_service;1"]
  89. .getService(Components.interfaces.nsIFindService);
  90. // Set data attributes per user input.
  91. findService.searchString = dialog.findKey.value;
  92. findService.matchCase = dialog.caseSensitive.checked;
  93. findService.wrapFind = dialog.wrap.checked;
  94. findService.findBackwards = dialog.up.selected;
  95. }
  96. function onLoad()
  97. {
  98. initDialogObject();
  99. // get the find instance
  100. var arg0 = window.arguments[0];
  101. // If the dialog was opened from window.find(),
  102. // arg0 will be an instance of nsIWebBrowserFind
  103. if (arg0 instanceof Components.interfaces.nsIWebBrowserFind) {
  104. gFindInst = arg0;
  105. } else {
  106. gFindInstData = arg0;
  107. gFindInst = gFindInstData.webBrowserFind;
  108. }
  109. fillDialog();
  110. doEnabling();
  111. if (dialog.findKey.value)
  112. dialog.findKey.select();
  113. dialog.findKey.focus();
  114. }
  115. function onUnload()
  116. {
  117. window.opener.findDialog = 0;
  118. }
  119. function onAccept()
  120. {
  121. if (gFindInstData && gFindInst != gFindInstData.webBrowserFind) {
  122. gFindInstData.init();
  123. gFindInst = gFindInstData.webBrowserFind;
  124. }
  125. // Transfer dialog contents to the find service.
  126. saveFindData();
  127. // set up the find instance
  128. gFindInst.searchString = dialog.findKey.value;
  129. gFindInst.matchCase = dialog.caseSensitive.checked;
  130. gFindInst.wrapFind = dialog.wrap.checked;
  131. gFindInst.findBackwards = dialog.up.selected;
  132. // Search.
  133. var result = gFindInst.findNext();
  134. if (!result)
  135. {
  136. if (!dialog.bundle)
  137. dialog.bundle = document.getElementById("findBundle");
  138. window.alert(dialog.bundle.getString("notFoundWarning"));
  139. dialog.findKey.select();
  140. dialog.findKey.focus();
  141. }
  142. return false;
  143. }
  144. function doEnabling()
  145. {
  146. dialog.find.disabled = !dialog.findKey.value;
  147. }