MDL-54682 message: fix eslint issues
[moodle.git] / message / amd / src / message_area_profile.js
blob66387b4aa21da0ed0c0d8792677c19e51c1d8199
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 handles the profile area of the messaging area.
18  *
19  * @module     core_message/message_area_profile
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/ajax', 'core/templates', 'core/notification', 'core/str', 'core/config',
25         'core/custom_interaction_events', 'core_message/message_area_events'],
26     function($, Ajax, Templates, Notification, Str, Config, CustomEvents, Events) {
28         /** @type {Object} The list of selectors for the message area. */
29         var SELECTORS = {
30             PROFILE: "[data-region='profile']",
31             PROFILEADDCONTACT: "[data-action='profile-add-contact']",
32             PROFILEBLOCKCONTACT: "[data-action='profile-block-contact']",
33             PROFILEREMOVECONTACT: "[data-action='profile-remove-contact']",
34             PROFILESENDMESSAGE: "[data-action='profile-send-message']",
35             PROFILEUNBLOCKCONTACT: "[data-action='profile-unblock-contact']",
36             PROFILEVIEW: "[data-action='profile-view']",
37             SHOWCONTACTS: "[data-action='show-contacts']",
38             MESSAGESAREA: "[data-region='messages-area']",
39             MESSAGINGAREA: "[data-region='messaging-area']"
40         };
42         /**
43          * Profile class.
44          *
45          * @param {Messagearea} messageArea The messaging area object.
46          */
47         function Profile(messageArea) {
48             this.messageArea = messageArea;
49             this._init();
50         }
52         /** @type {Messagearea} The messaging area object. */
53         Profile.prototype.messageArea = null;
55         /**
56          * Initialise the event listeners.
57          *
58          * @private
59          */
60         Profile.prototype._init = function() {
61             CustomEvents.define(this.messageArea.node, [
62                 CustomEvents.events.activate
63             ]);
65             this.messageArea.onCustomEvent(Events.CONTACTSELECTED, this._viewProfile.bind(this));
66             this.messageArea.onDelegateEvent(CustomEvents.events.activate, SELECTORS.PROFILEVIEW,
67                 this._viewFullProfile.bind(this));
68             this.messageArea.onDelegateEvent(CustomEvents.events.activate, SELECTORS.PROFILESENDMESSAGE,
69                 this._sendMessage.bind(this));
70             this.messageArea.onDelegateEvent(CustomEvents.events.activate, SELECTORS.PROFILEUNBLOCKCONTACT,
71                 this._unblockContact.bind(this));
72             this.messageArea.onDelegateEvent(CustomEvents.events.activate, SELECTORS.PROFILEBLOCKCONTACT,
73                 this._blockContact.bind(this));
74             this.messageArea.onDelegateEvent(CustomEvents.events.activate, SELECTORS.PROFILEADDCONTACT,
75                 this._addContact.bind(this));
76             this.messageArea.onDelegateEvent(CustomEvents.events.activate, SELECTORS.PROFILEREMOVECONTACT,
77                 this._removeContact.bind(this));
78             this.messageArea.onDelegateEvent(CustomEvents.events.activate, SELECTORS.SHOWCONTACTS,
79                 this._hideMessagingArea.bind(this));
80         };
82         /**
83          * Handles viewing the profile.
84          *
85          * @param {Event} event
86          * @param {int} userid
87          * @return {Promise} The promise resolved when the profile has been rendered
88          * @private
89          */
90         Profile.prototype._viewProfile = function(event, userid) {
91             // Show loading template.
92             Templates.render('core/loading', {}).done(function(html, js) {
93                 Templates.replaceNodeContents(this.messageArea.find(SELECTORS.MESSAGESAREA), html, js);
94             }.bind(this));
96             // Call the web service to return the profile.
97             var promises = Ajax.call([{
98                 methodname: 'core_message_data_for_messagearea_get_profile',
99                 args: {
100                     currentuserid: this.messageArea.getCurrentUserId(),
101                     otheruserid: userid
102                 }
103             }]);
105             // Show the profile.
106             return promises[0].then(function(data) {
107                 return Templates.render('core_message/message_area_profile', data);
108             }).then(function(html, js) {
109                 Templates.replaceNodeContents(this.messageArea.find(SELECTORS.MESSAGESAREA), html, js);
110             }.bind(this)).fail(Notification.exception);
111         };
113         /**
114          * Handles viewing the user's full profile.
115          *
116          * @private
117          */
118         Profile.prototype._viewFullProfile = function() {
119             window.location.href = Config.wwwroot + '/user/profile.php?id=' + this._getUserId();
120         };
122         /**
123          * Handles viewing the messages with the user.
124          *
125          * @private
126          */
127         Profile.prototype._sendMessage = function() {
128             this.messageArea.trigger(Events.SENDMESSAGE, this._getUserId());
129         };
131         /**
132          * Handles blocking the contact.
133          *
134          * @return {Promise} The promise resolved when the contact has been blocked
135          * @private
136          */
137         Profile.prototype._blockContact = function() {
138             var action = this._performAction('core_message_block_contacts', 'unblockcontact', 'profile-block-contact',
139                 'profile-unblock-contact', '');
140             return action.then(function() {
141                 this.messageArea.trigger(Events.CONTACTBLOCKED, this._getUserId());
142             }.bind(this));
143         };
145         /**
146          * Handles unblocking the contact.
147          *
148          * @return {Promise} The promise resolved when the contact has been unblocked
149          * @private
150          */
151         Profile.prototype._unblockContact = function() {
152             var action = this._performAction('core_message_unblock_contacts', 'blockcontact', 'profile-unblock-contact',
153                 'profile-block-contact', 'danger');
154             return action.then(function() {
155                 this.messageArea.trigger(Events.CONTACTUNBLOCKED, this._getUserId());
156             }.bind(this));
157         };
159         /**
160          * Handles adding the contact.
161          *
162          * @return {Promise} The promise resolved when the contact has been added
163          * @private
164          */
165         Profile.prototype._addContact = function() {
166             var action = this._performAction('core_message_create_contacts', 'removecontact', 'profile-add-contact',
167                 'profile-remove-contact', 'danger');
168             return action.then(function() {
169                 this.messageArea.trigger(Events.CONTACTADDED, this._getUserId());
170             }.bind(this));
171         };
173         /**
174          * Handles removing the contact.
175          *
176          * @return {Promise} The promise resolved when the contact has been removed
177          * @private
178          */
179         Profile.prototype._removeContact = function() {
180             var action = this._performAction('core_message_delete_contacts', 'addcontact', 'profile-remove-contact',
181                 'profile-add-contact', '');
182             return action.then(function() {
183                 this.messageArea.trigger(Events.CONTACTREMOVED, this._getUserId());
184             }.bind(this));
185         };
187         /**
188          * Helper function to perform actions on the profile page.
189          *
190          * @param {String} service The web service to call.
191          * @param {String} string The string to change the button value to
192          * @param {String} oldaction The data-action of the button
193          * @param {string} newaction The data-action to change the button to
194          * @param {String} newclass The CSS class we want to add
195          * @return {Promise} The promise resolved when the action has been performed
196          * @private
197          */
198         Profile.prototype._performAction = function(service, string, oldaction, newaction, newclass) {
199             var promises = Ajax.call([{
200                 methodname: service,
201                 args: {
202                     userid: this.messageArea.getCurrentUserId(),
203                     userids: [
204                         this._getUserId()
205                     ]
206                 }
207             }]);
209             return promises[0].then(function() {
210                 return Str.get_string(string, 'message');
211             }).then(function(s) {
212                 this._changeText(s, oldaction, newaction, newclass);
213             }.bind(this)).fail(Notification.exception);
214         };
216         /**
217          * Changes the text in the profile area.
218          *
219          * @param {String} text The string to change the button value to
220          * @param {string} oldaction The data-action of the button
221          * @param {string} newaction The data-action to change the button to
222          * @param {String} newclass The CSS class we want to add
223          * @private
224          */
225         Profile.prototype._changeText = function(text, oldaction, newaction, newclass) {
226             var anchor = this.messageArea.find("[data-action='" + oldaction + "']");
227             // Change the text.
228             anchor.text(text);
229             // Remove any class.
230             anchor.removeClass();
231             // Add the class if there is one.
232             if (newclass) {
233                 anchor.addClass(newclass);
234             }
236             anchor.attr('data-action', newaction);
237         };
239         /**
240          * Returns the ID of the user whos profile we are viewing.
241          *
242          * @return {int} The user ID
243          * @private
244          */
245         Profile.prototype._getUserId = function() {
246             return this.messageArea.find(SELECTORS.PROFILE).data('userid');
247         };
249         /**
250          * Hide the messaging area. This only applies on smaller screen resolutions.
251          */
252         Profile.prototype._hideMessagingArea = function() {
253             this.messageArea.find(SELECTORS.MESSAGINGAREA)
254                 .removeClass('show-messages')
255                 .addClass('hide-messages');
256         };
258         return Profile;
259     }