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 |
* Common javascript for handling actions on the admin page and the user's view of the question bank.
|
|
|
18 |
*
|
|
|
19 |
* @module qbank_columnsortorder/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 SortableList from 'core/sortable_list';
|
|
|
26 |
import $ from 'jquery';
|
|
|
27 |
import * as repository from 'qbank_columnsortorder/repository';
|
|
|
28 |
import Notification from "core/notification";
|
|
|
29 |
import RefreshUi from 'core_question/refresh_ui';
|
|
|
30 |
|
|
|
31 |
export const SELECTORS = {
|
|
|
32 |
columnList: '.qbank-column-list',
|
|
|
33 |
sortableColumn: '.qbank-sortable-column',
|
|
|
34 |
removeLink: '[data-action=remove]',
|
|
|
35 |
moveHandler: '[data-drag-type=move]',
|
|
|
36 |
addColumn: '.addcolumn',
|
|
|
37 |
addLink: '[data-action=add]',
|
|
|
38 |
actionLink: '.action-link',
|
|
|
39 |
};
|
|
|
40 |
|
|
|
41 |
/**
|
|
|
42 |
* Sets up sortable list in the column sort order page.
|
|
|
43 |
*
|
|
|
44 |
* @param {Element} listRoot Element containing the sortable list.
|
|
|
45 |
* @param {Boolean} vertical Is the list in vertical orientation, rather than horizonal?
|
|
|
46 |
* @param {Boolean} global Should changes be saved to global config, rather than user preferences?
|
|
|
47 |
* @return {jQuery} sortable column elements, for attaching additional event listeners.
|
|
|
48 |
*/
|
|
|
49 |
export const setupSortableLists = (listRoot, vertical = false, global = false) => {
|
|
|
50 |
const sortableList = new SortableList(listRoot, {
|
|
|
51 |
moveHandlerSelector: SELECTORS.moveHandler,
|
|
|
52 |
isHorizontal: !vertical,
|
|
|
53 |
});
|
|
|
54 |
sortableList.getElementName = element => Promise.resolve(element.data('name'));
|
|
|
55 |
|
|
|
56 |
const sortableColumns = $(SELECTORS.sortableColumn);
|
|
|
57 |
|
|
|
58 |
sortableColumns.on(SortableList.EVENTS.DROP, () => {
|
|
|
59 |
repository.setColumnbankOrder(getColumnOrder(listRoot), global).catch(Notification.exception);
|
|
|
60 |
listRoot.querySelectorAll(SELECTORS.sortableColumn).forEach(item => item.classList.remove('active'));
|
|
|
61 |
});
|
|
|
62 |
|
|
|
63 |
sortableColumns.on(SortableList.EVENTS.DRAGSTART, (event) => {
|
|
|
64 |
event.currentTarget.classList.add('active');
|
|
|
65 |
});
|
|
|
66 |
|
|
|
67 |
return sortableColumns;
|
|
|
68 |
};
|
|
|
69 |
|
|
|
70 |
/**
|
|
|
71 |
* Set up event handlers for action buttons.
|
|
|
72 |
*
|
|
|
73 |
* For each action, call the web service to update the appropriate setting or user preference, then call the fragment to
|
|
|
74 |
* refresh the view.
|
|
|
75 |
*
|
|
|
76 |
* @param {Element} uiRoot The root of the question bank UI.
|
|
|
77 |
* @param {Boolean} global Should changes be saved to global config, rather than user preferences?
|
|
|
78 |
*/
|
|
|
79 |
export const setupActionButtons = (uiRoot, global = false) => {
|
|
|
80 |
uiRoot.addEventListener('click', async(e) => {
|
|
|
81 |
const actionLink = e.target.closest(SELECTORS.actionLink);
|
|
|
82 |
if (!actionLink) {
|
|
|
83 |
return;
|
|
|
84 |
}
|
|
|
85 |
try {
|
|
|
86 |
e.preventDefault();
|
|
|
87 |
const action = actionLink.dataset.action;
|
|
|
88 |
if (action === 'add' || action === 'remove') {
|
|
|
89 |
const hiddenColumns = [];
|
|
|
90 |
const addColumnList = document.querySelector(SELECTORS.addColumn);
|
|
|
91 |
if (addColumnList) {
|
|
|
92 |
addColumnList.querySelectorAll(SELECTORS.addLink).forEach(item => {
|
|
|
93 |
if (action === 'add' && item === actionLink) {
|
|
|
94 |
return;
|
|
|
95 |
}
|
|
|
96 |
hiddenColumns.push(item.dataset.column);
|
|
|
97 |
});
|
|
|
98 |
}
|
|
|
99 |
if (action === 'remove') {
|
|
|
100 |
hiddenColumns.push(actionLink.dataset.column);
|
|
|
101 |
}
|
|
|
102 |
await repository.setHiddenColumns(hiddenColumns, global);
|
|
|
103 |
} else if (action === 'reset') {
|
|
|
104 |
await repository.resetColumns(global);
|
|
|
105 |
}
|
|
|
106 |
const actionUrl = new URL(actionLink.href);
|
|
|
107 |
const returnUrl = new URL(actionUrl.searchParams.get('returnurl').replaceAll('&', '&'));
|
|
|
108 |
await RefreshUi.refresh(uiRoot, returnUrl);
|
|
|
109 |
} catch (ex) {
|
|
|
110 |
await Notification.exception(ex);
|
|
|
111 |
}
|
|
|
112 |
});
|
|
|
113 |
};
|
|
|
114 |
|
|
|
115 |
/**
|
|
|
116 |
* Gets the newly reordered columns to display in the question bank view.
|
|
|
117 |
* @param {Element} listRoot
|
|
|
118 |
* @returns {Array}
|
|
|
119 |
*/
|
|
|
120 |
export const getColumnOrder = listRoot => {
|
|
|
121 |
const columns = Array.from(listRoot.querySelectorAll('[data-columnid]'))
|
|
|
122 |
.map(column => column.dataset.columnid);
|
|
|
123 |
|
|
|
124 |
return columns.filter((value, index) => columns.indexOf(value) === index);
|
|
|
125 |
};
|