Nav.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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;
  9. use yii\base\InvalidConfigException;
  10. use yii\helpers\ArrayHelper;
  11. use yii\helpers\Html;
  12. /**
  13. * Nav renders a nav HTML component.
  14. *
  15. * For example:
  16. *
  17. * ```php
  18. * echo Nav::widget([
  19. * 'items' => [
  20. * [
  21. * 'label' => 'Home',
  22. * 'url' => ['site/index'],
  23. * 'linkOptions' => [...],
  24. * ],
  25. * [
  26. * 'label' => 'Dropdown',
  27. * 'items' => [
  28. * ['label' => 'Level 1 - Dropdown A', 'url' => '#'],
  29. * '<li class="divider"></li>',
  30. * '<li class="dropdown-header">Dropdown Header</li>',
  31. * ['label' => 'Level 1 - Dropdown B', 'url' => '#'],
  32. * ],
  33. * ],
  34. * ],
  35. * ]);
  36. * ```
  37. *
  38. * Note: Multilevel dropdowns beyond Level 1 are not supported in Bootstrap 3.
  39. *
  40. * @see http://getbootstrap.com/components/#dropdowns
  41. * @see http://getbootstrap.com/components/#nav
  42. *
  43. * @author Antonio Ramirez <amigo.cobos@gmail.com>
  44. * @since 2.0
  45. */
  46. class Nav extends Widget
  47. {
  48. /**
  49. * @var array list of items in the nav widget. Each array element represents a single
  50. * menu item which can be either a string or an array with the following structure:
  51. *
  52. * - label: string, required, the nav item label.
  53. * - url: optional, the item's URL. Defaults to "#".
  54. * - visible: boolean, optional, whether this menu item is visible. Defaults to true.
  55. * - linkOptions: array, optional, the HTML attributes of the item's link.
  56. * - options: array, optional, the HTML attributes of the item container (LI).
  57. * - active: boolean, optional, whether the item should be on active state or not.
  58. * - items: array|string, optional, the configuration array for creating a [[Dropdown]] widget,
  59. * or a string representing the dropdown menu. Note that Bootstrap does not support sub-dropdown menus.
  60. *
  61. * If a menu item is a string, it will be rendered directly without HTML encoding.
  62. */
  63. public $items = [];
  64. /**
  65. * @var boolean whether the nav items labels should be HTML-encoded.
  66. */
  67. public $encodeLabels = true;
  68. /**
  69. * @var boolean whether to automatically activate items according to whether their route setting
  70. * matches the currently requested route.
  71. * @see isItemActive
  72. */
  73. public $activateItems = true;
  74. /**
  75. * @var boolean whether to activate parent menu items when one of the corresponding child menu items is active.
  76. */
  77. public $activateParents = false;
  78. /**
  79. * @var string the route used to determine if a menu item is active or not.
  80. * If not set, it will use the route of the current request.
  81. * @see params
  82. * @see isItemActive
  83. */
  84. public $route;
  85. /**
  86. * @var array the parameters used to determine if a menu item is active or not.
  87. * If not set, it will use `$_GET`.
  88. * @see route
  89. * @see isItemActive
  90. */
  91. public $params;
  92. /**
  93. * Initializes the widget.
  94. */
  95. public function init()
  96. {
  97. parent::init();
  98. if ($this->route === null && Yii::$app->controller !== null) {
  99. $this->route = Yii::$app->controller->getRoute();
  100. }
  101. if ($this->params === null) {
  102. $this->params = Yii::$app->request->getQueryParams();
  103. }
  104. Html::addCssClass($this->options, 'nav');
  105. }
  106. /**
  107. * Renders the widget.
  108. */
  109. public function run()
  110. {
  111. echo $this->renderItems();
  112. BootstrapAsset::register($this->getView());
  113. }
  114. /**
  115. * Renders widget items.
  116. */
  117. public function renderItems()
  118. {
  119. $items = [];
  120. foreach ($this->items as $i => $item) {
  121. if (isset($item['visible']) && !$item['visible']) {
  122. unset($items[$i]);
  123. continue;
  124. }
  125. $items[] = $this->renderItem($item);
  126. }
  127. return Html::tag('ul', implode("\n", $items), $this->options);
  128. }
  129. /**
  130. * Renders a widget's item.
  131. * @param string|array $item the item to render.
  132. * @return string the rendering result.
  133. * @throws InvalidConfigException
  134. */
  135. public function renderItem($item)
  136. {
  137. if (is_string($item)) {
  138. return $item;
  139. }
  140. if (!isset($item['label'])) {
  141. throw new InvalidConfigException("The 'label' option is required.");
  142. }
  143. $label = $this->encodeLabels ? Html::encode($item['label']) : $item['label'];
  144. $options = ArrayHelper::getValue($item, 'options', []);
  145. $items = ArrayHelper::getValue($item, 'items');
  146. $url = ArrayHelper::getValue($item, 'url', '#');
  147. $linkOptions = ArrayHelper::getValue($item, 'linkOptions', []);
  148. if (isset($item['active'])) {
  149. $active = ArrayHelper::remove($item, 'active', false);
  150. } else {
  151. $active = $this->isItemActive($item);
  152. }
  153. if ($items !== null) {
  154. $linkOptions['data-toggle'] = 'dropdown';
  155. Html::addCssClass($options, 'dropdown');
  156. Html::addCssClass($linkOptions, 'dropdown-toggle');
  157. $label .= ' ' . Html::tag('b', '', ['class' => 'caret']);
  158. if (is_array($items)) {
  159. if ($this->activateItems) {
  160. $items = $this->isChildActive($items, $active);
  161. }
  162. $items = Dropdown::widget([
  163. 'items' => $items,
  164. 'encodeLabels' => $this->encodeLabels,
  165. 'clientOptions' => false,
  166. 'view' => $this->getView(),
  167. ]);
  168. }
  169. }
  170. if ($this->activateItems && $active) {
  171. Html::addCssClass($options, 'active');
  172. }
  173. return Html::tag('li', Html::a($label, $url, $linkOptions) . $items, $options);
  174. }
  175. /**
  176. * Check to see if a child item is active optionally activating the parent.
  177. * @param array $items @see items
  178. * @param boolean $active should the parent be active too
  179. * @return array @see items
  180. */
  181. protected function isChildActive($items, &$active)
  182. {
  183. foreach ($items as $i => $child) {
  184. if (ArrayHelper::remove($items[$i], 'active', false) || $this->isItemActive($child)) {
  185. Html::addCssClass($items[$i]['options'], 'active');
  186. if ($this->activateParents) {
  187. $active = true;
  188. }
  189. }
  190. }
  191. return $items;
  192. }
  193. /**
  194. * Checks whether a menu item is active.
  195. * This is done by checking if [[route]] and [[params]] match that specified in the `url` option of the menu item.
  196. * When the `url` option of a menu item is specified in terms of an array, its first element is treated
  197. * as the route for the item and the rest of the elements are the associated parameters.
  198. * Only when its route and parameters match [[route]] and [[params]], respectively, will a menu item
  199. * be considered active.
  200. * @param array $item the menu item to be checked
  201. * @return boolean whether the menu item is active
  202. */
  203. protected function isItemActive($item)
  204. {
  205. if (isset($item['url']) && is_array($item['url']) && isset($item['url'][0])) {
  206. $route = $item['url'][0];
  207. if ($route[0] !== '/' && Yii::$app->controller) {
  208. $route = Yii::$app->controller->module->getUniqueId() . '/' . $route;
  209. }
  210. if (ltrim($route, '/') !== $this->route) {
  211. return false;
  212. }
  213. unset($item['url']['#']);
  214. if (count($item['url']) > 1) {
  215. foreach (array_splice($item['url'], 1) as $name => $value) {
  216. if ($value !== null && (!isset($this->params[$name]) || $this->params[$name] != $value)) {
  217. return false;
  218. }
  219. }
  220. }
  221. return true;
  222. }
  223. return false;
  224. }
  225. }