Dropdown.php 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. /**
  12. * Dropdown renders a Bootstrap dropdown menu component.
  13. *
  14. * @see http://getbootstrap.com/javascript/#dropdowns
  15. * @author Antonio Ramirez <amigo.cobos@gmail.com>
  16. * @since 2.0
  17. */
  18. class Dropdown extends Widget
  19. {
  20. /**
  21. * @var array list of menu items in the dropdown. Each array element can be either an HTML string,
  22. * or an array representing a single menu with the following structure:
  23. *
  24. * - label: string, required, the label of the item link
  25. * - url: string, optional, the url of the item link. Defaults to "#".
  26. * - visible: boolean, optional, whether this menu item is visible. Defaults to true.
  27. * - linkOptions: array, optional, the HTML attributes of the item link.
  28. * - options: array, optional, the HTML attributes of the item.
  29. * - items: array, optional, the submenu items. The structure is the same as this property.
  30. * Note that Bootstrap doesn't support dropdown submenu. You have to add your own CSS styles to support it.
  31. *
  32. * To insert divider use `<li role="presentation" class="divider"></li>`.
  33. */
  34. public $items = [];
  35. /**
  36. * @var boolean whether the labels for header items should be HTML-encoded.
  37. */
  38. public $encodeLabels = true;
  39. /**
  40. * Initializes the widget.
  41. * If you override this method, make sure you call the parent implementation first.
  42. */
  43. public function init()
  44. {
  45. parent::init();
  46. Html::addCssClass($this->options, 'dropdown-menu');
  47. }
  48. /**
  49. * Renders the widget.
  50. */
  51. public function run()
  52. {
  53. echo $this->renderItems($this->items);
  54. $this->registerPlugin('dropdown');
  55. }
  56. /**
  57. * Renders menu items.
  58. * @param array $items the menu items to be rendered
  59. * @return string the rendering result.
  60. * @throws InvalidConfigException if the label option is not specified in one of the items.
  61. */
  62. protected function renderItems($items)
  63. {
  64. $lines = [];
  65. foreach ($items as $i => $item) {
  66. if (isset($item['visible']) && !$item['visible']) {
  67. unset($items[$i]);
  68. continue;
  69. }
  70. if (is_string($item)) {
  71. $lines[] = $item;
  72. continue;
  73. }
  74. if (!isset($item['label'])) {
  75. throw new InvalidConfigException("The 'label' option is required.");
  76. }
  77. $label = $this->encodeLabels ? Html::encode($item['label']) : $item['label'];
  78. $options = ArrayHelper::getValue($item, 'options', []);
  79. $linkOptions = ArrayHelper::getValue($item, 'linkOptions', []);
  80. $linkOptions['tabindex'] = '-1';
  81. $content = Html::a($label, ArrayHelper::getValue($item, 'url', '#'), $linkOptions);
  82. if (!empty($item['items'])) {
  83. $content .= $this->renderItems($item['items']);
  84. Html::addCssClass($options, 'dropdown-submenu');
  85. }
  86. $lines[] = Html::tag('li', $content, $options);
  87. }
  88. return Html::tag('ul', implode("\n", $lines), $this->options);
  89. }
  90. }