Skip to content

Commit

Permalink
Improved menu ordering feature
Browse files Browse the repository at this point in the history
  • Loading branch information
Gravitano committed Jun 9, 2015
1 parent a90f073 commit bb5bfd2
Showing 1 changed file with 41 additions and 6 deletions.
47 changes: 41 additions & 6 deletions src/Pingpong/Menus/MenuBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,13 @@ class MenuBuilder implements Countable
*/
protected $views;

/**
* Determine whether the ordering feature is enabled or not.
*
* @var boolean
*/
protected $ordering = false;

/**
* Constructor.
*
Expand Down Expand Up @@ -248,11 +255,13 @@ public function dropdown($title, \Closure $callback, $order = 0, array $attribut
*
* @return static
*/
public function route($route, $title, $parameters = array(), $attributes = array())
public function route($route, $title, $parameters = array(), $order = null, $attributes = array())
{
$route = array($route, $parameters);

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

$this->items[] = $item;

Expand Down Expand Up @@ -296,11 +305,12 @@ public function url($url, $title, $order = 0, $attributes = array())
/**
* Add new divider item.
*
* @param int $order
* @return \Pingpong\Menus\MenuItem
*/
public function addDivider()
public function addDivider($order = null)
{
$this->items[] = new MenuItem(array('name' => 'divider'));
$this->items[] = new MenuItem(array('name' => 'divider', 'order' => $order));

return $this;
}
Expand All @@ -310,11 +320,12 @@ public function addDivider()
*
* @return \Pingpong\Menus\MenuItem
*/
public function addHeader($title)
public function addHeader($title, $order = null)
{
$this->items[] = new MenuItem(array(
'name' => 'header',
'title' => $title,
'order' => $order
));

return $this;
Expand Down Expand Up @@ -428,14 +439,38 @@ public function toArray()
return $this->toCollection()->toArray();
}

/**
* Enable menu ordering.
*
* @return self
*/
public function enableOrdering()
{
$this->ordering = true;

return $this;
}

/**
* Disable menu ordering.
*
* @return self
*/
public function disableOrdering()
{
$this->ordering = true;

return $this;
}

/**
* Get menu items and order it by 'order' key.
*
* @return array
*/
public function getOrderedItems()
{
if (config('menus.ordering')) {
if (config('menus.ordering') || $this->ordering) {
return $this->toCollection()->sortBy(function ($item) {
return $item->order;
})->all();
Expand Down

0 comments on commit bb5bfd2

Please sign in to comment.