pdf_find_bar.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* Copyright 2012 Mozilla Foundation
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. 'use strict';
  17. /* globals PDFFindController, FindStates, mozL10n */
  18. /**
  19. * Creates a "search bar" given set of DOM elements
  20. * that act as controls for searching, or for setting
  21. * search preferences in the UI. This object also sets
  22. * up the appropriate events for the controls. Actual
  23. * searching is done by PDFFindController
  24. */
  25. var PDFFindBar = {
  26. opened: false,
  27. bar: null,
  28. toggleButton: null,
  29. findField: null,
  30. highlightAll: null,
  31. caseSensitive: null,
  32. findMsg: null,
  33. findStatusIcon: null,
  34. findPreviousButton: null,
  35. findNextButton: null,
  36. initialize: function(options) {
  37. if(typeof PDFFindController === 'undefined' || PDFFindController === null) {
  38. throw 'PDFFindBar cannot be initialized ' +
  39. 'without a PDFFindController instance.';
  40. }
  41. this.bar = options.bar;
  42. this.toggleButton = options.toggleButton;
  43. this.findField = options.findField;
  44. this.highlightAll = options.highlightAllCheckbox;
  45. this.caseSensitive = options.caseSensitiveCheckbox;
  46. this.findMsg = options.findMsg;
  47. this.findStatusIcon = options.findStatusIcon;
  48. this.findPreviousButton = options.findPreviousButton;
  49. this.findNextButton = options.findNextButton;
  50. var self = this;
  51. this.toggleButton.addEventListener('click', function() {
  52. self.toggle();
  53. });
  54. this.findField.addEventListener('input', function() {
  55. self.dispatchEvent('');
  56. });
  57. this.bar.addEventListener('keydown', function(evt) {
  58. switch (evt.keyCode) {
  59. case 13: // Enter
  60. if (evt.target === self.findField) {
  61. self.dispatchEvent('again', evt.shiftKey);
  62. }
  63. break;
  64. case 27: // Escape
  65. self.close();
  66. break;
  67. }
  68. });
  69. this.findPreviousButton.addEventListener('click',
  70. function() { self.dispatchEvent('again', true); }
  71. );
  72. this.findNextButton.addEventListener('click', function() {
  73. self.dispatchEvent('again', false);
  74. });
  75. this.highlightAll.addEventListener('click', function() {
  76. self.dispatchEvent('highlightallchange');
  77. });
  78. this.caseSensitive.addEventListener('click', function() {
  79. self.dispatchEvent('casesensitivitychange');
  80. });
  81. },
  82. dispatchEvent: function(aType, aFindPrevious) {
  83. var event = document.createEvent('CustomEvent');
  84. event.initCustomEvent('find' + aType, true, true, {
  85. query: this.findField.value,
  86. caseSensitive: this.caseSensitive.checked,
  87. highlightAll: this.highlightAll.checked,
  88. findPrevious: aFindPrevious
  89. });
  90. return window.dispatchEvent(event);
  91. },
  92. updateUIState: function(state, previous) {
  93. var notFound = false;
  94. var findMsg = '';
  95. var status = '';
  96. switch (state) {
  97. case FindStates.FIND_FOUND:
  98. break;
  99. case FindStates.FIND_PENDING:
  100. status = 'pending';
  101. break;
  102. case FindStates.FIND_NOTFOUND:
  103. findMsg = mozL10n.get('find_not_found', null, 'Phrase not found');
  104. notFound = true;
  105. break;
  106. case FindStates.FIND_WRAPPED:
  107. if (previous) {
  108. findMsg = mozL10n.get('find_reached_top', null,
  109. 'Reached top of document, continued from bottom');
  110. } else {
  111. findMsg = mozL10n.get('find_reached_bottom', null,
  112. 'Reached end of document, continued from top');
  113. }
  114. break;
  115. }
  116. if (notFound) {
  117. this.findField.classList.add('notFound');
  118. } else {
  119. this.findField.classList.remove('notFound');
  120. }
  121. this.findField.setAttribute('data-status', status);
  122. this.findMsg.textContent = findMsg;
  123. },
  124. open: function() {
  125. if (!this.opened) {
  126. this.opened = true;
  127. this.toggleButton.classList.add('toggled');
  128. this.bar.classList.remove('hidden');
  129. }
  130. this.findField.select();
  131. this.findField.focus();
  132. },
  133. close: function() {
  134. if (!this.opened) return;
  135. this.opened = false;
  136. this.toggleButton.classList.remove('toggled');
  137. this.bar.classList.add('hidden');
  138. PDFFindController.active = false;
  139. },
  140. toggle: function() {
  141. if (this.opened) {
  142. this.close();
  143. } else {
  144. this.open();
  145. }
  146. }
  147. };