Proyectos de Subversion Moodle

Rev

Ir a la última revisión | | Ultima modificación | Ver Log |

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