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 |
import Templates from 'core/templates';
|
|
|
17 |
import {get_string as getString} from 'core/str';
|
|
|
18 |
import {disableStickyFooter, enableStickyFooter} from 'core/sticky-footer';
|
|
|
19 |
|
|
|
20 |
/**
|
|
|
21 |
* Base class for defining a bulk actions area within a page.
|
|
|
22 |
*
|
|
|
23 |
* @module core/bulkactions/bulk_actions
|
|
|
24 |
* @copyright 2023 Mihail Geshoski <mihail@moodle.com>
|
|
|
25 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
26 |
*/
|
|
|
27 |
|
|
|
28 |
/** @constant {Object} The object containing the relevant selectors. */
|
|
|
29 |
const Selectors = {
|
|
|
30 |
stickyFooterContainer: '#sticky-footer',
|
|
|
31 |
selectedItemsCountContainer: '[data-type="bulkactions"] [data-for="bulkcount"]',
|
|
|
32 |
cancelBulkActionModeElement: '[data-type="bulkactions"] [data-action="bulkcancel"]',
|
|
|
33 |
bulkModeContainer: '[data-type="bulkactions"]',
|
|
|
34 |
bulkActionsContainer: '[data-type="bulkactions"] [data-for="bulktools"]'
|
|
|
35 |
};
|
|
|
36 |
|
|
|
37 |
export default class BulkActions {
|
|
|
38 |
|
|
|
39 |
/** @property {string|null} initialStickyFooterContent The initial content of the sticky footer. */
|
|
|
40 |
initialStickyFooterContent = null;
|
|
|
41 |
|
|
|
42 |
/** @property {Array} selectedItems The array of selected item elements. */
|
|
|
43 |
selectedItems = [];
|
|
|
44 |
|
|
|
45 |
/** @property {boolean} isBulkActionsModeEnabled Whether the bulk actions mode is enabled. */
|
|
|
46 |
isBulkActionsModeEnabled = false;
|
|
|
47 |
|
|
|
48 |
/**
|
|
|
49 |
* The class constructor.
|
|
|
50 |
*
|
|
|
51 |
* @returns {void}
|
|
|
52 |
*/
|
|
|
53 |
constructor() {
|
|
|
54 |
if (!this.getStickyFooterContainer()) {
|
|
|
55 |
throw new Error('Sticky footer not found.');
|
|
|
56 |
}
|
|
|
57 |
// Store any pre-existing content in the sticky footer. When bulk actions mode is enabled, this content will be
|
|
|
58 |
// replaced with the bulk actions content and restored when bulk actions mode is disabled.
|
|
|
59 |
this.initialStickyFooterContent = this.getStickyFooterContainer().innerHTML;
|
|
|
60 |
// Register and handle the item select change event.
|
|
|
61 |
this.registerItemSelectChangeEvent(async() => {
|
|
|
62 |
this.selectedItems = this.getSelectedItems();
|
|
|
63 |
if (this.selectedItems.length > 0) { // At least one item is selected.
|
|
|
64 |
// If the bulk actions mode is already enabled only update the selected items count.
|
|
|
65 |
if (this.isBulkActionsModeEnabled) {
|
|
|
66 |
await this.updateBulkItemSelection();
|
|
|
67 |
} else { // Otherwise, enable the bulk action mode.
|
|
|
68 |
await this.enableBulkActionsMode();
|
|
|
69 |
}
|
|
|
70 |
} else { // No items are selected, disable the bulk action mode.
|
|
|
71 |
this.disableBulkActionsMode();
|
|
|
72 |
}
|
|
|
73 |
});
|
|
|
74 |
}
|
|
|
75 |
|
|
|
76 |
/**
|
|
|
77 |
* Returns the array of the relevant bulk action objects.
|
|
|
78 |
*
|
|
|
79 |
* @method getBulkActions
|
|
|
80 |
* @returns {Array}
|
|
|
81 |
*/
|
|
|
82 |
getBulkActions() {
|
|
|
83 |
throw new Error(`getBulkActions() must be implemented in ${this.constructor.name}`);
|
|
|
84 |
}
|
|
|
85 |
|
|
|
86 |
/**
|
|
|
87 |
* Returns the array of selected items.
|
|
|
88 |
*
|
|
|
89 |
* @method getSelectedItems
|
|
|
90 |
* @returns {Array}
|
|
|
91 |
*/
|
|
|
92 |
getSelectedItems() {
|
|
|
93 |
throw new Error(`getSelectedItems() must be implemented in ${this.constructor.name}`);
|
|
|
94 |
}
|
|
|
95 |
|
|
|
96 |
/**
|
|
|
97 |
* Adds the listener for the item select change event.
|
|
|
98 |
* The event handler function that is passed as a parameter should be called right after the event is triggered.
|
|
|
99 |
*
|
|
|
100 |
* @method registerItemSelectChangeEvent
|
|
|
101 |
* @param {function} eventHandler The event handler function.
|
|
|
102 |
* @returns {void}
|
|
|
103 |
*/
|
|
|
104 |
registerItemSelectChangeEvent(eventHandler) {
|
|
|
105 |
throw new Error(`registerItemSelectChangeEvent(${eventHandler}) must be implemented in ${this.constructor.name}`);
|
|
|
106 |
}
|
|
|
107 |
|
|
|
108 |
/**
|
|
|
109 |
* Returns the sticky footer container.
|
|
|
110 |
*
|
|
|
111 |
* @method getStickyFooterContainer
|
|
|
112 |
* @returns {HTMLElement}
|
|
|
113 |
*/
|
|
|
114 |
getStickyFooterContainer() {
|
|
|
115 |
return document.querySelector(Selectors.stickyFooterContainer);
|
|
|
116 |
}
|
|
|
117 |
|
|
|
118 |
/**
|
|
|
119 |
* Enables the bulk action mode.
|
|
|
120 |
*
|
|
|
121 |
* @method enableBulkActionsMode
|
|
|
122 |
* @returns {Promise}
|
|
|
123 |
*/
|
|
|
124 |
async enableBulkActionsMode() {
|
|
|
125 |
// Make sure that the sticky footer is enabled.
|
|
|
126 |
enableStickyFooter();
|
|
|
127 |
// Render the bulk actions content in the sticky footer container.
|
|
|
128 |
this.getStickyFooterContainer().innerHTML = await this.renderBulkActions();
|
|
|
129 |
const bulkModeContainer = this.getStickyFooterContainer().querySelector(Selectors.bulkModeContainer);
|
|
|
130 |
const bulkActionsContainer = bulkModeContainer.querySelector(Selectors.bulkActionsContainer);
|
|
|
131 |
this.getBulkActions().forEach((bulkAction) => {
|
|
|
132 |
// Register the listener events for each available bulk action.
|
|
|
133 |
bulkAction.registerListenerEvents(bulkActionsContainer);
|
|
|
134 |
// Set the selected items for each available bulk action.
|
|
|
135 |
bulkAction.setSelectedItems(this.selectedItems);
|
|
|
136 |
});
|
|
|
137 |
// Register the click listener event for the cancel bulk mode button.
|
|
|
138 |
bulkModeContainer.addEventListener('click', (e) => {
|
|
|
139 |
if (e.target.closest(Selectors.cancelBulkActionModeElement)) {
|
|
|
140 |
// Uncheck all selected items.
|
|
|
141 |
this.selectedItems.forEach((item) => {
|
|
|
142 |
item.checked = false;
|
|
|
143 |
});
|
|
|
144 |
// Disable the bulk action mode.
|
|
|
145 |
this.disableBulkActionsMode();
|
|
|
146 |
}
|
|
|
147 |
});
|
|
|
148 |
this.isBulkActionsModeEnabled = true;
|
|
|
149 |
}
|
|
|
150 |
|
|
|
151 |
/**
|
|
|
152 |
* Disables the bulk action mode.
|
|
|
153 |
*
|
|
|
154 |
* @method disableBulkActionsMode
|
|
|
155 |
* @returns {void}
|
|
|
156 |
*/
|
|
|
157 |
disableBulkActionsMode() {
|
|
|
158 |
// If there was any previous (initial) content in the sticky footer, restore it.
|
|
|
159 |
if (this.initialStickyFooterContent.length > 0) {
|
|
|
160 |
this.getStickyFooterContainer().innerHTML = this.initialStickyFooterContent;
|
|
|
161 |
} else { // No previous content to restore, disable the sticky footer.
|
|
|
162 |
disableStickyFooter();
|
|
|
163 |
}
|
|
|
164 |
this.isBulkActionsModeEnabled = false;
|
|
|
165 |
}
|
|
|
166 |
|
|
|
167 |
/**
|
|
|
168 |
* Renders the bulk actions content.
|
|
|
169 |
*
|
|
|
170 |
* @method renderBulkActions
|
|
|
171 |
* @returns {Promise}
|
|
|
172 |
*/
|
|
|
173 |
async renderBulkActions() {
|
|
|
174 |
let data = {
|
|
|
175 |
'bulkselectioncount': this.selectedItems.length,
|
|
|
176 |
'actions': []
|
|
|
177 |
};
|
|
|
178 |
// Render the bulk actions trigger element for each available bulk action.
|
|
|
179 |
await Promise.all(this.getBulkActions().map(async(bulkAction) => {
|
|
|
180 |
data.actions.push({'actiontrigger': await bulkAction.renderBulkActionTrigger()});
|
|
|
181 |
}));
|
|
|
182 |
|
|
|
183 |
return Templates.render('core/bulkactions/bulk_actions', data);
|
|
|
184 |
}
|
|
|
185 |
|
|
|
186 |
/**
|
|
|
187 |
* Updates the selected items count in the bulk actions content.
|
|
|
188 |
*
|
|
|
189 |
* @method updateBulkItemSelection
|
|
|
190 |
* @returns {void}
|
|
|
191 |
*/
|
|
|
192 |
async updateBulkItemSelection() {
|
|
|
193 |
const bulkSelection = await getString('bulkselection', 'core', this.selectedItems.length);
|
|
|
194 |
document.querySelector(Selectors.selectedItemsCountContainer).innerHTML = bulkSelection;
|
|
|
195 |
}
|
|
|
196 |
}
|