search.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. // Show search
  80. document.getElementById('searchbox').style.display = 'block';
  81. // Register input event
  82. document
  83. .getElementById('searchbox')
  84. .addEventListener('input', _.debounce(self.search, 500), true);
  85. document
  86. .querySelector('form.searchbox')
  87. .addEventListener('submit', function(event) {
  88. // Avoid form submit
  89. event.preventDefault();
  90. _.debounce(self.search, 500);
  91. }, true);
  92. // Register reset
  93. document
  94. .querySelector('form.searchbox')
  95. .addEventListener('reset', _.debounce(self.reset, 500), true);
  96. // Register esc key shortcut reset if focused
  97. document.addEventListener('keyup', function(event) {
  98. if (event.defaultPrevented) {
  99. return;
  100. }
  101. var key = event.key || event.keyCode;
  102. if (
  103. document.getElementById('searchbox') === document.activeElement &&
  104. document.getElementById('searchbox').value === ''
  105. ) {
  106. if (key === 'Escape' || key === 'Esc' || key === 27) {
  107. _.debounce(self.reset, 500);
  108. }
  109. }
  110. });
  111. // Register ctrl+F key shortcut to focus
  112. document.addEventListener('keydown', function(event) {
  113. if (event.defaultPrevented) {
  114. return;
  115. }
  116. var key = event.key || event.keyCode;
  117. if (document.getElementById('searchbox') !== document.activeElement) {
  118. if (
  119. (event.ctrlKey || event.metaKey) && // CTRL or mac CMD
  120. !event.shiftKey && // Not SHIFT
  121. (key === 'f' || key === 70) // F
  122. ) {
  123. event.preventDefault();
  124. document.getElementById('searchbox').focus();
  125. document.getElementById('searchbox').select();
  126. }
  127. }
  128. });
  129. }
  130. };
  131. OCA.Search = Search;
  132. })();