pdf_find_bar.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * Javascript code in this page
  4. *
  5. * Copyright 2020 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. var _ui_utils = require("./ui_utils.js");
  29. const MATCHES_COUNT_LIMIT = 1000;
  30. class PDFFindBar {
  31. constructor(options, eventBus, l10n = _ui_utils.NullL10n) {
  32. this.opened = false;
  33. this.bar = options.bar || null;
  34. this.toggleButton = options.toggleButton || null;
  35. this.findField = options.findField || null;
  36. this.highlightAll = options.highlightAllCheckbox || null;
  37. this.caseSensitive = options.caseSensitiveCheckbox || null;
  38. this.entireWord = options.entireWordCheckbox || null;
  39. this.findMsg = options.findMsg || null;
  40. this.findResultsCount = options.findResultsCount || null;
  41. this.findPreviousButton = options.findPreviousButton || null;
  42. this.findNextButton = options.findNextButton || null;
  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.eventBus._on("resize", this._adjustWidth.bind(this));
  79. }
  80. reset() {
  81. this.updateUIState();
  82. }
  83. dispatchEvent(type, findPrev) {
  84. this.eventBus.dispatch("find", {
  85. source: this,
  86. type,
  87. query: this.findField.value,
  88. phraseSearch: true,
  89. caseSensitive: this.caseSensitive.checked,
  90. entireWord: this.entireWord.checked,
  91. highlightAll: this.highlightAll.checked,
  92. findPrevious: findPrev
  93. });
  94. }
  95. updateUIState(state, previous, matchesCount) {
  96. let findMsg = "";
  97. let status = "";
  98. switch (state) {
  99. case _pdf_find_controller.FindState.FOUND:
  100. break;
  101. case _pdf_find_controller.FindState.PENDING:
  102. status = "pending";
  103. break;
  104. case _pdf_find_controller.FindState.NOT_FOUND:
  105. findMsg = this.l10n.get("find_not_found", null, "Phrase not found");
  106. status = "notFound";
  107. break;
  108. case _pdf_find_controller.FindState.WRAPPED:
  109. if (previous) {
  110. findMsg = this.l10n.get("find_reached_top", null, "Reached top of document, continued from bottom");
  111. } else {
  112. findMsg = this.l10n.get("find_reached_bottom", null, "Reached end of document, continued from top");
  113. }
  114. break;
  115. }
  116. this.findField.setAttribute("data-status", status);
  117. Promise.resolve(findMsg).then(msg => {
  118. this.findMsg.textContent = msg;
  119. this._adjustWidth();
  120. });
  121. this.updateResultsCount(matchesCount);
  122. }
  123. updateResultsCount({
  124. current = 0,
  125. total = 0
  126. } = {}) {
  127. if (!this.findResultsCount) {
  128. return;
  129. }
  130. const limit = MATCHES_COUNT_LIMIT;
  131. let matchesCountMsg = "";
  132. if (total > 0) {
  133. if (total > limit) {
  134. matchesCountMsg = this.l10n.get("find_match_count_limit", {
  135. limit
  136. }, "More than {{limit}} match" + (limit !== 1 ? "es" : ""));
  137. } else {
  138. matchesCountMsg = this.l10n.get("find_match_count", {
  139. current,
  140. total
  141. }, "{{current}} of {{total}} match" + (total !== 1 ? "es" : ""));
  142. }
  143. }
  144. Promise.resolve(matchesCountMsg).then(msg => {
  145. this.findResultsCount.textContent = msg;
  146. this.findResultsCount.classList.toggle("hidden", !total);
  147. this._adjustWidth();
  148. });
  149. }
  150. open() {
  151. if (!this.opened) {
  152. this.opened = true;
  153. this.toggleButton.classList.add("toggled");
  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.bar.classList.add("hidden");
  167. this.eventBus.dispatch("findbarclose", {
  168. source: this
  169. });
  170. }
  171. toggle() {
  172. if (this.opened) {
  173. this.close();
  174. } else {
  175. this.open();
  176. }
  177. }
  178. _adjustWidth() {
  179. if (!this.opened) {
  180. return;
  181. }
  182. this.bar.classList.remove("wrapContainers");
  183. const findbarHeight = this.bar.clientHeight;
  184. const inputContainerHeight = this.bar.firstElementChild.clientHeight;
  185. if (findbarHeight > inputContainerHeight) {
  186. this.bar.classList.add("wrapContainers");
  187. }
  188. }
  189. }
  190. exports.PDFFindBar = PDFFindBar;