MDL-69878 core_message: always show message drawer close icon
[moodle.git] / message / amd / src / message_drawer.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/>.
16 /**
17  * Controls the message drawer.
18  *
19  * @module     core_message/message_drawer
20  * @copyright  2018 Ryan Wyllie <ryan@moodle.com>
21  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
22  */
23 define(
24 [
25     'jquery',
26     'core/custom_interaction_events',
27     'core/pubsub',
28     'core_message/message_drawer_view_contact',
29     'core_message/message_drawer_view_contacts',
30     'core_message/message_drawer_view_conversation',
31     'core_message/message_drawer_view_group_info',
32     'core_message/message_drawer_view_overview',
33     'core_message/message_drawer_view_search',
34     'core_message/message_drawer_view_settings',
35     'core_message/message_drawer_router',
36     'core_message/message_drawer_routes',
37     'core_message/message_drawer_events',
38     'core/pending',
39     'core/drawer',
40 ],
41 function(
42     $,
43     CustomEvents,
44     PubSub,
45     ViewContact,
46     ViewContacts,
47     ViewConversation,
48     ViewGroupInfo,
49     ViewOverview,
50     ViewSearch,
51     ViewSettings,
52     Router,
53     Routes,
54     Events,
55     Pending,
56     Drawer
57 ) {
59     var SELECTORS = {
60         DRAWER: '[data-region="right-hand-drawer"]',
61         JUMPTO: '.popover-region [data-region="jumpto"]',
62         PANEL_BODY_CONTAINER: '[data-region="panel-body-container"]',
63         PANEL_HEADER_CONTAINER: '[data-region="panel-header-container"]',
64         VIEW_CONTACT: '[data-region="view-contact"]',
65         VIEW_CONTACTS: '[data-region="view-contacts"]',
66         VIEW_CONVERSATION: '[data-region="view-conversation"]',
67         VIEW_GROUP_INFO: '[data-region="view-group-info"]',
68         VIEW_OVERVIEW: '[data-region="view-overview"]',
69         VIEW_SEARCH: '[data-region="view-search"]',
70         VIEW_SETTINGS: '[data-region="view-settings"]',
71         ROUTES: '[data-route]',
72         ROUTES_BACK: '[data-route-back]',
73         HEADER_CONTAINER: '[data-region="header-container"]',
74         BODY_CONTAINER: '[data-region="body-container"]',
75         FOOTER_CONTAINER: '[data-region="footer-container"]',
76         CLOSE_BUTTON: '[data-action="closedrawer"]'
77     };
79     /**
80      * Get elements for route.
81      *
82      * @param {String} namespace Unique identifier for the Routes
83      * @param {Object} root The message drawer container.
84      * @param {string} selector The route container.
85      *
86      * @return {array} elements Found route container objects.
87     */
88     var getParametersForRoute = function(namespace, root, selector) {
90         var header = root.find(SELECTORS.HEADER_CONTAINER).find(selector);
91         if (!header.length) {
92             header = root.find(SELECTORS.PANEL_HEADER_CONTAINER).find(selector);
93         }
94         var body = root.find(SELECTORS.BODY_CONTAINER).find(selector);
95         if (!body.length) {
96             body = root.find(SELECTORS.PANEL_BODY_CONTAINER).find(selector);
97         }
98         var footer = root.find(SELECTORS.FOOTER_CONTAINER).find(selector);
100         return [
101             namespace,
102             header.length ? header : null,
103             body.length ? body : null,
104             footer.length ? footer : null
105         ];
106     };
108     var routes = [
109         [Routes.VIEW_CONTACT, SELECTORS.VIEW_CONTACT, ViewContact.show, ViewContact.description],
110         [Routes.VIEW_CONTACTS, SELECTORS.VIEW_CONTACTS, ViewContacts.show, ViewContacts.description],
111         [Routes.VIEW_CONVERSATION, SELECTORS.VIEW_CONVERSATION, ViewConversation.show, ViewConversation.description],
112         [Routes.VIEW_GROUP_INFO, SELECTORS.VIEW_GROUP_INFO, ViewGroupInfo.show, ViewGroupInfo.description],
113         [Routes.VIEW_OVERVIEW, SELECTORS.VIEW_OVERVIEW, ViewOverview.show, ViewOverview.description],
114         [Routes.VIEW_SEARCH, SELECTORS.VIEW_SEARCH, ViewSearch.show, ViewSearch.description],
115         [Routes.VIEW_SETTINGS, SELECTORS.VIEW_SETTINGS, ViewSettings.show, ViewSettings.description]
116     ];
118     /**
119      * Create routes.
120      *
121      * @param {String} namespace Unique identifier for the Routes
122      * @param {Object} root The message drawer container.
123      */
124     var createRoutes = function(namespace, root) {
125         routes.forEach(function(route) {
126             Router.add(namespace, route[0], getParametersForRoute(namespace, root, route[1]), route[2], route[3]);
127         });
128     };
130     /**
131      * Show the message drawer.
132      *
133      * @param {string} namespace The route namespace.
134      * @param {Object} root The message drawer container.
135      */
136     var show = function(namespace, root) {
137         if (!root.attr('data-shown')) {
138             Router.go(namespace, Routes.VIEW_OVERVIEW);
139             root.attr('data-shown', true);
140         }
142         var drawerRoot = Drawer.getDrawerRoot(root);
143         if (drawerRoot.length) {
144             Drawer.show(drawerRoot);
145         }
146     };
148     /**
149      * Hide the message drawer.
150      *
151      * @param {Object} root The message drawer container.
152      */
153     var hide = function(root) {
154         var drawerRoot = Drawer.getDrawerRoot(root);
155         if (drawerRoot.length) {
156             Drawer.hide(drawerRoot);
157         }
158     };
160     /**
161      * Check if the drawer is visible.
162      *
163      * @param {Object} root The message drawer container.
164      * @return {boolean}
165      */
166     var isVisible = function(root) {
167         var drawerRoot = Drawer.getDrawerRoot(root);
168         if (drawerRoot.length) {
169             return Drawer.isVisible(drawerRoot);
170         }
171         return true;
172     };
174     /**
175      * Set Jump from button
176      *
177      * @param {String} buttonid The originating button id
178      */
179     var setJumpFrom = function(buttonid) {
180         $(SELECTORS.DRAWER).attr('data-origin', buttonid);
181     };
183     /**
184      * Listen to and handle events for routing, showing and hiding the message drawer.
185      *
186      * @param {string} namespace The route namespace.
187      * @param {Object} root The message drawer container.
188      * @param {bool} alwaysVisible Is this messaging app always shown?
189      */
190     var registerEventListeners = function(namespace, root, alwaysVisible) {
191         CustomEvents.define(root, [CustomEvents.events.activate]);
192         var paramRegex = /^data-route-param-?(\d*)$/;
194         root.on(CustomEvents.events.activate, SELECTORS.ROUTES, function(e, data) {
195             var element = $(e.target).closest(SELECTORS.ROUTES);
196             var route = element.attr('data-route');
197             var attributes = [];
199             for (var i = 0; i < element[0].attributes.length; i++) {
200                 attributes.push(element[0].attributes[i]);
201             }
203             var paramAttributes = attributes.filter(function(attribute) {
204                 var name = attribute.nodeName;
205                 var match = paramRegex.test(name);
206                 return match;
207             });
208             paramAttributes.sort(function(a, b) {
209                 var aParts = paramRegex.exec(a.nodeName);
210                 var bParts = paramRegex.exec(b.nodeName);
211                 var aIndex = aParts.length > 1 ? aParts[1] : 0;
212                 var bIndex = bParts.length > 1 ? bParts[1] : 0;
214                 if (aIndex < bIndex) {
215                     return -1;
216                 } else if (bIndex < aIndex) {
217                     return 1;
218                 } else {
219                     return 0;
220                 }
221             });
223             var params = paramAttributes.map(function(attribute) {
224                 return attribute.nodeValue;
225             });
227             var routeParams = [namespace, route].concat(params);
229             Router.go.apply(null, routeParams);
231             data.originalEvent.preventDefault();
232         });
234         root.on(CustomEvents.events.activate, SELECTORS.ROUTES_BACK, function(e, data) {
235             Router.back(namespace);
237             data.originalEvent.preventDefault();
238         });
240         // These are theme-specific to help us fix random behat fails.
241         // These events target those events defined in BS3 and BS4 onwards.
242         root.on('hide.bs.collapse', '.collapse', function(e) {
243             var pendingPromise = new Pending();
244             $(e.target).one('hidden.bs.collapse', function() {
245                 pendingPromise.resolve();
246             });
247         });
249         root.on('show.bs.collapse', '.collapse', function(e) {
250             var pendingPromise = new Pending();
251             $(e.target).one('shown.bs.collapse', function() {
252                 pendingPromise.resolve();
253             });
254         });
256         $(SELECTORS.JUMPTO).focus(function() {
257             var firstInput = root.find(SELECTORS.CLOSE_BUTTON);
258             if (firstInput.length) {
259                 firstInput.focus();
260             } else {
261                 $(SELECTORS.HEADER_CONTAINER).find(SELECTORS.ROUTES_BACK).focus();
262             }
263         });
265         $(SELECTORS.DRAWER).focus(function() {
266             var button = $(this).attr('data-origin');
267             if (button) {
268                 $('#' + button).focus();
269             }
270         });
272         if (!alwaysVisible) {
273             PubSub.subscribe(Events.SHOW, function() {
274                 show(namespace, root);
275             });
277             PubSub.subscribe(Events.HIDE, function() {
278                 hide(root);
279             });
281             PubSub.subscribe(Events.TOGGLE_VISIBILITY, function(buttonid) {
282                 if (isVisible(root)) {
283                     hide(root);
284                     $(SELECTORS.JUMPTO).attr('tabindex', -1);
285                 } else {
286                     show(namespace, root);
287                     setJumpFrom(buttonid);
288                     $(SELECTORS.JUMPTO).attr('tabindex', 0);
289                 }
290             });
291         }
293         PubSub.subscribe(Events.SHOW_CONVERSATION, function(args) {
294             setJumpFrom(args.buttonid);
295             show(namespace, root);
296             Router.go(namespace, Routes.VIEW_CONVERSATION, args.conversationid);
297         });
299         var closebutton = root.find(SELECTORS.CLOSE_BUTTON);
300         closebutton.on(CustomEvents.events.activate, function() {
301             var button = $(SELECTORS.DRAWER).attr('data-origin');
302             if (button) {
303                 $('#' + button).focus();
304             }
305             PubSub.publish(Events.TOGGLE_VISIBILITY);
306         });
308         PubSub.subscribe(Events.CREATE_CONVERSATION_WITH_USER, function(args) {
309             setJumpFrom(args.buttonid);
310             show(namespace, root);
311             Router.go(namespace, Routes.VIEW_CONVERSATION, null, 'create', args.userid);
312         });
314         PubSub.subscribe(Events.SHOW_SETTINGS, function() {
315             show(namespace, root);
316             Router.go(namespace, Routes.VIEW_SETTINGS);
317         });
319         PubSub.subscribe(Events.PREFERENCES_UPDATED, function(preferences) {
320             var filteredPreferences = preferences.filter(function(preference) {
321                 return preference.type == 'message_entertosend';
322             });
323             var enterToSendPreference = filteredPreferences.length ? filteredPreferences[0] : null;
325             if (enterToSendPreference) {
326                 var viewConversationFooter = root.find(SELECTORS.FOOTER_CONTAINER).find(SELECTORS.VIEW_CONVERSATION);
327                 viewConversationFooter.attr('data-enter-to-send', enterToSendPreference.value);
328             }
329         });
330     };
332     /**
333      * Initialise the message drawer.
334      *
335      * @param {Object} root The message drawer container.
336      * @param {String} uniqueId Unique identifier for the Routes
337      * @param {bool} alwaysVisible Should we show the app now, or wait for the user?
338      * @param {Object} route
339      */
340     var init = function(root, uniqueId, alwaysVisible, route) {
341         root = $(root);
342         createRoutes(uniqueId, root);
343         registerEventListeners(uniqueId, root, alwaysVisible);
345         if (alwaysVisible) {
346             show(uniqueId, root);
348             if (route) {
349                 var routeParams = route.params || [];
350                 routeParams = [uniqueId, route.path].concat(routeParams);
351                 Router.go.apply(null, routeParams);
352             }
353         }
354     };
356     return {
357         init: init,
358     };
359 });