Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Laravel 5.2 availability #42

Merged
merged 13 commits into from
Dec 23, 2015
19 changes: 9 additions & 10 deletions Menu.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,13 @@ public function __construct(Factory $views, Repository $config)
* Make new menu.
*
* @param string $name
* @param Closure $callback
*
* @return \Pingpong\Menus\MenuBuilder
*/
public function make($name)
public function make($name, \Closure $callback)
{
$builder = new MenuBuilder($name, $this->config);

$builder->setViewFactory($this->views);

$this->menus[$name] = $builder;

return $builder;
return $this->create($name, $callback);
}

/**
Expand All @@ -56,9 +51,13 @@ public function make($name)
*/
public function create($name, Closure $resolver)
{
$menus = $this->make($name);
$builder = new MenuBuilder($name, $this->config);

$builder->setViewFactory($this->views);

$this->menus[$name] = $builder;

return $resolver($menus);
return $resolver($builder);
}

/**
Expand Down
43 changes: 39 additions & 4 deletions MenuBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -335,15 +335,26 @@ public function add(array $attributes = array())
*
* @return $this
*/
public function dropdown($title, \Closure $callback, $order = 0, array $attributes = array())
public function dropdown($title, \Closure $callback, $order = null, array $attributes = array())
{
$item = MenuItem::make(compact('title', 'order') + $attributes);
$properties = compact('title', 'order', 'attributes');

if (func_num_args() == 3) {
$arguments = func_get_args();

$title = array_get($arguments, 0);
$attributes = array_get($arguments, 2);

$properties = compact('title', 'attributes');
}

$item = MenuItem::make($properties);

call_user_func($callback, $item);

$this->items[] = $item;

return $this;
return $item;
}

/**
Expand All @@ -358,6 +369,16 @@ public function dropdown($title, \Closure $callback, $order = 0, array $attribut
*/
public function route($route, $title, $parameters = array(), $order = null, $attributes = array())
{
if (func_num_args() == 4) {
$arguments = func_get_args();

return $this->add([
'route' => [array_get($arguments, 0), array_get($arguments, 2)],
'title' => array_get($arguments, 1),
'attributes' => array_get($arguments, 3)
]);
}

$route = array($route, $parameters);

$item = MenuItem::make(
Expand Down Expand Up @@ -394,8 +415,18 @@ protected function formatUrl($url)
*/
public function url($url, $title, $order = 0, $attributes = array())
{
$url = $this->formatUrl($url);
if (func_num_args() == 3) {
$arguments = func_get_args();

return $this->add([
'url' => $this->formatUrl(array_get($arguments, 0)),
'title' => array_get($arguments, 1),
'attributes' => array_get($arguments, 2)
]);
}

$url = $this->formatUrl($url);

$item = MenuItem::make(compact('url', 'title', 'order', 'attributes'));

$this->items[] = $item;
Expand Down Expand Up @@ -593,6 +624,10 @@ protected function renderMenu()
$menu = $presenter->getOpenTagWrapper();

foreach ($this->getOrderedItems() as $item) {
if ($item->hidden()) {
continue;
}

if ($item->hasSubMenu()) {
$menu .= $presenter->getMenuWithDropDownWrapper($item);
} elseif ($item->isHeader()) {
Expand Down
106 changes: 96 additions & 10 deletions MenuItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Pingpong\Menus;

use Closure;
use Illuminate\Contracts\Support\Arrayable as ArrayableContract;
use Collective\Html\HtmlFacade as HTML;
use Illuminate\Support\Facades\Request;
Expand Down Expand Up @@ -37,8 +38,16 @@ class MenuItem implements ArrayableContract
'attributes',
'active',
'order',
'hideWhen'
);

/**
* The hideWhen callback.
*
* @var Closure
*/
protected $hideWhen;

/**
* Constructor.
*
Expand Down Expand Up @@ -133,15 +142,26 @@ public function child($attributes)
*
* @return $this
*/
public function dropdown($title, $order = 0, \Closure $callback)
public function dropdown($title, \Closure $callback, $order = 0, array $attributes = array())
{
$child = static::make(compact('title', 'order'));
$properties = compact('title', 'order', 'attributes');

if (func_num_args() == 3) {
$arguments = func_get_args();

$title = array_get($arguments, 0);
$attributes = array_get($arguments, 2);

$properties = compact('title', 'attributes');
}

$child = static::make($properties);

call_user_func($callback, $child);

$this->childs[] = $child;

return $this;
return $child;
}

/**
Expand All @@ -156,6 +176,16 @@ public function dropdown($title, $order = 0, \Closure $callback)
*/
public function route($route, $title, $parameters = array(), $order = 0, $attributes = array())
{
if (func_num_args() == 4) {
$arguments = func_get_args();

return $this->add([
'route' => [array_get($arguments, 0), array_get($arguments, 2)],
'title' => array_get($arguments, 1),
'attributes' => array_get($arguments, 3)
]);
}

$route = array($route, $parameters);

return $this->add(compact('route', 'title', 'order', 'attributes'));
Expand All @@ -172,6 +202,16 @@ public function route($route, $title, $parameters = array(), $order = 0, $attrib
*/
public function url($url, $title, $order = 0, $attributes = array())
{
if (func_num_args() == 3) {
$arguments = func_get_args();

return $this->add([
'url' => array_get($arguments, 0),
'title' => array_get($arguments, 1),
'attributes' => array_get($arguments, 2)
]);
}

return $this->add(compact('url', 'title', 'order', 'attributes'));
}

Expand All @@ -184,23 +224,27 @@ public function url($url, $title, $order = 0, $attributes = array())
*/
public function add(array $properties)
{
$this->childs[] = static::make($properties);
$item = static::make($properties);

return $this;
$this->childs[] = $item;

return $item;
}

/**
* Add new divider.
*
* @param int $order
*
*
* @return self
*/
public function addDivider($order = null)
{
$this->childs[] = static::make(array('name' => 'divider', 'order' => $order));
$item = static::make(array('name' => 'divider', 'order' => $order));

return $this;
$this->childs[] = $item;

return $item;
}

/**
Expand All @@ -222,12 +266,14 @@ public function divider()
*/
public function addHeader($title)
{
$this->childs[] = static::make(array(
$item = static::make(array(
'name' => 'header',
'title' => $title,
));

return $this;
$this->childs[] = $item;

return $item;
}

/**
Expand Down Expand Up @@ -500,6 +546,46 @@ protected function getActiveStateFromUrl()
return Request::is($this->url);
}

/**
* Set order value.
*
* @param int $order
* @return self
*/
public function order($order)
{
$this->order = $order;

return $this;
}

/**
* Set hide condition for current menu item.
*
* @param Closure
* @return boolean
*/
public function hideWhen(Closure $callback)
{
$this->hideWhen = $callback;

return $this;
}

/**
* Determine whether the menu item is hidden.
*
* @return boolean
*/
public function hidden()
{
if (is_null($this->hideWhen)) {
return false;
}

return call_user_func($this->hideWhen) == true;
}

/**
* Get the instance as an array.
*
Expand Down
4 changes: 4 additions & 0 deletions Presenters/Presenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ public function getChildMenuItems(MenuItem $item)
{
$results = '';
foreach ($item->getChilds() as $child) {
if ($child->hidden()) {
continue;
}

if ($child->hasSubMenu()) {
$results .= $this->getMultiLevelDropdownWrapper($child);
} elseif ($child->isHeader()) {
Expand Down
8 changes: 4 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@
}
],
"require": {
"php": ">=5.3.0",
"illuminate/support": "5.1.*",
"illuminate/config": "5.1.*",
"illuminate/view": "5.1.*",
"php": ">=5.5.9",
"illuminate/support": "5.2.*",
"illuminate/config": "5.2.*",
"illuminate/view": "5.2.*",
"laravelcollective/html": "~5.0"
},
"require-dev": {
Expand Down
18 changes: 9 additions & 9 deletions src/config/config.php
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
<?php

return array(
return [

'styles' => array(
'navbar' => 'Pingpong\Menus\Presenters\Bootstrap\NavbarPresenter',
'styles' => [
'navbar' => 'Pingpong\Menus\Presenters\Bootstrap\NavbarPresenter',
'navbar-right' => 'Pingpong\Menus\Presenters\Bootstrap\NavbarRightPresenter',
'nav-pills' => 'Pingpong\Menus\Presenters\Bootstrap\NavPillsPresenter',
'nav-tab' => 'Pingpong\Menus\Presenters\Bootstrap\NavTabPresenter',
'sidebar' => 'Pingpong\Menus\Presenters\Bootstrap\SidebarMenuPresenter',
'navmenu' => 'Pingpong\Menus\Presenters\Bootstrap\NavMenuPresenter',
),
'nav-pills' => 'Pingpong\Menus\Presenters\Bootstrap\NavPillsPresenter',
'nav-tab' => 'Pingpong\Menus\Presenters\Bootstrap\NavTabPresenter',
'sidebar' => 'Pingpong\Menus\Presenters\Bootstrap\SidebarMenuPresenter',
'navmenu' => 'Pingpong\Menus\Presenters\Bootstrap\NavMenuPresenter',
],

'ordering' => false,

);
];