diff --git a/BootstrapMenu.php b/BootstrapMenu.php index 5d1e1d3..e71af70 100644 --- a/BootstrapMenu.php +++ b/BootstrapMenu.php @@ -11,6 +11,7 @@ public function __construct($options = array()) $this->set('ul-root', array('class'=>'nav navbar-nav', 'id'=>'#myMenu')); $this->set('ul', array('class'=>'dropdown-menu')); $this->set('li-parent', array('class'=>'dropdown')); + $this->set('li', array('class'=>'test-classitem')); $this->set('a-parent', array('class'=>"dropdown-toggle", 'data-toggle'=>"dropdown", 'role'=>"button", 'aria-haspopup'=>"true", 'aria-expanded'=>"false")); } } diff --git a/QuickMenu.php b/QuickMenu.php index 52c40bf..f361b4e 100644 --- a/QuickMenu.php +++ b/QuickMenu.php @@ -2,8 +2,8 @@ /** * Class Quick Menu - * @author David Ticona Saravia - * @version 0.7 (03/2017) + * @author David Ticona Saravia + * @version 1.0.1 (04/2020) */ class QuickMenu { @@ -12,7 +12,6 @@ class QuickMenu private $activeClass = 'active'; protected $activeItem = ''; private $arrAttr = array(); - private $strAttr = array(); private $arrData = array(); private $result = array(); @@ -34,11 +33,16 @@ public function __construct($options = array()) */ public function set($name, $value) { - $tags = array('ul', 'ul-root', 'li', 'li-parent', 'a', 'a-parent', 'active-class'); + $tags = array('ul', 'ul-root', 'li', 'li-parent', 'a', 'a-parent'); if (in_array($name, $tags)) { $this->arrAttr[$name] = $value; } + /* legacy */ + if ($name=='active-class') + { + $this->activeClass = $value; + } } /** * Set dropdown icon for display with submenus @@ -59,8 +63,8 @@ public function setActiveItem($href, $activeClass = '') if ($activeClass != '') { $this->activeClass = $activeClass; + $this->set('active-class', $this->activeClass); /* legacy */ } - $this->set('active-class', array('class' => $this->activeClass)); } /** @@ -168,10 +172,6 @@ public function html() { return $this->buildFromResult($this->result); } - foreach ($this->arrAttr as $tag => $attr) - { - $this->strAttr[$tag] = $this->buildAttributes($tag); - } return $this->build($this->arrData); } @@ -208,11 +208,12 @@ public function setResult($result, $columnID, $columnParent) /** * @param string $tag + * @param string $extra Add css class * @return string Tag Attributes stored */ - protected function getAttr($tag) + protected function getAttr($tag, $extra = '') { - return isset($this->strAttr[$tag]) ? $this->strAttr[$tag] : ''; + return $this->buildAttributes($tag, $extra); } /** @@ -230,15 +231,20 @@ protected function getTextItem($item, $isParent) /** * Renderize the tag attributes from array * @param string $tag The tag + * @param string $extra append a css class * @return string The string atributes */ - private function buildAttributes($tag) + private function buildAttributes($tag, $extra = '') { $str = ''; if (isset($this->arrAttr[$tag])) { foreach ($this->arrAttr[$tag] as $name => $value) { + if (($extra!='')&&($name=='class')) + { + $value = "{$value} {$extra}"; + } $str .= " {$name}=\"{$value}\""; } } @@ -259,8 +265,8 @@ protected function build($array, $depth = 0) $isParent = isset($item['children']); $li = ($isParent) ? 'li-parent' : 'li'; $a = ($isParent) ? 'a-parent' : 'a'; - $active = ($this->activeItem == $item['href']) ? $this->getAttr('active-class') : ''; - $str .= 'getAttr($li) . " {$active} >"; + $activeClass = ($this->activeItem == $item['href']) ? $this->activeClass : ''; + $str .= 'getAttr($li, $activeClass) . ">"; $str .= 'getAttr($a) . '>' . $this->getTextItem($item, $isParent) . ''; if ($isParent) { @@ -287,7 +293,8 @@ protected function buildFromResult(array $array, $parent = 0, $level = 0) $isParent = isset($array[$item_id]); $li = ($isParent) ? 'li-parent' : 'li'; $a = ($isParent) ? 'a-parent' : 'a'; - $str .= 'getAttr($li) . '>'; + $activeClass = ($this->activeItem == $item['href']) ? $this->activeClass : ''; + $str .= 'getAttr($li, $activeClass) . '>'; $str .= "getAttr($a) . '>' . $this->getTextItem($item, $isParent) . ''; if ($isParent) { diff --git a/ejemplo1.php b/ejemplo1.php index 9412737..fd99357 100644 --- a/ejemplo1.php +++ b/ejemplo1.php @@ -5,7 +5,7 @@ include "BootstrapMenu.php"; $str = '[{"text":"Home", "href": "#home", "title": "Home"}, {"text":"About", "href": "#", "title": "About", "children": [{"text":"Action", "href": "#action", "title": "Action"}, {"text":"Another action", "href": "#another", "title": "Another action"}]}, {"text":"Something else here", "href": "#something", "title": "Something else here"}]'; $qMenu = new BootstrapMenu(array('data'=>$str)); -$qMenu->setActiveItem('http://codeignitertutoriales.com'); +$qMenu->setActiveItem('http://codeignitertutoriales.com', 'active'); $qMenu->insert(array("text"=>'Ooh!', "href"=>'http://codeignitertutoriales.com', "title"=>'Awesome'), 'Another action', 'About'); $qMenu->insert(array("text"=>'Ultimo item', "href"=>'https://github.com/davicotico', "title"=>'My Github')); $qMenu->replace(array('text'=>'About Wow', 'href'=>'about', 'title'=>'Hey'), 'Home'); @@ -26,7 +26,7 @@