pdf_find_bar.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * Javascript code in this page
  4. *
  5. * Copyright 2021 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.entireWord = options.entireWordCheckbox;
  38. this.findMsg = options.findMsg;
  39. this.findResultsCount = options.findResultsCount;
  40. this.findPreviousButton = options.findPreviousButton;
  41. this.findNextButton = options.findNextButton;
  42. this.eventBus = eventBus;
  43. this.l10n = l10n;
  44. this.toggleButton.addEventListener("click", () => {
  45. this.toggle();
  46. });
  47. this.findField.addEventListener("input", () => {
  48. this.dispatchEvent("");
  49. });
  50. this.bar.addEventListener("keydown", e => {
  51. switch (e.keyCode) {
  52. case 13:
  53. if (e.target === this.findField) {
  54. this.dispatchEvent("again", e.shiftKey);
  55. }
  56. break;
  57. case 27:
  58. this.close();
  59. break;
  60. }
  61. });
  62. this.findPreviousButton.addEventListener("click", () => {
  63. this.dispatchEvent("again", true);
  64. });
  65. this.findNextButton.addEventListener("click", () => {
  66. this.dispatchEvent("again", false);
  67. });
  68. this.highlightAll.addEventListener("click", () => {
  69. this.dispatchEvent("highlightallchange");
  70. });
  71. this.caseSensitive.addEventListener("click", () => {
  72. this.dispatchEvent("casesensitivitychange");
  73. });
  74. this.entireWord.addEventListener("click", () => {
  75. this.dispatchEvent("entirewordchange");
  76. });
  77. this.eventBus._on("resize", this._adjustWidth.bind(this));
  78. }
  79. reset() {
  80. this.updateUIState();
  81. }
  82. dispatchEvent(type, findPrev) {
  83. this.eventBus.dispatch("find", {
  84. source: this,
  85. type,
  86. query: this.findField.value,
  87. phraseSearch: true,
  88. caseSensitive: this.caseSensitive.checked,
  89. entireWord: this.entireWord.checked,
  90. highlightAll: this.highlightAll.checked,
  91. findPrevious: findPrev
  92. });
  93. }
  94. updateUIState(state, previous, matchesCount) {
  95. let findMsg = Promise.resolve("");
  96. let status = "";
  97. switch (state) {
  98. case _pdf_find_controller.FindState.FOUND:
  99. break;
  100. case _pdf_find_controller.FindState.PENDING:
  101. status = "pending";
  102. break;
  103. case _pdf_find_controller.FindState.NOT_FOUND:
  104. findMsg = this.l10n.get("find_not_found");
  105. status = "notFound";
  106. break;
  107. case _pdf_find_controller.FindState.WRAPPED:
  108. findMsg = this.l10n.get(`find_reached_${previous ? "top" : "bottom"}`);
  109. break;
  110. }
  111. this.findField.setAttribute("data-status", status);
  112. findMsg.then(msg => {
  113. this.findMsg.textContent = msg;
  114. this._adjustWidth();
  115. });
  116. this.updateResultsCount(matchesCount);
  117. }
  118. updateResultsCount({
  119. current = 0,
  120. total = 0
  121. } = {}) {
  122. const limit = MATCHES_COUNT_LIMIT;
  123. let matchCountMsg = Promise.resolve("");
  124. if (total > 0) {
  125. if (total > limit) {
  126. let key = "find_match_count_limit";
  127. matchCountMsg = this.l10n.get(key, {
  128. limit
  129. });
  130. } else {
  131. let key = "find_match_count";
  132. matchCountMsg = this.l10n.get(key, {
  133. current,
  134. total
  135. });
  136. }
  137. }
  138. matchCountMsg.then(msg => {
  139. this.findResultsCount.textContent = msg;
  140. this.findResultsCount.classList.toggle("hidden", !total);
  141. this._adjustWidth();
  142. });
  143. }
  144. open() {
  145. if (!this.opened) {
  146. this.opened = true;
  147. this.toggleButton.classList.add("toggled");
  148. this.toggleButton.setAttribute("aria-expanded", "true");
  149. this.bar.classList.remove("hidden");
  150. }
  151. this.findField.select();
  152. this.findField.focus();
  153. this._adjustWidth();
  154. }
  155. close() {
  156. if (!this.opened) {
  157. return;
  158. }
  159. this.opened = false;
  160. this.toggleButton.classList.remove("toggled");
  161. this.toggleButton.setAttribute("aria-expanded", "false");
  162. this.bar.classList.add("hidden");
  163. this.eventBus.dispatch("findbarclose", {
  164. source: this
  165. });
  166. }
  167. toggle() {
  168. if (this.opened) {
  169. this.close();
  170. } else {
  171. this.open();
  172. }
  173. }
  174. _adjustWidth() {
  175. if (!this.opened) {
  176. return;
  177. }
  178. this.bar.classList.remove("wrapContainers");
  179. const findbarHeight = this.bar.clientHeight;
  180. const inputContainerHeight = this.bar.firstElementChild.clientHeight;
  181. if (findbarHeight > inputContainerHeight) {
  182. this.bar.classList.add("wrapContainers");
  183. }
  184. }
  185. }
  186. exports.PDFFindBar = PDFFindBar;