contentbank/amd/src/search.js

  1. // This file is part of Moodle - http://moodle.org/
  2. //
  3. // Moodle is free software: you can redistribute it and/or modify
  4. // it under the terms of the GNU General Public License as published by
  5. // the Free Software Foundation, either version 3 of the License, or
  6. // (at your option) any later version.
  7. //
  8. // Moodle is distributed in the hope that it will be useful,
  9. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. // GNU General Public License for more details.
  12. //
  13. // You should have received a copy of the GNU General Public License
  14. // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
  15. /**
  16. * Search methods for finding contents in the content bank.
  17. *
  18. * @module core_contentbank/search
  19. * @copyright 2020 Sara Arjona <sara@moodle.com>
  20. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  21. */
  22. import $ from 'jquery';
  23. import selectors from 'core_contentbank/selectors';
  24. import {getString} from 'core/str';
  25. import Pending from 'core/pending';
  26. import {debounce} from 'core/utils';
  27. /**
  28. * Set up the search.
  29. *
  30. * @method init
  31. */
  32. export const init = () => {
  33. const pendingPromise = new Pending();
  34. const root = $(selectors.regions.contentbank);
  35. registerListenerEvents(root);
  36. pendingPromise.resolve();
  37. };
  38. /**
  39. * Register contentbank search related event listeners.
  40. *
  41. * @method registerListenerEvents
  42. * @param {Object} root The root element for the contentbank.
  43. */
  44. const registerListenerEvents = (root) => {
  45. const searchInput = root.find(selectors.elements.searchinput)[0];
  46. root.on('click', selectors.actions.search, function(e) {
  47. e.preventDefault();
  48. toggleSearchResultsView(root, searchInput.value);
  49. });
  50. root.on('click', selectors.actions.clearSearch, function(e) {
  51. e.preventDefault();
  52. searchInput.value = "";
  53. searchInput.focus();
  54. toggleSearchResultsView(root, searchInput.value);
  55. });
  56. // The search input is also triggered.
  57. searchInput.addEventListener('input', debounce(() => {
  58. // Display the search results.
  59. toggleSearchResultsView(root, searchInput.value);
  60. }, 300));
  61. };
  62. /**
  63. * Toggle (display/hide) the search results depending on the value of the search query.
  64. *
  65. * @method toggleSearchResultsView
  66. * @param {HTMLElement} body The root element for the contentbank.
  67. * @param {String} searchQuery The search query.
  68. */
  69. const toggleSearchResultsView = async(body, searchQuery) => {
  70. const clearSearchButton = body.find(selectors.actions.clearSearch)[0];
  71. const navbarBreadcrumb = body.find(selectors.elements.cbnavbarbreadcrumb)[0];
  72. const navbarTotal = body.find(selectors.elements.cbnavbartotalsearch)[0];
  73. // Update the results.
  74. const filteredContents = filterContents(body, searchQuery);
  75. if (searchQuery.length > 0) {
  76. // As the search query is present, search results should be displayed.
  77. // Display the "clear" search button in the activity chooser search bar.
  78. clearSearchButton.classList.remove('d-none');
  79. // Change the cb-navbar to display total items found.
  80. navbarBreadcrumb.classList.add('d-none');
  81. navbarTotal.innerHTML = await getString('itemsfound', 'core_contentbank', filteredContents.length);
  82. navbarTotal.classList.remove('d-none');
  83. } else {
  84. // As search query is not present, the search results should be removed.
  85. // Hide the "clear" search button in the activity chooser search bar.
  86. clearSearchButton.classList.add('d-none');
  87. // Display again the breadcrumb in the navbar.
  88. navbarBreadcrumb.classList.remove('d-none');
  89. navbarTotal.classList.add('d-none');
  90. }
  91. };
  92. /**
  93. * Return the list of contents which have a name that matches the given search term.
  94. *
  95. * @method filterContents
  96. * @param {HTMLElement} body The root element for the contentbank.
  97. * @param {String} searchTerm The search term to match.
  98. * @return {Array}
  99. */
  100. const filterContents = (body, searchTerm) => {
  101. const contents = Array.from(body.find(selectors.elements.listitem));
  102. const searchResults = [];
  103. contents.forEach((content) => {
  104. const contentName = content.getAttribute('data-name');
  105. if (searchTerm === '' || contentName.toLowerCase().includes(searchTerm.toLowerCase())) {
  106. // The content matches the search criteria so it should be displayed and hightlighted.
  107. searchResults.push(content);
  108. const contentNameElement = content.querySelector(selectors.regions.cbcontentname);
  109. contentNameElement.innerHTML = highlight(contentName, searchTerm);
  110. content.classList.remove('d-none');
  111. } else {
  112. content.classList.add('d-none');
  113. }
  114. });
  115. return searchResults;
  116. };
  117. /**
  118. * Highlight a given string in a text.
  119. *
  120. * @method highlight
  121. * @param {String} text The whole text.
  122. * @param {String} highlightText The piece of text to highlight.
  123. * @return {String}
  124. */
  125. const highlight = (text, highlightText) => {
  126. let result = text;
  127. if (highlightText !== '') {
  128. const pos = text.toLowerCase().indexOf(highlightText.toLowerCase());
  129. if (pos > -1) {
  130. result = text.substr(0, pos) + '<span class="matchtext">' + text.substr(pos, highlightText.length) + '</span>' +
  131. text.substr(pos + highlightText.length);
  132. }
  133. }
  134. return result;
  135. };