You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

search.js 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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 OCA.Search
  26. *
  27. * The Search class manages a search
  28. *
  29. * This is a simple method. Register a new search with your function as references.
  30. * The events will forward the search or reset directly
  31. *
  32. * @param {function} searchCallback the function to run on a query search
  33. * @param {function} resetCallback the function to run when the user reset the form
  34. */
  35. var Search = function(searchCallback, resetCallback) {
  36. this.initialize(searchCallback, resetCallback);
  37. };
  38. /**
  39. * @memberof OC
  40. */
  41. Search.prototype = {
  42. /**
  43. * Initialize the search box
  44. *
  45. * @param {function} searchCallback the function to run on a query search
  46. * @param {function} resetCallback the function to run when the user reset the form
  47. */
  48. initialize: function(searchCallback, resetCallback) {
  49. var self = this;
  50. if (typeof searchCallback !== 'function') {
  51. throw new Error('searchCallback must be a function');
  52. }
  53. if (typeof resetCallback !== 'function') {
  54. throw new Error('resetCallback must be a function');
  55. }
  56. if (!document.getElementById('searchbox')) {
  57. throw new Error('searchBox not available');
  58. }
  59. this.searchCallback = searchCallback;
  60. this.resetCallback = resetCallback;
  61. console.debug('New search handler registered');
  62. /**
  63. * Search
  64. */
  65. this.search = function(event) {
  66. event.preventDefault();
  67. var query = document.getElementById('searchbox').value;
  68. self.searchCallback(query);
  69. };
  70. /**
  71. * Reset form
  72. */
  73. this.reset = function(event) {
  74. event.preventDefault();
  75. document.getElementById('searchbox').value = '';
  76. self.resetCallback();
  77. };
  78. // Show search
  79. document.getElementById('searchbox').style.display = 'block';
  80. // Register input event
  81. document
  82. .getElementById('searchbox')
  83. .addEventListener('input', _.debounce(self.search, 500), true);
  84. document
  85. .querySelector('form.searchbox')
  86. .addEventListener('submit', function(event) {
  87. // Avoid form submit
  88. event.preventDefault();
  89. _.debounce(self.search, 500);
  90. }, true);
  91. // Register reset
  92. document
  93. .querySelector('form.searchbox')
  94. .addEventListener('reset', _.debounce(self.reset, 500), true);
  95. // Register esc key shortcut reset if focused
  96. document.addEventListener('keyup', function(event) {
  97. if (event.defaultPrevented) {
  98. return;
  99. }
  100. var key = event.key || event.keyCode;
  101. if (
  102. document.getElementById('searchbox') === document.activeElement &&
  103. document.getElementById('searchbox').value === ''
  104. ) {
  105. if (key === 'Escape' || key === 'Esc' || key === 27) {
  106. _.debounce(self.reset, 500);
  107. }
  108. }
  109. });
  110. // Register ctrl+F key shortcut to focus
  111. document.addEventListener('keydown', function(event) {
  112. if (event.defaultPrevented) {
  113. return;
  114. }
  115. var key = event.key || event.keyCode;
  116. if (document.getElementById('searchbox') !== document.activeElement) {
  117. if (
  118. (event.ctrlKey || event.metaKey) && // CTRL or mac CMD
  119. !event.shiftKey && // Not SHIFT
  120. (key === 'f' || key === 70) // F
  121. ) {
  122. event.preventDefault();
  123. document.getElementById('searchbox').focus();
  124. document.getElementById('searchbox').select();
  125. }
  126. }
  127. });
  128. }
  129. };
  130. OCA.Search = Search;
  131. })();