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