pdf_find_bar.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * Javascript code in this page
  4. *
  5. * Copyright 2018 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 _ui_utils = require("./ui_utils");
  28. var _pdf_find_controller = require("./pdf_find_controller");
  29. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  30. function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
  31. function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
  32. var MATCHES_COUNT_LIMIT = 1000;
  33. var PDFFindBar =
  34. /*#__PURE__*/
  35. function () {
  36. function PDFFindBar(options) {
  37. var _this = this;
  38. var eventBus = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : (0, _ui_utils.getGlobalEventBus)();
  39. var l10n = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : _ui_utils.NullL10n;
  40. _classCallCheck(this, PDFFindBar);
  41. this.opened = false;
  42. this.bar = options.bar || null;
  43. this.toggleButton = options.toggleButton || null;
  44. this.findField = options.findField || null;
  45. this.highlightAll = options.highlightAllCheckbox || null;
  46. this.caseSensitive = options.caseSensitiveCheckbox || null;
  47. this.entireWord = options.entireWordCheckbox || null;
  48. this.findMsg = options.findMsg || null;
  49. this.findResultsCount = options.findResultsCount || null;
  50. this.findPreviousButton = options.findPreviousButton || null;
  51. this.findNextButton = options.findNextButton || null;
  52. this.eventBus = eventBus;
  53. this.l10n = l10n;
  54. this.toggleButton.addEventListener('click', function () {
  55. _this.toggle();
  56. });
  57. this.findField.addEventListener('input', function () {
  58. _this.dispatchEvent('');
  59. });
  60. this.bar.addEventListener('keydown', function (e) {
  61. switch (e.keyCode) {
  62. case 13:
  63. if (e.target === _this.findField) {
  64. _this.dispatchEvent('again', e.shiftKey);
  65. }
  66. break;
  67. case 27:
  68. _this.close();
  69. break;
  70. }
  71. });
  72. this.findPreviousButton.addEventListener('click', function () {
  73. _this.dispatchEvent('again', true);
  74. });
  75. this.findNextButton.addEventListener('click', function () {
  76. _this.dispatchEvent('again', false);
  77. });
  78. this.highlightAll.addEventListener('click', function () {
  79. _this.dispatchEvent('highlightallchange');
  80. });
  81. this.caseSensitive.addEventListener('click', function () {
  82. _this.dispatchEvent('casesensitivitychange');
  83. });
  84. this.entireWord.addEventListener('click', function () {
  85. _this.dispatchEvent('entirewordchange');
  86. });
  87. this.eventBus.on('resize', this._adjustWidth.bind(this));
  88. }
  89. _createClass(PDFFindBar, [{
  90. key: "reset",
  91. value: function reset() {
  92. this.updateUIState();
  93. }
  94. }, {
  95. key: "dispatchEvent",
  96. value: function dispatchEvent(type, findPrev) {
  97. this.eventBus.dispatch('find', {
  98. source: this,
  99. type: type,
  100. query: this.findField.value,
  101. phraseSearch: true,
  102. caseSensitive: this.caseSensitive.checked,
  103. entireWord: this.entireWord.checked,
  104. highlightAll: this.highlightAll.checked,
  105. findPrevious: findPrev
  106. });
  107. }
  108. }, {
  109. key: "updateUIState",
  110. value: function updateUIState(state, previous, matchesCount) {
  111. var _this2 = this;
  112. var notFound = false;
  113. var findMsg = '';
  114. var status = '';
  115. switch (state) {
  116. case _pdf_find_controller.FindState.FOUND:
  117. break;
  118. case _pdf_find_controller.FindState.PENDING:
  119. status = 'pending';
  120. break;
  121. case _pdf_find_controller.FindState.NOT_FOUND:
  122. findMsg = this.l10n.get('find_not_found', null, 'Phrase not found');
  123. notFound = true;
  124. break;
  125. case _pdf_find_controller.FindState.WRAPPED:
  126. if (previous) {
  127. findMsg = this.l10n.get('find_reached_top', null, 'Reached top of document, continued from bottom');
  128. } else {
  129. findMsg = this.l10n.get('find_reached_bottom', null, 'Reached end of document, continued from top');
  130. }
  131. break;
  132. }
  133. this.findField.classList.toggle('notFound', notFound);
  134. this.findField.setAttribute('data-status', status);
  135. Promise.resolve(findMsg).then(function (msg) {
  136. _this2.findMsg.textContent = msg;
  137. _this2._adjustWidth();
  138. });
  139. this.updateResultsCount(matchesCount);
  140. }
  141. }, {
  142. key: "updateResultsCount",
  143. value: function updateResultsCount() {
  144. var _this3 = this;
  145. var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
  146. _ref$current = _ref.current,
  147. current = _ref$current === void 0 ? 0 : _ref$current,
  148. _ref$total = _ref.total,
  149. total = _ref$total === void 0 ? 0 : _ref$total;
  150. if (!this.findResultsCount) {
  151. return;
  152. }
  153. var matchesCountMsg = '',
  154. limit = MATCHES_COUNT_LIMIT;
  155. if (total > 0) {
  156. if (total > limit) {
  157. matchesCountMsg = this.l10n.get('find_match_count_limit', {
  158. limit: limit
  159. }, 'More than {{limit}} match' + (limit !== 1 ? 'es' : ''));
  160. } else {
  161. matchesCountMsg = this.l10n.get('find_match_count', {
  162. current: current,
  163. total: total
  164. }, '{{current}} of {{total}} match' + (total !== 1 ? 'es' : ''));
  165. }
  166. }
  167. Promise.resolve(matchesCountMsg).then(function (msg) {
  168. _this3.findResultsCount.textContent = msg;
  169. _this3.findResultsCount.classList.toggle('hidden', !total);
  170. _this3._adjustWidth();
  171. });
  172. }
  173. }, {
  174. key: "open",
  175. value: function open() {
  176. if (!this.opened) {
  177. this.opened = true;
  178. this.toggleButton.classList.add('toggled');
  179. this.bar.classList.remove('hidden');
  180. }
  181. this.findField.select();
  182. this.findField.focus();
  183. this._adjustWidth();
  184. }
  185. }, {
  186. key: "close",
  187. value: function close() {
  188. if (!this.opened) {
  189. return;
  190. }
  191. this.opened = false;
  192. this.toggleButton.classList.remove('toggled');
  193. this.bar.classList.add('hidden');
  194. this.eventBus.dispatch('findbarclose', {
  195. source: this
  196. });
  197. }
  198. }, {
  199. key: "toggle",
  200. value: function toggle() {
  201. if (this.opened) {
  202. this.close();
  203. } else {
  204. this.open();
  205. }
  206. }
  207. }, {
  208. key: "_adjustWidth",
  209. value: function _adjustWidth() {
  210. if (!this.opened) {
  211. return;
  212. }
  213. this.bar.classList.remove('wrapContainers');
  214. var findbarHeight = this.bar.clientHeight;
  215. var inputContainerHeight = this.bar.firstElementChild.clientHeight;
  216. if (findbarHeight > inputContainerHeight) {
  217. this.bar.classList.add('wrapContainers');
  218. }
  219. }
  220. }]);
  221. return PDFFindBar;
  222. }();
  223. exports.PDFFindBar = PDFFindBar;