Skip to content
This repository has been archived by the owner on Dec 12, 2023. It is now read-only.

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
d8vjork committed Feb 17, 2021
1 parent 57c5e5a commit 3668592
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 17 deletions.
2 changes: 1 addition & 1 deletion config/menus.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

'integrations' => [

'inertia' => true,
'inertia' => false,

'blade' => false,

Expand Down
30 changes: 18 additions & 12 deletions src/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,20 @@ class Manager
public static $menus = [];

/**
* Register the given menus.
* @var array
*/
protected static $active = [];

/**
* Activate the given menus.
*
* @param array $menus
*
* @return static
*/
public static function menus(array $menus)
public static function activate(array $menus)
{
static::$menus = array_merge(static::$menus, $menus);
static::$active = array_merge_recursive(static::$active, $menus);

return new static();
}
Expand Down Expand Up @@ -59,9 +64,10 @@ public static function menusIn($directory)
}
}

static::menus(
Collection::make($menus)->sort()->mapWithKeys(fn ($class) => [$class => $class::$routes])->all()
);
static::$menus = Collection::make($menus)
->sort()
->mapWithKeys(fn ($class) => [$class => $class::$routes])
->all();
}

/**
Expand All @@ -85,18 +91,18 @@ public static function enabled($integration)
*
* @return void
*/
public static function dataInject(Request $request, $key, $data)
public static function dataInject(Request $request)
{
if (static::enabled('inertia') && $request->inertia()) {
inertia()->share($key, array_merge(inertia()->getShared($key), $data));
}

if (static::enabled('json') && $request->wantsJson()) {
// TODO:
}

if (static::enabled('blade') && !$request->wantsJson()) {
Session::flash(static::prefix(), array_merge(Session::get(static::prefix(), []), $data));
Session::flash(static::prefix(), static::$active);
}

if (static::enabled('inertia') && !$request->wantsJson()) {
inertia()->share(static::prefix(), static::$active);
}
}

Expand Down
10 changes: 6 additions & 4 deletions src/Menu.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use DateInterval;
use Illuminate\Http\Request;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Str;
Expand All @@ -30,10 +31,11 @@ abstract class Menu implements Arrayable
*/
public function __construct(Request $request)
{
$resolved = $this->resolve($request);
$activeMenuArr = [];

Manager::dataInject($request, $this->identifier(), $resolved)
&& $resolved;
Arr::set($activeMenuArr, $this->identifier(), $this->resolve($request));

Manager::activate($activeMenuArr);
}

/**
Expand Down Expand Up @@ -129,7 +131,7 @@ protected function addLink($title, $uri, $params = [], $meta = [])
*
* @return \SkoreLabs\LaravelMenuBuilder\MenuGroup
*/
public function addGroup($title = null, array $items = [])
public function addGroup($title = 'default', array $items = [])
{
return new MenuGroup($title, $items);
}
Expand Down
2 changes: 2 additions & 0 deletions src/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ public function register()
? $menu::make($event->request)
: null;
}

Manager::dataInject($event->request);
});
}
}

0 comments on commit 3668592

Please sign in to comment.