feat: Voci del drawer dinamiche

- I link del drawer vengono dinamicamente importati dai moduli installati
This commit is contained in:
Maicol Battistini 2021-12-14 19:22:51 +01:00
parent d066ca6989
commit fe462ac78c
No known key found for this signature in database
GPG Key ID: 4FDB0F87CDB1D34A
4 changed files with 74 additions and 23 deletions

View File

@ -5,11 +5,37 @@ namespace App\Http\Controllers;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\File;
use Nette\Utils\Json;
class Controller extends BaseController
{
use AuthorizesRequests;
use DispatchesJobs;
use ValidatesRequests;
public function getModules(Request $request, ?array $filter = null): JsonResponse|Collection
{
$packages = collect(Json::decode(File::get(base_path('vendor/composer/installed.json')))->packages);
$modules = $packages->filter(fn ($package) => $package->type === 'openstamanager-module');
$modules->transform(function ($module) {
foreach ($module->extra->osm_modules as $id => $data) {
$routes[] = cache()->get("modules.$id.drawer_routes");
}
$module->routes = array_merge(...$routes);
return $module;
});
$filtered = $modules->only($filter);
return $request->wantsJson() ? response()->json($filtered) : $filtered;
}
}

View File

@ -6,7 +6,9 @@ import '@material/mwc-menu';
import './WebComponents/TopAppBar';
import './WebComponents/MaterialDrawer';
import {Inertia} from '@inertiajs/inertia';
import type {Dialog as MWCDialog} from '@material/mwc-dialog';
import {ListItem as MWCListItem} from '@material/mwc-list/mwc-list-item';
import type {Menu as MWCMenu} from '@material/mwc-menu';
import $ from 'cash-dom';
@ -32,6 +34,18 @@ if (drawer) {
drawer.parentElement.addEventListener('MDCTopAppBar:nav', () => {
drawer.open = !drawer.open;
});
// Drawer items click
$(drawer).find('a.drawer-item').on('click', function (event: PointerEvent) {
event.preventDefault();
Inertia.visit(this.href);
const drawerItem: MWCListItem = this.firstElementChild;
drawerItem.activated = true;
$(this).siblings('.drawer-item')
.filter((index, item) => $(item).has('mwc-list-item[activated]'))
.find('mwc-list-item')
.prop('activated', false);
});
}
$('mwc-menu').each((index, menu: MWCMenu) => {

View File

@ -1,22 +1,32 @@
<mwc-list activatable>
@foreach([
'tipi-attivita' => [
'icon' => 'shape',
'text' => __('Tipi attività')
]
] as $route => $details)
@php
$modules = app(\App\Http\Controllers\Controller::class)
->getModules(request());
$routes = [
'dashboard' => [
'icon' => 'view-dashboard-outline',
'text' => __('Dashboard')
],
];
foreach ($modules as $module) {
$to_merge[] = $module->routes;
}
$routes = array_merge($routes, ...$to_merge);
@endphp
@foreach($routes as $route => $details)
@switch($route)
@case('hr')
<li divider padded role="separator"></li>
@break
<li divider padded role="separator"></li>
@break
@default
<a href="{{Route::has($route) ? route($route) : $route}}">
<mwc-list-item graphic="icon" @if(Request::is($route)) selected activated @endif>
<i class="mdi mdi-{{$details['icon']}}{{empty($details['no_outline']) ? '-outline' : ''}}"
slot="graphic" aria-hidden="true"></i>
<span class="mdc-typography--subtitle2">{{$details['text']}}</span>
</mwc-list-item>
</a>
<a class="drawer-item" href="{{Route::has($route) ? route($route) : ''}}">
<mwc-list-item graphic="icon" @if(request()->routeIs($route)) activated @endif>
<i class="mdi mdi-{{$details['icon']}}"
slot="graphic" aria-hidden="true"></i>
<span class="mdc-typography--subtitle2">{{$details['text']}}</span>
</mwc-list-item>
</a>
@endswitch
@endforeach
</mwc-list>

View File

@ -3,6 +3,7 @@
/** @noinspection UnusedFunctionResultInspection */
use App\Http\Controllers\AuthController;
use App\Http\Controllers\Controller;
use App\Http\Controllers\PasswordController;
use App\Http\Controllers\SetupController;
use Illuminate\Support\Facades\Route;
@ -23,17 +24,9 @@ Route::get('/', static function () {
return redirect()->route('setup');
}
if (auth()->hasUser()) {
return redirect()->route('dashboard');
}
return redirect()->route('auth.login');
});
Route::inertia('dashboard', 'Dashboard')
->middleware('auth')
->name('dashboard');
Route::name('auth.')
->middleware('guest')
->group(static function () {
@ -88,3 +81,11 @@ Route::get('lang/{language}', static function ($language) {
return redirect()->back();
})->name('app.language');
Route::inertia('dashboard', 'Dashboard')
->middleware('auth')
->name('dashboard');
Route::get('modules/{filter?}', [Controller::class, 'getModules'])
->middleware('auth')
->name('modules');