mod/data/amd/src/selectpreset.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. * Javascript module to control the form responsible for selecting a preset.
  17. *
  18. * @module mod_data/selectpreset
  19. * @copyright 2021 Mihail Geshoski <mihail@moodle.com>
  20. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  21. */
  22. const selectors = {
  23. presetRadioButton: 'input[name="fullname"]',
  24. selectPresetButton: 'input[name="selectpreset"]',
  25. selectedPresetRadioButton: 'input[name="fullname"]:checked',
  26. };
  27. /**
  28. * Initialize module.
  29. */
  30. export const init = () => {
  31. const radioButton = document.querySelectorAll(selectors.presetRadioButton);
  32. // Initialize the "Use a preset" button properly.
  33. disableUsePresetButton();
  34. radioButton.forEach((elem) => {
  35. elem.addEventListener('change', function(event) {
  36. event.preventDefault();
  37. // Enable the "Use a preset" button when any of the radio buttons in the presets list is checked.
  38. disableUsePresetButton();
  39. });
  40. });
  41. };
  42. /**
  43. * Decide whether to disable or not the "Use a preset" button.
  44. * When there is no preset selected, the button should be displayed disabled; otherwise, it will appear enabled as a primary button.
  45. *
  46. * @method
  47. * @private
  48. */
  49. const disableUsePresetButton = () => {
  50. let selectPresetButton = document.querySelector(selectors.selectPresetButton);
  51. const selectedRadioButton = document.querySelector(selectors.selectedPresetRadioButton);
  52. if (selectedRadioButton) {
  53. // There is one preset selected, so the button should be enabled.
  54. selectPresetButton.removeAttribute('disabled');
  55. selectPresetButton.classList.remove('btn-secondary');
  56. selectPresetButton.classList.add('btn-primary');
  57. selectPresetButton.setAttribute('data-presetname', selectedRadioButton.getAttribute('value'));
  58. selectPresetButton.setAttribute('data-cmid', selectedRadioButton.getAttribute('data-cmid'));
  59. } else {
  60. // There is no any preset selected, so the button should be disabled.
  61. selectPresetButton.setAttribute('disabled', true);
  62. selectPresetButton.classList.remove('btn-primary');
  63. selectPresetButton.classList.add('btn-secondary');
  64. selectPresetButton.removeAttribute('data-presetname');
  65. selectPresetButton.removeAttribute('data-cmid');
  66. }
  67. };