| 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 contacts section of the contacts page.
 | 
        
           |  |  | 18 |  *
 | 
        
           |  |  | 19 |  * @module     core_message/message_drawer_view_contacts_section_contacts
 | 
        
           |  |  | 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 |     Events,
 | 
        
           |  |  | 40 |     LazyLoadList
 | 
        
           |  |  | 41 | ) {
 | 
        
           |  |  | 42 |   | 
        
           |  |  | 43 |     var limit = 100;
 | 
        
           |  |  | 44 |     var initialOffset = 0;
 | 
        
           |  |  | 45 |   | 
        
           |  |  | 46 |     var SELECTORS = {
 | 
        
           |  |  | 47 |         BLOCK_ICON_CONTAINER: '[data-region="block-icon-container"]',
 | 
        
           |  |  | 48 |         CONTACT: '[data-region="contact"]',
 | 
        
           |  |  | 49 |         CONTENT_CONTAINER: '[data-region="contacts-content-container"]'
 | 
        
           |  |  | 50 |     };
 | 
        
           |  |  | 51 |   | 
        
           |  |  | 52 |     var TEMPLATES = {
 | 
        
           |  |  | 53 |         CONTACTS_LIST: 'core_message/message_drawer_contacts_list'
 | 
        
           |  |  | 54 |     };
 | 
        
           |  |  | 55 |   | 
        
           |  |  | 56 |     /**
 | 
        
           |  |  | 57 |      * Find a contact element.
 | 
        
           |  |  | 58 |      *
 | 
        
           |  |  | 59 |      * @param {Object} body Contacts body container element.
 | 
        
           |  |  | 60 |      * @param {Number} userId User id of contact.
 | 
        
           |  |  | 61 |      * @return {Object} contact element.
 | 
        
           |  |  | 62 |      */
 | 
        
           |  |  | 63 |     var findContact = function(body, userId) {
 | 
        
           |  |  | 64 |         return body.find('[data-contact-user-id="' + userId + '"]');
 | 
        
           |  |  | 65 |     };
 | 
        
           |  |  | 66 |   | 
        
           |  |  | 67 |     /**
 | 
        
           |  |  | 68 |      * Render the contacts in the content container.
 | 
        
           |  |  | 69 |      *
 | 
        
           |  |  | 70 |      * @param {Object} contentContainer Content container element.
 | 
        
           |  |  | 71 |      * @param {Array} contacts List of contacts.
 | 
        
           |  |  | 72 |      * @return {Object} jQuery promise
 | 
        
           |  |  | 73 |      */
 | 
        
           |  |  | 74 |     var render = function(contentContainer, contacts) {
 | 
        
           |  |  | 75 |         var formattedContacts = contacts.map(function(contact) {
 | 
        
           |  |  | 76 |             return $.extend(contact, {id: contact.userid});
 | 
        
           |  |  | 77 |         });
 | 
        
           |  |  | 78 |         return Templates.render(TEMPLATES.CONTACTS_LIST, {contacts: formattedContacts})
 | 
        
           |  |  | 79 |             .then(function(html) {
 | 
        
           |  |  | 80 |                 contentContainer.append(html);
 | 
        
           |  |  | 81 |                 return html;
 | 
        
           |  |  | 82 |             })
 | 
        
           |  |  | 83 |             .catch(Notification.exception);
 | 
        
           |  |  | 84 |     };
 | 
        
           |  |  | 85 |   | 
        
           |  |  | 86 |     /**
 | 
        
           |  |  | 87 |      * Load the user contacts and call the renderer.
 | 
        
           |  |  | 88 |      *
 | 
        
           |  |  | 89 |      * @param {Number} offset The offset to use for loading contacts
 | 
        
           |  |  | 90 |      * @return {Function} the callback.
 | 
        
           |  |  | 91 |      */
 | 
        
           |  |  | 92 |     var getLoadFunction = function(offset) {
 | 
        
           |  |  | 93 |         return function(listRoot, userId) {
 | 
        
           |  |  | 94 |             return MessageRepository.getContacts(userId, (limit + 1), offset)
 | 
        
           |  |  | 95 |                 .then(function(result) {
 | 
        
           |  |  | 96 |                     return result;
 | 
        
           |  |  | 97 |                 })
 | 
        
           |  |  | 98 |                 .then(function(contacts) {
 | 
        
           |  |  | 99 |                     if (contacts.length > limit) {
 | 
        
           |  |  | 100 |                         contacts.pop();
 | 
        
           |  |  | 101 |                     } else {
 | 
        
           |  |  | 102 |                         LazyLoadList.setLoadedAll(listRoot, true);
 | 
        
           |  |  | 103 |                     }
 | 
        
           |  |  | 104 |                     return contacts;
 | 
        
           |  |  | 105 |                 })
 | 
        
           |  |  | 106 |                 .then(function(contacts) {
 | 
        
           |  |  | 107 |                     offset = offset + limit;
 | 
        
           |  |  | 108 |                     return contacts;
 | 
        
           |  |  | 109 |                 })
 | 
        
           |  |  | 110 |                 .catch(Notification.exception);
 | 
        
           |  |  | 111 |         };
 | 
        
           |  |  | 112 |     };
 | 
        
           |  |  | 113 |   | 
        
           |  |  | 114 |     /**
 | 
        
           |  |  | 115 |      * Remove contact from view.
 | 
        
           |  |  | 116 |      *
 | 
        
           |  |  | 117 |      * @param {Object} body Contacts body container element.
 | 
        
           |  |  | 118 |      * @param {Number} userId Contact userid.
 | 
        
           |  |  | 119 |      */
 | 
        
           |  |  | 120 |     var removeContact = function(body, userId) {
 | 
        
           |  |  | 121 |         findContact(body, userId).remove();
 | 
        
           |  |  | 122 |     };
 | 
        
           |  |  | 123 |   | 
        
           |  |  | 124 |     /**
 | 
        
           |  |  | 125 |      * Show the contact has been blocked.
 | 
        
           |  |  | 126 |      *
 | 
        
           |  |  | 127 |      * @param {Object} body Contacts body container element.
 | 
        
           |  |  | 128 |      * @param {Number} userId Contact userid.
 | 
        
           |  |  | 129 |      */
 | 
        
           |  |  | 130 |     var showContactBlocked = function(body, userId) {
 | 
        
           |  |  | 131 |         var contact = findContact(body, userId);
 | 
        
           |  |  | 132 |         if (contact.length) {
 | 
        
           |  |  | 133 |             contact.find(SELECTORS.BLOCK_ICON_CONTAINER).removeClass('hidden');
 | 
        
           |  |  | 134 |         }
 | 
        
           |  |  | 135 |     };
 | 
        
           |  |  | 136 |   | 
        
           |  |  | 137 |     /**
 | 
        
           |  |  | 138 |      * Show the contact has been unblocked.
 | 
        
           |  |  | 139 |      *
 | 
        
           |  |  | 140 |      * @param {Object} body Contacts body container element.
 | 
        
           |  |  | 141 |      * @param {Number} userId Contact userid.
 | 
        
           |  |  | 142 |      */
 | 
        
           |  |  | 143 |     var showContactUnblocked = function(body, userId) {
 | 
        
           |  |  | 144 |         var contact = findContact(body, userId);
 | 
        
           |  |  | 145 |         if (contact.length) {
 | 
        
           |  |  | 146 |             contact.find(SELECTORS.BLOCK_ICON_CONTAINER).addClass('hidden');
 | 
        
           |  |  | 147 |         }
 | 
        
           |  |  | 148 |     };
 | 
        
           |  |  | 149 |   | 
        
           |  |  | 150 |     /**
 | 
        
           |  |  | 151 |      * Listen to and handle events for contacts.
 | 
        
           |  |  | 152 |      *
 | 
        
           |  |  | 153 |      * @param {Object} root Contacts section container element.
 | 
        
           |  |  | 154 |      */
 | 
        
           |  |  | 155 |     var registerEventListeners = function(root) {
 | 
        
           |  |  | 156 |         PubSub.subscribe(Events.CONTACT_ADDED, function(profile) {
 | 
        
           |  |  | 157 |             var listContentContainer = LazyLoadList.getContentContainer(root);
 | 
        
           |  |  | 158 |             render(listContentContainer, [profile]);
 | 
        
           |  |  | 159 |             LazyLoadList.hideEmptyMessage(root);
 | 
        
           |  |  | 160 |             LazyLoadList.showContent(root);
 | 
        
           |  |  | 161 |         });
 | 
        
           |  |  | 162 |   | 
        
           |  |  | 163 |         PubSub.subscribe(Events.CONTACT_REMOVED, function(userId) {
 | 
        
           |  |  | 164 |             removeContact(root, userId);
 | 
        
           |  |  | 165 |             var contacts = root.find(SELECTORS.CONTACT);
 | 
        
           |  |  | 166 |   | 
        
           |  |  | 167 |             if (!contacts.length) {
 | 
        
           |  |  | 168 |                 LazyLoadList.hideContent(root);
 | 
        
           |  |  | 169 |                 LazyLoadList.showEmptyMessage(root);
 | 
        
           |  |  | 170 |             }
 | 
        
           |  |  | 171 |         });
 | 
        
           |  |  | 172 |   | 
        
           |  |  | 173 |         PubSub.subscribe(Events.CONTACT_BLOCKED, function(userId) {
 | 
        
           |  |  | 174 |             showContactBlocked(root, userId);
 | 
        
           |  |  | 175 |         });
 | 
        
           |  |  | 176 |   | 
        
           |  |  | 177 |         PubSub.subscribe(Events.CONTACT_UNBLOCKED, function(userId) {
 | 
        
           |  |  | 178 |             showContactUnblocked(root, userId);
 | 
        
           |  |  | 179 |         });
 | 
        
           |  |  | 180 |     };
 | 
        
           |  |  | 181 |   | 
        
           |  |  | 182 |     /**
 | 
        
           |  |  | 183 |      * Setup the contacts section.
 | 
        
           |  |  | 184 |      *
 | 
        
           |  |  | 185 |      * @param {Object} root Contacts section container.
 | 
        
           |  |  | 186 |      */
 | 
        
           |  |  | 187 |     var show = function(root) {
 | 
        
           |  |  | 188 |         if (!root.attr('data-contacts-init')) {
 | 
        
           |  |  | 189 |             registerEventListeners(root);
 | 
        
           |  |  | 190 |             root.attr('data-contacts-init', true);
 | 
        
           |  |  | 191 |         }
 | 
        
           |  |  | 192 |   | 
        
           |  |  | 193 |         // The root element is already the lazy loaded list root.
 | 
        
           |  |  | 194 |         LazyLoadList.show(root, getLoadFunction(initialOffset), render);
 | 
        
           |  |  | 195 |     };
 | 
        
           |  |  | 196 |   | 
        
           |  |  | 197 |     return {
 | 
        
           |  |  | 198 |         show: show,
 | 
        
           |  |  | 199 |     };
 | 
        
           |  |  | 200 | });
 |