diff --git a/CHANGE.md b/CHANGE.md index 6b10471..bc02fcf 100644 --- a/CHANGE.md +++ b/CHANGE.md @@ -1,6 +1,13 @@ Change Log: `yii2-nav-x` ======================== +## Version 1.2.2 + +**Date:** 19-Sep-2018 + +- Enhancements to support Bootstrap v4.x. +- Move all source code to `src` directory. + ## Version 1.2.1 **Date:** 13-May-2017 diff --git a/LICENSE.md b/LICENSE.md index 36f6702..7f04f80 100644 --- a/LICENSE.md +++ b/LICENSE.md @@ -1,4 +1,4 @@ -Copyright (c) 2014 - 2017, Kartik Visweswaran +Copyright (c) 2014 - 2018, Kartik Visweswaran Krajee.com All rights reserved. diff --git a/NavXAsset.php b/NavXAsset.php deleted file mode 100644 index ce259db..0000000 --- a/NavXAsset.php +++ /dev/null @@ -1,29 +0,0 @@ - - * @since 1.0 - */ -class NavXAsset extends AssetBundle -{ - /** - * @inheritdoc - */ - public function init() - { - $this->setSourcePath(__DIR__ . '/assets'); - $this->setupAssets('css', ['css/nav-x']); - parent::init(); - } -} diff --git a/README.md b/README.md index 2187d3d..60268bf 100644 --- a/README.md +++ b/README.md @@ -8,10 +8,13 @@ yii2-nav-x [![Monthly Downloads](https://poser.pugx.org/kartik-v/yii2-nav-x/d/monthly)](https://packagist.org/packages/kartik-v/yii2-nav-x) [![Daily Downloads](https://poser.pugx.org/kartik-v/yii2-nav-x/d/daily)](https://packagist.org/packages/kartik-v/yii2-nav-x) -An extended bootstrap Nav widget for Yii Framework 2 with submenu drilldown. This widget extends the `\yii\bootstrap\Nav` widget -with some additional controls and adding the CSS Styles for enabling a submenu drilldown. The dropdown menu style is optimized for both -desktop and mobile devices. The drilldown is triggered on `active` instead of `hover` so that it works equally well on mobile devices. The -extension uses the `\kartik\dropdown\DropdownX` extension by default for rendering dropdown navigation, but this can be overridden. +An extended bootstrap Nav widget for Yii Framework 2 with submenu drilldown. This widget extends the `\yii\bootstrap\Nav` widget for Bootstrap 3.x and +`\yii\bootstrap4\Nav` widget for Bootstrap 4.x with some additional controls and adding the CSS Styles for enabling a submenu drilldown. The dropdown menu style +is optimized for both desktop and mobile devices. The drilldown is triggered on `active` instead of `hover` so that it works equally well on mobile devices. The +extension uses the one of the following Dropdown Class extensions by default for rendering dropdown navigation, but this can be overridden: + +- `\kartik\dropdown\DropdownX` for Bootstrap 3.x +- `\kartik\bs4dropdown\Dropdown` for Bootstrap 4.x ### Demo You can see detailed [documentation](http://demos.krajee.com/nav-x) on usage of the extension. @@ -61,4 +64,4 @@ echo NavX::widget([ ## License -**yii2-nav-x** is released under the BSD 3-Clause License. See the bundled `LICENSE.md` for details. \ No newline at end of file +**yii2-nav-x** is released under the BSD-3-Clause License. See the bundled `LICENSE.md` for details. \ No newline at end of file diff --git a/assets/css/nav-x.css b/assets/css/nav-x.css deleted file mode 100644 index f70ac32..0000000 --- a/assets/css/nav-x.css +++ /dev/null @@ -1,29 +0,0 @@ -/*! - * @package yii2-nav-x - * @author Kartik Visweswaran - * @copyright Copyright © Kartik Visweswaran, Krajee.com, 2014 - 2017 - * @version 1.2.1 - * - * Nav X styling and enhancements for Bootstrap. - * Built for Yii Framework 2.0 - * Author: Kartik Visweswaran - * For more Yii related demos visit http://demos.krajee.com - */ -@media (max-width: 767px) { - .navbar-nav .open .open .dropdown-menu > li > a, - .navbar-nav .open .open .dropdown-menu .dropdown-header { - padding-left: 35px; - } - .navbar-default .navbar-nav .open .dropdown-menu > .open > a, - .navbar-default .navbar-nav .open .dropdown-menu > .open > a:hover, - .navbar-default .navbar-nav .open .dropdown-menu > .open > a:focus { - color: #555; - background-color: #e7e7e7; - } - .navbar-inverse .navbar-nav .open .dropdown-menu > .open > a, - .navbar-inverse .navbar-nav .open .dropdown-menu > .open > a:hover, - .navbar-inverse .navbar-nav .open .dropdown-menu > .open > a:focus { - color: #fff; - background-color: #080808; - } -} diff --git a/assets/css/nav-x.min.css b/assets/css/nav-x.min.css deleted file mode 100644 index aa8fe96..0000000 --- a/assets/css/nav-x.min.css +++ /dev/null @@ -1,11 +0,0 @@ -/*! - * @package yii2-nav-x - * @author Kartik Visweswaran - * @copyright Copyright © Kartik Visweswaran, Krajee.com, 2014 - 2017 - * @version 1.2.1 - * - * Nav X styling and enhancements for Bootstrap. - * Built for Yii Framework 2.0 - * Author: Kartik Visweswaran - * For more Yii related demos visit http://demos.krajee.com - */@media (max-width:767px){.navbar-nav .open .open .dropdown-menu .dropdown-header,.navbar-nav .open .open .dropdown-menu>li>a{padding-left:35px}.navbar-default .navbar-nav .open .dropdown-menu>.open>a,.navbar-default .navbar-nav .open .dropdown-menu>.open>a:focus,.navbar-default .navbar-nav .open .dropdown-menu>.open>a:hover{color:#555;background-color:#e7e7e7}.navbar-inverse .navbar-nav .open .dropdown-menu>.open>a,.navbar-inverse .navbar-nav .open .dropdown-menu>.open>a:focus,.navbar-inverse .navbar-nav .open .dropdown-menu>.open>a:hover{color:#fff;background-color:#080808}} diff --git a/composer.json b/composer.json index 5ba8e04..850032e 100644 --- a/composer.json +++ b/composer.json @@ -13,11 +13,11 @@ } ], "require": { - "kartik-v/yii2-dropdown-x": "*" + "kartik-v/yii2-krajee-base": ">=1.9" }, "autoload": { "psr-4": { - "kartik\\nav\\": "" + "kartik\\nav\\": "src" } }, "extra": { diff --git a/src/NavX.php b/src/NavX.php new file mode 100644 index 0000000..ba1e615 --- /dev/null +++ b/src/NavX.php @@ -0,0 +1,116 @@ + + * @since 1.0 + */ +class NavX extends Widget +{ + /** + * @var array list of items in the nav widget. Each array element represents a single + * menu item which can be either a string or an array with the following structure: + * + * - label: string, required, the nav item label. + * - url: optional, the item's URL. Defaults to "#". + * - visible: bool, optional, whether this menu item is visible. Defaults to true. + * - linkOptions: array, optional, the HTML attributes of the item's link. + * - options: array, optional, the HTML attributes of the item container (LI). + * - active: bool, optional, whether the item should be on active state or not. + * - dropDownOptions: array, optional, the HTML options that will passed to the [[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. + * - encode: bool, optional, whether the label will be HTML-encoded. If set, supersedes the $encodeLabels option for only this item. + * + * If a menu item is a string, it will be rendered directly without HTML encoding. + */ + public $items = []; + + /** + * @var bool whether the nav items labels should be HTML-encoded. + */ + public $encodeLabels = true; + + /** + * @var bool whether to automatically activate items according to whether their route setting + * matches the currently requested route. + * @see isItemActive + */ + public $activateItems = true; + + /** + * @var bool whether to activate parent menu items when one of the corresponding child menu items is active. + */ + public $activateParents = false; + + /** + * @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; + + /** + * @var string name of a class to use for rendering dropdowns within this widget. Defaults to [[Dropdown]]. + */ + public $dropdownClass; + + /** + * @var array the dropdown widget options + */ + public $dropdownOptions = []; + + /** + * @inheritdoc + */ + public function init() + { + if (!class_exists($this->dropdownClass)) { + throw new InvalidConfigException("The dropdownClass '{$this->dropdownClass}' was not found. Please ensure the '{$this->dropdownClass}' extension is installed and accessible."); + } + parent::init(); + } + + /** + * @inheritdoc + */ + public function run() + { + $opts = [ + 'items' => $this->items, + 'encodeLabels' => $this->encodeLabels, + 'activateItems' => $this->activateItems, + 'activateParents' => $this->activateParents, + 'dropdownOptions' => $this->dropdownOptions, + 'options' => $this->options, + 'clientOptions' => $this->pluginOptions, + ]; + $props = ['route', 'params', 'dropdownClass']; + foreach ($props as $prop) { + if (isset($this->$prop)) { + $opts[$prop] = $this->$prop; + } + } + echo $this->isBs4() ? NavXBs4::widget($opts) : NavXBs3::widget($opts); + } +} diff --git a/src/NavXBs3.php b/src/NavXBs3.php new file mode 100644 index 0000000..a3557db --- /dev/null +++ b/src/NavXBs3.php @@ -0,0 +1,27 @@ + + * @since 1.0 + */ +class NavXBs3 extends Nav +{ + use NavXTrait; + /** + * @var string the class name to render the Dropdown items. Defaults to `\kartik\dropdown\DropdownX`. + */ + public $dropdownClass = 'kartik\dropdown\DropdownX'; +} diff --git a/src/NavXBs4.php b/src/NavXBs4.php new file mode 100644 index 0000000..e3250ed --- /dev/null +++ b/src/NavXBs4.php @@ -0,0 +1,27 @@ + + * @since 1.0 + */ +class NavXBs4 extends Nav +{ + use NavXTrait; + /** + * @var string the class name to render the Dropdown items. Defaults to `\kartik\dropdown\DropdownX`. + */ + public $dropdownClass = 'kartik\bs4dropdown\Dropdown'; +} diff --git a/NavX.php b/src/NavXTrait.php similarity index 57% rename from NavX.php rename to src/NavXTrait.php index 8a7fced..4c52f54 100644 --- a/NavX.php +++ b/src/NavXTrait.php @@ -1,69 +1,50 @@ * @since 1.0 */ -class NavX extends Nav +trait NavXTrait { /** - * @var string the class name to render the Dropdown items. Defaults to `\kartik\dropdown\DropdownX`. - */ - public $dropdownClass = '\kartik\dropdown\DropdownX'; - - /** - * @var array the dropdown widget options - */ - public $dropdownOptions = []; - - /** - * @inheritdoc - * @throws InvalidConfigException - */ - public function init() - { - if (!class_exists($this->dropdownClass)) { - throw new InvalidConfigException("The dropdownClass '{$this->dropdownClass}' does not exist or is not accessible."); - } - NavXAsset::register($this->getView()); - parent::init(); - } - - /** - * @inheritdoc + * Renders the given items as a dropdown. + * This method is called to create sub-menus. + * @param array $items the given items. Please refer to [[Dropdown::items]] for the array structure. + * @param array $parentItem the parent item information. Please refer to [[items]] for the structure of this array. + * @return string the rendering result. + * @throws \Exception */ protected function renderDropdown($items, $parentItem) { - /** - * @var \yii\bootstrap\Dropdown $ddWidget - */ - $ddWidget = $this->dropdownClass; + /** @var \yii\base\Widget $dropdownClass */ + $dropdownClass = $this->dropdownClass; $ddOptions = array_replace_recursive($this->dropdownOptions, [ + 'options' => ArrayHelper::getValue($parentItem, 'dropDownOptions', []), 'items' => $items, 'encodeLabels' => $this->encodeLabels, 'clientOptions' => false, 'view' => $this->getView(), ]); - return $ddWidget::widget($ddOptions); + return $dropdownClass::widget($ddOptions); } /** - * @inheritdoc + * Check to see if a child item is active optionally activating the parent. + * @param array $items @see items + * @param bool $active should the parent be active too + * @return array @see items */ protected function isChildActive($items, &$active) {