選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

Dropdown.php 5.2KB

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