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 message popover in the nav bar.
|
|
|
18 |
*
|
|
|
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
|
|
|
22 |
*/
|
|
|
23 |
define(
|
|
|
24 |
[
|
|
|
25 |
'jquery',
|
|
|
26 |
'core/custom_interaction_events',
|
|
|
27 |
'core/pubsub',
|
|
|
28 |
'core_message/message_drawer_events'
|
|
|
29 |
],
|
|
|
30 |
function(
|
|
|
31 |
$,
|
|
|
32 |
CustomEvents,
|
|
|
33 |
PubSub,
|
|
|
34 |
MessageDrawerEvents
|
|
|
35 |
) {
|
|
|
36 |
var SELECTORS = {
|
|
|
37 |
COUNT_CONTAINER: '[data-region="count-container"]'
|
|
|
38 |
};
|
|
|
39 |
|
|
|
40 |
/**
|
|
|
41 |
* Toggle the message drawer visibility.
|
|
|
42 |
*
|
|
|
43 |
* @param {String} buttonid The button id for the popover.
|
|
|
44 |
*/
|
|
|
45 |
var toggleMessageDrawerVisibility = function(buttonid) {
|
|
|
46 |
PubSub.publish(MessageDrawerEvents.TOGGLE_VISIBILITY, buttonid);
|
|
|
47 |
};
|
|
|
48 |
|
|
|
49 |
/**
|
|
|
50 |
* Decrement the unread conversation count in the nav bar if a conversation
|
|
|
51 |
* is read. When there are no unread conversations then hide the counter.
|
|
|
52 |
*
|
|
|
53 |
* @param {Object} button The button element for the popover.
|
|
|
54 |
* @return {Function}
|
|
|
55 |
*/
|
|
|
56 |
var handleDecrementConversationCount = function(button) {
|
|
|
57 |
return function() {
|
|
|
58 |
var countContainer = button.find(SELECTORS.COUNT_CONTAINER);
|
|
|
59 |
var count = parseInt(countContainer.text(), 10);
|
|
|
60 |
|
|
|
61 |
if (isNaN(count)) {
|
|
|
62 |
countContainer.addClass('hidden');
|
|
|
63 |
} else if (!count || count < 2) {
|
|
|
64 |
countContainer.addClass('hidden');
|
|
|
65 |
} else {
|
|
|
66 |
count = count - 1;
|
|
|
67 |
countContainer.text(count);
|
|
|
68 |
}
|
|
|
69 |
};
|
|
|
70 |
};
|
|
|
71 |
|
|
|
72 |
/**
|
|
|
73 |
* Add events listeners for when the popover icon is clicked and when conversations
|
|
|
74 |
* are read.
|
|
|
75 |
*
|
|
|
76 |
* @param {Object} button The button element for the popover.
|
|
|
77 |
*/
|
|
|
78 |
var registerEventListeners = function(button) {
|
|
|
79 |
CustomEvents.define(button, [CustomEvents.events.activate]);
|
|
|
80 |
|
|
|
81 |
button.on(CustomEvents.events.activate, function(e, data) {
|
|
|
82 |
toggleMessageDrawerVisibility(button.attr('id'));
|
|
|
83 |
button.focus();
|
|
|
84 |
data.originalEvent.preventDefault();
|
|
|
85 |
});
|
|
|
86 |
|
|
|
87 |
PubSub.subscribe(MessageDrawerEvents.CONVERSATION_READ, handleDecrementConversationCount(button));
|
|
|
88 |
PubSub.subscribe(MessageDrawerEvents.CONTACT_REQUEST_ACCEPTED, handleDecrementConversationCount(button));
|
|
|
89 |
PubSub.subscribe(MessageDrawerEvents.CONTACT_REQUEST_DECLINED, handleDecrementConversationCount(button));
|
|
|
90 |
};
|
|
|
91 |
|
|
|
92 |
/**
|
|
|
93 |
* Initialise the message popover.
|
|
|
94 |
*
|
|
|
95 |
* @param {Object} button The button element for the popover.
|
|
|
96 |
*/
|
|
|
97 |
var init = function(button) {
|
|
|
98 |
button = $(button);
|
|
|
99 |
registerEventListeners(button);
|
|
|
100 |
};
|
|
|
101 |
|
|
|
102 |
return {
|
|
|
103 |
init: init,
|
|
|
104 |
};
|
|
|
105 |
});
|