pdf_find_bar.js 7.1 KB

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