Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
Y
yii2
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
PSDI Army
yii2
Commits
534aa4f9
Commit
534aa4f9
authored
Aug 12, 2013
by
Qiang Xue
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Support auto-activation of Nav.
parent
293b1737
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
67 additions
and
8 deletions
+67
-8
main.php
apps/basic/views/layouts/main.php
+4
-4
Nav.php
framework/yii/bootstrap/Nav.php
+63
-4
No files found.
apps/basic/views/layouts/main.php
View file @
534aa4f9
<?php
use
yii\bootstrap\NavBar
;
use
yii\helpers\Html
;
use
yii\widgets\Menu
;
use
yii\bootstrap\Nav
;
use
yii\bootstrap\NavBar
;
use
yii\widgets\Breadcrumbs
;
/**
...
...
@@ -28,8 +28,8 @@ app\config\AppAsset::register($this);
'class'
=>
'navbar-inverse navbar-fixed-top'
,
),
));
echo
Menu
::
widget
(
array
(
'options'
=>
array
(
'class'
=>
'nav
nav
bar-nav pull-right'
),
echo
Nav
::
widget
(
array
(
'options'
=>
array
(
'class'
=>
'navbar-nav pull-right'
),
'items'
=>
array
(
array
(
'label'
=>
'Home'
,
'url'
=>
array
(
'/site/index'
)),
array
(
'label'
=>
'About'
,
'url'
=>
array
(
'/site/about'
)),
...
...
framework/yii/bootstrap/Nav.php
View file @
534aa4f9
...
...
@@ -7,6 +7,7 @@
namespace
yii\bootstrap
;
use
Yii
;
use
yii\base\InvalidConfigException
;
use
yii\helpers\ArrayHelper
;
use
yii\helpers\Html
;
...
...
@@ -21,9 +22,8 @@ use yii\helpers\Html;
* 'items' => array(
* array(
* 'label' => 'Home',
* 'url' =>
'/'
,
* 'url' =>
array('site/index')
,
* 'linkOptions' => array(...),
* 'active' => true,
* ),
* array(
* 'label' => 'Dropdown',
...
...
@@ -63,7 +63,7 @@ class Nav extends Widget
* - 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.
* -
dropdown
: array|string, optional, the configuration array for creating a [[Dropdown]] widget,
* -
items
: array|string, optional, the configuration array for creating a [[Dropdown]] widget,
* or a string representing the dropdown menu. Note that Bootstrap does not support sub-dropdown menus.
*/
public
$items
=
array
();
...
...
@@ -71,6 +71,26 @@ class Nav extends Widget
* @var boolean whether the nav items labels should be HTML-encoded.
*/
public
$encodeLabels
=
true
;
/**
* @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
;
/**
...
...
@@ -79,6 +99,12 @@ class Nav extends Widget
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
=
$_GET
;
}
Html
::
addCssClass
(
$this
->
options
,
'nav'
);
}
...
...
@@ -124,7 +150,13 @@ class Nav extends Widget
$url
=
Html
::
url
(
ArrayHelper
::
getValue
(
$item
,
'url'
,
'#'
));
$linkOptions
=
ArrayHelper
::
getValue
(
$item
,
'linkOptions'
,
array
());
if
(
ArrayHelper
::
getValue
(
$item
,
'active'
))
{
if
(
isset
(
$item
[
'active'
]))
{
$active
=
ArrayHelper
::
remove
(
$item
,
'active'
,
false
);
}
else
{
$active
=
$this
->
isItemActive
(
$item
);
}
if
(
$active
)
{
Html
::
addCssClass
(
$options
,
'active'
);
}
...
...
@@ -144,4 +176,31 @@ class Nav extends Widget
return
Html
::
tag
(
'li'
,
Html
::
a
(
$label
,
$url
,
$linkOptions
)
.
$items
,
$options
);
}
/**
* 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
)
{
if
(
isset
(
$item
[
'url'
])
&&
is_array
(
$item
[
'url'
])
&&
trim
(
$item
[
'url'
][
0
],
'/'
)
===
$this
->
route
)
{
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
;
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment