Skip to content

Commit

Permalink
Fix issue #1 conflict set 'li' and activeClass
Browse files Browse the repository at this point in the history
  • Loading branch information
davicotico committed Apr 22, 2020
1 parent f881b30 commit 4e03db3
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 17 deletions.
1 change: 1 addition & 0 deletions BootstrapMenu.php
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
}
}
37 changes: 22 additions & 15 deletions QuickMenu.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

/**
* Class Quick Menu
* @author David Ticona Saravia <davicotico@gmail.com>
* @version 0.7 (03/2017)
* @author David Ticona Saravia <davicotico@yandex.com>
* @version 1.0.1 (04/2020)
*/
class QuickMenu
{
Expand All @@ -12,7 +12,6 @@ class QuickMenu
private $activeClass = 'active';
protected $activeItem = '';
private $arrAttr = array();
private $strAttr = array();
private $arrData = array();
private $result = array();

Expand All @@ -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
Expand All @@ -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));
}

/**
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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);
}

/**
Expand All @@ -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}\"";
}
}
Expand All @@ -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 .= '<li' . $this->getAttr($li) . " {$active} >";
$activeClass = ($this->activeItem == $item['href']) ? $this->activeClass : '';
$str .= '<li' . $this->getAttr($li, $activeClass) . ">";
$str .= '<a href="' . $item['href'] . '" title="' . $item['title'] . '"' . $this->getAttr($a) . '>' . $this->getTextItem($item, $isParent) . '</a>';
if ($isParent)
{
Expand All @@ -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 .= '<li' . $this->getAttr($li) . '>';
$activeClass = ($this->activeItem == $item['href']) ? $this->activeClass : '';
$str .= '<li' . $this->getAttr($li, $activeClass) . '>';
$str .= "<a href=\"{$item['href']}\" target=\"{$item['target']}\"" . $this->getAttr($a) . '>' . $this->getTextItem($item, $isParent) . '</a>';
if ($isParent)
{
Expand Down
4 changes: 2 additions & 2 deletions ejemplo1.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -26,7 +26,7 @@
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]-->
<style>
body {padding-top: 70px};
body {padding-top: 70px}
</style>
</head>
<body>
Expand Down

0 comments on commit 4e03db3

Please sign in to comment.