moodle/message/amd/build/message_drawer_router.min.js
2023-03-09 09:53:19 +08:00

14 lines
3.8 KiB
JavaScript

/**
* A simple router for the message drawer that allows navigating between
* the "pages" in the drawer.
*
* This module will maintain a linear history of the unique pages access
* to allow navigating back.
*
* @module core_message/message_drawer_router
* @copyright 2018 Ryan Wyllie <ryan@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
define("core_message/message_drawer_router",["jquery","core/pubsub","core/str","core_message/message_drawer_events","core/aria"],(function($,PubSub,Str,MessageDrawerEvents,Aria){var routes={},history={},SELECTORS_CAN_RECEIVE_FOCUS='input:not([type="hidden"]), a[href], button, textarea, select, [tabindex]',SELECTORS_ROUTES_BACK="[data-route-back]",changeRoute=function(namespace,newRoute){var newConfig,fromPanel=[].slice.call(arguments).some((function(arg){return"frompanel"==arg})),args=[].slice.call(arguments,2),renderPromise=$.Deferred().resolve().promise();if(Object.keys(routes[namespace]).forEach((function(route){var config=routes[namespace][route],isMatch=route===newRoute;isMatch&&(newConfig=config),config.parameters.forEach((function(element){"object"==typeof element&&null!==element&&(element.removeClass("previous"),element.attr("data-from-panel",!1),isMatch?(fromPanel&&element.attr("data-from-panel",!0),element.removeClass("hidden"),Aria.unhide(element.get())):element.attr("data-in-panel")&&"view-search"!=newRoute&&"view-overview"!=newRoute||(element.addClass("hidden"),Aria.hide(element.get())))}))})),newConfig&&newConfig.onGo){renderPromise=newConfig.onGo.apply(void 0,newConfig.parameters.concat(args));for(var currentFocusElement=$(document.activeElement),hasFocus=!1,firstFocusable=null,i=1;i<newConfig.parameters.length;i++){var element=newConfig.parameters[i];if("object"==typeof element&&null!==element&&(firstFocusable||(firstFocusable=element),element.has(currentFocusElement).length)){hasFocus=!0;break}}hasFocus||firstFocusable.find(SELECTORS_CAN_RECEIVE_FOCUS).filter(":visible").first().focus()}var record={route:newRoute,params:args,renderPromise:renderPromise};return PubSub.publish(MessageDrawerEvents.ROUTE_CHANGED,record),record},go=function(namespace){var currentFocusElement=$(document.activeElement),record=changeRoute.apply(namespace,arguments),inHistory=!1;history[namespace]||(history[namespace]=[]),history[namespace]=history[namespace].reduce((function(carry,previous){return previous.route===record.route&&(inHistory=!0),inHistory||carry.push(previous),carry}),[]);var historylength=history[namespace].length,previousRecord=historylength?history[namespace][historylength-1]:null;if(previousRecord){for(var prevConfig=routes[namespace][previousRecord.route],elements=prevConfig.parameters,i=1;i<elements.length;i++)"object"==typeof elements[i]&&null!==elements[i]&&elements[i].addClass("previous");previousRecord.focusElement=currentFocusElement,prevConfig.getDescription&&prevConfig.getDescription.apply(null,prevConfig.parameters.concat(previousRecord.params)).then((function(description){return Str.get_string("backto","core_message",description)})).then((function(label){return record.renderPromise.then((function(){routes[namespace][record.route].parameters.forEach((function(element){"object"==typeof element&&element&&element.find(SELECTORS_ROUTES_BACK).attr("aria-label",label)}))}))})).catch((function(){}))}return history[namespace].push(record),record};return{add:function(namespace,route,parameters,onGo,getDescription){routes[namespace]||(routes[namespace]=[]),routes[namespace][route]={parameters:parameters,onGo:onGo,getDescription:getDescription}},go:go,back:function(namespace){if(history[namespace].length){history[namespace].pop();var previous=history[namespace].pop();previous&&(go.apply(void 0,[namespace,previous.route].concat(previous.params)),window.setTimeout((function(){previous.focusElement.focus()}),50))}}}}));
//# sourceMappingURL=message_drawer_router.min.js.map