| 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 page of the message drawer.
  | 
        
        
            | 
            | 
           18 | 
            *
  | 
        
        
            | 
            | 
           19 | 
            * @module     core_message/message_drawer_view_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/pubsub',
  | 
        
        
            | 
            | 
           27 | 
               'core/str',
  | 
        
        
            | 
            | 
           28 | 
               'core_message/message_drawer_events',
  | 
        
        
            | 
            | 
           29 | 
               'core_message/message_drawer_view_contacts_section_contacts',
  | 
        
        
            | 
            | 
           30 | 
               'core_message/message_drawer_view_contacts_section_requests'
  | 
        
        
            | 
            | 
           31 | 
           ],
  | 
        
        
            | 
            | 
           32 | 
           function(
  | 
        
        
            | 
            | 
           33 | 
               $,
  | 
        
        
            | 
            | 
           34 | 
               PubSub,
  | 
        
        
            | 
            | 
           35 | 
               Str,
  | 
        
        
            | 
            | 
           36 | 
               MessageDrawerEvents,
  | 
        
        
            | 
            | 
           37 | 
               ContactsSection,
  | 
        
        
            | 
            | 
           38 | 
               RequestsSection
  | 
        
        
            | 
            | 
           39 | 
           ) {
  | 
        
        
            | 
            | 
           40 | 
              | 
        
        
            | 
            | 
           41 | 
               var SELECTORS = {
  | 
        
        
            | 
            | 
           42 | 
                   ACTION_SHOW_CONTACTS_SECTION: '[data-action="show-contacts-section"]',
  | 
        
        
            | 
            | 
           43 | 
                   ACTION_SHOW_REQUESTS_SECTION: '[data-action="show-requests-section"]',
  | 
        
        
            | 
            | 
           44 | 
                   CONTACT_REQUEST_COUNT: '[data-region="contact-request-count"]',
  | 
        
        
            | 
            | 
           45 | 
                   CONTACTS_SECTION_CONTAINER: '[data-section="contacts"]',
  | 
        
        
            | 
            | 
           46 | 
                   REQUESTS_SECTION_CONTAINER: '[data-section="requests"]',
  | 
        
        
            | 
            | 
           47 | 
               };
  | 
        
        
            | 
            | 
           48 | 
              | 
        
        
            | 
            | 
           49 | 
               /**
  | 
        
        
            | 
            | 
           50 | 
                * Get the container element for the contacts section.
  | 
        
        
            | 
            | 
           51 | 
                *
  | 
        
        
            | 
            | 
           52 | 
                * @param {Object} body Contacts page body element.
  | 
        
        
            | 
            | 
           53 | 
                * @return {Object}
  | 
        
        
            | 
            | 
           54 | 
                */
  | 
        
        
            | 
            | 
           55 | 
               var getContactsSectionContainer = function(body) {
  | 
        
        
            | 
            | 
           56 | 
                   return body.find(SELECTORS.CONTACTS_SECTION_CONTAINER);
  | 
        
        
            | 
            | 
           57 | 
               };
  | 
        
        
            | 
            | 
           58 | 
              | 
        
        
            | 
            | 
           59 | 
               /**
  | 
        
        
            | 
            | 
           60 | 
                * Get the container element for the requests section.
  | 
        
        
            | 
            | 
           61 | 
                *
  | 
        
        
            | 
            | 
           62 | 
                * @param {Object} body Contacts page body element.
  | 
        
        
            | 
            | 
           63 | 
                * @return {Object}
  | 
        
        
            | 
            | 
           64 | 
                */
  | 
        
        
            | 
            | 
           65 | 
               var getRequestsSectionContainer = function(body) {
  | 
        
        
            | 
            | 
           66 | 
                   return body.find(SELECTORS.REQUESTS_SECTION_CONTAINER);
  | 
        
        
            | 
            | 
           67 | 
               };
  | 
        
        
            | 
            | 
           68 | 
              | 
        
        
            | 
            | 
           69 | 
               /**
  | 
        
        
            | 
            | 
           70 | 
                * Get the element that triggers showing the contacts section.
  | 
        
        
            | 
            | 
           71 | 
                *
  | 
        
        
            | 
            | 
           72 | 
                * @param {Object} body Contacts page body element.
  | 
        
        
            | 
            | 
           73 | 
                * @return {Object}
  | 
        
        
            | 
            | 
           74 | 
                */
  | 
        
        
            | 
            | 
           75 | 
               var getShowContactsAction = function(body) {
  | 
        
        
            | 
            | 
           76 | 
                   return body.find(SELECTORS.ACTION_SHOW_CONTACTS_SECTION);
  | 
        
        
            | 
            | 
           77 | 
               };
  | 
        
        
            | 
            | 
           78 | 
              | 
        
        
            | 
            | 
           79 | 
               /**
  | 
        
        
            | 
            | 
           80 | 
                * Get the element that triggers showing the requests section.
  | 
        
        
            | 
            | 
           81 | 
                *
  | 
        
        
            | 
            | 
           82 | 
                * @param {Object} body Contacts page body element.
  | 
        
        
            | 
            | 
           83 | 
                * @return {Object}
  | 
        
        
            | 
            | 
           84 | 
                */
  | 
        
        
            | 
            | 
           85 | 
               var getShowRequestsAction = function(body) {
  | 
        
        
            | 
            | 
           86 | 
                   return body.find(SELECTORS.ACTION_SHOW_REQUESTS_SECTION);
  | 
        
        
            | 
            | 
           87 | 
               };
  | 
        
        
            | 
            | 
           88 | 
              | 
        
        
            | 
            | 
           89 | 
               /**
  | 
        
        
            | 
            | 
           90 | 
                * Check if the given section is visible.
  | 
        
        
            | 
            | 
           91 | 
                *
  | 
        
        
            | 
            | 
           92 | 
                * @param {Object} sectionRoot The root element for the section
  | 
        
        
            | 
            | 
           93 | 
                * @return {Bool}
  | 
        
        
            | 
            | 
           94 | 
                */
  | 
        
        
            | 
            | 
           95 | 
               var isSectionVisible = function(sectionRoot) {
  | 
        
        
            | 
            | 
           96 | 
                   return sectionRoot.hasClass('active');
  | 
        
        
            | 
            | 
           97 | 
               };
  | 
        
        
            | 
            | 
           98 | 
              | 
        
        
            | 
            | 
           99 | 
               /**
  | 
        
        
            | 
            | 
           100 | 
                * Decrement the contact request count. If the count is zero or below then
  | 
        
        
            | 
            | 
           101 | 
                * hide the count.
  | 
        
        
            | 
            | 
           102 | 
                *
  | 
        
        
            | 
            | 
           103 | 
                * @param {Object} body Conversation body container element.
  | 
        
        
            | 
            | 
           104 | 
                * @return {Function} A function to handle decrementing the count.
  | 
        
        
            | 
            | 
           105 | 
                */
  | 
        
        
            | 
            | 
           106 | 
               var decrementContactRequestCount = function(body) {
  | 
        
        
            | 
            | 
           107 | 
                   return function() {
  | 
        
        
            | 
            | 
           108 | 
                       var countContainer = body.find(SELECTORS.CONTACT_REQUEST_COUNT);
  | 
        
        
            | 
            | 
           109 | 
                       var count = parseInt(countContainer.text(), 10);
  | 
        
        
            | 
            | 
           110 | 
                       count = isNaN(count) ? 0 : count - 1;
  | 
        
        
            | 
            | 
           111 | 
              | 
        
        
            | 
            | 
           112 | 
                       if (count <= 0) {
  | 
        
        
            | 
            | 
           113 | 
                           countContainer.addClass('hidden');
  | 
        
        
            | 
            | 
           114 | 
                       } else {
  | 
        
        
            | 
            | 
           115 | 
                           countContainer.text(count);
  | 
        
        
            | 
            | 
           116 | 
                       }
  | 
        
        
            | 
            | 
           117 | 
                   };
  | 
        
        
            | 
            | 
           118 | 
               };
  | 
        
        
            | 
            | 
           119 | 
              | 
        
        
            | 
            | 
           120 | 
               /**
  | 
        
        
            | 
            | 
           121 | 
                * Listen to and handle events for contacts.
  | 
        
        
            | 
            | 
           122 | 
                *
  | 
        
        
            | 
            | 
           123 | 
                * @param {Object} body Contacts body container element.
  | 
        
        
            | 
            | 
           124 | 
                */
  | 
        
        
            | 
            | 
           125 | 
               var registerEventListeners = function(body) {
  | 
        
        
            | 
            | 
           126 | 
                   var contactsSection = getContactsSectionContainer(body);
  | 
        
        
            | 
            | 
           127 | 
                   var requestsSection = getRequestsSectionContainer(body);
  | 
        
        
            | 
            | 
           128 | 
                   var showContactsAction = getShowContactsAction(body);
  | 
        
        
            | 
            | 
           129 | 
                   var showRequestsAction = getShowRequestsAction(body);
  | 
        
        
            | 
            | 
           130 | 
              | 
        
        
           | 1441 | 
           ariadna | 
           131 | 
                   showContactsAction[0].addEventListener('show.bs.tab', function() {
  | 
        
        
           | 1 | 
           efrain | 
           132 | 
                       ContactsSection.show(contactsSection);
  | 
        
        
            | 
            | 
           133 | 
                   });
  | 
        
        
            | 
            | 
           134 | 
              | 
        
        
           | 1441 | 
           ariadna | 
           135 | 
                   showRequestsAction[0].addEventListener('show.bs.tab', function() {
  | 
        
        
           | 1 | 
           efrain | 
           136 | 
                       RequestsSection.show(requestsSection);
  | 
        
        
            | 
            | 
           137 | 
                   });
  | 
        
        
            | 
            | 
           138 | 
              | 
        
        
            | 
            | 
           139 | 
                   PubSub.subscribe(MessageDrawerEvents.CONTACT_REQUEST_ACCEPTED, decrementContactRequestCount(body));
  | 
        
        
            | 
            | 
           140 | 
                   PubSub.subscribe(MessageDrawerEvents.CONTACT_REQUEST_DECLINED, decrementContactRequestCount(body));
  | 
        
        
            | 
            | 
           141 | 
               };
  | 
        
        
            | 
            | 
           142 | 
              | 
        
        
            | 
            | 
           143 | 
               /**
  | 
        
        
            | 
            | 
           144 | 
                * Setup the contact page.
  | 
        
        
            | 
            | 
           145 | 
                *
  | 
        
        
            | 
            | 
           146 | 
                * @param {string} namespace The route namespace.
  | 
        
        
            | 
            | 
           147 | 
                * @param {Object} header Contacts header container element.
  | 
        
        
            | 
            | 
           148 | 
                * @param {Object} body Contacts body container element.
  | 
        
        
            | 
            | 
           149 | 
                * @param {Object} footer Contacts footer container element.
  | 
        
        
            | 
            | 
           150 | 
                * @param {String|null} tab Tab to show, either 'requests' or 'contacts', if any.
  | 
        
        
            | 
            | 
           151 | 
                * @return {Object} jQuery promise
  | 
        
        
            | 
            | 
           152 | 
                */
  | 
        
        
            | 
            | 
           153 | 
               var show = function(namespace, header, body, footer, tab) {
  | 
        
        
            | 
            | 
           154 | 
                   body = $(body);
  | 
        
        
            | 
            | 
           155 | 
              | 
        
        
            | 
            | 
           156 | 
                   if (!body.attr('data-contacts-init')) {
  | 
        
        
            | 
            | 
           157 | 
                       registerEventListeners(body);
  | 
        
        
            | 
            | 
           158 | 
                       body.attr('data-contacts-init', true);
  | 
        
        
            | 
            | 
           159 | 
                   }
  | 
        
        
            | 
            | 
           160 | 
              | 
        
        
            | 
            | 
           161 | 
                   var contactsSection = getContactsSectionContainer(body);
  | 
        
        
            | 
            | 
           162 | 
                   var requestsSection = getRequestsSectionContainer(body);
  | 
        
        
            | 
            | 
           163 | 
              | 
        
        
            | 
            | 
           164 | 
                   if (tab) {
  | 
        
        
            | 
            | 
           165 | 
                       var showContactsAction = getShowContactsAction(body);
  | 
        
        
            | 
            | 
           166 | 
                       var showRequestsAction = getShowRequestsAction(body);
  | 
        
        
            | 
            | 
           167 | 
              | 
        
        
            | 
            | 
           168 | 
                       // Unfortunately we need to hardcode the class changes here rather than trigger
  | 
        
        
            | 
            | 
           169 | 
                       // the bootstrap tab functionality because the bootstrap JS doesn't appear to be
  | 
        
        
            | 
            | 
           170 | 
                       // loaded by this point which means the tab plugin isn't added and the event listeners
  | 
        
        
            | 
            | 
           171 | 
                       // haven't been set up so we can't just trigger a click either.
  | 
        
        
            | 
            | 
           172 | 
                       if (tab == 'requests') {
  | 
        
        
            | 
            | 
           173 | 
                           showContactsAction.removeClass('active');
  | 
        
        
            | 
            | 
           174 | 
                           contactsSection.removeClass('show active');
  | 
        
        
            | 
            | 
           175 | 
                           showRequestsAction.addClass('active');
  | 
        
        
            | 
            | 
           176 | 
                           requestsSection.addClass('show active');
  | 
        
        
            | 
            | 
           177 | 
                       } else {
  | 
        
        
            | 
            | 
           178 | 
                           showRequestsAction.removeClass('active');
  | 
        
        
            | 
            | 
           179 | 
                           requestsSection.removeClass('show active');
  | 
        
        
            | 
            | 
           180 | 
                           showContactsAction.addClass('active');
  | 
        
        
            | 
            | 
           181 | 
                           contactsSection.addClass('show active');
  | 
        
        
            | 
            | 
           182 | 
                       }
  | 
        
        
            | 
            | 
           183 | 
                   }
  | 
        
        
            | 
            | 
           184 | 
              | 
        
        
            | 
            | 
           185 | 
                   if (isSectionVisible(contactsSection)) {
  | 
        
        
            | 
            | 
           186 | 
                       ContactsSection.show(contactsSection);
  | 
        
        
            | 
            | 
           187 | 
                   } else {
  | 
        
        
            | 
            | 
           188 | 
                       RequestsSection.show(requestsSection);
  | 
        
        
            | 
            | 
           189 | 
                   }
  | 
        
        
            | 
            | 
           190 | 
              | 
        
        
            | 
            | 
           191 | 
                   return $.Deferred().resolve().promise();
  | 
        
        
            | 
            | 
           192 | 
               };
  | 
        
        
            | 
            | 
           193 | 
              | 
        
        
            | 
            | 
           194 | 
               /**
  | 
        
        
            | 
            | 
           195 | 
                * String describing this page used for aria-labels.
  | 
        
        
            | 
            | 
           196 | 
                *
  | 
        
        
            | 
            | 
           197 | 
                * @return {Object} jQuery promise
  | 
        
        
            | 
            | 
           198 | 
                */
  | 
        
        
            | 
            | 
           199 | 
               var description = function() {
  | 
        
        
            | 
            | 
           200 | 
                   return Str.get_string('messagedrawerviewcontacts', 'core_message');
  | 
        
        
            | 
            | 
           201 | 
               };
  | 
        
        
            | 
            | 
           202 | 
              | 
        
        
            | 
            | 
           203 | 
               return {
  | 
        
        
            | 
            | 
           204 | 
                   show: show,
  | 
        
        
            | 
            | 
           205 | 
                   description: description
  | 
        
        
            | 
            | 
           206 | 
               };
  | 
        
        
            | 
            | 
           207 | 
           });
  |