user/amd/src/comboboxsearch/user.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. * Allow the user to search for learners.
  17. *
  18. * @module core_user/comboboxsearch/user
  19. * @copyright 2023 Mathew May <mathew.solutions>
  20. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  21. */
  22. import search_combobox from 'core/comboboxsearch/search_combobox';
  23. import {getStrings} from 'core/str';
  24. import {renderForPromise, replaceNodeContents} from 'core/templates';
  25. import $ from 'jquery';
  26. export default class UserSearch extends search_combobox {
  27. courseID;
  28. groupID;
  29. // A map of user profile field names that is human-readable.
  30. profilestringmap = null;
  31. constructor() {
  32. super();
  33. // Register a couple of events onto the document since we need to check if they are moving off the component.
  34. ['click', 'focus'].forEach(eventType => {
  35. // Since we are handling dropdowns manually, ensure we can close it when moving off.
  36. document.addEventListener(eventType, e => {
  37. if (this.searchDropdown.classList.contains('show') && !this.combobox.contains(e.target)) {
  38. this.toggleDropdown();
  39. }
  40. }, true);
  41. });
  42. // Register keyboard events.
  43. this.component.addEventListener('keydown', this.keyHandler.bind(this));
  44. // Define our standard lookups.
  45. this.selectors = {...this.selectors,
  46. courseid: '[data-region="courseid"]',
  47. groupid: '[data-region="groupid"]',
  48. resetPageButton: '[data-action="resetpage"]',
  49. };
  50. this.courseID = this.component.querySelector(this.selectors.courseid).dataset.courseid;
  51. this.groupID = document.querySelector(this.selectors.groupid)?.dataset?.groupid;
  52. this.instance = this.component.querySelector(this.selectors.instance).dataset.instance;
  53. // We need to render some content by default for ARIA purposes.
  54. this.renderDefault();
  55. }
  56. static init() {
  57. return new UserSearch();
  58. }
  59. /**
  60. * The overall div that contains the searching widget.
  61. *
  62. * @returns {string}
  63. */
  64. componentSelector() {
  65. return '.user-search';
  66. }
  67. /**
  68. * The dropdown div that contains the searching widget result space.
  69. *
  70. * @returns {string}
  71. */
  72. dropdownSelector() {
  73. return '.usersearchdropdown';
  74. }
  75. /**
  76. * Build the content then replace the node.
  77. */
  78. async renderDropdown() {
  79. const {html, js} = await renderForPromise('core_user/comboboxsearch/resultset', {
  80. users: this.getMatchedResults().slice(0, 5),
  81. hasresults: this.getMatchedResults().length > 0,
  82. instance: this.instance,
  83. matches: this.getMatchedResults().length,
  84. searchterm: this.getSearchTerm(),
  85. selectall: this.selectAllResultsLink(),
  86. });
  87. replaceNodeContents(this.getHTMLElements().searchDropdown, html, js);
  88. // Remove aria-activedescendant when the available options change.
  89. this.searchInput.removeAttribute('aria-activedescendant');
  90. }
  91. /**
  92. * Build the content then replace the node by default we want our form to exist.
  93. */
  94. async renderDefault() {
  95. this.setMatchedResults(await this.filterDataset(await this.getDataset()));
  96. this.filterMatchDataset();
  97. await this.renderDropdown();
  98. }
  99. /**
  100. * Get the data we will be searching against in this component.
  101. *
  102. * @returns {Promise<*>}
  103. */
  104. fetchDataset() {
  105. throw new Error(`fetchDataset() must be implemented in ${this.constructor.name}`);
  106. }
  107. /**
  108. * Dictate to the search component how and what we want to match upon.
  109. *
  110. * @param {Array} filterableData
  111. * @returns {Array} The users that match the given criteria.
  112. */
  113. async filterDataset(filterableData) {
  114. if (this.getPreppedSearchTerm()) {
  115. const stringMap = await this.getStringMap();
  116. return filterableData.filter((user) => Object.keys(user).some((key) => {
  117. if (user[key] === "" || user[key] === null || !stringMap.get(key)) {
  118. return false;
  119. }
  120. return user[key].toString().toLowerCase().includes(this.getPreppedSearchTerm());
  121. }));
  122. } else {
  123. return [];
  124. }
  125. }
  126. /**
  127. * Given we have a subset of the dataset, set the field that we matched upon to inform the end user.
  128. *
  129. * @returns {Array} The results with the matched fields inserted.
  130. */
  131. async filterMatchDataset() {
  132. const stringMap = await this.getStringMap();
  133. this.setMatchedResults(
  134. this.getMatchedResults().map((user) => {
  135. for (const [key, value] of Object.entries(user)) {
  136. // Sometimes users have null values in their profile fields.
  137. if (value === null) {
  138. continue;
  139. }
  140. const valueString = value.toString().toLowerCase();
  141. const preppedSearchTerm = this.getPreppedSearchTerm();
  142. const searchTerm = this.getSearchTerm();
  143. // Ensure we match only on expected keys.
  144. const matchingFieldName = stringMap.get(key);
  145. if (matchingFieldName && valueString.includes(preppedSearchTerm)) {
  146. user.matchingFieldName = matchingFieldName;
  147. // Safely prepare our matching results.
  148. const escapedValueString = valueString.replace(/</g, '&lt;');
  149. const escapedMatchingField = escapedValueString.replace(
  150. preppedSearchTerm.replace(/</g, '&lt;'),
  151. `<span class="fw-bold">${searchTerm.replace(/</g, '&lt;')}</span>`
  152. );
  153. if (user.email) {
  154. user.matchingField = `${escapedMatchingField} (${user.email})`;
  155. } else {
  156. user.matchingField = escapedMatchingField;
  157. }
  158. break;
  159. }
  160. }
  161. return user;
  162. })
  163. );
  164. }
  165. /**
  166. * The handler for when a user changes the value of the component (selects an option from the dropdown).
  167. *
  168. * @param {Event} e The change event.
  169. */
  170. changeHandler(e) {
  171. this.toggleDropdown(); // Otherwise the dropdown stays open when user choose an option using keyboard.
  172. if (e.target.value === '0') {
  173. window.location = this.selectAllResultsLink();
  174. } else {
  175. window.location = this.selectOneLink(e.target.value);
  176. }
  177. }
  178. /**
  179. * The handler for when a user presses a key within the component.
  180. *
  181. * @param {KeyboardEvent} e The triggering event that we are working with.
  182. */
  183. keyHandler(e) {
  184. // Switch the key presses to handle keyboard nav.
  185. switch (e.key) {
  186. case 'ArrowUp':
  187. case 'ArrowDown':
  188. if (
  189. this.getSearchTerm() !== ''
  190. && !this.searchDropdown.classList.contains('show')
  191. && e.target.contains(this.combobox)
  192. ) {
  193. this.renderAndShow();
  194. }
  195. break;
  196. case 'Enter':
  197. case ' ':
  198. if (e.target.closest(this.selectors.resetPageButton)) {
  199. e.stopPropagation();
  200. window.location = e.target.closest(this.selectors.resetPageButton).href;
  201. break;
  202. }
  203. break;
  204. case 'Escape':
  205. this.toggleDropdown();
  206. this.searchInput.focus({preventScroll: true});
  207. break;
  208. }
  209. }
  210. /**
  211. * When called, hide or show the users dropdown.
  212. *
  213. * @param {Boolean} on Flag to toggle hiding or showing values.
  214. */
  215. toggleDropdown(on = false) {
  216. if (on) {
  217. this.searchDropdown.classList.add('show');
  218. $(this.searchDropdown).show();
  219. this.getHTMLElements().searchInput.setAttribute('aria-expanded', 'true');
  220. this.searchInput.focus({preventScroll: true});
  221. } else {
  222. this.searchDropdown.classList.remove('show');
  223. $(this.searchDropdown).hide();
  224. // As we are manually handling the dropdown, we need to do some housekeeping manually.
  225. this.getHTMLElements().searchInput.setAttribute('aria-expanded', 'false');
  226. this.searchInput.removeAttribute('aria-activedescendant');
  227. this.searchDropdown.querySelectorAll('.active[role="option"]').forEach(option => {
  228. option.classList.remove('active');
  229. });
  230. }
  231. }
  232. /**
  233. * Build up the view all link.
  234. */
  235. selectAllResultsLink() {
  236. throw new Error(`selectAllResultsLink() must be implemented in ${this.constructor.name}`);
  237. }
  238. /**
  239. * Build up the view all link that is dedicated to a particular result.
  240. * We will call this function when a user interacts with the combobox to redirect them to show their results in the page.
  241. *
  242. * @param {Number} userID The ID of the user selected.
  243. */
  244. selectOneLink(userID) {
  245. throw new Error(`selectOneLink(${userID}) must be implemented in ${this.constructor.name}`);
  246. }
  247. /**
  248. * Given the set of profile fields we can possibly search, fetch their strings,
  249. * so we can report to screen readers the field that matched.
  250. *
  251. * @returns {Promise<void>}
  252. */
  253. getStringMap() {
  254. if (!this.profilestringmap) {
  255. const requiredStrings = [
  256. 'username',
  257. 'fullname',
  258. 'firstname',
  259. 'lastname',
  260. 'email',
  261. 'city',
  262. 'country',
  263. 'department',
  264. 'institution',
  265. 'idnumber',
  266. 'phone1',
  267. 'phone2',
  268. ];
  269. this.profilestringmap = getStrings(requiredStrings.map((key) => ({key})))
  270. .then((stringArray) => new Map(
  271. requiredStrings.map((key, index) => ([key, stringArray[index]]))
  272. ));
  273. }
  274. return this.profilestringmap;
  275. }
  276. }