Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
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
 * AMD module for categories actions.
18
 *
19
 * @module     tool_dataprivacy/categoriesactions
20
 * @copyright  2018 David Monllao
21
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
22
 */
23
import * as Ajax from 'core/ajax';
24
import * as Notification from 'core/notification';
25
import * as Str from 'core/str';
26
import ModalEvents from 'core/modal_events';
27
import ModalSaveCancel from 'core/modal_save_cancel';
28
 
29
/**
30
 * List of action selectors.
31
 *
32
 * @type {{DELETE: string}}
33
 */
34
const ACTIONS = {
35
    DELETE: '[data-action="deletecategory"]',
36
};
37
 
38
export default class CategoriesActions {
39
 
40
    static init() {
41
        return new this();
42
    }
43
 
44
    /**
45
     * CategoriesActions class.
46
     */
47
    constructor() {
48
        this.registerEvents();
49
    }
50
 
51
    deleteCategory(id) {
52
        return Ajax.call([{
53
            methodname: 'tool_dataprivacy_delete_category',
54
            args: {id}
55
        }])[0];
56
    }
57
 
58
    handleCategoryRemoval(id) {
59
        this.deleteCategory(id)
60
            .then((data) => {
61
                if (data.result) {
62
                    document.querySelector(`tr[data-categoryid="${id}"]`)?.remove();
63
                } else {
64
                    Notification.addNotification({
65
                        message: data.warnings[0].message,
66
                        type: 'error'
67
                    });
68
                }
69
 
70
                return;
71
            })
72
            .catch(Notification.exception);
73
 
74
    }
75
 
76
    /**
77
     * Register event listeners.
78
     */
79
    registerEvents() {
80
        document.addEventListener('click', (e) => {
81
            const target = e.target.closest(ACTIONS.DELETE);
82
            if (!target) {
83
                return;
84
            }
85
 
86
            e.preventDefault();
87
 
88
            this.confirmCategoryRemoval(target);
89
        });
90
    }
91
 
92
    confirmCategoryRemoval(target) {
93
        const id = target.dataset.id;
94
        var categoryname = target.dataset.name;
95
        var stringkeys = [
96
            {
97
                key: 'deletecategory',
98
                component: 'tool_dataprivacy'
99
            },
100
            {
101
                key: 'deletecategorytext',
102
                component: 'tool_dataprivacy',
103
                param: categoryname
104
            },
105
            {
106
                key: 'delete'
107
            }
108
        ];
109
 
110
        Str.get_strings(stringkeys).then(([
111
            title,
112
            body,
113
            save,
114
        ]) => ModalSaveCancel.create({
115
            title,
116
            body,
117
            buttons: {
118
                save,
119
            },
120
            show: true,
121
            removeOnClose: true,
122
        }))
123
        .then((modal) => {
124
            // Handle save event.
125
            modal.getRoot().on(ModalEvents.save, () => this.handleCategoryRemoval(id));
126
 
127
            return modal;
128
        })
129
        .catch(Notification.exception);
130
    }
131
}