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
 * Handle clicking on action links of the feedback alert.
18
 *
19
 * @module     core/userfeedback
20
 * @copyright  2020 Shamim Rezaie <shamim@moodle.com>
21
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
22
 */
23
 
24
import Ajax from 'core/ajax';
25
import Notification from 'core/notification';
26
 
27
const Selectors = {
28
    regions: {
29
        root: '[data-region="core/userfeedback"]',
30
    },
31
    actions: {},
32
};
33
Selectors.actions.give = `${Selectors.regions.root} [data-action="give"]`;
34
Selectors.actions.remind = `${Selectors.regions.root} [data-action="remind"]`;
35
 
36
/**
37
 * Attach the necessary event handlers to the action links
38
 */
39
export const registerEventListeners = () => {
40
    document.addEventListener('click', e => {
41
        const giveAction = e.target.closest(Selectors.actions.give);
42
        if (giveAction) {
43
            e.preventDefault();
44
 
45
            if (!window.open(giveAction.href)) {
46
                throw new Error('Unable to open popup');
47
            }
48
 
49
            Promise.resolve(giveAction)
50
                .then(hideRoot)
51
                .then(recordAction)
52
                .catch(Notification.exception);
53
        }
54
 
55
        const remindAction = e.target.closest(Selectors.actions.remind);
56
        if (remindAction) {
57
            e.preventDefault();
58
 
59
            Promise.resolve(remindAction)
60
                .then(hideRoot)
61
                .then(recordAction)
62
                .catch(Notification.exception);
63
        }
64
    });
65
};
66
 
67
/**
68
 * Record the action that the user took.
69
 *
70
 * @param {HTMLElement} clickedItem The action element that the user chose.
71
 * @returns {Promise}
72
 */
73
const recordAction = clickedItem => {
74
    if (clickedItem.dataset.record) {
75
        return Ajax.call([{
76
            methodname: 'core_create_userfeedback_action_record',
77
            args: {
78
                action: clickedItem.dataset.action,
79
                contextid: M.cfg.contextid,
80
            }
81
        }])[0];
82
    }
83
 
84
    return Promise.resolve();
85
};
86
 
87
/**
88
 * Hide the root node of the CTA notification.
89
 *
90
 * @param {HTMLElement} clickedItem The action element that the user chose.
91
 * @returns {HTMLElement}
92
 */
93
const hideRoot = clickedItem => {
94
    if (clickedItem.dataset.hide) {
95
        clickedItem.closest(Selectors.regions.root).remove();
96
    }
97
 
98
    return clickedItem;
99
};