pdf_find_bar.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. }
  70. PDFFindBar.prototype = {
  71. reset: function PDFFindBar_reset() {
  72. this.updateUIState();
  73. },
  74. dispatchEvent: function PDFFindBar_dispatchEvent(type, findPrev) {
  75. this.eventBus.dispatch('find', {
  76. source: this,
  77. type: type,
  78. query: this.findField.value,
  79. caseSensitive: this.caseSensitive.checked,
  80. phraseSearch: true,
  81. highlightAll: this.highlightAll.checked,
  82. findPrevious: findPrev
  83. });
  84. },
  85. updateUIState: function PDFFindBar_updateUIState(state, previous, matchCount) {
  86. var notFound = false;
  87. var findMsg = '';
  88. var status = '';
  89. switch (state) {
  90. case FindStates.FIND_FOUND:
  91. break;
  92. case FindStates.FIND_PENDING:
  93. status = 'pending';
  94. break;
  95. case FindStates.FIND_NOTFOUND:
  96. findMsg = mozL10n.get('find_not_found', null, 'Phrase not found');
  97. notFound = true;
  98. break;
  99. case FindStates.FIND_WRAPPED:
  100. if (previous) {
  101. findMsg = mozL10n.get('find_reached_top', null, 'Reached top of document, continued from bottom');
  102. } else {
  103. findMsg = mozL10n.get('find_reached_bottom', null, 'Reached end of document, continued from top');
  104. }
  105. break;
  106. }
  107. if (notFound) {
  108. this.findField.classList.add('notFound');
  109. } else {
  110. this.findField.classList.remove('notFound');
  111. }
  112. this.findField.setAttribute('data-status', status);
  113. this.findMsg.textContent = findMsg;
  114. this.updateResultsCount(matchCount);
  115. },
  116. updateResultsCount: function (matchCount) {
  117. if (!this.findResultsCount) {
  118. return;
  119. }
  120. if (!matchCount) {
  121. this.findResultsCount.classList.add('hidden');
  122. return;
  123. }
  124. this.findResultsCount.textContent = matchCount.toLocaleString();
  125. this.findResultsCount.classList.remove('hidden');
  126. },
  127. open: function PDFFindBar_open() {
  128. if (!this.opened) {
  129. this.opened = true;
  130. this.toggleButton.classList.add('toggled');
  131. this.bar.classList.remove('hidden');
  132. }
  133. this.findField.select();
  134. this.findField.focus();
  135. },
  136. close: function PDFFindBar_close() {
  137. if (!this.opened) {
  138. return;
  139. }
  140. this.opened = false;
  141. this.toggleButton.classList.remove('toggled');
  142. this.bar.classList.add('hidden');
  143. this.findController.active = false;
  144. },
  145. toggle: function PDFFindBar_toggle() {
  146. if (this.opened) {
  147. this.close();
  148. } else {
  149. this.open();
  150. }
  151. }
  152. };
  153. return PDFFindBar;
  154. }();
  155. exports.PDFFindBar = PDFFindBar;