-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathDropdownLink.php
More file actions
105 lines (94 loc) · 2.64 KB
/
DropdownLink.php
File metadata and controls
105 lines (94 loc) · 2.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
<?php
declare(strict_types=1);
/**
* This file is part of the EaseTWBootstrap4 package
*
* https://github.com/VitexSoftware/php-ease-twbootstrap4
*
* (c) Vítězslav Dvořák <http://vitexsoftware.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Ease\TWB4;
/**
* Description of Dropdown.
*
* @author Vítězslav Dvořák <info@vitexsoftware.cz>
*/
class DropdownLink extends \Ease\Html\DivTag
{
private \Ease\Html\DivTag $dropdownMenu;
/**
* Dropdown menu.
*
* @param string $heading
* @param string $type one of primary|secondary|success|danger|warning|info|light|dark|link
* @param array $items
*/
public function __construct(
$heading,
$type = 'link',
$items = [],
array $properties = [],
) {
$properties['class'] = 'dropdown';
$handle = $this->handle($heading, $type);
parent::__construct($handle, $properties);
$this->dropdownMenu = new \Ease\Html\DivTag(
null,
['class' => 'dropdown-menu', 'aria-labelledby' => $handle->getTagID()],
);
if (!empty($items)) {
foreach ($items as $url => $label) {
$this->addDropdownItem($label, $url);
}
}
}
/**
* Dropdown handle.
*
* @param string $heading
* @param string $type one of primary|secondary|success|danger|warning|info|light|dark|link
*
* @return \Ease\Html\ATag
*/
public function handle($heading, $type)
{
$handle = new \Ease\Html\ATag(
'#',
$heading,
['class' => 'nav-link '.$type.' dropdown-toggle', 'type' => 'button',
'data-toggle' => 'dropdown', 'role' => 'button', 'aria-haspopup' => 'true',
'aria-expanded' => 'false'],
);
$handle->setTagID($heading);
return $handle;
}
/**
* add one dropdown item.
*
* @param string $label or empty for divider
* @param string $url
*/
public function addDropdownItem($label, $url): void
{
if (empty($label)) {
$this->dropdownMenu->addItem(new \Ease\Html\DivTag(
null,
['class' => 'dropdown-divider'],
));
} else {
$this->dropdownMenu->addItem(new \Ease\Html\ATag(
$url,
$label,
['class' => 'dropdown-item'],
));
}
}
public function finalize(): void
{
$this->addItem($this->dropdownMenu);
parent::finalize();
}
}