forked from wbraganca/yii2-mmenu-widget
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMenu.php
118 lines (109 loc) · 3.65 KB
/
Menu.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
<?php
/**
* @link https://github.com/wbraganca/yii2-mmenu
* @copyright Copyright (c) 2014 Wanderson Bragança
* @license https://github.com/wbraganca/yii2-mmenu/blob/master/LICENSE
*/
namespace wbraganca\mmenu;
use Yii;
use yii\base\InvalidConfigException;
use yii\helpers\Html;
use yii\helpers\Url;
use yii\helpers\ArrayHelper;
use yii\helpers\Json;
/**
* The yii2-mmenu widget is a Yii 2 wrapper for the jQuery-mmenu.
* See more: http://mmenu.frebsite.nl/
*
* @author Wanderson Bragança <[email protected]>
* @since 1.0.0
*/
class Menu extends \yii\widgets\Menu
{
/**
* @var array
*/
public $clientOptions = [];
/**
* @var string the template used to render the body of a menu which is NOT a link.
* In this template, the token `{label}` will be replaced with the label of the menu item.
* This property will be overridden by the `template` option set in individual menu items via [[items]].
*/
public $labelTemplate = '{icon}{label}';
/**
* @var string the template used to render the body of a menu which is a link.
* In this template, the token `{url}` will be replaced with the corresponding link URL;
* while `{label}` will be replaced with the link text.
* This property will be overridden by the `template` option set in individual menu items via [[items]].
*/
public $linkTemplate = '<a href="{url}">{icon}{label}</a>';
/**
* @var string
*/
public $noLinkTemplate = '<span>{icon}{label}</span>';
public function init()
{
parent::init();
MenuAsset::register($this->getView());
$this->activateParents = true;
}
public function run()
{
echo Html::tag('nav', $this->renderMenu(), ['id' => $this->id]);
$this->registerScript();
}
/**
* Renders the main menu
* @return string
*/
protected function renderMenu()
{
if ($this->route === null && Yii::$app->controller !== null) {
$this->route = Yii::$app->controller->getRoute();
}
if ($this->params === null) {
$this->params = $_GET;
}
$items = $this->normalizeItems($this->items, $hasActiveChild);
$options = $this->options;
$tag = ArrayHelper::remove($options, 'tag', 'ul');
return Html::tag($tag, $this->renderItems($items), $options);
}
/**
* Renders the content of a side navigation menu item.
*
* @param array $item the menu item to be rendered. Please refer to [[items]] to see what data might be in the item.
* @return string the rendering result
* @throws InvalidConfigException
*/
protected function renderItem($item)
{
$this->validateItems($item);
$url = ArrayHelper::getValue($item, 'url', '#');
$url = ($url === '') ? '' : Url::to($url);
$template = ($url === '') ? $this->noLinkTemplate : $this->linkTemplate;
$icon = empty($item['icon']) ? '' : '<i class="' . $item['icon'] . '"></i> ';
return strtr($template, [
'{url}' => $url,
'{label}' => $item['label'],
'{icon}' => $icon
]);
}
/**
* Validates each item for a valid label and url.
*
* @throws InvalidConfigException
*/
protected function validateItems($item)
{
if (! isset($item['label'])) {
throw new InvalidConfigException("The 'label' option is required.");
}
}
protected function registerScript()
{
$clientOptions = Json::encode($this->clientOptions);
$view = $this->getView();
$view->registerJs('$("nav#' . $this->id .'").mmenu(' . $clientOptions . ');');
}
}