pdf_find_bar.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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 notFound = false;
  97. let findMsg = "";
  98. let status = "";
  99. switch (state) {
  100. case _pdf_find_controller.FindState.FOUND:
  101. break;
  102. case _pdf_find_controller.FindState.PENDING:
  103. status = "pending";
  104. break;
  105. case _pdf_find_controller.FindState.NOT_FOUND:
  106. findMsg = this.l10n.get("find_not_found", null, "Phrase not found");
  107. notFound = true;
  108. break;
  109. case _pdf_find_controller.FindState.WRAPPED:
  110. if (previous) {
  111. findMsg = this.l10n.get("find_reached_top", null, "Reached top of document, continued from bottom");
  112. } else {
  113. findMsg = this.l10n.get("find_reached_bottom", null, "Reached end of document, continued from top");
  114. }
  115. break;
  116. }
  117. this.findField.classList.toggle("notFound", notFound);
  118. this.findField.setAttribute("data-status", status);
  119. Promise.resolve(findMsg).then(msg => {
  120. this.findMsg.textContent = msg;
  121. this._adjustWidth();
  122. });
  123. this.updateResultsCount(matchesCount);
  124. }
  125. updateResultsCount({
  126. current = 0,
  127. total = 0
  128. } = {}) {
  129. if (!this.findResultsCount) {
  130. return;
  131. }
  132. const limit = MATCHES_COUNT_LIMIT;
  133. let matchesCountMsg = "";
  134. if (total > 0) {
  135. if (total > limit) {
  136. matchesCountMsg = this.l10n.get("find_match_count_limit", {
  137. limit
  138. }, "More than {{limit}} match" + (limit !== 1 ? "es" : ""));
  139. } else {
  140. matchesCountMsg = this.l10n.get("find_match_count", {
  141. current,
  142. total
  143. }, "{{current}} of {{total}} match" + (total !== 1 ? "es" : ""));
  144. }
  145. }
  146. Promise.resolve(matchesCountMsg).then(msg => {
  147. this.findResultsCount.textContent = msg;
  148. this.findResultsCount.classList.toggle("hidden", !total);
  149. this._adjustWidth();
  150. });
  151. }
  152. open() {
  153. if (!this.opened) {
  154. this.opened = true;
  155. this.toggleButton.classList.add("toggled");
  156. this.bar.classList.remove("hidden");
  157. }
  158. this.findField.select();
  159. this.findField.focus();
  160. this._adjustWidth();
  161. }
  162. close() {
  163. if (!this.opened) {
  164. return;
  165. }
  166. this.opened = false;
  167. this.toggleButton.classList.remove("toggled");
  168. this.bar.classList.add("hidden");
  169. this.eventBus.dispatch("findbarclose", {
  170. source: this
  171. });
  172. }
  173. toggle() {
  174. if (this.opened) {
  175. this.close();
  176. } else {
  177. this.open();
  178. }
  179. }
  180. _adjustWidth() {
  181. if (!this.opened) {
  182. return;
  183. }
  184. this.bar.classList.remove("wrapContainers");
  185. const findbarHeight = this.bar.clientHeight;
  186. const inputContainerHeight = this.bar.firstElementChild.clientHeight;
  187. if (findbarHeight > inputContainerHeight) {
  188. this.bar.classList.add("wrapContainers");
  189. }
  190. }
  191. }
  192. exports.PDFFindBar = PDFFindBar;