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 |
* A simple router for the message drawer that allows navigating between
|
|
|
18 |
* the "pages" in the drawer.
|
|
|
19 |
*
|
|
|
20 |
* This module will maintain a linear history of the unique pages access
|
|
|
21 |
* to allow navigating back.
|
|
|
22 |
*
|
|
|
23 |
* @module core_message/message_drawer_router
|
|
|
24 |
* @copyright 2018 Ryan Wyllie <ryan@moodle.com>
|
|
|
25 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
26 |
*/
|
|
|
27 |
define(
|
|
|
28 |
[
|
|
|
29 |
'jquery',
|
|
|
30 |
'core/pubsub',
|
|
|
31 |
'core/str',
|
|
|
32 |
'core_message/message_drawer_events',
|
|
|
33 |
'core/aria',
|
11 |
efrain |
34 |
'core/pending',
|
1 |
efrain |
35 |
],
|
|
|
36 |
function(
|
|
|
37 |
$,
|
|
|
38 |
PubSub,
|
|
|
39 |
Str,
|
|
|
40 |
MessageDrawerEvents,
|
11 |
efrain |
41 |
Aria,
|
|
|
42 |
PendingPromise,
|
1 |
efrain |
43 |
) {
|
|
|
44 |
|
|
|
45 |
/* @var {object} routes Message drawer route elements and callbacks. */
|
|
|
46 |
var routes = {};
|
|
|
47 |
|
|
|
48 |
/* @var {object} history Store for route objects history. */
|
|
|
49 |
var history = {};
|
|
|
50 |
|
|
|
51 |
var SELECTORS = {
|
|
|
52 |
CAN_RECEIVE_FOCUS: 'input:not([type="hidden"]), a[href], button, textarea, select, [tabindex]',
|
|
|
53 |
ROUTES_BACK: '[data-route-back]'
|
|
|
54 |
};
|
|
|
55 |
|
|
|
56 |
/**
|
|
|
57 |
* Add a route.
|
|
|
58 |
*
|
|
|
59 |
* @param {String} namespace Unique identifier for the Routes
|
|
|
60 |
* @param {string} route Route config name.
|
|
|
61 |
* @param {array} parameters Route parameters.
|
|
|
62 |
* @param {callback} onGo Route initialization function.
|
|
|
63 |
* @param {callback} getDescription Route initialization function.
|
|
|
64 |
*/
|
|
|
65 |
var add = function(namespace, route, parameters, onGo, getDescription) {
|
|
|
66 |
if (!routes[namespace]) {
|
|
|
67 |
routes[namespace] = [];
|
|
|
68 |
}
|
|
|
69 |
|
|
|
70 |
routes[namespace][route] =
|
|
|
71 |
{
|
|
|
72 |
parameters: parameters,
|
|
|
73 |
onGo: onGo,
|
|
|
74 |
getDescription: getDescription
|
|
|
75 |
};
|
|
|
76 |
};
|
|
|
77 |
|
|
|
78 |
/**
|
|
|
79 |
* Go to a defined route and run the route callbacks.
|
|
|
80 |
*
|
|
|
81 |
* @param {String} namespace Unique identifier for the Routes
|
|
|
82 |
* @param {string} newRoute Route config name.
|
|
|
83 |
* @return {object} record Current route record with route config name and parameters.
|
|
|
84 |
*/
|
|
|
85 |
var changeRoute = function(namespace, newRoute) {
|
|
|
86 |
var newConfig;
|
11 |
efrain |
87 |
var pendingPromise = new PendingPromise(`message-drawer-router-${namespace}-${newRoute}`);
|
1 |
efrain |
88 |
|
|
|
89 |
// Check if the Route change call is made from an element in the app panel.
|
|
|
90 |
var fromPanel = [].slice.call(arguments).some(function(arg) {
|
|
|
91 |
return arg == 'frompanel';
|
|
|
92 |
});
|
|
|
93 |
// Get the rest of the arguments, if any.
|
|
|
94 |
var args = [].slice.call(arguments, 2);
|
|
|
95 |
var renderPromise = $.Deferred().resolve().promise();
|
|
|
96 |
|
|
|
97 |
Object.keys(routes[namespace]).forEach(function(route) {
|
|
|
98 |
var config = routes[namespace][route];
|
|
|
99 |
var isMatch = route === newRoute;
|
|
|
100 |
|
|
|
101 |
if (isMatch) {
|
|
|
102 |
newConfig = config;
|
|
|
103 |
}
|
|
|
104 |
|
|
|
105 |
config.parameters.forEach(function(element) {
|
|
|
106 |
// Some parameters may be null, or not an element.
|
|
|
107 |
if (typeof element !== 'object' || element === null) {
|
|
|
108 |
return;
|
|
|
109 |
}
|
|
|
110 |
|
|
|
111 |
element.removeClass('previous');
|
|
|
112 |
element.attr('data-from-panel', false);
|
|
|
113 |
|
|
|
114 |
if (isMatch) {
|
|
|
115 |
if (fromPanel) {
|
|
|
116 |
// Set this attribute to let the conversation renderer know not to show a back button.
|
|
|
117 |
element.attr('data-from-panel', true);
|
|
|
118 |
}
|
|
|
119 |
element.removeClass('hidden');
|
|
|
120 |
Aria.unhide(element.get());
|
|
|
121 |
} else {
|
|
|
122 |
// For the message index page elements in the left panel should not be hidden.
|
|
|
123 |
if (!element.attr('data-in-panel')) {
|
|
|
124 |
element.addClass('hidden');
|
|
|
125 |
Aria.hide(element.get());
|
|
|
126 |
} else if (newRoute == 'view-search' || newRoute == 'view-overview') {
|
|
|
127 |
element.addClass('hidden');
|
|
|
128 |
Aria.hide(element.get());
|
|
|
129 |
}
|
|
|
130 |
}
|
|
|
131 |
});
|
|
|
132 |
});
|
|
|
133 |
|
|
|
134 |
if (newConfig) {
|
|
|
135 |
if (newConfig.onGo) {
|
|
|
136 |
renderPromise = newConfig.onGo.apply(undefined, newConfig.parameters.concat(args));
|
|
|
137 |
var currentFocusElement = $(document.activeElement);
|
|
|
138 |
var hasFocus = false;
|
|
|
139 |
var firstFocusable = null;
|
|
|
140 |
|
|
|
141 |
// No need to start at 0 as we know that is the namespace.
|
|
|
142 |
for (var i = 1; i < newConfig.parameters.length; i++) {
|
|
|
143 |
var element = newConfig.parameters[i];
|
|
|
144 |
|
|
|
145 |
// Some parameters may be null, or not an element.
|
|
|
146 |
if (typeof element !== 'object' || element === null) {
|
|
|
147 |
continue;
|
|
|
148 |
}
|
|
|
149 |
|
|
|
150 |
if (!firstFocusable) {
|
|
|
151 |
firstFocusable = element;
|
|
|
152 |
}
|
|
|
153 |
|
|
|
154 |
if (element.has(currentFocusElement).length) {
|
|
|
155 |
hasFocus = true;
|
|
|
156 |
break;
|
|
|
157 |
}
|
|
|
158 |
}
|
|
|
159 |
|
|
|
160 |
if (!hasFocus) {
|
|
|
161 |
// This page doesn't have focus yet so focus the first focusable
|
|
|
162 |
// element in the new view.
|
|
|
163 |
firstFocusable.find(SELECTORS.CAN_RECEIVE_FOCUS).filter(':visible').first().focus();
|
|
|
164 |
}
|
|
|
165 |
}
|
|
|
166 |
}
|
|
|
167 |
|
|
|
168 |
var record = {
|
|
|
169 |
route: newRoute,
|
|
|
170 |
params: args,
|
|
|
171 |
renderPromise: renderPromise
|
|
|
172 |
};
|
|
|
173 |
|
|
|
174 |
PubSub.publish(MessageDrawerEvents.ROUTE_CHANGED, record);
|
|
|
175 |
|
11 |
efrain |
176 |
renderPromise.then(() => pendingPromise.resolve());
|
1 |
efrain |
177 |
return record;
|
|
|
178 |
};
|
|
|
179 |
|
|
|
180 |
/**
|
|
|
181 |
* Go to a defined route and store the route history.
|
|
|
182 |
*
|
|
|
183 |
* @param {String} namespace Unique identifier for the Routes
|
|
|
184 |
* @return {object} record Current route record with route config name and parameters.
|
|
|
185 |
*/
|
|
|
186 |
var go = function(namespace) {
|
|
|
187 |
var currentFocusElement = $(document.activeElement);
|
|
|
188 |
|
|
|
189 |
var record = changeRoute.apply(namespace, arguments);
|
|
|
190 |
var inHistory = false;
|
|
|
191 |
|
|
|
192 |
if (!history[namespace]) {
|
|
|
193 |
history[namespace] = [];
|
|
|
194 |
}
|
|
|
195 |
|
|
|
196 |
// History stores a unique list of routes. Check to see if the new route
|
|
|
197 |
// is already in the history, if it is then forget all history after it.
|
|
|
198 |
// This ensures there are no duplicate routes in history and that it represents
|
|
|
199 |
// a linear path of routes (it never stores something like [foo, bar, foo])).
|
|
|
200 |
history[namespace] = history[namespace].reduce(function(carry, previous) {
|
|
|
201 |
if (previous.route === record.route) {
|
|
|
202 |
inHistory = true;
|
|
|
203 |
}
|
|
|
204 |
|
|
|
205 |
if (!inHistory) {
|
|
|
206 |
carry.push(previous);
|
|
|
207 |
}
|
|
|
208 |
|
|
|
209 |
return carry;
|
|
|
210 |
}, []);
|
|
|
211 |
|
|
|
212 |
var historylength = history[namespace].length;
|
|
|
213 |
var previousRecord = historylength ? history[namespace][historylength - 1] : null;
|
|
|
214 |
|
|
|
215 |
if (previousRecord) {
|
|
|
216 |
var prevConfig = routes[namespace][previousRecord.route];
|
|
|
217 |
var elements = prevConfig.parameters;
|
|
|
218 |
|
|
|
219 |
// The first one will be the namespace, skip it.
|
|
|
220 |
for (var i = 1; i < elements.length; i++) {
|
|
|
221 |
// Some parameters may be null, or not an element.
|
|
|
222 |
if (typeof elements[i] !== 'object' || elements[i] === null) {
|
|
|
223 |
continue;
|
|
|
224 |
}
|
|
|
225 |
|
|
|
226 |
elements[i].addClass('previous');
|
|
|
227 |
}
|
|
|
228 |
|
|
|
229 |
previousRecord.focusElement = currentFocusElement;
|
|
|
230 |
|
|
|
231 |
if (prevConfig.getDescription) {
|
|
|
232 |
// If the route has a description then set it on the back button for
|
|
|
233 |
// the new page we're displaying.
|
|
|
234 |
prevConfig.getDescription.apply(null, prevConfig.parameters.concat(previousRecord.params))
|
|
|
235 |
.then(function(description) {
|
|
|
236 |
return Str.get_string('backto', 'core_message', description);
|
|
|
237 |
})
|
|
|
238 |
.then(function(label) {
|
|
|
239 |
// Wait for the new page to finish rendering so that we know
|
|
|
240 |
// that the back button is visible.
|
|
|
241 |
return record.renderPromise.then(function() {
|
|
|
242 |
// Find the elements for the new route we displayed.
|
|
|
243 |
routes[namespace][record.route].parameters.forEach(function(element) {
|
|
|
244 |
// Some parameters may be null, or not an element.
|
|
|
245 |
if (typeof element !== 'object' || !element) {
|
|
|
246 |
return;
|
|
|
247 |
}
|
|
|
248 |
// Update the aria label for the back button.
|
|
|
249 |
element.find(SELECTORS.ROUTES_BACK).attr('aria-label', label);
|
|
|
250 |
});
|
|
|
251 |
});
|
|
|
252 |
})
|
|
|
253 |
.catch(function() {
|
|
|
254 |
// Silently ignore.
|
|
|
255 |
});
|
|
|
256 |
}
|
|
|
257 |
}
|
|
|
258 |
history[namespace].push(record);
|
|
|
259 |
return record;
|
|
|
260 |
};
|
|
|
261 |
|
|
|
262 |
/**
|
|
|
263 |
* Go back to the previous route record stored in history.
|
|
|
264 |
*
|
|
|
265 |
* @param {String} namespace Unique identifier for the Routes
|
|
|
266 |
*/
|
|
|
267 |
var back = function(namespace) {
|
|
|
268 |
if (history[namespace].length) {
|
|
|
269 |
// Remove the current route.
|
|
|
270 |
history[namespace].pop();
|
|
|
271 |
var previous = history[namespace].pop();
|
|
|
272 |
|
|
|
273 |
if (previous) {
|
|
|
274 |
// If we have a previous route then show it.
|
|
|
275 |
go.apply(undefined, [namespace, previous.route].concat(previous.params));
|
|
|
276 |
// Delay the focus 50 milliseconds otherwise it doesn't correctly
|
|
|
277 |
// focus the element for some reason...
|
|
|
278 |
window.setTimeout(function() {
|
|
|
279 |
previous.focusElement.focus();
|
|
|
280 |
}, 50);
|
|
|
281 |
}
|
|
|
282 |
}
|
|
|
283 |
};
|
|
|
284 |
|
|
|
285 |
return {
|
|
|
286 |
add: add,
|
|
|
287 |
go: go,
|
|
|
288 |
back: back
|
|
|
289 |
};
|
|
|
290 |
});
|