|
- <?php
-
-
- namespace yii\bootstrap;
-
- use Yii;
- use yii\base\InvalidConfigException;
- use yii\helpers\ArrayHelper;
-
-
- class Nav extends Widget
- {
-
-
- public $items = [];
-
-
- public $encodeLabels = true;
-
-
- public $activateItems = true;
-
-
- public $activateParents = false;
-
-
- public $route;
-
-
- public $params;
-
-
- public $dropDownCaret;
-
-
-
-
- public function init()
- {
- parent::init();
- if ($this->route === null && Yii::$app->controller !== null) {
- $this->route = Yii::$app->controller->getRoute();
- }
- if ($this->params === null) {
- $this->params = Yii::$app->request->getQueryParams();
- }
- if ($this->dropDownCaret === null) {
- $this->dropDownCaret = Html::tag('b', '', ['class' => 'caret']);
- }
- Html::addCssClass($this->options, ['widget' => 'nav']);
- }
-
-
-
- public function run()
- {
- BootstrapAsset::register($this->getView());
- return $this->renderItems();
- }
-
-
-
- public function renderItems()
- {
- $items = [];
- foreach ($this->items as $i => $item) {
- if (isset($item['visible']) && !$item['visible']) {
- continue;
- }
- $items[] = $this->renderItem($item);
- }
-
- return Html::tag('ul', implode("\n", $items), $this->options);
- }
-
-
-
- public function renderItem($item)
- {
- if (is_string($item)) {
- return $item;
- }
- if (!isset($item['label'])) {
- throw new InvalidConfigException("The 'label' option is required.");
- }
- $encodeLabel = isset($item['encode']) ? $item['encode'] : $this->encodeLabels;
- $label = $encodeLabel ? Html::encode($item['label']) : $item['label'];
- $options = ArrayHelper::getValue($item, 'options', []);
- $items = ArrayHelper::getValue($item, 'items');
- $url = ArrayHelper::getValue($item, 'url', '#');
- $linkOptions = ArrayHelper::getValue($item, 'linkOptions', []);
-
- if (isset($item['active'])) {
- $active = ArrayHelper::remove($item, 'active', false);
- } else {
- $active = $this->isItemActive($item);
- }
-
- if (empty($items)) {
- $items = '';
- } else {
- $linkOptions['data-toggle'] = 'dropdown';
- Html::addCssClass($options, ['widget' => 'dropdown']);
- Html::addCssClass($linkOptions, ['widget' => 'dropdown-toggle']);
- if ($this->dropDownCaret !== '') {
- $label .= ' ' . $this->dropDownCaret;
- }
- if (is_array($items)) {
- if ($this->activateItems) {
- $items = $this->isChildActive($items, $active);
- }
- $items = $this->renderDropdown($items, $item);
- }
- }
-
- if ($this->activateItems && $active) {
- Html::addCssClass($options, 'active');
- }
-
- return Html::tag('li', Html::a($label, $url, $linkOptions) . $items, $options);
- }
-
-
-
- protected function renderDropdown($items, $parentItem)
- {
- return Dropdown::widget([
- 'options' => ArrayHelper::getValue($parentItem, 'dropDownOptions', []),
- 'items' => $items,
- 'encodeLabels' => $this->encodeLabels,
- 'clientOptions' => false,
- 'view' => $this->getView(),
- ]);
- }
-
-
-
- protected function isChildActive($items, &$active)
- {
- foreach ($items as $i => $child) {
- if (ArrayHelper::remove($items[$i], 'active', false) || $this->isItemActive($child)) {
- Html::addCssClass($items[$i]['options'], 'active');
- if ($this->activateParents) {
- $active = true;
- }
- }
- }
- return $items;
- }
-
-
-
- protected function isItemActive($item)
- {
- if (isset($item['url']) && is_array($item['url']) && isset($item['url'][0])) {
- $route = $item['url'][0];
- if ($route[0] !== '/' && Yii::$app->controller) {
- $route = Yii::$app->controller->module->getUniqueId() . '/' . $route;
- }
- if (ltrim($route, '/') !== $this->route) {
- return false;
- }
- unset($item['url']['#']);
- if (count($item['url']) > 1) {
- $params = $item['url'];
- unset($params[0]);
- foreach ($params as $name => $value) {
- if ($value !== null && (!isset($this->params[$name]) || $this->params[$name] != $value)) {
- return false;
- }
- }
- }
-
- return true;
- }
-
- return false;
- }
- }
|