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 |
* Comments admin management
|
|
|
18 |
*
|
|
|
19 |
* @module core_comment/admin
|
|
|
20 |
* @copyright 2022 Paul Holden <paulh@moodle.com>
|
|
|
21 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
22 |
*/
|
|
|
23 |
|
|
|
24 |
"use strict";
|
|
|
25 |
|
|
|
26 |
import {dispatchEvent} from 'core/event_dispatcher';
|
|
|
27 |
import Notification from 'core/notification';
|
|
|
28 |
import Pending from 'core/pending';
|
|
|
29 |
import {prefetchStrings} from 'core/prefetch';
|
|
|
30 |
import {getString} from 'core/str';
|
|
|
31 |
import {deleteComment, deleteComments} from 'core_comment/repository';
|
|
|
32 |
import * as reportEvents from 'core_reportbuilder/local/events';
|
|
|
33 |
import * as reportSelectors from 'core_reportbuilder/local/selectors';
|
|
|
34 |
|
|
|
35 |
const Selectors = {
|
|
|
36 |
commentDelete: '[data-action="comment-delete"]',
|
|
|
37 |
commentDeleteChecked: '[data-togglegroup="report-select-all"][data-toggle="slave"]:checked',
|
|
|
38 |
commentDeleteSelected: '[data-action="comment-delete-selected"]',
|
|
|
39 |
};
|
|
|
40 |
|
|
|
41 |
/**
|
|
|
42 |
* Initialise module
|
|
|
43 |
*/
|
|
|
44 |
export const init = () => {
|
|
|
45 |
prefetchStrings('core_admin', [
|
|
|
46 |
'confirmdeletecomments',
|
|
|
47 |
]);
|
|
|
48 |
|
|
|
49 |
prefetchStrings('core', [
|
|
|
50 |
'delete',
|
|
|
51 |
'deleteselected'
|
|
|
52 |
]);
|
|
|
53 |
|
|
|
54 |
document.addEventListener('click', event => {
|
|
|
55 |
const commentDelete = event.target.closest(Selectors.commentDelete);
|
|
|
56 |
if (commentDelete) {
|
|
|
57 |
event.preventDefault();
|
|
|
58 |
|
|
|
59 |
// Use triggerElement to return focus to the action menu toggle.
|
|
|
60 |
const triggerElement = commentDelete.closest('.dropdown').querySelector('.dropdown-toggle');
|
|
|
61 |
Notification.saveCancelPromise(
|
|
|
62 |
getString('delete', 'core'),
|
|
|
63 |
getString('confirmdeletecomments', 'core_admin'),
|
|
|
64 |
getString('delete', 'core'),
|
|
|
65 |
{triggerElement}
|
|
|
66 |
).then(() => {
|
|
|
67 |
const pendingPromise = new Pending('core_comment/comment:delete');
|
|
|
68 |
const reportElement = event.target.closest(reportSelectors.regions.report);
|
|
|
69 |
|
|
|
70 |
return deleteComment(commentDelete.dataset.commentId)
|
|
|
71 |
.then(() => {
|
|
|
72 |
dispatchEvent(reportEvents.tableReload, {preservePagination: true}, reportElement);
|
|
|
73 |
return pendingPromise.resolve();
|
|
|
74 |
})
|
|
|
75 |
.catch(Notification.exception);
|
|
|
76 |
}).catch(() => {
|
|
|
77 |
return;
|
|
|
78 |
});
|
|
|
79 |
}
|
|
|
80 |
|
|
|
81 |
const commentDeleteSelected = event.target.closest(Selectors.commentDeleteSelected);
|
|
|
82 |
if (commentDeleteSelected) {
|
|
|
83 |
event.preventDefault();
|
|
|
84 |
|
|
|
85 |
const reportElement = document.querySelector(reportSelectors.regions.report);
|
|
|
86 |
const commentDeleteChecked = reportElement.querySelectorAll(Selectors.commentDeleteChecked);
|
|
|
87 |
if (commentDeleteChecked.length === 0) {
|
|
|
88 |
return;
|
|
|
89 |
}
|
|
|
90 |
|
|
|
91 |
Notification.saveCancelPromise(
|
|
|
92 |
getString('deleteselected', 'core'),
|
|
|
93 |
getString('confirmdeletecomments', 'core_admin'),
|
|
|
94 |
getString('delete', 'core'),
|
|
|
95 |
{triggerElement: commentDeleteSelected}
|
|
|
96 |
).then(() => {
|
|
|
97 |
const pendingPromise = new Pending('core_comment/comments:delete');
|
|
|
98 |
const deleteCommentIds = [...commentDeleteChecked].map(check => check.value);
|
|
|
99 |
|
|
|
100 |
return deleteComments(deleteCommentIds)
|
|
|
101 |
.then(() => {
|
|
|
102 |
dispatchEvent(reportEvents.tableReload, {preservePagination: true}, reportElement);
|
|
|
103 |
return pendingPromise.resolve();
|
|
|
104 |
})
|
|
|
105 |
.catch(Notification.exception);
|
|
|
106 |
}).catch(() => {
|
|
|
107 |
return;
|
|
|
108 |
});
|
|
|
109 |
}
|
|
|
110 |
});
|
|
|
111 |
};
|