|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace macgyer\yii2materializecss\widgets; |
| 4 | + |
| 5 | +use macgyer\yii2materializecss\assets\MaterializePluginAsset; |
| 6 | +use macgyer\yii2materializecss\lib\BaseWidget; |
| 7 | +use macgyer\yii2materializecss\lib\Html; |
| 8 | +use yii\base\InvalidConfigException; |
| 9 | +use yii\helpers\ArrayHelper; |
| 10 | + |
| 11 | +/** |
| 12 | + * Dropdown renders a Bootstrap dropdown menu component. |
| 13 | + * |
| 14 | + * For example, |
| 15 | + * |
| 16 | + * ```php |
| 17 | + * <div class="dropdown"> |
| 18 | + * <a href="#" data-toggle="dropdown" class="dropdown-toggle">Label <b class="caret"></b></a> |
| 19 | + * <?php |
| 20 | + * echo Dropdown::widget([ |
| 21 | + * 'items' => [ |
| 22 | + * ['label' => 'DropdownA', 'url' => '/'], |
| 23 | + * ['label' => 'DropdownB', 'url' => '#'], |
| 24 | + * ], |
| 25 | + * ]); |
| 26 | + * ?> |
| 27 | + * </div> |
| 28 | + * ``` |
| 29 | + * @see http://getbootstrap.com/javascript/#dropdowns |
| 30 | + * @author Antonio Ramirez <amigo.cobos@gmail.com> |
| 31 | + * @since 2.0 |
| 32 | + */ |
| 33 | +class Dropdown extends BaseWidget |
| 34 | +{ |
| 35 | + /** |
| 36 | + * @var array list of menu items in the dropdown. Each array element can be either an HTML string, |
| 37 | + * or an array representing a single menu with the following structure: |
| 38 | + * |
| 39 | + * - label: string, required, the label of the item link |
| 40 | + * - url: string|array, optional, the url of the item link. This will be processed by [[Url::to()]]. |
| 41 | + * If not set, the item will be treated as a menu header when the item has no sub-menu. |
| 42 | + * - visible: boolean, optional, whether this menu item is visible. Defaults to true. |
| 43 | + * - linkOptions: array, optional, the HTML attributes of the item link. |
| 44 | + * - options: array, optional, the HTML attributes of the item. |
| 45 | + * - items: array, optional, the submenu items. The structure is the same as this property. |
| 46 | + * Note that Bootstrap doesn't support dropdown submenu. You have to add your own CSS styles to support it. |
| 47 | + * - submenuOptions: array, optional, the HTML attributes for sub-menu container tag. If specified it will be |
| 48 | + * merged with [[submenuOptions]]. |
| 49 | + * |
| 50 | + * To insert divider use `<li role="presentation" class="divider"></li>`. |
| 51 | + */ |
| 52 | + public $items = []; |
| 53 | + /** |
| 54 | + * @var boolean whether the labels for header items should be HTML-encoded. |
| 55 | + */ |
| 56 | + public $encodeLabels = true; |
| 57 | + /** |
| 58 | + * @var array|null the HTML attributes for sub-menu container tags. |
| 59 | + * If not set - [[options]] value will be used for it. |
| 60 | + * @since 2.0.5 |
| 61 | + */ |
| 62 | + public $submenuOptions; |
| 63 | + |
| 64 | + public $toggleTarget; |
| 65 | + |
| 66 | + /** |
| 67 | + * Initializes the widget. |
| 68 | + * If you override this method, make sure you call the parent implementation first. |
| 69 | + */ |
| 70 | + public function init() |
| 71 | + { |
| 72 | + if ($this->submenuOptions === null) { |
| 73 | + // copying of [[options]] kept for BC |
| 74 | + // @todo separate [[submenuOptions]] from [[options]] completely before 2.1 release |
| 75 | + $this->submenuOptions = $this->options; |
| 76 | + unset($this->submenuOptions['id']); |
| 77 | + } |
| 78 | + parent::init(); |
| 79 | + Html::addCssClass($this->options, ['widget' => 'dropdown-content']); |
| 80 | + |
| 81 | + $this->options['id'] = $this->toggleTarget; |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * Renders the widget. |
| 86 | + */ |
| 87 | + public function run() |
| 88 | + { |
| 89 | + MaterializePluginAsset::register($this->getView()); |
| 90 | + $this->registerClientEvents(); |
| 91 | + return $this->renderItems($this->items, $this->options); |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * Renders menu items. |
| 96 | + * @param array $items the menu items to be rendered |
| 97 | + * @param array $options the container HTML attributes |
| 98 | + * @return string the rendering result. |
| 99 | + * @throws InvalidConfigException if the label option is not specified in one of the items. |
| 100 | + */ |
| 101 | + protected function renderItems($items, $options = []) |
| 102 | + { |
| 103 | + $lines = []; |
| 104 | + foreach ($items as $item) { |
| 105 | + if (isset($item['visible']) && !$item['visible']) { |
| 106 | + continue; |
| 107 | + } |
| 108 | + if (is_string($item)) { |
| 109 | + $lines[] = $item; |
| 110 | + continue; |
| 111 | + } |
| 112 | + if (!array_key_exists('label', $item)) { |
| 113 | + throw new InvalidConfigException("The 'label' option is required."); |
| 114 | + } |
| 115 | + $encodeLabel = isset($item['encode']) ? $item['encode'] : $this->encodeLabels; |
| 116 | + $label = $encodeLabel ? Html::encode($item['label']) : $item['label']; |
| 117 | + $itemOptions = ArrayHelper::getValue($item, 'options', []); |
| 118 | + $linkOptions = ArrayHelper::getValue($item, 'linkOptions', []); |
| 119 | + $linkOptions['tabindex'] = '-1'; |
| 120 | + $url = array_key_exists('url', $item) ? $item['url'] : null; |
| 121 | + if (empty($item['items'])) { |
| 122 | + if ($url === null) { |
| 123 | + $content = $label; |
| 124 | + Html::addCssClass($itemOptions, ['widget' => 'dropdown-header']); |
| 125 | + } else { |
| 126 | + $content = Html::a($label, $url, $linkOptions); |
| 127 | + } |
| 128 | + } else { |
| 129 | + $submenuOptions = $this->submenuOptions; |
| 130 | + if (isset($item['submenuOptions'])) { |
| 131 | + $submenuOptions = array_merge($submenuOptions, $item['submenuOptions']); |
| 132 | + } |
| 133 | + $content = Html::a($label, $url === null ? '#' : $url, $linkOptions) |
| 134 | + . $this->renderItems($item['items'], $submenuOptions); |
| 135 | + Html::addCssClass($itemOptions, ['widget' => 'dropdown-submenu']); |
| 136 | + } |
| 137 | + |
| 138 | + $lines[] = Html::tag('li', $content, $itemOptions); |
| 139 | + } |
| 140 | + |
| 141 | + return Html::tag('ul', implode("\n", $lines), $options); |
| 142 | + } |
| 143 | +} |
0 commit comments