|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace macgyer\yii2materializecss\widgets; |
| 4 | + |
| 5 | +use macgyer\yii2materializecss\lib\BaseWidget; |
| 6 | +use macgyer\yii2materializecss\lib\Html; |
| 7 | +use yii\base\InvalidConfigException; |
| 8 | +use yii\helpers\ArrayHelper; |
| 9 | + |
| 10 | +/** |
| 11 | + * Class FixedActionButton |
| 12 | + * @package macgyer\yii2materializecss\widgets |
| 13 | + */ |
| 14 | +class FixedActionButton extends BaseWidget |
| 15 | +{ |
| 16 | + /** |
| 17 | + * @var array list of button items in the fixed action button. Each element can be either an HTML string |
| 18 | + * or an array representing a single item with the following specification: |
| 19 | + * |
| 20 | + * - label: string, required, the label of the item link |
| 21 | + * - url: string|array, optional, the url of the item link. This will be processed by [[Url::to()]]. |
| 22 | + * - visible: boolean, optional, whether this menu item is visible. Defaults to true. |
| 23 | + * - linkOptions: array, optional, the HTML attributes of the item link. |
| 24 | + * - options: array, optional, the HTML attributes of the item. |
| 25 | + **/ |
| 26 | + public $items; |
| 27 | + |
| 28 | + /** |
| 29 | + * @var boolean whether the labels for header items should be HTML-encoded. |
| 30 | + */ |
| 31 | + public $encodeLabels = true; |
| 32 | + |
| 33 | + /** |
| 34 | + * @var array the HTML attributes for the widget container tag |
| 35 | + * |
| 36 | + * @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered. |
| 37 | + */ |
| 38 | + public $options = ['class' => 'fixed-action-btn']; |
| 39 | + |
| 40 | + /** |
| 41 | + * @var array the HTML attributes for the container around the button items |
| 42 | + * |
| 43 | + * @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered. |
| 44 | + */ |
| 45 | + public $itemsContainerOptions = []; |
| 46 | + |
| 47 | + /** |
| 48 | + * @var bool whether the button items are only visible after click |
| 49 | + */ |
| 50 | + public $clickToToggle = false; |
| 51 | + |
| 52 | + /** |
| 53 | + * @var bool whether to display a horizontal FAB |
| 54 | + */ |
| 55 | + public $horizontal = false; |
| 56 | + |
| 57 | + /** |
| 58 | + * @var string the tag used to render the button |
| 59 | + * |
| 60 | + * Should be a for correct functionality of click to toggle FAB |
| 61 | + */ |
| 62 | + public $buttonTagName = 'a'; |
| 63 | + |
| 64 | + /** |
| 65 | + * @var string the label on the button |
| 66 | + */ |
| 67 | + public $buttonLabel = 'Button'; |
| 68 | + |
| 69 | + /** |
| 70 | + * @var bool whether the label should be HTML-encoded. |
| 71 | + */ |
| 72 | + public $buttonEncodeLabel = true; |
| 73 | + |
| 74 | + /** |
| 75 | + * @var array the options for the optional icon |
| 76 | + * |
| 77 | + * To specify an icon you can use the following parameters |
| 78 | + * |
| 79 | + * ```php |
| 80 | + * [ |
| 81 | + * 'name' => 'name of the icon', // required |
| 82 | + * 'position' => 'position of the icon', // optional, 'left' or 'right', defaults to 'left' |
| 83 | + * 'options' => 'the HTML attributes for the img', // optional |
| 84 | + * ] |
| 85 | + * ``` |
| 86 | + */ |
| 87 | + public $buttonIcon; |
| 88 | + |
| 89 | + /** |
| 90 | + * @var array the HTML attributes for the visible button |
| 91 | + * |
| 92 | + * @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered. |
| 93 | + */ |
| 94 | + public $buttonOptions = []; |
| 95 | + |
| 96 | + /** |
| 97 | + * Initialize the widget. |
| 98 | + */ |
| 99 | + public function init() |
| 100 | + { |
| 101 | + parent::init(); |
| 102 | + |
| 103 | + Html::addCssClass($this->buttonOptions, ['widget' => 'btn-floating']); |
| 104 | + |
| 105 | + if ($this->clickToToggle) { |
| 106 | + Html::addCssClass($this->options, ['container' => 'click-to-toggle']); |
| 107 | + } |
| 108 | + |
| 109 | + if ($this->horizontal) { |
| 110 | + Html::addCssClass($this->options, ['containerLayout' => 'horizontal']); |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * Executes the widget. |
| 116 | + * @return string the result of widget execution to be outputted. |
| 117 | + */ |
| 118 | + public function run() |
| 119 | + { |
| 120 | + $html = Html::beginTag('div', $this->options); |
| 121 | + |
| 122 | + // Visible button |
| 123 | + $html .= Button::widget([ |
| 124 | + 'tagName' => $this->buttonTagName, |
| 125 | + 'label' => $this->buttonLabel, |
| 126 | + 'encodeLabel' => $this->buttonEncodeLabel, |
| 127 | + 'options' => $this->buttonOptions, |
| 128 | + 'icon' => $this->buttonIcon |
| 129 | + ]); |
| 130 | + |
| 131 | + $html .= $this->renderItems(); |
| 132 | + $html .= Html::endTag('div'); |
| 133 | + |
| 134 | + return $html; |
| 135 | + } |
| 136 | + |
| 137 | + /** |
| 138 | + * @return string |
| 139 | + * @throws InvalidConfigException |
| 140 | + */ |
| 141 | + protected function renderItems() |
| 142 | + { |
| 143 | + $elements = []; |
| 144 | + $items = $this->items; |
| 145 | + |
| 146 | + foreach ($items as $item) { |
| 147 | + if (isset($item['visible']) && !$item['visible']) { |
| 148 | + continue; |
| 149 | + } |
| 150 | + |
| 151 | + if (is_string($item)) { |
| 152 | + $elements[] = $item; |
| 153 | + continue; |
| 154 | + } |
| 155 | + |
| 156 | + if (!array_key_exists('label', $item)) { |
| 157 | + throw new InvalidConfigException("The 'label' option is required."); |
| 158 | + } |
| 159 | + |
| 160 | + $encodeLabel = isset($item['encode']) ? $item['encode'] : $this->encodeLabels; |
| 161 | + $label = $encodeLabel ? Html::encode($item['label']) : $item['label']; |
| 162 | + |
| 163 | + $itemOptions = ArrayHelper::getValue($item, 'options', []); |
| 164 | + $linkOptions = ArrayHelper::getValue($item, 'linkOptions', []); |
| 165 | + |
| 166 | + $url = array_key_exists('url', $item) ? $item['url'] : null; |
| 167 | + if ($url === null) { |
| 168 | + $content = $label; |
| 169 | + } else { |
| 170 | + $content = Html::a($label, $url, $linkOptions); |
| 171 | + } |
| 172 | + |
| 173 | + $elements[] = Html::tag('li', $content, $itemOptions); |
| 174 | + } |
| 175 | + |
| 176 | + return Html::tag('ul', implode("\n", $elements), $this->itemsContainerOptions); |
| 177 | + } |
| 178 | +} |
0 commit comments