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
 * A module to handle Delete/Update operations of the manage subscription page.
18
 *
19
 * @module core_calendar/manage_subscriptions
20
 * @copyright 2021 Huong Nguyen <huongnv13@gmail.com>
21
 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
22
 * @since 4.0
23
 */
24
 
25
import * as CalendarSelectors from 'core_calendar/selectors';
26
import * as CalendarRepository from 'core_calendar/repository';
27
import ModalSaveCancel from 'core/modal_save_cancel';
28
import * as ModalEvents from 'core/modal_events';
29
import {exception as displayException, addNotification, fetchNotifications} from 'core/notification';
30
import Prefetch from 'core/prefetch';
31
import {getString} from 'core/str';
32
import {eventTypes} from 'core/local/inplace_editable/events';
33
 
34
/**
35
 * Get subscription id for given element.
36
 *
37
 * @param {HTMLElement} element update/delete link
38
 * @return {Number}
39
 */
40
const getSubscriptionId = element => {
41
    return parseInt(element.closest('tr').dataset.subid);
42
};
43
 
44
/**
45
 * Get subscription name for given element.
46
 *
47
 * @param {HTMLElement} element update/delete link
48
 * @return {String}
49
 */
50
const getSubscriptionName = element => {
51
    return element.closest('tr').dataset.subname;
52
};
53
 
54
/**
55
 * Get subscription table row for subscription id.
56
 *
57
 * @param {string} subscriptionId Subscription id
58
 * @return {Element}
59
 */
60
const getSubscriptionRow = subscriptionId => {
61
    return document.querySelector(`tr[data-subid="${subscriptionId}"]`);
62
};
63
 
64
/**
65
 * Create modal.
66
 *
67
 * @param {HTMLElement} element
68
 * @param {string} messageCode Message code.
69
 * @return {promise} Promise for modal
70
 */
71
const createModal = (element, messageCode) => {
72
    const subscriptionName = getSubscriptionName(element);
73
    return ModalSaveCancel.create({
74
        title: getString('confirmation', 'admin'),
75
        body: getString(messageCode, 'calendar', subscriptionName),
76
        buttons: {
77
            save: getString('yes')
78
        },
79
    }).then(modal => {
80
        modal.getRoot().on(ModalEvents.hidden, () => {
81
            element.focus();
82
        });
83
        modal.show();
84
        return modal;
85
    });
86
};
87
 
88
/**
89
 * Response handler for delete action.
90
 *
91
 * @param {HTMLElement} element
92
 * @param {Object} data
93
 * @return {Promise}
94
 */
95
const responseHandlerForDelete = async(element, data) => {
96
    const subscriptionName = getSubscriptionName(element);
97
    const message = data.status ? await getString('subscriptionremoved', 'calendar', subscriptionName) : data.warnings[0].message;
98
    const type = data.status ? 'info' : 'error';
99
    return addNotification({message, type});
100
};
101
 
102
/**
103
 * Register events for update/delete links.
104
 */
105
const registerEventListeners = () => {
106
    document.addEventListener('click', e => {
107
        const deleteAction = e.target.closest(CalendarSelectors.actions.deleteSubscription);
108
        if (deleteAction) {
109
            e.preventDefault();
110
            const modalPromise = createModal(deleteAction, 'confirmsubscriptiondelete');
111
            modalPromise.then(modal => {
112
                modal.getRoot().on(ModalEvents.save, () => {
113
                    const subscriptionId = getSubscriptionId(deleteAction);
114
                    CalendarRepository.deleteSubscription(subscriptionId).then(data => {
115
                        const response = responseHandlerForDelete(deleteAction, data);
116
                        return response.then(() => {
117
                            const subscriptionRow = getSubscriptionRow(subscriptionId);
118
                            return subscriptionRow.remove();
119
                        });
120
                    }).catch(displayException);
121
                });
122
 
123
                return modal;
124
            }).catch(displayException);
125
        }
126
    });
127
 
128
    document.addEventListener(eventTypes.elementUpdated, e => {
129
        const inplaceEditable = e.target;
130
        if (inplaceEditable.getAttribute('data-component') == 'core_calendar') {
131
            fetchNotifications();
132
        }
133
    });
134
};
135
 
136
/**
137
 * Initialises.
138
 */
139
export const init = () => {
140
    Prefetch.prefetchStrings('moodle', ['yes']);
141
    Prefetch.prefetchStrings('core_admin', ['confirmation']);
142
    Prefetch.prefetchStrings('core_calendar', ['confirmsubscriptiondelete', 'subscriptionremoved']);
143
    registerEventListeners();
144
};