Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

Dropdown.php 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. use yii\helpers\Html;
  11. use yii\helpers\Url;
  12. /**
  13. * Dropdown renders a Bootstrap dropdown menu component.
  14. *
  15. * For example,
  16. *
  17. * ```php
  18. * <div class="dropdown">
  19. * <a href="#" data-toggle="dropdown" class="dropdown-toggle">Label <b class="caret"></b></a>
  20. * <?php
  21. * echo Dropdown::widget([
  22. * 'items' => [
  23. * ['label' => 'DropdownA', 'url' => '/'],
  24. * ['label' => 'DropdownB', 'url' => '#'],
  25. * ],
  26. * ]);
  27. * ?>
  28. * </div>
  29. * ```
  30. * @see http://getbootstrap.com/javascript/#dropdowns
  31. * @author Antonio Ramirez <amigo.cobos@gmail.com>
  32. * @since 2.0
  33. */
  34. class Dropdown extends Widget
  35. {
  36. /**
  37. * @var array list of menu items in the dropdown. Each array element can be either an HTML string,
  38. * or an array representing a single menu with the following structure:
  39. *
  40. * - label: string, required, the label of the item link
  41. * - url: string|array, optional, the url of the item link. This will be processed by [[Url::to()]].
  42. * If not set, the item will be treated as a menu header when the item has no sub-menu.
  43. * - visible: boolean, optional, whether this menu item is visible. Defaults to true.
  44. * - linkOptions: array, optional, the HTML attributes of the item link.
  45. * - options: array, optional, the HTML attributes of the item.
  46. * - items: array, optional, the submenu items. The structure is the same as this property.
  47. * Note that Bootstrap doesn't support dropdown submenu. You have to add your own CSS styles to support it.
  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. * Initializes the widget.
  58. * If you override this method, make sure you call the parent implementation first.
  59. */
  60. public function init()
  61. {
  62. parent::init();
  63. Html::addCssClass($this->options, 'dropdown-menu');
  64. }
  65. /**
  66. * Renders the widget.
  67. */
  68. public function run()
  69. {
  70. BootstrapPluginAsset::register($this->getView());
  71. $this->registerClientEvents();
  72. return $this->renderItems($this->items, $this->options);
  73. }
  74. /**
  75. * Renders menu items.
  76. * @param array $items the menu items to be rendered
  77. * @param array $options the container HTML attributes
  78. * @return string the rendering result.
  79. * @throws InvalidConfigException if the label option is not specified in one of the items.
  80. */
  81. protected function renderItems($items, $options = [])
  82. {
  83. $lines = [];
  84. foreach ($items as $i => $item) {
  85. if (isset($item['visible']) && !$item['visible']) {
  86. continue;
  87. }
  88. if (is_string($item)) {
  89. $lines[] = $item;
  90. continue;
  91. }
  92. if (!array_key_exists('label', $item)) {
  93. throw new InvalidConfigException("The 'label' option is required.");
  94. }
  95. $encodeLabel = isset($item['encode']) ? $item['encode'] : $this->encodeLabels;
  96. $label = $encodeLabel ? Html::encode($item['label']) : $item['label'];
  97. $itemOptions = ArrayHelper::getValue($item, 'options', []);
  98. $linkOptions = ArrayHelper::getValue($item, 'linkOptions', []);
  99. $linkOptions['tabindex'] = '-1';
  100. $url = array_key_exists('url', $item) ? $item['url'] : null;
  101. if (empty($item['items'])) {
  102. if ($url === null) {
  103. $content = $label;
  104. Html::addCssClass($itemOptions, 'dropdown-header');
  105. } else {
  106. $content = Html::a($label, $url, $linkOptions);
  107. }
  108. } else {
  109. $submenuOptions = $options;
  110. unset($submenuOptions['id']);
  111. $content = Html::a($label, $url === null ? '#' : $url, $linkOptions)
  112. . $this->renderItems($item['items'], $submenuOptions);
  113. Html::addCssClass($itemOptions, 'dropdown-submenu');
  114. }
  115. $lines[] = Html::tag('li', $content, $itemOptions);
  116. }
  117. return Html::tag('ul', implode("\n", $lines), $options);
  118. }
  119. }