Proyectos de Subversion Moodle

Rev

Ir a la última revisión | | Ultima modificación | Ver Log |

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