search.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /**
  2. * @copyright Copyright (c) 2018 John Molakvoæ <skjnldsv@protonmail.com>
  3. *
  4. * @author John Molakvoæ <skjnldsv@protonmail.com>
  5. *
  6. * @license GNU AGPL version 3 or any later version
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License as
  10. * published by the Free Software Foundation, either version 3 of the
  11. * License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. (function() {
  23. 'use strict';
  24. /**
  25. * @class Search
  26. * @memberOf OCA
  27. *
  28. * The Search class manages a search
  29. *
  30. * This is a simple method. Register a new search with your function as references.
  31. * The events will forward the search or reset directly
  32. *
  33. * @param {Function} searchCallback the function to run on a query search
  34. * @param {Function} resetCallback the function to run when the user reset the form
  35. */
  36. var Search = function(searchCallback, resetCallback) {
  37. this.initialize(searchCallback, resetCallback);
  38. };
  39. /**
  40. * @memberof OC
  41. */
  42. Search.prototype = {
  43. /**
  44. * Initialize the search box
  45. *
  46. * @param {Function} searchCallback the function to run on a query search
  47. * @param {Function} resetCallback the function to run when the user reset the form
  48. */
  49. initialize: function(searchCallback, resetCallback) {
  50. var self = this;
  51. if (typeof searchCallback !== 'function') {
  52. throw new Error('searchCallback must be a function');
  53. }
  54. if (typeof resetCallback !== 'function') {
  55. throw new Error('resetCallback must be a function');
  56. }
  57. if (!document.getElementById('searchbox')) {
  58. throw new Error('searchBox not available');
  59. }
  60. this.searchCallback = searchCallback;
  61. this.resetCallback = resetCallback;
  62. console.debug('New search handler registered');
  63. /**
  64. * Search
  65. */
  66. this._search = function(event) {
  67. event.preventDefault();
  68. var query = document.getElementById('searchbox').value;
  69. self.searchCallback(query);
  70. };
  71. /**
  72. * Reset form
  73. */
  74. this._reset = function(event) {
  75. event.preventDefault();
  76. document.getElementById('searchbox').value = '';
  77. self.resetCallback();
  78. };
  79. /**
  80. * Submit event handler
  81. */
  82. this._submitHandler = function(event) {
  83. // Avoid form submit
  84. event.preventDefault();
  85. _.debounce(self.search, 500);
  86. }
  87. /**
  88. * keyUp event handler for the escape key
  89. */
  90. this._keyUpHandler = function(event) {
  91. if (event.defaultPrevented) {
  92. return;
  93. }
  94. var key = event.key || event.keyCode;
  95. if (
  96. document.getElementById('searchbox') === document.activeElement &&
  97. document.getElementById('searchbox').value === ''
  98. ) {
  99. if (key === 'Escape' || key === 'Esc' || key === 27) {
  100. document.activeElement.blur(); // remove focus on searchbox
  101. _.debounce(self.reset, 500);
  102. }
  103. }
  104. }
  105. this._searchDebounced = _.debounce(self._search, 500)
  106. this._resetDebounced = _.debounce(self._reset, 500)
  107. /**
  108. * keyDown event handler for the ctrl+F shortcut
  109. */
  110. this._keyDownHandler = function(event) {
  111. if (event.defaultPrevented) {
  112. return;
  113. }
  114. var key = event.key || event.keyCode;
  115. if (document.getElementById('searchbox') !== document.activeElement) {
  116. if (
  117. (event.ctrlKey || event.metaKey) && // CTRL or mac CMD
  118. !event.shiftKey && // Not SHIFT
  119. (key === 'f' || key === 70) // F
  120. ) {
  121. event.preventDefault();
  122. document.getElementById('searchbox').focus();
  123. document.getElementById('searchbox').select();
  124. }
  125. }
  126. }
  127. // Show search
  128. document.getElementById('searchbox').style.display = 'block';
  129. // Register input event
  130. document
  131. .getElementById('searchbox')
  132. .addEventListener('input', this._searchDebounced, true);
  133. document
  134. .querySelector('form.searchbox')
  135. .addEventListener('submit', this._submitHandler, true);
  136. // Register reset
  137. document
  138. .querySelector('form.searchbox')
  139. .addEventListener('reset', this._resetDebounced, true);
  140. // Register esc key shortcut reset if focused
  141. document.addEventListener('keyup', this._keyUpHandler);
  142. // Register ctrl+F key shortcut to focus
  143. document.addEventListener('keydown', this._keyDownHandler);
  144. },
  145. unregister: function() {
  146. // Hide search
  147. document.getElementById('searchbox').style.display = 'none';
  148. // Reset search
  149. document.getElementById('searchbox').value = '';
  150. this.resetCallback();
  151. // Unregister input event
  152. document
  153. .getElementById('searchbox')
  154. .removeEventListener('input', this._searchDebounced, true);
  155. document
  156. .querySelector('form.searchbox')
  157. .removeEventListener('submit', this._submitHandler, true);
  158. // Unregister reset
  159. document
  160. .querySelector('form.searchbox')
  161. .removeEventListener('reset', this._resetDebounced, true);
  162. // Unregister esc key shortcut reset if focused
  163. document.removeEventListener('keyup', this._keyUpHandler);
  164. // Unregister ctrl+F key shortcut to focus
  165. document.removeEventListener('keydown', this._keyDownHandler);
  166. console.debug('Search handler unregistered');
  167. }
  168. };
  169. OCA.Search = Search;
  170. })();