No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

257 líneas
8.6KB

  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. * 'options' => ['class' =>'nav-pills'], // set this to nav-tab to get tab-styled navigation
  36. * ]);
  37. * ```
  38. *
  39. * Note: Multilevel dropdowns beyond Level 1 are not supported in Bootstrap 3.
  40. *
  41. * @see http://getbootstrap.com/components/#dropdowns
  42. * @see http://getbootstrap.com/components/#nav
  43. *
  44. * @author Antonio Ramirez <amigo.cobos@gmail.com>
  45. * @since 2.0
  46. */
  47. class Nav extends Widget
  48. {
  49. /**
  50. * @var array list of items in the nav widget. Each array element represents a single
  51. * menu item which can be either a string or an array with the following structure:
  52. *
  53. * - label: string, required, the nav item label.
  54. * - url: optional, the item's URL. Defaults to "#".
  55. * - visible: boolean, optional, whether this menu item is visible. Defaults to true.
  56. * - linkOptions: array, optional, the HTML attributes of the item's link.
  57. * - options: array, optional, the HTML attributes of the item container (LI).
  58. * - active: boolean, optional, whether the item should be on active state or not.
  59. * - items: array|string, optional, the configuration array for creating a [[Dropdown]] widget,
  60. * or a string representing the dropdown menu. Note that Bootstrap does not support sub-dropdown menus.
  61. *
  62. * If a menu item is a string, it will be rendered directly without HTML encoding.
  63. */
  64. public $items = [];
  65. /**
  66. * @var boolean whether the nav items labels should be HTML-encoded.
  67. */
  68. public $encodeLabels = true;
  69. /**
  70. * @var boolean whether to automatically activate items according to whether their route setting
  71. * matches the currently requested route.
  72. * @see isItemActive
  73. */
  74. public $activateItems = true;
  75. /**
  76. * @var boolean whether to activate parent menu items when one of the corresponding child menu items is active.
  77. */
  78. public $activateParents = false;
  79. /**
  80. * @var string the route used to determine if a menu item is active or not.
  81. * If not set, it will use the route of the current request.
  82. * @see params
  83. * @see isItemActive
  84. */
  85. public $route;
  86. /**
  87. * @var array the parameters used to determine if a menu item is active or not.
  88. * If not set, it will use `$_GET`.
  89. * @see route
  90. * @see isItemActive
  91. */
  92. public $params;
  93. /**
  94. * Initializes the widget.
  95. */
  96. public function init()
  97. {
  98. parent::init();
  99. if ($this->route === null && Yii::$app->controller !== null) {
  100. $this->route = Yii::$app->controller->getRoute();
  101. }
  102. if ($this->params === null) {
  103. $this->params = Yii::$app->request->getQueryParams();
  104. }
  105. Html::addCssClass($this->options, 'nav');
  106. }
  107. /**
  108. * Renders the widget.
  109. */
  110. public function run()
  111. {
  112. BootstrapAsset::register($this->getView());
  113. return $this->renderItems();
  114. }
  115. /**
  116. * Renders widget items.
  117. */
  118. public function renderItems()
  119. {
  120. $items = [];
  121. foreach ($this->items as $i => $item) {
  122. if (isset($item['visible']) && !$item['visible']) {
  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. $encodeLabel = isset($item['encode']) ? $item['encode'] : $this->encodeLabels;
  144. $label = $encodeLabel ? Html::encode($item['label']) : $item['label'];
  145. $options = ArrayHelper::getValue($item, 'options', []);
  146. $items = ArrayHelper::getValue($item, 'items');
  147. $url = ArrayHelper::getValue($item, 'url', '#');
  148. $linkOptions = ArrayHelper::getValue($item, 'linkOptions', []);
  149. if (isset($item['active'])) {
  150. $active = ArrayHelper::remove($item, 'active', false);
  151. } else {
  152. $active = $this->isItemActive($item);
  153. }
  154. if ($items !== null) {
  155. $linkOptions['data-toggle'] = 'dropdown';
  156. Html::addCssClass($options, 'dropdown');
  157. Html::addCssClass($linkOptions, 'dropdown-toggle');
  158. $label .= ' ' . Html::tag('b', '', ['class' => 'caret']);
  159. if (is_array($items)) {
  160. if ($this->activateItems) {
  161. $items = $this->isChildActive($items, $active);
  162. }
  163. $items = $this->renderDropdown($items, $item);
  164. }
  165. }
  166. if ($this->activateItems && $active) {
  167. Html::addCssClass($options, 'active');
  168. }
  169. return Html::tag('li', Html::a($label, $url, $linkOptions) . $items, $options);
  170. }
  171. /**
  172. * Renders the given items as a dropdown.
  173. * This method is called to create sub-menus.
  174. * @param array $items the given items. Please refer to [[Dropdown::items]] for the array structure.
  175. * @param array $parentItem the parent item information. Please refer to [[items]] for the structure of this array.
  176. * @return string the rendering result.
  177. * @since 2.0.1
  178. */
  179. protected function renderDropdown($items, $parentItem)
  180. {
  181. return Dropdown::widget([
  182. 'items' => $items,
  183. 'encodeLabels' => $this->encodeLabels,
  184. 'clientOptions' => false,
  185. 'view' => $this->getView(),
  186. ]);
  187. }
  188. /**
  189. * Check to see if a child item is active optionally activating the parent.
  190. * @param array $items @see items
  191. * @param boolean $active should the parent be active too
  192. * @return array @see items
  193. */
  194. protected function isChildActive($items, &$active)
  195. {
  196. foreach ($items as $i => $child) {
  197. if (ArrayHelper::remove($items[$i], 'active', false) || $this->isItemActive($child)) {
  198. Html::addCssClass($items[$i]['options'], 'active');
  199. if ($this->activateParents) {
  200. $active = true;
  201. }
  202. }
  203. }
  204. return $items;
  205. }
  206. /**
  207. * Checks whether a menu item is active.
  208. * This is done by checking if [[route]] and [[params]] match that specified in the `url` option of the menu item.
  209. * When the `url` option of a menu item is specified in terms of an array, its first element is treated
  210. * as the route for the item and the rest of the elements are the associated parameters.
  211. * Only when its route and parameters match [[route]] and [[params]], respectively, will a menu item
  212. * be considered active.
  213. * @param array $item the menu item to be checked
  214. * @return boolean whether the menu item is active
  215. */
  216. protected function isItemActive($item)
  217. {
  218. if (isset($item['url']) && is_array($item['url']) && isset($item['url'][0])) {
  219. $route = $item['url'][0];
  220. if ($route[0] !== '/' && Yii::$app->controller) {
  221. $route = Yii::$app->controller->module->getUniqueId() . '/' . $route;
  222. }
  223. if (ltrim($route, '/') !== $this->route) {
  224. return false;
  225. }
  226. unset($item['url']['#']);
  227. if (count($item['url']) > 1) {
  228. foreach (array_splice($item['url'], 1) as $name => $value) {
  229. if ($value !== null && (!isset($this->params[$name]) || $this->params[$name] != $value)) {
  230. return false;
  231. }
  232. }
  233. }
  234. return true;
  235. }
  236. return false;
  237. }
  238. }