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
 * Controls the requests section of the contacts page.
18
 *
19
 * @module     core_message/message_drawer_view_contacts_section_requests
20
 * @copyright  2018 Ryan Wyllie <ryan@moodle.com>
21
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
22
 */
23
define(
24
[
25
    'jquery',
26
    'core/notification',
27
    'core/pubsub',
28
    'core/templates',
29
    'core_message/message_repository',
30
    'core_message/message_drawer_events',
31
    'core_message/message_drawer_lazy_load_list'
32
],
33
function(
34
    $,
35
    Notification,
36
    PubSub,
37
    Templates,
38
    MessageRepository,
39
    MessageDrawerEvents,
40
    LazyLoadList
41
) {
42
 
43
    var SELECTORS = {
44
        CONTACT_REQUEST: '[data-region="contact-request"]'
45
    };
46
 
47
    var TEMPLATES = {
48
        REQUESTS_LIST: 'core_message/message_drawer_view_contacts_body_section_requests_list'
49
    };
50
 
51
    /**
52
     * Render the requests in the content container.
53
     *
54
     * @param {Object} contentContainer List container element.
55
     * @param {Array} requests List of requests.
56
     * @return {Object} jQuery promise
57
     */
58
    var render = function(contentContainer, requests) {
59
        var formattedRequests = requests.map(function(request) {
60
            return {
61
                // This is actually the user id.
62
                id: request.id,
63
                profileimageurl: request.profileimageurl,
64
                fullname: request.fullname
65
            };
66
        });
67
        return Templates.render(TEMPLATES.REQUESTS_LIST, {requests: formattedRequests})
68
            .then(function(html) {
69
                contentContainer.append(html);
70
                return html;
71
            })
72
            .catch(Notification.exception);
73
    };
74
 
75
    /**
76
     * Load the user contacts and call the renderer.
77
     *
78
     * @param {Object} listRoot The lazy loaded list root element
79
     * @param {Integer} userId The logged in user id.
80
     * @return {Object} jQuery promise
81
     */
82
    var load = function(listRoot, userId) {
83
        return MessageRepository.getContactRequests(userId)
84
            .then(function(requests) {
85
                LazyLoadList.setLoadedAll(listRoot, true);
86
                return requests;
87
            })
88
            .catch(Notification.exception);
89
    };
90
 
91
    /**
92
     * Handle when a contact request is accepted or declined by removing the contact
93
     * list from the page.
94
     *
95
     * @param {Object} root The section root element
96
     * @return {Function} The event handler function
97
     */
98
    var handleContactRequestProcessed = function(root) {
99
        return function(request) {
100
            root.find('[data-request-id="' + request.userid + '"]').remove();
101
            var contactRequests = root.find(SELECTORS.CONTACT_REQUEST);
102
 
103
            if (!contactRequests.length) {
104
                LazyLoadList.showEmptyMessage(root);
105
                LazyLoadList.hideContent(root);
106
            }
107
        };
108
    };
109
 
110
    /**
111
     * Listen for any events that might affect the requests section.
112
     *
113
     * @param {Object} root The section root element
114
     */
115
    var registerEventListeners = function(root) {
116
        PubSub.subscribe(MessageDrawerEvents.CONTACT_REQUEST_ACCEPTED, handleContactRequestProcessed(root));
117
        PubSub.subscribe(MessageDrawerEvents.CONTACT_REQUEST_DECLINED, handleContactRequestProcessed(root));
118
    };
119
 
120
    /**
121
     * Setup the requests section.
122
     *
123
     * @param {Object} root Requests section container.
124
     */
125
    var show = function(root) {
126
        if (!root.attr('data-contacts-init')) {
127
            registerEventListeners(root);
128
            root.attr('data-contacts-init', true);
129
        }
130
 
131
        // The root element is already the lazy loaded list root.
132
        LazyLoadList.show(root, load, render);
133
    };
134
 
135
    return {
136
        show: show,
137
    };
138
});