message/amd/src/message_drawer_view_group_info.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 group info page of the message drawer.
  17. *
  18. * @module core_message/message_drawer_view_group_info
  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/str',
  26. 'core/templates',
  27. 'core_message/message_repository',
  28. 'core_message/message_drawer_lazy_load_list',
  29. ],
  30. function(
  31. $,
  32. Str,
  33. Templates,
  34. Repository,
  35. LazyLoadList
  36. ) {
  37. var LOAD_MEMBERS_LIMIT = 50;
  38. var SELECTORS = {
  39. CONTENT_CONTAINER: '[data-region="group-info-content-container"]',
  40. MEMBERS_LIST: '[data-region="members-list"]',
  41. };
  42. var TEMPLATES = {
  43. CONTENT: 'core_message/message_drawer_view_group_info_body_content',
  44. MEMBERS_LIST: 'core_message/message_drawer_view_group_info_participants_list'
  45. };
  46. /**
  47. * Get the content container of the group info view container.
  48. *
  49. * @param {Object} root Contact container element.
  50. * @return {Object} jQuery object
  51. */
  52. var getContentContainer = function(root) {
  53. return root.find(SELECTORS.CONTENT_CONTAINER);
  54. };
  55. /**
  56. * Render the group info page.
  57. *
  58. * @param {Object} root Container element.
  59. * @param {Object} conversation The group conversation.
  60. * @param {Number} loggedInUserId The logged in user's id.
  61. * @return {Object} jQuery promise
  62. */
  63. var render = function(root, conversation, loggedInUserId) {
  64. var placeholderCount = conversation.totalMemberCount > 50 ? 50 : conversation.totalMemberCount;
  65. var placeholders = Array.apply(null, Array(placeholderCount)).map(function() {
  66. return true;
  67. });
  68. var templateContext = {
  69. name: conversation.name,
  70. subname: conversation.subname,
  71. imageurl: conversation.imageUrl,
  72. placeholders: placeholders,
  73. loggedinuser: {
  74. id: loggedInUserId
  75. }
  76. };
  77. return Templates.render(TEMPLATES.CONTENT, templateContext)
  78. .then(function(html) {
  79. getContentContainer(root).append(html);
  80. return html;
  81. });
  82. };
  83. /**
  84. * Get the callback to load members of the conversation.
  85. *
  86. * @param {Object} conversation The conversation
  87. * @param {Number} limit How many members to load
  88. * @param {Number} offset How many memebers to skip
  89. * @return {Function} the callback.
  90. */
  91. var getLoadMembersCallback = function(conversation, limit, offset) {
  92. return function(root, userId) {
  93. return Repository.getConversationMembers(conversation.id, userId, limit + 1, offset)
  94. .then(function(members) {
  95. if (members.length > limit) {
  96. members = members.slice(0, -1);
  97. } else {
  98. LazyLoadList.setLoadedAll(root, true);
  99. }
  100. offset = offset + limit;
  101. // Filter out the logged in user so that they don't appear in the list.
  102. return members.filter(function(member) {
  103. return member.id != userId;
  104. });
  105. });
  106. };
  107. };
  108. /**
  109. * Function to render the members in the list.
  110. *
  111. * @param {Object} contentContainer The list content container.
  112. * @param {Array} members The list of members to render
  113. * @return {Object} jQuery promise
  114. */
  115. var renderMembersCallback = function(contentContainer, members) {
  116. return Templates.render(TEMPLATES.MEMBERS_LIST, {contacts: members})
  117. .then(function(html) {
  118. contentContainer.append(html);
  119. return html;
  120. });
  121. };
  122. /**
  123. * Setup the contact page.
  124. *
  125. * @param {string} namespace The route namespace.
  126. * @param {Object} header Contact header container element.
  127. * @param {Object} body Contact body container element.
  128. * @param {Object} footer Contact body container element.
  129. * @param {Number} conversation The conversation
  130. * @param {Number} loggedInUserId The logged in user id
  131. * @return {Object} jQuery promise
  132. */
  133. var show = function(namespace, header, body, footer, conversation, loggedInUserId) {
  134. var root = $(body);
  135. getContentContainer(root).empty();
  136. return render(root, conversation, loggedInUserId)
  137. .then(function() {
  138. var listRoot = LazyLoadList.getRoot(root);
  139. LazyLoadList.show(
  140. listRoot,
  141. getLoadMembersCallback(conversation, LOAD_MEMBERS_LIMIT, 0),
  142. renderMembersCallback
  143. );
  144. return;
  145. });
  146. };
  147. /**
  148. * String describing this page used for aria-labels.
  149. *
  150. * @param {Object} root Contact container element.
  151. * @param {Number} conversation The conversation
  152. * @return {Object} jQuery promise
  153. */
  154. var description = function(root, conversation) {
  155. return Str.get_string('messagedrawerviewgroupinfo', 'core_message', conversation.name);
  156. };
  157. return {
  158. show: show,
  159. description: description
  160. };
  161. });