message/amd/src/message_drawer_view_contacts_section_requests.js

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