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