message/amd/src/message_user_button.js

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