lib/amd/src/bulkactions/bulk_action.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. * Base class for defining a bulk action.
  17. *
  18. * @module core/bulkactions/bulk_action
  19. * @copyright 2023 Mihail Geshoski <mihail@moodle.com>
  20. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  21. */
  22. export default class BulkAction {
  23. /** @property {array} selectedItems The array of selected item elements. */
  24. selectedItems = [];
  25. /**
  26. * Registers the listener events for the bulk actions.
  27. *
  28. * @method registerListenerEvents
  29. * @param {HTMLElement} containerElement The container element for the bulk actions.
  30. * @returns {void}
  31. */
  32. registerListenerEvents(containerElement) {
  33. // Listen for the click event on the bulk action trigger element.
  34. containerElement.addEventListener('click', (e) => {
  35. if (e.target.closest(this.getBulkActionTriggerSelector())) {
  36. e.preventDefault();
  37. this.triggerBulkAction();
  38. }
  39. });
  40. }
  41. /**
  42. * Setter method for the selectedItems property.
  43. *
  44. * @method setSelectedItems
  45. * @param {Array} selectedItems The array of selected item elements..
  46. * @returns {void}
  47. */
  48. setSelectedItems(selectedItems) {
  49. this.selectedItems = selectedItems;
  50. }
  51. /**
  52. * Defines the selector of the element that triggers the bulk action.
  53. *
  54. * @method getBulkActionTriggerSelector
  55. * @returns {string}
  56. */
  57. getBulkActionTriggerSelector() {
  58. throw new Error(`getBulkActionTriggerSelector() must be implemented in ${this.constructor.name}`);
  59. }
  60. /**
  61. * Defines the behavior once the bulk action is triggered.
  62. *
  63. * @method triggerBulkAction
  64. */
  65. triggerBulkAction() {
  66. throw new Error(`triggerBulkAction() must be implemented in ${this.constructor.name}`);
  67. }
  68. /**
  69. * Renders the bulk action trigger element.
  70. *
  71. * @method renderBulkActionTrigger
  72. * @param {boolean} showInDropdown Whether the action is displayed under a 'More' dropdown or as a separate button.
  73. * @param {number} index The index of the action.
  74. * @returns {Promise}
  75. */
  76. renderBulkActionTrigger(showInDropdown = false, index) {
  77. throw new Error(`renderBulkActionTrigger(${showInDropdown}, ${index}) must be implemented in ${this.constructor.name}`);
  78. }
  79. }