| 1 |
<?php |
| 2 |
|
| 3 |
namespace ipl\Web\Widget; |
| 4 |
|
| 5 |
use ipl\Html\Attributes; |
| 6 |
use ipl\Html\BaseHtmlElement; |
| 7 |
use ipl\Html\Html; |
| 8 |
use ipl\Web\Url; |
| 9 |
|
| 10 |
/** |
| 11 |
* Toggleable overlay dropdown element for displaying a list of links |
| 12 |
*/ |
| 13 |
class Dropdown extends BaseHtmlElement |
| 14 |
{ |
| 15 |
/** @var array */ |
| 16 |
protected $links = []; |
| 17 |
|
| 18 |
protected $defaultAttributes = ['class' => 'dropdown']; |
| 19 |
|
| 20 |
protected $tag = 'div'; |
| 21 |
|
| 22 |
/** |
| 23 |
* Create a dropdown element |
| 24 |
* |
| 25 |
* @param mixed $content |
| 26 |
* @param Attributes|array $attributes |
| 27 |
*/ |
| 28 |
public function __construct($content, $attributes = null) |
| 29 |
{ |
| 30 |
$toggle = new ActionLink($content, '#', null, [ |
| 31 |
'aria-expanded' => false, |
| 32 |
'aria-haspopup' => true, |
| 33 |
'class' => 'dropdown-toggle', |
| 34 |
'role' => 'button' |
| 35 |
]); |
| 36 |
|
| 37 |
$this |
| 38 |
->setContent($toggle) |
| 39 |
->getAttributes() |
| 40 |
->add($attributes); |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Add a link to the dropdown |
| 45 |
* |
| 46 |
* @param mixed $content |
| 47 |
* @param Url|string $url |
| 48 |
* @param string $icon |
| 49 |
* |
| 50 |
* @return $this |
| 51 |
*/ |
| 52 |
public function addLink($content, $url, $icon = null) |
| 53 |
{ |
| 54 |
$this->links[] = new ActionLink($content, $url, $icon, ['class' => 'dropdown-item']); |
| 55 |
|
| 56 |
return $this; |
| 57 |
} |
| 58 |
|
| 59 |
protected function assemble() |
| 60 |
{ |
| 61 |
$this->add(Html::tag('div', ['class' => 'dropdown-menu'], $this->links)); |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* Check if the dropdown element contains any links |
| 66 |
* |
| 67 |
* @return bool |
| 68 |
*/ |
| 69 |
public function hasLinks(): bool |
| 70 |
{ |
| 71 |
return ! empty($this->links); |
| 72 |
} |
| 73 |
} |