From d38a4849b79952aca80d22f7d80bae0ca8939ef4 Mon Sep 17 00:00:00 2001 From: Sam Hemelryk Date: Thu, 23 Jun 2011 11:29:26 +0800 Subject: [PATCH] MDL-27940 navigaiton: Fixed an ordering problem so that when a parent was set for a node the nodes activity was accounted for --- lib/navigationlib.php | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/lib/navigationlib.php b/lib/navigationlib.php index 297fb8244f4..1c24fa2c866 100644 --- a/lib/navigationlib.php +++ b/lib/navigationlib.php @@ -117,7 +117,7 @@ class navigation_node implements renderable { public $mainnavonly = false; /** @var bool If set to true a title will be added to the action no matter what */ public $forcetitle = false; - /** @var navigation_node A reference to the node parent */ + /** @var navigation_node A reference to the node parent, you should never set this directly you should always call set_parent */ public $parent = null; /** @var bool Override to not display the icon even if one is provided **/ public $hideicon = false; @@ -169,9 +169,6 @@ class navigation_node implements renderable { if (array_key_exists('key', $properties)) { $this->key = $properties['key']; } - if (array_key_exists('parent', $properties)) { - $this->parent = $properties['parent']; - } // This needs to happen last because of the check_if_active call that occurs if (array_key_exists('action', $properties)) { $this->action = $properties['action']; @@ -182,6 +179,9 @@ class navigation_node implements renderable { $this->check_if_active(); } } + if (array_key_exists('parent', $properties)) { + $this->set_parent($properties['parent']); + } } else if (is_string($properties)) { $this->text = $properties; } @@ -305,7 +305,7 @@ class navigation_node implements renderable { $this->nodetype = self::NODETYPE_BRANCH; } // Set the parent to this node - $childnode->parent = $this; + $childnode->set_parent($this); // Default the key to the number of children if not provided if ($childnode->key === null) { @@ -648,6 +648,24 @@ class navigation_node implements renderable { } return array(array($tabs, $rows), $selected, $inactive, $activated, $return); } + + /** + * Sets the parent for this node and if this node is active ensures that the tree is properly + * adjusted as well. + * + * @param navigation_node $parent + */ + public function set_parent(navigation_node $parent) { + // Set the parent (thats the easy part) + $this->parent = $parent; + // Check if this node is active (this is checked during construction) + if ($this->isactive) { + // Force all of the parent nodes open so you can see this node + $this->parent->force_open(); + // Make all parents inactive so that its clear where we are. + $this->parent->make_inactive(); + } + } } /** -- 2.43.0