MDL-63303 message: fix bugs in message drawer part 4
[moodle.git] / message / amd / src / message_area.js
blobb9482aa005e29c93890965068bd01b8321597b23
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.
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/>.
16 /**
17  * This module instantiates the functionality of the messaging area.
18  *
19  * @module     core_message/message_area
20  * @package    core_message
21  * @copyright  2016 Mark Nelson <markn@moodle.com>
22  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23  */
24 define(['jquery', 'core_message/message_area_contacts', 'core_message/message_area_messages',
25         'core_message/message_area_profile', 'core_message/message_area_tabs', 'core_message/message_area_search'],
26     function($, Contacts, Messages, Profile, Tabs, Search) {
28         /**
29          * Messagearea class.
30          *
31          * @param {String} selector The selector for the page region containing the message area.
32          * @param {int} pollmin
33          * @param {int} pollmax
34          * @param {int} polltimeout
35          */
36         function Messagearea(selector, pollmin, pollmax, polltimeout) {
37             this.node = $(selector);
38             this.pollmin = pollmin;
39             this.pollmax = pollmax;
40             this.polltimeout = polltimeout;
41             this._init();
42         }
44         /** @type {jQuery} The jQuery node for the page region containing the message area. */
45         Messagearea.prototype.node = null;
47         /** @type {int} The minimum time to poll for messages. */
48         Messagearea.prototype.pollmin = null;
50         /** @type {int} The maximum time to poll for messages. */
51         Messagearea.prototype.pollmax = null;
53         /** @type {int} The time used once we have reached the maximum polling time. */
54         Messagearea.prototype.polltimeout = null;
56         /**
57          * Initialise the other objects we require.
58          */
59         Messagearea.prototype._init = function() {
60             new Contacts(this);
61             new Messages(this);
62             new Profile(this);
63             new Tabs(this);
64             new Search(this);
65         };
67         /**
68          * Handles adding a delegate event to the messaging area node.
69          *
70          * @param {String} action The action we are listening for
71          * @param {String} selector The selector for the page we are assigning the action to
72          * @param {Function} callable The function to call when the event happens
73          */
74         Messagearea.prototype.onDelegateEvent = function(action, selector, callable) {
75             this.node.on(action, selector, callable);
76         };
78         /**
79          * Handles adding a custom event to the messaging area node.
80          *
81          * @param {String} action The action we are listening for
82          * @param {Function} callable The function to call when the event happens
83          */
84         Messagearea.prototype.onCustomEvent = function(action, callable) {
85             this.node.on(action, callable);
86         };
88         /**
89          * Handles triggering an event on the messaging area node.
90          *
91          * @param {String} event The selector for the page region containing the message area
92          * @param {Object=} data The data to pass when we trigger the event
93          */
94         Messagearea.prototype.trigger = function(event, data) {
95             if (typeof data == 'undefined') {
96                 data = '';
97             }
98             this.node.trigger(event, data);
99         };
101         /**
102          * Handles finding a node in the messaging area.
103          *
104          * @param {String} selector The selector for the node we are looking for
105          * @return {jQuery} The node
106          */
107         Messagearea.prototype.find = function(selector) {
108             return this.node.find(selector);
109         };
111         /**
112          * Returns the ID of the user whose message area we are viewing.
113          *
114          * @return {int} The user id
115          */
116         Messagearea.prototype.getCurrentUserId = function() {
117             return this.node.data('userid');
118         };
120         /**
121          * Function to determine if we should be showing contacts initially or messages.
122          *
123          * @return {boolean} True to show contacts first, otherwise show messages.
124          */
125         Messagearea.prototype.showContactsFirst = function() {
126             return !!this.node.data('displaycontacts');
127         };
129         return Messagearea;
130     }