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 * Module to add/remove contact using ajax.
19 * @module core_message/toggle_contact_button
20 * @class toggle_contact_button
22 * @copyright 2016 Ryan Wyllie <ryan@moodle.com>
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 define(['jquery', 'core/ajax', 'core/templates', 'core/notification', 'core/custom_interaction_events'],
26 function($, Ajax, Templates, Notification, CustomEvents) {
29 * Check the state of the element, if the current user is a contact or not.
32 * @param {object} element jQuery object for the button
35 var isContact = function(element) {
36 return element.attr('data-is-contact') == '1';
40 * Record that the user is a contact.
43 * @param {object} element jQuery object for the button
45 var setContact = function(element) {
46 element.attr('data-is-contact', '1');
50 * Record that the user is not a contact.
52 * @method setNotContact
53 * @param {object} element jQuery object for the button
55 var setNotContact = function(element) {
56 element.attr('data-is-contact', '0');
60 * Get the id for the user being viewed.
63 * @param {object} element jQuery object for the button
66 var getUserId = function(element) {
67 return element.attr('data-userid');
71 * Check if this element is currently loading.
74 * @param {object} element jQuery object for the button
77 var isLoading = function(element) {
78 return element.hasClass('loading') || element.attr('disabled');
82 * Sends an ajax request to the server and handles the element state
83 * while the request is being performed.
86 * @param {object} element jQuery object for the button
87 * @param {object} request Request hash to send
88 * @return {object} jQuery promise
90 var sendRequest = function(element, request) {
91 if (isLoading(element)) {
95 element.addClass('loading');
96 element.attr('disabled', 'disabled');
98 return Ajax.call([request])[0]
99 .fail(Notification.exception)
101 element.removeClass('loading');
102 element.removeAttr('disabled');
107 * Send a request to the server to add the current user as
108 * a contact. The contents of the button are changed to the
109 * remove contact button upon success.
112 * @param {object} element jQuery object for the button
114 var addContact = function(element) {
115 if (isLoading(element)) {
120 methodname: 'core_message_create_contacts',
122 userids: [getUserId(element)],
125 sendRequest(element, request).done(function() {
127 Templates.render('message/remove_contact_button', {}).done(function(html, js) {
128 Templates.replaceNodeContents(element, html, js);
134 * Send a request to the server to remove the current user as
135 * a contact. The contents of the button are changed to the
136 * add contact button upon success.
138 * @method removeContact
139 * @param {object} element jQuery object for the button
141 var removeContact = function(element) {
142 if (isLoading(element)) {
147 methodname: 'core_message_delete_contacts',
149 userids: [getUserId(element)],
153 sendRequest(element, request).done(function() {
154 setNotContact(element);
155 Templates.render('message/add_contact_button', {}).done(function(html, js) {
156 Templates.replaceNodeContents(element, html, js);
162 * Enhances the given element with a loading gif and event handles to make
163 * ajax requests to add or remove a contact where appropriate.
166 * @param {object} element jQuery object for the button
168 var enhance = function(element) {
169 element = $(element);
171 if (!element.children('.loading-icon').length) {
172 // Add the loading gif if it isn't already there.
173 Templates.render('core/loading', {}).done(function(html, js) {
174 element.append(html, js);
178 CustomEvents.define(element, [CustomEvents.events.activate]);
180 element.on(CustomEvents.events.activate, function(e, data) {
181 if (isContact(element)) {
182 removeContact(element);
187 data.originalEvent.preventDefault();
191 return /** @alias module:message/toggle_contact_button */ {