Proyectos de Subversion Moodle

Rev

| 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 message a user from their profile page.
18
 *
19
 * @module     core_message/message_user_button
20
 * @copyright  2019 Mark Nelson <markn@moodle.com>
21
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
22
 */
23
define(['jquery', 'core/custom_interaction_events', 'core_message/message_drawer_helper', 'core/templates'],
24
    function($, CustomEvents, MessageDrawerHelper, Templates) {
25
 
26
 
27
        var SELECTORS = {
28
            MESSAGE_TEXTAREA: '[data-region="send-message-txt"]',
29
            MESSAGE_USER_BUTTON: '#message-user-button',
30
            MESSAGE_JUMP: '[data-region="jumpto"]'
31
        };
32
 
33
        var TEMPLATES = {
34
            CONTENT: 'core_message/message_jumpto'
35
        };
36
 
37
        /**
38
         * Get the id for the user being messaged.
39
         *
40
         * @param {object} element jQuery object for the button
41
         * @return {int}
42
         */
43
        var getUserId = function(element) {
44
            return parseInt(element.attr('data-userid'));
45
        };
46
 
47
        /**
48
         * Returns the conversation id, 0 if none.
49
         *
50
         * @param {object} element jQuery object for the button
51
         * @return {int}
52
         */
53
        var getConversationId = function(element) {
54
            return parseInt(element.attr('data-conversationid'));
55
        };
56
 
57
        /**
58
         * Handles opening the messaging drawer to send a
59
         * message to a given user.
60
         *
61
         * @method enhance
62
         * @param {object} element jQuery object for the button
63
         */
64
        var send = function(element) {
65
            element = $(element);
66
 
67
            var args = {
68
                conversationid: getConversationId(element),
69
                buttonid: $(element).attr('id'),
70
                userid: getUserId(element)
71
            };
72
 
73
            Templates.render(TEMPLATES.CONTENT, {})
74
                .then(function(html) {
75
                    element.after(html);
76
                })
77
                .then(function() {
78
                    $(SELECTORS.MESSAGE_USER_BUTTON).next().focus(function() {
79
                        $(SELECTORS.MESSAGE_TEXTAREA).focus();
80
                    });
81
                });
82
 
83
            CustomEvents.define(element, [CustomEvents.events.activate]);
84
 
85
            element.on(CustomEvents.events.activate, function(e, data) {
86
                if ($(e.target).hasClass('active')) {
87
                    MessageDrawerHelper.hide();
88
                    $(SELECTORS.MESSAGE_USER_BUTTON).next().attr('tabindex', -1);
89
                } else {
90
                    $(SELECTORS.MESSAGE_USER_BUTTON).next().attr('tabindex', 0);
91
                    if (args.conversationid) {
92
                        MessageDrawerHelper.showConversation(args);
93
                    } else {
94
                        MessageDrawerHelper.createConversationWithUser(args);
95
                    }
96
                }
97
                $(e.target).focus();
98
                $(e.target).toggleClass('active');
99
                e.preventDefault();
100
                data.originalEvent.preventDefault();
101
            });
102
        };
103
 
104
        return /** @alias module:core_message/message_user_button */ {
105
            send: send
106
        };
107
    });