1 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
17 * Controls the requests section of the contacts page.
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
29 'core_message/message_repository',
30 'core_message/message_drawer_events',
31 'core_message/message_drawer_lazy_load_list'
44 CONTACT_REQUEST: '[data-region="contact-request"]'
48 REQUESTS_LIST: 'core_message/message_drawer_view_contacts_body_section_requests_list'
52 * Render the requests in the content container.
54 * @param {Object} contentContainer List container element.
55 * @param {Array} requests List of requests.
56 * @return {Object} jQuery promise
58 var render = function(contentContainer, requests) {
59 var formattedRequests = requests.map(function(request) {
61 // This is actually the user id.
63 profileimageurl: request.profileimageurl,
64 fullname: request.fullname
67 return Templates.render(TEMPLATES.REQUESTS_LIST, {requests: formattedRequests})
68 .then(function(html) {
69 contentContainer.append(html);
72 .catch(Notification.exception);
76 * Load the user contacts and call the renderer.
78 * @param {Object} listRoot The lazy loaded list root element
79 * @param {Integer} userId The logged in user id.
80 * @return {Object} jQuery promise
82 var load = function(listRoot, userId) {
83 return MessageRepository.getContactRequests(userId)
84 .then(function(requests) {
85 LazyLoadList.setLoadedAll(listRoot, true);
88 .catch(Notification.exception);
92 * Handle when a contact request is accepted or declined by removing the contact
95 * @param {Object} root The section root element
96 * @return {Function} The event handler function
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);
103 if (!contactRequests.length) {
104 LazyLoadList.showEmptyMessage(root);
105 LazyLoadList.hideContent(root);
111 * Listen for any events that might affect the requests section.
113 * @param {Object} root The section root element
115 var registerEventListeners = function(root) {
116 PubSub.subscribe(MessageDrawerEvents.CONTACT_REQUEST_ACCEPTED, handleContactRequestProcessed(root));
117 PubSub.subscribe(MessageDrawerEvents.CONTACT_REQUEST_DECLINED, handleContactRequestProcessed(root));
121 * Setup the requests section.
123 * @param {Object} root Requests section container.
125 var show = function(root) {
126 if (!root.attr('data-contacts-init')) {
127 registerEventListeners(root);
128 root.attr('data-contacts-init', true);
131 // The root element is already the lazy loaded list root.
132 LazyLoadList.show(root, load, render);