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 message popover in the nav bar.
19 * @module core_message/message_popover
20 * @copyright 2018 Ryan Wyllie <ryan@moodle.com>
21 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 'core/custom_interaction_events',
28 'core_message/message_drawer_events'
37 COUNT_CONTAINER: '[data-region="count-container"]'
41 * Toggle the message drawer visibility.
43 var toggleMessageDrawerVisibility = function() {
44 PubSub.publish(MessageDrawerEvents.TOGGLE_VISIBILITY);
48 * Decrement the unread conversation count in the nav bar if a conversation
49 * is read. When there are no unread conversations then hide the counter.
51 * @param {Object} root The root element for the popover.
54 var handleDecrementConversationCount = function(root) {
56 var countContainer = root.find(SELECTORS.COUNT_CONTAINER);
57 var count = parseInt(countContainer.text(), 10);
60 countContainer.addClass('hidden');
61 } else if (!count || count < 2) {
62 countContainer.addClass('hidden');
65 countContainer.text(count);
71 * Add events listeners for when the popover icon is clicked and when conversations
74 * @param {Object} root The root element for the popover.
76 var registerEventListeners = function(root) {
77 CustomEvents.define(root, [CustomEvents.events.activate]);
79 root.on(CustomEvents.events.activate, function(e, data) {
80 toggleMessageDrawerVisibility();
81 data.originalEvent.preventDefault();
84 PubSub.subscribe(MessageDrawerEvents.CONVERSATION_READ, handleDecrementConversationCount(root));
85 PubSub.subscribe(MessageDrawerEvents.CONTACT_REQUEST_ACCEPTED, handleDecrementConversationCount(root));
86 PubSub.subscribe(MessageDrawerEvents.CONTACT_REQUEST_DECLINED, handleDecrementConversationCount(root));
90 * Initialise the message popover.
92 * @param {Object} root The root element for the popover.
94 var init = function(root) {
96 registerEventListeners(root);