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 |
* Javascript for handling actions on the admin page
|
|
|
18 |
*
|
|
|
19 |
* @module qbank_columnsortorder/admin_actions
|
|
|
20 |
* @copyright 2023 onwards Catalyst IT Europe Ltd
|
|
|
21 |
* @author Mark Johnson <mark.johnson@catalyst-eu.net>
|
|
|
22 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
23 |
*/
|
|
|
24 |
|
|
|
25 |
import * as actions from 'qbank_columnsortorder/actions';
|
|
|
26 |
import * as repository from 'qbank_columnsortorder/repository';
|
|
|
27 |
import Notification from "core/notification";
|
|
|
28 |
import Pending from 'core/pending';
|
|
|
29 |
|
|
|
30 |
/**
|
|
|
31 |
* Event handler to save the custom column widths when a field is edited.
|
|
|
32 |
*
|
|
|
33 |
* @param {Element} listRoot The root element of the list of columns.
|
|
|
34 |
*/
|
|
|
35 |
const setupSaveWidths = listRoot => {
|
|
|
36 |
listRoot.addEventListener('change', async() => {
|
|
|
37 |
const pendingPromise = new Pending('saveWidths');
|
|
|
38 |
const columns = listRoot.querySelectorAll(actions.SELECTORS.sortableColumn);
|
|
|
39 |
const widths = [];
|
|
|
40 |
columns.forEach(column => {
|
|
|
41 |
const widthInput = column.querySelector('.width-input');
|
|
|
42 |
const valid = widthInput.checkValidity();
|
|
|
43 |
widthInput.closest('.has-validation').classList.add('was-validated');
|
|
|
44 |
if (!valid) {
|
|
|
45 |
return;
|
|
|
46 |
}
|
|
|
47 |
widths.push({
|
|
|
48 |
column: column.dataset.columnid,
|
|
|
49 |
width: widthInput.value,
|
|
|
50 |
});
|
|
|
51 |
});
|
|
|
52 |
await repository.setColumnSize(JSON.stringify(widths), true).catch(Notification.exception);
|
|
|
53 |
pendingPromise.resolve();
|
|
|
54 |
});
|
|
|
55 |
};
|
|
|
56 |
|
|
|
57 |
/**
|
|
|
58 |
* Initialize module
|
|
|
59 |
*
|
|
|
60 |
* Set up event handlers for the action buttons, width fields and initialise column sorting.
|
|
|
61 |
*
|
|
|
62 |
* @param {String} id ID for the admin UI root element.
|
|
|
63 |
*/
|
|
|
64 |
export const init = id => {
|
|
|
65 |
const uiRoot = document.getElementById(id);
|
|
|
66 |
const listRoot = uiRoot.querySelector(actions.SELECTORS.columnList);
|
|
|
67 |
actions.setupSortableLists(listRoot, true, true);
|
|
|
68 |
actions.setupActionButtons(uiRoot, true);
|
|
|
69 |
setupSaveWidths(listRoot);
|
|
|
70 |
};
|