<?php
namespace PHPFUI;
class DropDown extends \PHPFUI\Base
{
private bool $hover = false;
public function __construct(\PHPFUI\HTML5Element $dropTarget, private HTML5Element $dropDown)
{
parent::__construct();
$this->dropDown->addClass('dropdown-pane');
$this->dropDown->addAttribute('data-dropdown');
$dropTarget->addAttribute('data-toggle', $this->dropDown->getId());
$this->add($dropTarget);
}
public function setAlignment(string $alignment) : static
{
$validAlignments = ['left',
'center',
'right', ];
if (! \in_array($alignment, $validAlignments))
{
throw new \Exception(__METHOD__ . ': $alignment must be one of (' . \implode(',', $validAlignments) . ')');
}
$this->dropDown->setAttribute('data-alignment', $alignment);
return $this;
}
public function setHover(bool $hover = true) : static
{
$this->hover = $hover;
return $this;
}
public function setPosition(string $position) : static
{
$validPositions = ['top',
'bottom',
'left',
'right', ];
if (! \in_array($position, $validPositions))
{
throw new \Exception(__METHOD__ . ': $position must be one of (' . \implode(',', $validPositions) . ')');
}
$this->dropDown->setAttribute('data-position', $position);
return $this;
}
protected function getBody() : string
{
return '';
}
protected function getEnd() : string
{
return (string)$this->dropDown;
}
protected function getStart() : string
{
if ($this->hover)
{
$this->dropDown->setAttribute('data-hover', 'true');
$this->dropDown->setAttribute('data-hover-pane', 'true');
}
else
{
$this->dropDown->setAttribute('data-auto-focus', 'true');
}
return '';
}
}