pdf_find_bar.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * JavaScript code in this page
  4. *
  5. * Copyright 2022 Mozilla Foundation
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. *
  19. * @licend The above is the entire license notice for the
  20. * JavaScript code in this page
  21. */
  22. "use strict";
  23. Object.defineProperty(exports, "__esModule", {
  24. value: true
  25. });
  26. exports.PDFFindBar = void 0;
  27. var _pdf_find_controller = require("./pdf_find_controller.js");
  28. const MATCHES_COUNT_LIMIT = 1000;
  29. class PDFFindBar {
  30. constructor(options, eventBus, l10n) {
  31. this.opened = false;
  32. this.bar = options.bar;
  33. this.toggleButton = options.toggleButton;
  34. this.findField = options.findField;
  35. this.highlightAll = options.highlightAllCheckbox;
  36. this.caseSensitive = options.caseSensitiveCheckbox;
  37. this.matchDiacritics = options.matchDiacriticsCheckbox;
  38. this.entireWord = options.entireWordCheckbox;
  39. this.findMsg = options.findMsg;
  40. this.findResultsCount = options.findResultsCount;
  41. this.findPreviousButton = options.findPreviousButton;
  42. this.findNextButton = options.findNextButton;
  43. this.eventBus = eventBus;
  44. this.l10n = l10n;
  45. this.toggleButton.addEventListener("click", () => {
  46. this.toggle();
  47. });
  48. this.findField.addEventListener("input", () => {
  49. this.dispatchEvent("");
  50. });
  51. this.bar.addEventListener("keydown", e => {
  52. switch (e.keyCode) {
  53. case 13:
  54. if (e.target === this.findField) {
  55. this.dispatchEvent("again", e.shiftKey);
  56. }
  57. break;
  58. case 27:
  59. this.close();
  60. break;
  61. }
  62. });
  63. this.findPreviousButton.addEventListener("click", () => {
  64. this.dispatchEvent("again", true);
  65. });
  66. this.findNextButton.addEventListener("click", () => {
  67. this.dispatchEvent("again", false);
  68. });
  69. this.highlightAll.addEventListener("click", () => {
  70. this.dispatchEvent("highlightallchange");
  71. });
  72. this.caseSensitive.addEventListener("click", () => {
  73. this.dispatchEvent("casesensitivitychange");
  74. });
  75. this.entireWord.addEventListener("click", () => {
  76. this.dispatchEvent("entirewordchange");
  77. });
  78. this.matchDiacritics.addEventListener("click", () => {
  79. this.dispatchEvent("diacriticmatchingchange");
  80. });
  81. this.eventBus._on("resize", this.#adjustWidth.bind(this));
  82. }
  83. reset() {
  84. this.updateUIState();
  85. }
  86. dispatchEvent(type, findPrev = false) {
  87. this.eventBus.dispatch("find", {
  88. source: this,
  89. type,
  90. query: this.findField.value,
  91. phraseSearch: true,
  92. caseSensitive: this.caseSensitive.checked,
  93. entireWord: this.entireWord.checked,
  94. highlightAll: this.highlightAll.checked,
  95. findPrevious: findPrev,
  96. matchDiacritics: this.matchDiacritics.checked
  97. });
  98. }
  99. updateUIState(state, previous, matchesCount) {
  100. let findMsg = Promise.resolve("");
  101. let status = "";
  102. switch (state) {
  103. case _pdf_find_controller.FindState.FOUND:
  104. break;
  105. case _pdf_find_controller.FindState.PENDING:
  106. status = "pending";
  107. break;
  108. case _pdf_find_controller.FindState.NOT_FOUND:
  109. findMsg = this.l10n.get("find_not_found");
  110. status = "notFound";
  111. break;
  112. case _pdf_find_controller.FindState.WRAPPED:
  113. findMsg = this.l10n.get(`find_reached_${previous ? "top" : "bottom"}`);
  114. break;
  115. }
  116. this.findField.setAttribute("data-status", status);
  117. this.findField.setAttribute("aria-invalid", state === _pdf_find_controller.FindState.NOT_FOUND);
  118. findMsg.then(msg => {
  119. this.findMsg.textContent = msg;
  120. this.#adjustWidth();
  121. });
  122. this.updateResultsCount(matchesCount);
  123. }
  124. updateResultsCount({
  125. current = 0,
  126. total = 0
  127. } = {}) {
  128. const limit = MATCHES_COUNT_LIMIT;
  129. let matchCountMsg = Promise.resolve("");
  130. if (total > 0) {
  131. if (total > limit) {
  132. let key = "find_match_count_limit";
  133. matchCountMsg = this.l10n.get(key, {
  134. limit
  135. });
  136. } else {
  137. let key = "find_match_count";
  138. matchCountMsg = this.l10n.get(key, {
  139. current,
  140. total
  141. });
  142. }
  143. }
  144. matchCountMsg.then(msg => {
  145. this.findResultsCount.textContent = msg;
  146. this.#adjustWidth();
  147. });
  148. }
  149. open() {
  150. if (!this.opened) {
  151. this.opened = true;
  152. this.toggleButton.classList.add("toggled");
  153. this.toggleButton.setAttribute("aria-expanded", "true");
  154. this.bar.classList.remove("hidden");
  155. }
  156. this.findField.select();
  157. this.findField.focus();
  158. this.#adjustWidth();
  159. }
  160. close() {
  161. if (!this.opened) {
  162. return;
  163. }
  164. this.opened = false;
  165. this.toggleButton.classList.remove("toggled");
  166. this.toggleButton.setAttribute("aria-expanded", "false");
  167. this.bar.classList.add("hidden");
  168. this.eventBus.dispatch("findbarclose", {
  169. source: this
  170. });
  171. }
  172. toggle() {
  173. if (this.opened) {
  174. this.close();
  175. } else {
  176. this.open();
  177. }
  178. }
  179. #adjustWidth() {
  180. if (!this.opened) {
  181. return;
  182. }
  183. this.bar.classList.remove("wrapContainers");
  184. const findbarHeight = this.bar.clientHeight;
  185. const inputContainerHeight = this.bar.firstElementChild.clientHeight;
  186. if (findbarHeight > inputContainerHeight) {
  187. this.bar.classList.add("wrapContainers");
  188. }
  189. }
  190. }
  191. exports.PDFFindBar = PDFFindBar;