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
21 * @copyright 2018 Ryan Wyllie <ryan@moodle.com>
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
30 'core_message/message_repository',
31 'core_message/message_drawer_events',
32 'core_message/message_drawer_lazy_load_list'
45 CONTACT_REQUEST: '[data-region="contact-request"]'
49 REQUESTS_LIST: 'core_message/message_drawer_view_contacts_body_section_requests_list'
53 * Render the requests in the content container.
55 * @param {Object} contentContainer List container element.
56 * @param {Array} requests List of requests.
57 * @return {Object} jQuery promise
59 var render = function(contentContainer, requests) {
60 var formattedRequests = requests.map(function(request) {
62 // This is actually the user id.
64 profileimageurl: request.profileimageurl,
65 fullname: request.fullname
68 return Templates.render(TEMPLATES.REQUESTS_LIST, {requests: formattedRequests})
69 .then(function(html) {
70 contentContainer.append(html);
73 .catch(Notification.exception);
77 * Load the user contacts and call the renderer.
79 * @param {Object} listRoot The lazy loaded list root element
80 * @param {Integer} userId The logged in user id.
81 * @return {Object} jQuery promise
83 var load = function(listRoot, userId) {
84 return MessageRepository.getContactRequests(userId)
85 .then(function(requests) {
86 LazyLoadList.setLoadedAll(listRoot, true);
89 .catch(Notification.exception);
93 * Handle when a contact request is accepted or declined by removing the contact
96 * @param {Object} root The section root element
97 * @return {Function} The event handler function
99 var handleContactRequestProcessed = function(root) {
100 return function(request) {
101 root.find('[data-request-id="' + request.userid + '"]').remove();
102 var contactRequests = root.find(SELECTORS.CONTACT_REQUEST);
104 if (!contactRequests.length) {
105 LazyLoadList.showEmptyMessage(root);
106 LazyLoadList.hideContent(root);
112 * Listen for any events that might affect the requests section.
114 * @param {Object} root The section root element
116 var registerEventListeners = function(root) {
117 PubSub.subscribe(MessageDrawerEvents.CONTACT_REQUEST_ACCEPTED, handleContactRequestProcessed(root));
118 PubSub.subscribe(MessageDrawerEvents.CONTACT_REQUEST_DECLINED, handleContactRequestProcessed(root));
122 * Setup the requests section.
124 * @param {Object} root Requests section container.
126 var show = function(root) {
127 if (!root.attr('data-contacts-init')) {
128 registerEventListeners(root);
129 root.attr('data-contacts-init', true);
132 // The root element is already the lazy loaded list root.
133 LazyLoadList.show(root, load, render);