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 |
* Module to add/remove contact using ajax.
|
|
|
18 |
*
|
|
|
19 |
* @module core_message/toggle_contact_button
|
|
|
20 |
* @copyright 2016 Ryan Wyllie <ryan@moodle.com>
|
|
|
21 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
22 |
*/
|
|
|
23 |
define(['jquery', 'core/ajax', 'core/templates', 'core/notification', 'core/custom_interaction_events'],
|
|
|
24 |
function($, Ajax, Templates, Notification, CustomEvents) {
|
|
|
25 |
|
|
|
26 |
/**
|
|
|
27 |
* Check the state of the element, if the current user is a contact or not.
|
|
|
28 |
*
|
|
|
29 |
* @method isContact
|
|
|
30 |
* @param {object} element jQuery object for the button
|
|
|
31 |
* @return {bool}
|
|
|
32 |
*/
|
|
|
33 |
var isContact = function(element) {
|
|
|
34 |
return element.attr('data-is-contact') == '1';
|
|
|
35 |
};
|
|
|
36 |
|
|
|
37 |
/**
|
11 |
efrain |
38 |
* Check the state of the element, if the current user has sent a contact request or not.
|
1 |
efrain |
39 |
*
|
11 |
efrain |
40 |
* @method isRequested
|
1 |
efrain |
41 |
* @param {object} element jQuery object for the button
|
11 |
efrain |
42 |
* @return {bool}
|
1 |
efrain |
43 |
*/
|
11 |
efrain |
44 |
let isRequested = (element) => element.attr('data-is-requested') == '1';
|
|
|
45 |
|
|
|
46 |
/**
|
|
|
47 |
* Record that the user has sent a contact request.
|
|
|
48 |
*
|
|
|
49 |
* @method setContactRequested
|
|
|
50 |
* @param {object} element jQuery object for the button
|
|
|
51 |
*/
|
|
|
52 |
var setContactRequested = function(element) {
|
|
|
53 |
element.attr('data-is-requested', '1');
|
1 |
efrain |
54 |
};
|
|
|
55 |
|
|
|
56 |
/**
|
|
|
57 |
* Record that the user is not a contact.
|
|
|
58 |
*
|
|
|
59 |
* @method setNotContact
|
|
|
60 |
* @param {object} element jQuery object for the button
|
|
|
61 |
*/
|
|
|
62 |
var setNotContact = function(element) {
|
|
|
63 |
element.attr('data-is-contact', '0');
|
|
|
64 |
};
|
|
|
65 |
|
|
|
66 |
/**
|
|
|
67 |
* Get the id for the user being viewed.
|
|
|
68 |
*
|
|
|
69 |
* @method getUserId
|
|
|
70 |
* @param {object} element jQuery object for the button
|
|
|
71 |
* @return {int}
|
|
|
72 |
*/
|
|
|
73 |
var getUserId = function(element) {
|
|
|
74 |
return element.attr('data-userid');
|
|
|
75 |
};
|
|
|
76 |
|
|
|
77 |
/**
|
|
|
78 |
* Get the id for the logged in user.
|
|
|
79 |
*
|
|
|
80 |
* @method getUserId
|
|
|
81 |
* @param {object} element jQuery object for the button
|
|
|
82 |
* @return {int}
|
|
|
83 |
*/
|
|
|
84 |
var getCurrentUserId = function(element) {
|
|
|
85 |
return element.attr('data-currentuserid');
|
|
|
86 |
};
|
|
|
87 |
|
|
|
88 |
/**
|
|
|
89 |
* Check whether a text label should be displayed or not.
|
|
|
90 |
*
|
|
|
91 |
* @method getUserId
|
|
|
92 |
* @param {object} element jQuery object for the button
|
|
|
93 |
* @return {int}
|
|
|
94 |
*/
|
|
|
95 |
var displayTextLabel = function(element) {
|
|
|
96 |
return element.attr('data-display-text-label') == '1';
|
|
|
97 |
};
|
|
|
98 |
|
|
|
99 |
/**
|
|
|
100 |
* Check if this element is currently loading.
|
|
|
101 |
*
|
|
|
102 |
* @method isLoading
|
|
|
103 |
* @param {object} element jQuery object for the button
|
|
|
104 |
* @return {bool}
|
|
|
105 |
*/
|
|
|
106 |
var isLoading = function(element) {
|
|
|
107 |
return element.hasClass('loading') || element.attr('disabled');
|
|
|
108 |
};
|
|
|
109 |
|
|
|
110 |
/**
|
|
|
111 |
* Sends an ajax request to the server and handles the element state
|
|
|
112 |
* while the request is being performed.
|
|
|
113 |
*
|
|
|
114 |
* @method sendRequest
|
|
|
115 |
* @param {object} element jQuery object for the button
|
|
|
116 |
* @param {object} request Request hash to send
|
|
|
117 |
* @return {object} jQuery promise
|
|
|
118 |
*/
|
|
|
119 |
var sendRequest = function(element, request) {
|
|
|
120 |
if (isLoading(element)) {
|
|
|
121 |
return $.Deferred();
|
|
|
122 |
}
|
|
|
123 |
|
|
|
124 |
element.addClass('loading');
|
|
|
125 |
element.attr('disabled', 'disabled');
|
|
|
126 |
|
|
|
127 |
return Ajax.call([request])[0]
|
|
|
128 |
.fail(Notification.exception)
|
|
|
129 |
.always(function() {
|
|
|
130 |
element.removeClass('loading');
|
|
|
131 |
element.removeAttr('disabled');
|
|
|
132 |
});
|
|
|
133 |
};
|
|
|
134 |
|
|
|
135 |
/**
|
|
|
136 |
* Send a request to the server to add the current user as
|
|
|
137 |
* a contact. The contents of the button are changed to the
|
|
|
138 |
* remove contact button upon success.
|
|
|
139 |
*
|
|
|
140 |
* @method addContact
|
|
|
141 |
* @param {object} element jQuery object for the button
|
|
|
142 |
*/
|
|
|
143 |
var addContact = function(element) {
|
|
|
144 |
if (isLoading(element)) {
|
|
|
145 |
return;
|
|
|
146 |
}
|
|
|
147 |
|
|
|
148 |
var request = {
|
|
|
149 |
methodname: 'core_message_create_contact_request',
|
|
|
150 |
args: {
|
|
|
151 |
userid: getCurrentUserId(element),
|
|
|
152 |
requesteduserid: getUserId(element),
|
|
|
153 |
}
|
|
|
154 |
};
|
|
|
155 |
sendRequest(element, request).done(function() {
|
11 |
efrain |
156 |
setContactRequested(element);
|
|
|
157 |
element.addClass('disabled');
|
1 |
efrain |
158 |
const templateContext = {
|
|
|
159 |
'displaytextlabel': displayTextLabel(element)
|
|
|
160 |
};
|
11 |
efrain |
161 |
Templates.render('message/contact_request_sent', templateContext).done(function(html, js) {
|
1 |
efrain |
162 |
Templates.replaceNodeContents(element, html, js);
|
|
|
163 |
});
|
|
|
164 |
});
|
|
|
165 |
};
|
|
|
166 |
|
|
|
167 |
/**
|
|
|
168 |
* Send a request to the server to remove the current user as
|
|
|
169 |
* a contact. The contents of the button are changed to the
|
|
|
170 |
* add contact button upon success.
|
|
|
171 |
*
|
|
|
172 |
* @method removeContact
|
|
|
173 |
* @param {object} element jQuery object for the button
|
|
|
174 |
*/
|
|
|
175 |
var removeContact = function(element) {
|
|
|
176 |
if (isLoading(element)) {
|
|
|
177 |
return;
|
|
|
178 |
}
|
|
|
179 |
|
|
|
180 |
var request = {
|
|
|
181 |
methodname: 'core_message_delete_contacts',
|
|
|
182 |
args: {
|
|
|
183 |
userids: [getUserId(element)],
|
|
|
184 |
}
|
|
|
185 |
};
|
|
|
186 |
|
|
|
187 |
sendRequest(element, request).done(function() {
|
|
|
188 |
setNotContact(element);
|
|
|
189 |
const templateContext = {
|
|
|
190 |
'displaytextlabel': displayTextLabel(element)
|
|
|
191 |
};
|
|
|
192 |
Templates.render('message/add_contact_button', templateContext).done(function(html, js) {
|
|
|
193 |
Templates.replaceNodeContents(element, html, js);
|
|
|
194 |
});
|
|
|
195 |
});
|
|
|
196 |
};
|
|
|
197 |
|
|
|
198 |
/**
|
|
|
199 |
* Enhances the given element with a loading gif and event handles to make
|
|
|
200 |
* ajax requests to add or remove a contact where appropriate.
|
|
|
201 |
*
|
|
|
202 |
* @public
|
|
|
203 |
* @method enhance
|
|
|
204 |
* @param {object} element jQuery object for the button
|
|
|
205 |
*/
|
|
|
206 |
var enhance = function(element) {
|
|
|
207 |
element = $(element);
|
|
|
208 |
|
11 |
efrain |
209 |
if (!element.children('.loading-icon').length && !isRequested(element)) {
|
1 |
efrain |
210 |
// Add the loading gif if it isn't already there.
|
|
|
211 |
Templates.render('core/loading', {}).done(function(html, js) {
|
|
|
212 |
element.append(html, js);
|
|
|
213 |
});
|
|
|
214 |
}
|
|
|
215 |
|
|
|
216 |
CustomEvents.define(element, [CustomEvents.events.activate]);
|
|
|
217 |
|
|
|
218 |
element.on(CustomEvents.events.activate, function(e, data) {
|
|
|
219 |
if (isContact(element)) {
|
|
|
220 |
removeContact(element);
|
11 |
efrain |
221 |
} else if (!isRequested(element)) {
|
1 |
efrain |
222 |
addContact(element);
|
|
|
223 |
}
|
|
|
224 |
e.preventDefault();
|
|
|
225 |
data.originalEvent.preventDefault();
|
|
|
226 |
});
|
|
|
227 |
};
|
|
|
228 |
|
|
|
229 |
return {
|
|
|
230 |
enhance: enhance
|
|
|
231 |
};
|
|
|
232 |
});
|