2
0

pdf_find_bar.js 6.7 KB

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