group/amd/src/index.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. * @module core_group/index
  17. * @copyright 2022 Matthew Hilton <matthewhilton@catalyst-au.net>
  18. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  19. */
  20. import GroupPicker from "./grouppicker";
  21. const groupPicker = new GroupPicker();
  22. /**
  23. * Initialise page.
  24. */
  25. export const init = () => {
  26. // Init event listeners.
  27. groupPicker.getDomElement().addEventListener("change", updateBulkActionButtons);
  28. // Call initially to set initial button state.
  29. updateBulkActionButtons();
  30. };
  31. /**
  32. * Updates the bulk action buttons depending on specific conditions.
  33. */
  34. export const updateBulkActionButtons = () => {
  35. const groupsSelected = groupPicker.getSelectedValues();
  36. const aGroupIsSelected = groupsSelected.length !== 0;
  37. // Collate the conditions where each button is enabled/disabled.
  38. const bulkActionsEnabledStatuses = {
  39. 'enablemessaging': aGroupIsSelected,
  40. 'disablemessaging': aGroupIsSelected
  41. };
  42. // Update the status of each button.
  43. Object.entries(bulkActionsEnabledStatuses).map(([buttonId, enabled]) => setElementEnabled(buttonId, enabled));
  44. };
  45. /**
  46. * Adds or removes the given element's disabled attribute.
  47. * @param {string} domElementId ID of the dom element (without the #)
  48. * @param {bool} enabled If false, the disable attribute is applied, else it is removed.
  49. */
  50. export const setElementEnabled = (domElementId, enabled) => {
  51. const element = document.getElementById(domElementId);
  52. if (!element) {
  53. // If there is no element, we do nothing.
  54. // The element could be purposefully hidden or removed.
  55. return;
  56. }
  57. if (!enabled) {
  58. element.setAttribute('disabled', 'disabled');
  59. } else {
  60. element.removeAttribute('disabled');
  61. }
  62. };