Dropdown.php 4.09 KB
Newer Older
Antonio Ramirez committed
1 2 3 4 5 6 7 8 9 10
<?php
/**
 * @link http://www.yiiframework.com/
 * @copyright Copyright (c) 2008 Yii Software LLC
 * @license http://www.yiiframework.com/license/
 */

namespace yii\bootstrap;

use yii\base\InvalidConfigException;
11
use yii\helpers\ArrayHelper;
Antonio Ramirez committed
12
use yii\helpers\Html;
13
use yii\helpers\Url;
Antonio Ramirez committed
14 15

/**
16
 * Dropdown renders a Bootstrap dropdown menu component.
Antonio Ramirez committed
17
 *
MarsuBoss committed
18
 * @see http://getbootstrap.com/javascript/#dropdowns
Antonio Ramirez committed
19 20 21 22 23
 * @author Antonio Ramirez <amigo.cobos@gmail.com>
 * @since 2.0
 */
class Dropdown extends Widget
{
24 25 26 27 28
    /**
     * @var array list of menu items in the dropdown. Each array element can be either an HTML string,
     * or an array representing a single menu with the following structure:
     *
     * - label: string, required, the label of the item link
29 30
     * - url: string|array, optional, the url of the item link. This will be processed by [[Url::to()]].
     *   If not set, the item will be treated as a menu header when the item has no sub-menu.
31 32 33 34 35 36 37 38 39 40 41 42 43
     * - visible: boolean, optional, whether this menu item is visible. Defaults to true.
     * - linkOptions: array, optional, the HTML attributes of the item link.
     * - options: array, optional, the HTML attributes of the item.
     * - items: array, optional, the submenu items. The structure is the same as this property.
     *   Note that Bootstrap doesn't support dropdown submenu. You have to add your own CSS styles to support it.
     *
     * To insert divider use `<li role="presentation" class="divider"></li>`.
     */
    public $items = [];
    /**
     * @var boolean whether the labels for header items should be HTML-encoded.
     */
    public $encodeLabels = true;
Antonio Ramirez committed
44

45

46 47 48 49 50 51 52 53 54
    /**
     * Initializes the widget.
     * If you override this method, make sure you call the parent implementation first.
     */
    public function init()
    {
        parent::init();
        Html::addCssClass($this->options, 'dropdown-menu');
    }
Antonio Ramirez committed
55

56 57 58 59 60
    /**
     * Renders the widget.
     */
    public function run()
    {
61
        echo $this->renderItems($this->items, $this->options);
62
        BootstrapPluginAsset::register($this->getView());
63
    }
Antonio Ramirez committed
64

65 66
    /**
     * Renders menu items.
67
     * @param array $items the menu items to be rendered
68
     * @param array $options the container HTML attributes
69
     * @return string the rendering result.
70 71
     * @throws InvalidConfigException if the label option is not specified in one of the items.
     */
72
    protected function renderItems($items, $options = [])
73 74 75 76 77 78 79 80 81 82 83
    {
        $lines = [];
        foreach ($items as $i => $item) {
            if (isset($item['visible']) && !$item['visible']) {
                unset($items[$i]);
                continue;
            }
            if (is_string($item)) {
                $lines[] = $item;
                continue;
            }
84
            if (!array_key_exists('label', $item)) {
85 86
                throw new InvalidConfigException("The 'label' option is required.");
            }
87 88
            $encodeLabel = isset($item['encode']) ? $item['encode'] : $this->encodeLabels;
            $label = $encodeLabel ? Html::encode($item['label']) : $item['label'];
89
            $itemOptions = ArrayHelper::getValue($item, 'options', []);
90 91
            $linkOptions = ArrayHelper::getValue($item, 'linkOptions', []);
            $linkOptions['tabindex'] = '-1';
92 93 94 95 96 97 98 99 100 101 102 103 104 105
            $url = array_key_exists('url', $item) ? $item['url'] : null;
            if (empty($item['items'])) {
                if ($url === null) {
                    $content = $label;
                    Html::addCssClass($itemOptions, 'dropdown-header');
                } else {
                    $content = Html::a($label, $url, $linkOptions);
                }
            } else {
                $submenuOptions = $options;
                unset($submenuOptions['id']);
                $content = Html::a($label, $url === null ? '#' : $url, $linkOptions)
                    . $this->renderItems($item['items'], $submenuOptions);
                Html::addCssClass($itemOptions, 'dropdown-submenu');
106
            }
107 108

            $lines[] = Html::tag('li', $content, $itemOptions);
109
        }
Antonio Ramirez committed
110

111
        return Html::tag('ul', implode("\n", $lines), $options);
112
    }
113
}