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 |
* Question bank UI refresh utility
|
|
|
18 |
*
|
|
|
19 |
* @module core_question/refresh_ui
|
|
|
20 |
* @copyright 2023 Catalyst IT Europe Ltd.
|
|
|
21 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
22 |
*/
|
|
|
23 |
|
|
|
24 |
import Fragment from 'core/fragment';
|
|
|
25 |
import Templates from 'core/templates';
|
|
|
26 |
|
|
|
27 |
export default {
|
|
|
28 |
/**
|
|
|
29 |
* Reload the question bank UI, retaining the current filters and sort data.
|
|
|
30 |
*
|
|
|
31 |
* @param {Element} uiRoot The root element of the UI to be refreshed. Must contain "component", "callback" and "contextid" in
|
|
|
32 |
* its data attributes, to be passed to the Fragment API.
|
|
|
33 |
* @param {URL} returnUrl The url of the current page, containing filter and sort parameters.
|
|
|
34 |
* @return {Promise} Resolved when the refresh is complete.
|
|
|
35 |
*/
|
|
|
36 |
refresh: (uiRoot, returnUrl) => {
|
|
|
37 |
return new Promise((resolve, reject) => {
|
|
|
38 |
const fragmentData = uiRoot.dataset;
|
|
|
39 |
const viewData = {};
|
|
|
40 |
const sortData = {};
|
|
|
41 |
if (returnUrl) {
|
|
|
42 |
returnUrl.searchParams.forEach((value, key) => {
|
|
|
43 |
// Match keys like 'sortdata[fieldname]' and convert them to an array,
|
|
|
44 |
// because the fragment API doesn't like non-alphanum argument keys.
|
|
|
45 |
const sortItem = key.match(/sortdata\[([^\]]+)\]/);
|
|
|
46 |
if (sortItem) {
|
|
|
47 |
// The item returned by sortItem.pop() is the contents of the matching group, the field name.
|
|
|
48 |
sortData[sortItem.pop()] = value;
|
|
|
49 |
} else {
|
|
|
50 |
viewData[key] = value;
|
|
|
51 |
}
|
|
|
52 |
});
|
|
|
53 |
}
|
|
|
54 |
viewData.sortdata = JSON.stringify(sortData);
|
|
|
55 |
// We have to use then() there, as loadFragment doesn't appear to work with await.
|
|
|
56 |
Fragment.loadFragment(fragmentData.component, fragmentData.callback, fragmentData.contextid, viewData)
|
|
|
57 |
.then((html, js) => {
|
|
|
58 |
Templates.replaceNode(uiRoot, html, js);
|
|
|
59 |
resolve();
|
|
|
60 |
return html;
|
|
|
61 |
})
|
|
|
62 |
.catch(reject);
|
|
|
63 |
});
|
|
|
64 |
}
|
|
|
65 |
};
|