MDL-72396 core: Allow setting of active tab

Allow easy setting of active tab for the navigation
views.
This commit is contained in:
Sujith Haridasan 2021-09-08 09:04:19 +05:30
parent 1d700796ca
commit c1a2df7f64
7 changed files with 139 additions and 19 deletions

View File

@ -47,6 +47,7 @@ if ($data = data_submitted() and confirm_sesskey() and isset($data->action) and
}
$PAGE->has_secondary_navigation_setter(false);
$PAGE->set_primary_active_tab('siteadminnode');
// and finally, if we get here, then there are matching settings and we have to print a form
// to modify them

View File

@ -65,6 +65,9 @@ $courserenderer = $PAGE->get_renderer('core', 'course');
$PAGE->set_heading($heading);
$content = $courserenderer->course_category($categoryid);
$PAGE->set_primary_active_tab('courses');
$PAGE->set_secondary_active_tab('categorymain');
echo $OUTPUT->header();
echo $OUTPUT->skip_link_target();
echo $content;

View File

@ -108,6 +108,7 @@ $PAGE->set_pagelayout('admin');
$PAGE->set_title($strmanagement);
$PAGE->set_heading($pageheading);
$PAGE->requires->js_call_amd('core_course/copy_modal', 'init', array($context->id));
$PAGE->set_secondary_active_tab('managecategory');
// This is a system level page that operates on other contexts.
require_login();

View File

@ -16,6 +16,8 @@
namespace core\navigation\views;
use navigation_node;
/**
* Class primary.
*
@ -63,7 +65,61 @@ class primary extends view {
}
// Search and set the active node.
$this->search_for_active_node();
$this->search_and_set_active_node($this);
$this->initialised = true;
}
/**
* Searches all children for the matching active node
*
* This method recursively traverse through the node tree to
* find the node to activate/highlight:
* 1. If the user had set primary node key to highlight, it
* tries to match this key with the node(s). Hence it would
* travel all the nodes.
* 2. If no primary key is provided by the dev, then it would
* check for the active node set in the tree.
*
* @param navigation_node $node
* @param array $actionnodes navigation nodes array to set active and inactive.
* @return navigation_node|null
*/
private function search_and_set_active_node(navigation_node $node,
array &$actionnodes = []): ?navigation_node {
global $PAGE;
$activekey = $PAGE->get_primary_activate_tab();
if ($activekey) {
if ($node->key && ($activekey === $node->key)) {
return $node;
}
} else if ($node->check_if_active(URL_MATCH_BASE)) {
return $node;
}
foreach ($node->children as $child) {
$outcome = $this->search_and_set_active_node($child, $actionnodes);
if ($outcome !== null) {
$outcome->make_active();
$actionnodes['active'] = $outcome;
if ($activekey === null) {
return $actionnodes['active'];
}
} else {
// If the child is active then make it inactive.
if ($child->isactive) {
$actionnodes['set_inactive'][] = $child;
}
}
}
// If we have successfully found an active node then reset any other nodes to inactive.
if (isset($actionnodes['set_inactive']) && isset($actionnodes['active'])) {
foreach ($actionnodes['set_inactive'] as $inactivenode) {
$inactivenode->make_inactive();
}
$actionnodes['set_inactive'] = [];
}
return ($actionnodes['active'] ?? null);
}
}

View File

@ -113,31 +113,42 @@ abstract class view extends navigation_node {
* @param int $strictness How stict to be with the scan for the active node.
* @return navigation_node|null
*/
protected function active_node_scan(navigation_node $node, int $strictness = URL_MATCH_EXACT): ?navigation_node {
protected function active_node_scan(navigation_node $node,
int $strictness = URL_MATCH_EXACT): ?navigation_node {
if ($node->check_if_active($strictness)) {
$result = null;
$activekey = $this->page->get_secondary_active_tab();
if ($activekey) {
if ($node->key && $activekey === $node->key) {
return $node;
}
} else if ($node->check_if_active($strictness)) {
return $node; // No need to continue, exit function.
}
if ($node->children->count() > 0) {
foreach ($node->children as $child) {
if ($this->active_node_scan($child, $strictness)) {
// If node is one of the new views then set the active node to the child.
if (!$node instanceof view) {
$node->make_active();
$child->make_inactive();
} else {
$child->make_active();
$this->activenode = $child;
}
return $node; // We have found the active node, set the parent status, no need to continue.
} else {
// Make sure to reset the active state.
foreach ($node->children as $child) {
if ($this->active_node_scan($child, $strictness)) {
// If node is one of the new views then set the active node to the child.
if (!$node instanceof view) {
$node->make_active();
$child->make_inactive();
$result = $node;
} else {
$child->make_active();
$this->activenode = $child;
$result = $child;
}
// If the secondary active tab not set then just return the result (fallback).
if ($activekey === null) {
return $result;
}
} else {
// Make sure to reset the active state.
$child->make_inactive();
}
}
return null;
return $result;
}
}

View File

@ -393,6 +393,16 @@ class moodle_page {
*/
protected $_hassecondarynavigation = true;
/**
* @var string the key of the secondary node to be activated.
*/
protected $_activekeysecondary = null;
/**
* @var string the key of the primary node to be activated.
*/
protected $_activenodeprimary = null;
/**
* Force the settings menu to be displayed on this page. This will only force the
* settings menu on an activity / resource page that is being displayed on a theme that
@ -2189,4 +2199,40 @@ class moodle_page {
public function has_secondary_navigation() : bool {
return $this->_hassecondarynavigation;
}
/**
* Set the key of the secondary nav node to be activated.
*
* @param string $navkey the key of the secondary nav node to be activated.
*/
public function set_secondary_active_tab(string $navkey) : void {
$this->_activekeysecondary = $navkey;
}
/**
* The key of secondary nav node to activate.
*
* @return string|null get the key of the secondary node to activate.
*/
public function get_secondary_active_tab(): ?string {
return $this->_activekeysecondary;
}
/**
* Set the key of the primary nav node to be activated.
*
* @param string $navkey
*/
public function set_primary_active_tab(string $navkey): void {
$this->_activenodeprimary = $navkey;
}
/**
* The key of the primary nav node to activate.
*
* @return string|null get the key of the primary nav node to activate.
*/
public function get_primary_activate_tab(): ?string {
return $this->_activenodeprimary;
}
}

View File

@ -90,6 +90,8 @@ $PAGE->set_subpage($currentpage->id);
$PAGE->set_title($pagetitle);
$PAGE->set_heading($header);
$PAGE->set_primary_active_tab('myhome');
if (!isguestuser()) { // Skip default home page for guests
if (get_home_page() != HOMEPAGE_MY) {
if (optional_param('setdefaulthome', false, PARAM_BOOL)) {