You can not select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
			
				
					215 lines
				
				6.1 KiB
			
		
		
			
		
	
	
					215 lines
				
				6.1 KiB
			| 
											13 years ago
										 | <?php
 | ||
|  | /**
 | ||
|  |  * @link http://www.yiiframework.com/
 | ||
|  |  * @copyright Copyright (c) 2008 Yii Software LLC
 | ||
|  |  * @license http://www.yiiframework.com/license/
 | ||
|  |  */
 | ||
|  | 
 | ||
|  | namespace yii\bootstrap;
 | ||
|  | 
 | ||
| 
											12 years ago
										 | use Yii;
 | ||
| 
											13 years ago
										 | use yii\base\InvalidConfigException;
 | ||
| 
											13 years ago
										 | use yii\helpers\ArrayHelper;
 | ||
| 
											13 years ago
										 | use yii\helpers\Html;
 | ||
|  | 
 | ||
|  | /**
 | ||
|  |  * Nav renders a nav HTML component.
 | ||
|  |  *
 | ||
|  |  * For example:
 | ||
|  |  *
 | ||
|  |  * ```php
 | ||
| 
											12 years ago
										 |  * echo Nav::widget([
 | ||
|  |  *     'items' => [
 | ||
|  |  *         [
 | ||
| 
											13 years ago
										 |  *             'label' => 'Home',
 | ||
| 
											12 years ago
										 |  *             'url' => ['site/index'],
 | ||
|  |  *             'linkOptions' => [...],
 | ||
| 
											12 years ago
										 |  *         ],
 | ||
| 
											12 years ago
										 |  *         [
 | ||
| 
											13 years ago
										 |  *             'label' => 'Dropdown',
 | ||
| 
											12 years ago
										 |  *             'items' => [
 | ||
|  |  *                  [
 | ||
| 
											13 years ago
										 |  *                      'label' => 'Level 1 -DropdownA',
 | ||
| 
											13 years ago
										 |  *                      'url' => '#',
 | ||
| 
											12 years ago
										 |  *                      'items' => [
 | ||
|  |  *                          ['label' => 'Level 2 -DropdownA', 'url' => '#'],
 | ||
|  |  *                      ],
 | ||
|  |  *                  ],
 | ||
|  |  *                  ['label' => 'Level 1 -DropdownB', 'url' => '#'],
 | ||
|  |  *             ],
 | ||
|  |  *         ],
 | ||
|  |  *     ],
 | ||
|  |  * ]);
 | ||
| 
											13 years ago
										 |  * ```
 | ||
|  |  *
 | ||
|  |  * @see http://twitter.github.io/bootstrap/components.html#nav
 | ||
|  |  * @author Antonio Ramirez <amigo.cobos@gmail.com>
 | ||
|  |  * @since 2.0
 | ||
|  |  */
 | ||
|  | class Nav extends Widget
 | ||
|  | {
 | ||
|  | 	/**
 | ||
|  | 	 * @var array list of items in the nav widget. Each array element represents a single
 | ||
| 
											12 years ago
										 | 	 * menu item which can be either a string or an array with the following structure:
 | ||
| 
											13 years ago
										 | 	 *
 | ||
| 
											13 years ago
										 | 	 * - label: string, required, the nav item label.
 | ||
|  | 	 * - url: optional, the item's URL. Defaults to "#".
 | ||
| 
											12 years ago
										 | 	 * - visible: boolean, optional, whether this menu item is visible. Defaults to true.
 | ||
| 
											13 years ago
										 | 	 * - linkOptions: array, optional, the HTML attributes of the item's link.
 | ||
|  | 	 * - options: array, optional, the HTML attributes of the item container (LI).
 | ||
|  | 	 * - active: boolean, optional, whether the item should be on active state or not.
 | ||
| 
											12 years ago
										 | 	 * - items: array|string, optional, the configuration array for creating a [[Dropdown]] widget,
 | ||
| 
											13 years ago
										 | 	 *   or a string representing the dropdown menu. Note that Bootstrap does not support sub-dropdown menus.
 | ||
| 
											12 years ago
										 | 	 *
 | ||
| 
											12 years ago
										 | 	 * If a menu item is a string, it will be rendered directly without HTML encoding.
 | ||
| 
											13 years ago
										 | 	 */
 | ||
| 
											12 years ago
										 | 	public $items = [];
 | ||
| 
											13 years ago
										 | 	/**
 | ||
|  | 	 * @var boolean whether the nav items labels should be HTML-encoded.
 | ||
|  | 	 */
 | ||
|  | 	public $encodeLabels = true;
 | ||
| 
											12 years ago
										 | 	/**
 | ||
|  | 	 * @var boolean whether to automatically activate items according to whether their route setting
 | ||
|  | 	 * matches the currently requested route.
 | ||
|  | 	 * @see isItemActive
 | ||
|  | 	 */
 | ||
|  | 	public $activateItems = true;
 | ||
|  | 	/**
 | ||
|  | 	 * @var string the route used to determine if a menu item is active or not.
 | ||
|  | 	 * If not set, it will use the route of the current request.
 | ||
|  | 	 * @see params
 | ||
|  | 	 * @see isItemActive
 | ||
|  | 	 */
 | ||
|  | 	public $route;
 | ||
|  | 	/**
 | ||
|  | 	 * @var array the parameters used to determine if a menu item is active or not.
 | ||
|  | 	 * If not set, it will use `$_GET`.
 | ||
|  | 	 * @see route
 | ||
|  | 	 * @see isItemActive
 | ||
|  | 	 */
 | ||
|  | 	public $params;
 | ||
| 
											13 years ago
										 | 
 | ||
|  | 
 | ||
|  | 	/**
 | ||
|  | 	 * Initializes the widget.
 | ||
|  | 	 */
 | ||
|  | 	public function init()
 | ||
|  | 	{
 | ||
| 
											13 years ago
										 | 		parent::init();
 | ||
| 
											12 years ago
										 | 		if ($this->route === null && Yii::$app->controller !== null) {
 | ||
|  | 			$this->route = Yii::$app->controller->getRoute();
 | ||
|  | 		}
 | ||
|  | 		if ($this->params === null) {
 | ||
|  | 			$this->params = $_GET;
 | ||
|  | 		}
 | ||
| 
											13 years ago
										 | 		Html::addCssClass($this->options, 'nav');
 | ||
| 
											13 years ago
										 | 	}
 | ||
|  | 
 | ||
|  | 	/**
 | ||
|  | 	 * Renders the widget.
 | ||
|  | 	 */
 | ||
|  | 	public function run()
 | ||
|  | 	{
 | ||
|  | 		echo $this->renderItems();
 | ||
| 
											12 years ago
										 | 		BootstrapAsset::register($this->getView());
 | ||
| 
											13 years ago
										 | 	}
 | ||
|  | 
 | ||
|  | 	/**
 | ||
|  | 	 * Renders widget items.
 | ||
|  | 	 */
 | ||
|  | 	public function renderItems()
 | ||
|  | 	{
 | ||
| 
											12 years ago
										 | 		$items = [];
 | ||
| 
											12 years ago
										 | 		foreach ($this->items as $i => $item) {
 | ||
|  | 			if (isset($item['visible']) && !$item['visible']) {
 | ||
|  | 				unset($items[$i]);
 | ||
|  | 				continue;
 | ||
|  | 			}
 | ||
| 
											13 years ago
										 | 			$items[] = $this->renderItem($item);
 | ||
|  | 		}
 | ||
|  | 
 | ||
|  | 		return Html::tag('ul', implode("\n", $items), $this->options);
 | ||
|  | 	}
 | ||
|  | 
 | ||
|  | 	/**
 | ||
|  | 	 * Renders a widget's item.
 | ||
| 
											12 years ago
										 | 	 * @param string|array $item the item to render.
 | ||
| 
											13 years ago
										 | 	 * @return string the rendering result.
 | ||
|  | 	 * @throws InvalidConfigException
 | ||
|  | 	 */
 | ||
|  | 	public function renderItem($item)
 | ||
|  | 	{
 | ||
|  | 		if (is_string($item)) {
 | ||
|  | 			return $item;
 | ||
|  | 		}
 | ||
|  | 		if (!isset($item['label'])) {
 | ||
|  | 			throw new InvalidConfigException("The 'label' option is required.");
 | ||
|  | 		}
 | ||
| 
											13 years ago
										 | 		$label = $this->encodeLabels ? Html::encode($item['label']) : $item['label'];
 | ||
| 
											12 years ago
										 | 		$options = ArrayHelper::getValue($item, 'options', []);
 | ||
| 
											13 years ago
										 | 		$items = ArrayHelper::getValue($item, 'items');
 | ||
| 
											13 years ago
										 | 		$url = Html::url(ArrayHelper::getValue($item, 'url', '#'));
 | ||
| 
											12 years ago
										 | 		$linkOptions = ArrayHelper::getValue($item, 'linkOptions', []);
 | ||
| 
											13 years ago
										 | 
 | ||
| 
											12 years ago
										 | 		if (isset($item['active'])) {
 | ||
|  | 			$active = ArrayHelper::remove($item, 'active', false);
 | ||
|  | 		} else {
 | ||
|  | 			$active = $this->isItemActive($item);
 | ||
|  | 		}
 | ||
|  | 
 | ||
|  | 		if ($active) {
 | ||
| 
											13 years ago
										 | 			Html::addCssClass($options, 'active');
 | ||
| 
											13 years ago
										 | 		}
 | ||
| 
											13 years ago
										 | 
 | ||
| 
											13 years ago
										 | 		if ($items !== null) {
 | ||
| 
											13 years ago
										 | 			$linkOptions['data-toggle'] = 'dropdown';
 | ||
| 
											13 years ago
										 | 			Html::addCssClass($options, 'dropdown');
 | ||
|  | 			Html::addCssClass($urlOptions, 'dropdown-toggle');
 | ||
| 
											12 years ago
										 | 			$label .= ' ' . Html::tag('b', '', ['class' => 'caret']);
 | ||
| 
											13 years ago
										 | 			if (is_array($items)) {
 | ||
| 
											12 years ago
										 | 				$items = Dropdown::widget([
 | ||
| 
											13 years ago
										 | 					'items' => $items,
 | ||
| 
											12 years ago
										 | 					'encodeLabels' => $this->encodeLabels,
 | ||
| 
											13 years ago
										 | 					'clientOptions' => false,
 | ||
| 
											12 years ago
										 | 				]);
 | ||
| 
											13 years ago
										 | 			}
 | ||
| 
											13 years ago
										 | 		}
 | ||
|  | 
 | ||
| 
											13 years ago
										 | 		return Html::tag('li', Html::a($label, $url, $linkOptions) . $items, $options);
 | ||
| 
											13 years ago
										 | 	}
 | ||
| 
											12 years ago
										 | 
 | ||
|  | 
 | ||
|  | 	/**
 | ||
|  | 	 * Checks whether a menu item is active.
 | ||
|  | 	 * This is done by checking if [[route]] and [[params]] match that specified in the `url` option of the menu item.
 | ||
|  | 	 * When the `url` option of a menu item is specified in terms of an array, its first element is treated
 | ||
|  | 	 * as the route for the item and the rest of the elements are the associated parameters.
 | ||
|  | 	 * Only when its route and parameters match [[route]] and [[params]], respectively, will a menu item
 | ||
|  | 	 * be considered active.
 | ||
|  | 	 * @param array $item the menu item to be checked
 | ||
|  | 	 * @return boolean whether the menu item is active
 | ||
|  | 	 */
 | ||
|  | 	protected function isItemActive($item)
 | ||
|  | 	{
 | ||
| 
											12 years ago
										 | 		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;
 | ||
|  | 			}
 | ||
| 
											12 years ago
										 | 			unset($item['url']['#']);
 | ||
|  | 			if (count($item['url']) > 1) {
 | ||
|  | 				foreach (array_splice($item['url'], 1) as $name => $value) {
 | ||
|  | 					if (!isset($this->params[$name]) || $this->params[$name] != $value) {
 | ||
|  | 						return false;
 | ||
|  | 					}
 | ||
|  | 				}
 | ||
|  | 			}
 | ||
|  | 			return true;
 | ||
|  | 		}
 | ||
|  | 		return false;
 | ||
|  | 	}
 | ||
| 
											13 years ago
										 | }
 |