pdf_find_bar.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /* Copyright 2017 Mozilla Foundation
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. 'use strict';
  16. Object.defineProperty(exports, "__esModule", {
  17. value: true
  18. });
  19. exports.PDFFindBar = undefined;
  20. var _pdf_find_controller = require('pdfjs-web/pdf_find_controller');
  21. var _ui_utils = require('pdfjs-web/ui_utils');
  22. var PDFFindBar = function PDFFindBarClosure() {
  23. function PDFFindBar(options) {
  24. this.opened = false;
  25. this.bar = options.bar || null;
  26. this.toggleButton = options.toggleButton || null;
  27. this.findField = options.findField || null;
  28. this.highlightAll = options.highlightAllCheckbox || null;
  29. this.caseSensitive = options.caseSensitiveCheckbox || null;
  30. this.findMsg = options.findMsg || null;
  31. this.findResultsCount = options.findResultsCount || null;
  32. this.findStatusIcon = options.findStatusIcon || null;
  33. this.findPreviousButton = options.findPreviousButton || null;
  34. this.findNextButton = options.findNextButton || null;
  35. this.findController = options.findController || null;
  36. this.eventBus = options.eventBus;
  37. if (this.findController === null) {
  38. throw new Error('PDFFindBar cannot be used without a ' + 'PDFFindController instance.');
  39. }
  40. var self = this;
  41. this.toggleButton.addEventListener('click', function () {
  42. self.toggle();
  43. });
  44. this.findField.addEventListener('input', function () {
  45. self.dispatchEvent('');
  46. });
  47. this.bar.addEventListener('keydown', function (evt) {
  48. switch (evt.keyCode) {
  49. case 13:
  50. if (evt.target === self.findField) {
  51. self.dispatchEvent('again', evt.shiftKey);
  52. }
  53. break;
  54. case 27:
  55. self.close();
  56. break;
  57. }
  58. });
  59. this.findPreviousButton.addEventListener('click', function () {
  60. self.dispatchEvent('again', true);
  61. });
  62. this.findNextButton.addEventListener('click', function () {
  63. self.dispatchEvent('again', false);
  64. });
  65. this.highlightAll.addEventListener('click', function () {
  66. self.dispatchEvent('highlightallchange');
  67. });
  68. this.caseSensitive.addEventListener('click', function () {
  69. self.dispatchEvent('casesensitivitychange');
  70. });
  71. this.eventBus.on('resize', this._adjustWidth.bind(this));
  72. }
  73. PDFFindBar.prototype = {
  74. reset: function PDFFindBar_reset() {
  75. this.updateUIState();
  76. },
  77. dispatchEvent: function PDFFindBar_dispatchEvent(type, findPrev) {
  78. this.eventBus.dispatch('find', {
  79. source: this,
  80. type: type,
  81. query: this.findField.value,
  82. caseSensitive: this.caseSensitive.checked,
  83. phraseSearch: true,
  84. highlightAll: this.highlightAll.checked,
  85. findPrevious: findPrev
  86. });
  87. },
  88. updateUIState: function PDFFindBar_updateUIState(state, previous, matchCount) {
  89. var notFound = false;
  90. var findMsg = '';
  91. var status = '';
  92. switch (state) {
  93. case _pdf_find_controller.FindStates.FIND_FOUND:
  94. break;
  95. case _pdf_find_controller.FindStates.FIND_PENDING:
  96. status = 'pending';
  97. break;
  98. case _pdf_find_controller.FindStates.FIND_NOTFOUND:
  99. findMsg = _ui_utils.mozL10n.get('find_not_found', null, 'Phrase not found');
  100. notFound = true;
  101. break;
  102. case _pdf_find_controller.FindStates.FIND_WRAPPED:
  103. if (previous) {
  104. findMsg = _ui_utils.mozL10n.get('find_reached_top', null, 'Reached top of document, continued from bottom');
  105. } else {
  106. findMsg = _ui_utils.mozL10n.get('find_reached_bottom', null, 'Reached end of document, continued from top');
  107. }
  108. break;
  109. }
  110. if (notFound) {
  111. this.findField.classList.add('notFound');
  112. } else {
  113. this.findField.classList.remove('notFound');
  114. }
  115. this.findField.setAttribute('data-status', status);
  116. this.findMsg.textContent = findMsg;
  117. this.updateResultsCount(matchCount);
  118. this._adjustWidth();
  119. },
  120. updateResultsCount: function (matchCount) {
  121. if (!this.findResultsCount) {
  122. return;
  123. }
  124. if (!matchCount) {
  125. this.findResultsCount.classList.add('hidden');
  126. return;
  127. }
  128. this.findResultsCount.textContent = matchCount.toLocaleString();
  129. this.findResultsCount.classList.remove('hidden');
  130. },
  131. open: function PDFFindBar_open() {
  132. if (!this.opened) {
  133. this.opened = true;
  134. this.toggleButton.classList.add('toggled');
  135. this.bar.classList.remove('hidden');
  136. }
  137. this.findField.select();
  138. this.findField.focus();
  139. this._adjustWidth();
  140. },
  141. close: function PDFFindBar_close() {
  142. if (!this.opened) {
  143. return;
  144. }
  145. this.opened = false;
  146. this.toggleButton.classList.remove('toggled');
  147. this.bar.classList.add('hidden');
  148. this.findController.active = false;
  149. },
  150. toggle: function PDFFindBar_toggle() {
  151. if (this.opened) {
  152. this.close();
  153. } else {
  154. this.open();
  155. }
  156. },
  157. _adjustWidth: function PDFFindBar_adjustWidth() {
  158. if (!this.opened) {
  159. return;
  160. }
  161. this.bar.classList.remove('wrapContainers');
  162. var findbarHeight = this.bar.clientHeight;
  163. var inputContainerHeight = this.bar.firstElementChild.clientHeight;
  164. if (findbarHeight > inputContainerHeight) {
  165. this.bar.classList.add('wrapContainers');
  166. }
  167. }
  168. };
  169. return PDFFindBar;
  170. }();
  171. exports.PDFFindBar = PDFFindBar;