SideNavWidget.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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 common\widgets;
  8. use Yii;
  9. use yii\base\InvalidConfigException;
  10. use yii\bootstrap\BootstrapAsset;
  11. use yii\bootstrap\Widget;
  12. use yii\helpers\ArrayHelper;
  13. use yii\helpers\Url;
  14. use yii\helpers\Html;
  15. /**
  16. * Nav renders a nav HTML component.
  17. *
  18. * For example:
  19. *
  20. * ```php
  21. * echo Nav::widget([
  22. * 'items' => [
  23. * [
  24. * 'label' => 'Home',
  25. * 'url' => ['site/index'],
  26. * 'linkOptions' => [...],
  27. * ],
  28. * [
  29. * 'label' => 'Dropdown',
  30. * 'items' => [
  31. * ['label' => 'Level 1 - Dropdown A', 'url' => '#'],
  32. * '<li class="divider"></li>',
  33. * '<li class="dropdown-header">Dropdown Header</li>',
  34. * ['label' => 'Level 1 - Dropdown B', 'url' => '#'],
  35. * ],
  36. * ],
  37. * ],
  38. * ]);
  39. * ```
  40. *
  41. * Note: Multilevel dropdowns beyond Level 1 are not supported in Bootstrap 3.
  42. *
  43. * @see http://getbootstrap.com/components.html#dropdowns
  44. * @see http://getbootstrap.com/components/#nav
  45. *
  46. * @author Antonio Ramirez <amigo.cobos@gmail.com>
  47. * @since 2.0
  48. */
  49. class SideNavWidget extends Widget
  50. {
  51. /**
  52. * @var array list of items in the nav widget. Each array element represents a single
  53. * menu item which can be either a string or an array with the following structure:
  54. *
  55. * - label: string, required, the nav item label.
  56. * - url: optional, the item's URL. Defaults to "#".
  57. * - visible: boolean, optional, whether this menu item is visible. Defaults to true.
  58. * - linkOptions: array, optional, the HTML attributes of the item's link.
  59. * - options: array, optional, the HTML attributes of the item container (LI).
  60. * - active: boolean, optional, whether the item should be on active state or not.
  61. * - items: array|string, optional, the configuration array for creating a [[Dropdown]] widget,
  62. * or a string representing the dropdown menu. Note that Bootstrap does not support sub-dropdown menus.
  63. *
  64. * If a menu item is a string, it will be rendered directly without HTML encoding.
  65. */
  66. public $items = [];
  67. /**
  68. * @var boolean whether the nav items labels should be HTML-encoded.
  69. */
  70. public $encodeLabels = true;
  71. /**
  72. * @var string the route used to determine if a menu item is active or not.
  73. * If not set, it will use the route of the current request.
  74. * @see params
  75. * @see isItemActive
  76. */
  77. public $activeUrl;
  78. /**
  79. * Initializes the widget.
  80. */
  81. public function init()
  82. {
  83. parent::init();
  84. if (!isset($this->options['class'])) {
  85. Html::addCssClass($this->options, 'list-group');
  86. }
  87. }
  88. /**
  89. * Renders the widget.
  90. */
  91. public function run()
  92. {
  93. $this->view->registerCss(".submenu .list-group-item{padding-left:60px;}");
  94. echo $this->renderItems();
  95. BootstrapAsset::register($this->getView());
  96. }
  97. /**
  98. * Renders widget items.
  99. */
  100. public function renderItems()
  101. {
  102. $items = [];
  103. foreach ($this->items as $i => $item) {
  104. if (isset($item['visible']) && !$item['visible']) {
  105. unset($items[$i]);
  106. continue;
  107. }
  108. $items[] = $this->renderItem($item, count($this->items) !== 1);
  109. }
  110. return Html::tag('div', implode("\n", $items), $this->options);
  111. }
  112. /**
  113. * Renders a widget's item.
  114. * @param string|array $item the item to render.
  115. * @param bool $collapsed whether to collapse item if not active
  116. * @throws \yii\base\InvalidConfigException
  117. * @return string the rendering result.
  118. * @throws InvalidConfigException if label is not defined
  119. */
  120. public function renderItem($item, $collapsed = true)
  121. {
  122. if (is_string($item)) {
  123. return $item;
  124. }
  125. if (!isset($item['label'])) {
  126. throw new InvalidConfigException("The 'label' option is required.");
  127. }
  128. $label = $this->encodeLabels ? Html::encode($item['label']) : $item['label'];
  129. $items = ArrayHelper::getValue($item, 'items');
  130. $url = Url::to(ArrayHelper::getValue($item, 'url', '#'));
  131. $linkOptions = ArrayHelper::getValue($item, 'linkOptions', []);
  132. Html::addCssClass($linkOptions, 'list-group-item');
  133. if (isset($item['active'])) {
  134. $active = ArrayHelper::remove($item, 'active', false);
  135. } else {
  136. $active = ($url == $this->activeUrl);
  137. }
  138. if ($items !== null) {
  139. $id = $this->id . '-' . static::$counter++;
  140. if ($url == '#') {
  141. $url = '#' . $id;
  142. $linkOptions['data-toggle'] = 'collapse';
  143. $linkOptions['data-parent'] = '#' . $this->id;
  144. }
  145. $label = Html::tag('b', '', ['class' => 'caret', 'data-toggle' => 'collapse', 'data-parent' => '#' . $this->id, 'href' => '#' . $id]) . ' ' . $label;
  146. if (is_array($items)) {
  147. if ($active === false) {
  148. foreach ($items as $subItem) {
  149. if (isset($subItem['active']) && $subItem['active']) {
  150. $active = true;
  151. }
  152. }
  153. }
  154. $items = static::widget([
  155. 'id' => $id,
  156. 'items' => $items,
  157. 'encodeLabels' => $this->encodeLabels,
  158. 'view' => $this->getView(),
  159. 'options' => [
  160. 'class' => "submenu panel-collapse collapse" . ($active || !$collapsed ? ' in' : '')
  161. ]
  162. ]);
  163. }
  164. }
  165. if ($active) {
  166. Html::addCssClass($linkOptions, 'active');
  167. }
  168. return Html::a($label, $url, $linkOptions) . $items;
  169. }
  170. }