pdf_find_bar.js 6.9 KB

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