Skip to content

Commit 4e03db3

Browse files
committed
Fix issue #1 conflict set 'li' and activeClass
1 parent f881b30 commit 4e03db3

File tree

3 files changed

+25
-17
lines changed

3 files changed

+25
-17
lines changed

BootstrapMenu.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ public function __construct($options = array())
1111
$this->set('ul-root', array('class'=>'nav navbar-nav', 'id'=>'#myMenu'));
1212
$this->set('ul', array('class'=>'dropdown-menu'));
1313
$this->set('li-parent', array('class'=>'dropdown'));
14+
$this->set('li', array('class'=>'test-classitem'));
1415
$this->set('a-parent', array('class'=>"dropdown-toggle", 'data-toggle'=>"dropdown", 'role'=>"button", 'aria-haspopup'=>"true", 'aria-expanded'=>"false"));
1516
}
1617
}

QuickMenu.php

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
/**
44
* Class Quick Menu
5-
* @author David Ticona Saravia <davicotico@gmail.com>
6-
* @version 0.7 (03/2017)
5+
* @author David Ticona Saravia <davicotico@yandex.com>
6+
* @version 1.0.1 (04/2020)
77
*/
88
class QuickMenu
99
{
@@ -12,7 +12,6 @@ class QuickMenu
1212
private $activeClass = 'active';
1313
protected $activeItem = '';
1414
private $arrAttr = array();
15-
private $strAttr = array();
1615
private $arrData = array();
1716
private $result = array();
1817

@@ -34,11 +33,16 @@ public function __construct($options = array())
3433
*/
3534
public function set($name, $value)
3635
{
37-
$tags = array('ul', 'ul-root', 'li', 'li-parent', 'a', 'a-parent', 'active-class');
36+
$tags = array('ul', 'ul-root', 'li', 'li-parent', 'a', 'a-parent');
3837
if (in_array($name, $tags))
3938
{
4039
$this->arrAttr[$name] = $value;
4140
}
41+
/* legacy */
42+
if ($name=='active-class')
43+
{
44+
$this->activeClass = $value;
45+
}
4246
}
4347
/**
4448
* Set dropdown icon for display with submenus
@@ -59,8 +63,8 @@ public function setActiveItem($href, $activeClass = '')
5963
if ($activeClass != '')
6064
{
6165
$this->activeClass = $activeClass;
66+
$this->set('active-class', $this->activeClass); /* legacy */
6267
}
63-
$this->set('active-class', array('class' => $this->activeClass));
6468
}
6569

6670
/**
@@ -168,10 +172,6 @@ public function html()
168172
{
169173
return $this->buildFromResult($this->result);
170174
}
171-
foreach ($this->arrAttr as $tag => $attr)
172-
{
173-
$this->strAttr[$tag] = $this->buildAttributes($tag);
174-
}
175175
return $this->build($this->arrData);
176176
}
177177

@@ -208,11 +208,12 @@ public function setResult($result, $columnID, $columnParent)
208208

209209
/**
210210
* @param string $tag
211+
* @param string $extra Add css class
211212
* @return string Tag Attributes stored
212213
*/
213-
protected function getAttr($tag)
214+
protected function getAttr($tag, $extra = '')
214215
{
215-
return isset($this->strAttr[$tag]) ? $this->strAttr[$tag] : '';
216+
return $this->buildAttributes($tag, $extra);
216217
}
217218

218219
/**
@@ -230,15 +231,20 @@ protected function getTextItem($item, $isParent)
230231
/**
231232
* Renderize the tag attributes from array
232233
* @param string $tag The tag
234+
* @param string $extra append a css class
233235
* @return string The string atributes
234236
*/
235-
private function buildAttributes($tag)
237+
private function buildAttributes($tag, $extra = '')
236238
{
237239
$str = '';
238240
if (isset($this->arrAttr[$tag]))
239241
{
240242
foreach ($this->arrAttr[$tag] as $name => $value)
241243
{
244+
if (($extra!='')&&($name=='class'))
245+
{
246+
$value = "{$value} {$extra}";
247+
}
242248
$str .= " {$name}=\"{$value}\"";
243249
}
244250
}
@@ -259,8 +265,8 @@ protected function build($array, $depth = 0)
259265
$isParent = isset($item['children']);
260266
$li = ($isParent) ? 'li-parent' : 'li';
261267
$a = ($isParent) ? 'a-parent' : 'a';
262-
$active = ($this->activeItem == $item['href']) ? $this->getAttr('active-class') : '';
263-
$str .= '<li' . $this->getAttr($li) . " {$active} >";
268+
$activeClass = ($this->activeItem == $item['href']) ? $this->activeClass : '';
269+
$str .= '<li' . $this->getAttr($li, $activeClass) . ">";
264270
$str .= '<a href="' . $item['href'] . '" title="' . $item['title'] . '"' . $this->getAttr($a) . '>' . $this->getTextItem($item, $isParent) . '</a>';
265271
if ($isParent)
266272
{
@@ -287,7 +293,8 @@ protected function buildFromResult(array $array, $parent = 0, $level = 0)
287293
$isParent = isset($array[$item_id]);
288294
$li = ($isParent) ? 'li-parent' : 'li';
289295
$a = ($isParent) ? 'a-parent' : 'a';
290-
$str .= '<li' . $this->getAttr($li) . '>';
296+
$activeClass = ($this->activeItem == $item['href']) ? $this->activeClass : '';
297+
$str .= '<li' . $this->getAttr($li, $activeClass) . '>';
291298
$str .= "<a href=\"{$item['href']}\" target=\"{$item['target']}\"" . $this->getAttr($a) . '>' . $this->getTextItem($item, $isParent) . '</a>';
292299
if ($isParent)
293300
{

ejemplo1.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
include "BootstrapMenu.php";
66
$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"}]';
77
$qMenu = new BootstrapMenu(array('data'=>$str));
8-
$qMenu->setActiveItem('http://codeignitertutoriales.com');
8+
$qMenu->setActiveItem('http://codeignitertutoriales.com', 'active');
99
$qMenu->insert(array("text"=>'Ooh!', "href"=>'http://codeignitertutoriales.com', "title"=>'Awesome'), 'Another action', 'About');
1010
$qMenu->insert(array("text"=>'Ultimo item', "href"=>'https://github.com/davicotico', "title"=>'My Github'));
1111
$qMenu->replace(array('text'=>'About Wow', 'href'=>'about', 'title'=>'Hey'), 'Home');
@@ -26,7 +26,7 @@
2626
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
2727
<![endif]-->
2828
<style>
29-
body {padding-top: 70px};
29+
body {padding-top: 70px}
3030
</style>
3131
</head>
3232
<body>

0 commit comments

Comments
 (0)