message/amd/src/message_drawer_helper.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. * Provides some helper functions to trigger actions in the message drawer.
  17. *
  18. * @module core_message/message_drawer_helper
  19. * @copyright 2018 Ryan Wyllie <ryan@moodle.com>
  20. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  21. */
  22. import {publish, subscribe} from 'core/pubsub';
  23. import MessageDrawerEvents from 'core_message/message_drawer_events';
  24. /** @property {boolean} Whether the drawer is ready or not */
  25. let drawerMarkedReady = false;
  26. /**
  27. * Trigger an event to create a new conversation in the message drawer.
  28. *
  29. * @param {object} args
  30. * @param {Number} args.userId The user id to start a conversation.
  31. */
  32. export const createConversationWithUser = async(args) => {
  33. await waitForDrawerToLoad();
  34. publish(MessageDrawerEvents.CREATE_CONVERSATION_WITH_USER, args);
  35. };
  36. /**
  37. * Trigger an event to hide the message drawer.
  38. */
  39. export const hide = async() => {
  40. await waitForDrawerToLoad();
  41. publish(MessageDrawerEvents.HIDE);
  42. };
  43. /**
  44. * Trigger an event to show the message drawer.
  45. */
  46. export const show = async() => {
  47. await waitForDrawerToLoad();
  48. publish(MessageDrawerEvents.SHOW);
  49. };
  50. /**
  51. * Trigger an event to show the given conversation.
  52. *
  53. * @param {object} args
  54. * @param {int} args.conversationId Id for the conversation to show.
  55. */
  56. export const showConversation = async(args) => {
  57. await waitForDrawerToLoad();
  58. publish(MessageDrawerEvents.SHOW_CONVERSATION, args);
  59. };
  60. /**
  61. * Trigger an event to show messaging settings.
  62. */
  63. export const showSettings = async() => {
  64. await waitForDrawerToLoad();
  65. publish(MessageDrawerEvents.SHOW_SETTINGS);
  66. };
  67. /**
  68. * Helper to wait for the drawer to be ready before performing an action.
  69. *
  70. * @returns {Promise<void>}
  71. */
  72. export const waitForDrawerToLoad = () => new Promise((resolve) => {
  73. if (drawerMarkedReady) {
  74. resolve();
  75. } else {
  76. subscribe(MessageDrawerEvents.READY, resolve);
  77. }
  78. });
  79. /**
  80. * Helper to allow the drawer to mark itself as ready.
  81. */
  82. export const markDrawerReady = () => {
  83. drawerMarkedReady = true;
  84. publish(MessageDrawerEvents.READY);
  85. };