pdf_find_bar.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /* Copyright 2012 Mozilla Foundation
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. import { FindState } from "./pdf_find_controller.js";
  16. const MATCHES_COUNT_LIMIT = 1000;
  17. /**
  18. * Creates a "search bar" given a set of DOM elements that act as controls
  19. * for searching or for setting search preferences in the UI. This object
  20. * also sets up the appropriate events for the controls. Actual searching
  21. * is done by PDFFindController.
  22. */
  23. class PDFFindBar {
  24. constructor(options, eventBus, l10n) {
  25. this.opened = false;
  26. this.bar = options.bar;
  27. this.toggleButton = options.toggleButton;
  28. this.findField = options.findField;
  29. this.highlightAll = options.highlightAllCheckbox;
  30. this.caseSensitive = options.caseSensitiveCheckbox;
  31. this.matchDiacritics = options.matchDiacriticsCheckbox;
  32. this.entireWord = options.entireWordCheckbox;
  33. this.findMsg = options.findMsg;
  34. this.findResultsCount = options.findResultsCount;
  35. this.findPreviousButton = options.findPreviousButton;
  36. this.findNextButton = options.findNextButton;
  37. this.eventBus = eventBus;
  38. this.l10n = l10n;
  39. // Add event listeners to the DOM elements.
  40. this.toggleButton.addEventListener("click", () => {
  41. this.toggle();
  42. });
  43. this.findField.addEventListener("input", () => {
  44. this.dispatchEvent("");
  45. });
  46. this.bar.addEventListener("keydown", e => {
  47. switch (e.keyCode) {
  48. case 13: // Enter
  49. if (e.target === this.findField) {
  50. this.dispatchEvent("again", e.shiftKey);
  51. }
  52. break;
  53. case 27: // Escape
  54. this.close();
  55. break;
  56. }
  57. });
  58. this.findPreviousButton.addEventListener("click", () => {
  59. this.dispatchEvent("again", true);
  60. });
  61. this.findNextButton.addEventListener("click", () => {
  62. this.dispatchEvent("again", false);
  63. });
  64. this.highlightAll.addEventListener("click", () => {
  65. this.dispatchEvent("highlightallchange");
  66. });
  67. this.caseSensitive.addEventListener("click", () => {
  68. this.dispatchEvent("casesensitivitychange");
  69. });
  70. this.entireWord.addEventListener("click", () => {
  71. this.dispatchEvent("entirewordchange");
  72. });
  73. this.matchDiacritics.addEventListener("click", () => {
  74. this.dispatchEvent("diacriticmatchingchange");
  75. });
  76. this.eventBus._on("resize", this.#adjustWidth.bind(this));
  77. }
  78. reset() {
  79. this.updateUIState();
  80. }
  81. dispatchEvent(type, findPrev = false) {
  82. this.eventBus.dispatch("find", {
  83. source: this,
  84. type,
  85. query: this.findField.value,
  86. phraseSearch: true,
  87. caseSensitive: this.caseSensitive.checked,
  88. entireWord: this.entireWord.checked,
  89. highlightAll: this.highlightAll.checked,
  90. findPrevious: findPrev,
  91. matchDiacritics: this.matchDiacritics.checked,
  92. });
  93. }
  94. updateUIState(state, previous, matchesCount) {
  95. let findMsg = Promise.resolve("");
  96. let status = "";
  97. switch (state) {
  98. case FindState.FOUND:
  99. break;
  100. case FindState.PENDING:
  101. status = "pending";
  102. break;
  103. case FindState.NOT_FOUND:
  104. findMsg = this.l10n.get("find_not_found");
  105. status = "notFound";
  106. break;
  107. case FindState.WRAPPED:
  108. findMsg = this.l10n.get(`find_reached_${previous ? "top" : "bottom"}`);
  109. break;
  110. }
  111. this.findField.setAttribute("data-status", status);
  112. this.findField.setAttribute("aria-invalid", state === FindState.NOT_FOUND);
  113. findMsg.then(msg => {
  114. this.findMsg.textContent = msg;
  115. this.#adjustWidth();
  116. });
  117. this.updateResultsCount(matchesCount);
  118. }
  119. updateResultsCount({ current = 0, total = 0 } = {}) {
  120. const limit = MATCHES_COUNT_LIMIT;
  121. let matchCountMsg = Promise.resolve("");
  122. if (total > 0) {
  123. if (total > limit) {
  124. let key = "find_match_count_limit";
  125. if (typeof PDFJSDev !== "undefined" && PDFJSDev.test("MOZCENTRAL")) {
  126. // TODO: Remove this hard-coded `[other]` form once plural support has
  127. // been implemented in the mozilla-central specific `l10n.js` file.
  128. key += "[other]";
  129. }
  130. matchCountMsg = this.l10n.get(key, { limit });
  131. } else {
  132. let key = "find_match_count";
  133. if (typeof PDFJSDev !== "undefined" && PDFJSDev.test("MOZCENTRAL")) {
  134. // TODO: Remove this hard-coded `[other]` form once plural support has
  135. // been implemented in the mozilla-central specific `l10n.js` file.
  136. key += "[other]";
  137. }
  138. matchCountMsg = this.l10n.get(key, { current, total });
  139. }
  140. }
  141. matchCountMsg.then(msg => {
  142. this.findResultsCount.textContent = msg;
  143. // Since `updateResultsCount` may be called from `PDFFindController`,
  144. // ensure that the width of the findbar is always updated correctly.
  145. this.#adjustWidth();
  146. });
  147. }
  148. open() {
  149. if (!this.opened) {
  150. this.opened = true;
  151. this.toggleButton.classList.add("toggled");
  152. this.toggleButton.setAttribute("aria-expanded", "true");
  153. this.bar.classList.remove("hidden");
  154. }
  155. this.findField.select();
  156. this.findField.focus();
  157. this.#adjustWidth();
  158. }
  159. close() {
  160. if (!this.opened) {
  161. return;
  162. }
  163. this.opened = false;
  164. this.toggleButton.classList.remove("toggled");
  165. this.toggleButton.setAttribute("aria-expanded", "false");
  166. this.bar.classList.add("hidden");
  167. this.eventBus.dispatch("findbarclose", { source: this });
  168. }
  169. toggle() {
  170. if (this.opened) {
  171. this.close();
  172. } else {
  173. this.open();
  174. }
  175. }
  176. #adjustWidth() {
  177. if (!this.opened) {
  178. return;
  179. }
  180. // The find bar has an absolute position and thus the browser extends
  181. // its width to the maximum possible width once the find bar does not fit
  182. // entirely within the window anymore (and its elements are automatically
  183. // wrapped). Here we detect and fix that.
  184. this.bar.classList.remove("wrapContainers");
  185. const findbarHeight = this.bar.clientHeight;
  186. const inputContainerHeight = this.bar.firstElementChild.clientHeight;
  187. if (findbarHeight > inputContainerHeight) {
  188. // The findbar is taller than the input container, which means that
  189. // the browser wrapped some of the elements. For a consistent look,
  190. // wrap all of them to adjust the width of the find bar.
  191. this.bar.classList.add("wrapContainers");
  192. }
  193. }
  194. }
  195. export { PDFFindBar };