pdf_find_bar.js 5.7 KB

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