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

Release/2.x #20

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"codedor/laravel-locale-collection": "^1.0",
"codedor/laravel-online-scope": "^1.0",
"filament/filament": "^3.1",
"illuminate/contracts": "^10.0",
"illuminate/contracts": "^10.0 || ^11.0",
"spatie/eloquent-sortable": "^4.0",
"spatie/laravel-navigation": "^1.2",
"spatie/laravel-package-tools": "^1.12",
Expand Down
9 changes: 9 additions & 0 deletions config/filament-menu.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

use Codedor\FilamentMenu\NavigationElements;

return [
'navigation-elements' => [
'link-picker' => NavigationElements\LinkPickerElement::class,
],
];
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('menu_items', function (Blueprint $table) {
$table->after('working_title', function (Blueprint $table) {
$table->string('type')->nullable();
$table->json('data')->nullable();
});

$table->dropColumn(['link', 'label', 'translated_link', 'online']);
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('menu_items', function (Blueprint $table) {
$table->dropColumn('type');
$table->dropColumn('data');

$table->json('link')->nullable();
$table->json('label')->nullable();
$table->json('translated_link')->nullable();
$table->json('online')->nullable();
});
}
};
49 changes: 28 additions & 21 deletions resources/views/components/menu-item.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,29 +29,36 @@ class="space-y-2"
@svg('heroicon-o-ellipsis-vertical', 'text-gray-400 w-5 h-5')
</button>

<span class="py-2 px-4">
<span>{{ $item->working_title }}</span>
<span class="py-2 px-4 flex items-center gap-4">
<div class="flex flex-col">
<span>{{ $item->working_title }}</span>
@if ($item->type)
<span class="text-sm opacity-50">{{ $item->type::$name }}</span>
@endif
</div>

@foreach($item->getTranslations('online') as $locale => $online)
<span
@style([
match ($item->getTranslation('online', $locale)) {
false, null, '' => \Filament\Support\get_color_css_variables('danger', shades: [500, 700]),
default => \Filament\Support\get_color_css_variables('success', shades: [500, 700]),
}
])
class="
text-custom-700 bg-custom-500/10 dark:text-custom-500
rtl:space-x-reverse min-h-6 px-2 py-0.5 text-sm font-medium tracking-tight
inline-flex items-center justify-center space-x-1
rounded-xl whitespace-nowrap
"
>
{{ $locale }}
</span>
@endforeach
<div>
@foreach($item->onlineValues() as $locale => $online)
<span
@style([
match ($online) {
false, null, '' => \Filament\Support\get_color_css_variables('danger', shades: [500, 700]),
default => \Filament\Support\get_color_css_variables('success', shades: [500, 700]),
}
])
class="
text-custom-700 bg-custom-500/10 dark:text-custom-500
rtl:space-x-reverse min-h-6 px-2 py-0.5 text-sm font-medium tracking-tight
inline-flex items-center justify-center space-x-1
rounded-xl whitespace-nowrap
"
>
{{ $locale }}
</span>
@endforeach
</div>

@if(count($item->children) > 0)
@if (count($item->children) > 0)
<button
type="button"
x-on:click="open = !open"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<li @class(['active' => $active])>
<a href="{!! $link !!}">
<span>{{ $label }}</span>
</a>

@if (count($children))
<ul style="margin-left: 2rem">
@foreach ($children as $item)
@if (isset($item['attributes']['type']))
{{ (new $item['attributes']['type'])->render($item) }}
@endif
@endforeach
</ul>
@endif
</li>
13 changes: 0 additions & 13 deletions resources/views/components/render/item.blade.php

This file was deleted.

4 changes: 3 additions & 1 deletion resources/views/components/render/root.blade.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
<div>
<ul>
@foreach ($navigation as $item)
<x-filament-menu::render.item :$item />
@if (isset($item['attributes']['type']))
{{ (new $item['attributes']['type'])->render($item) }}
@endif
@endforeach
</ul>
</div>
4 changes: 1 addition & 3 deletions src/Filament/MenuPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,7 @@ public function register(Panel $panel): void
}
}

public function boot(Panel $panel): void
{
}
public function boot(Panel $panel): void {}

public function menuResource(bool $condition = true): static
{
Expand Down
111 changes: 41 additions & 70 deletions src/Filament/Pages/MenuBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,10 @@
use Codedor\FilamentMenu\Filament\Resources\MenuResource;
use Codedor\FilamentMenu\Models\Menu;
use Codedor\FilamentMenu\Models\MenuItem;
use Codedor\LinkPicker\Filament\LinkPickerInput;
use Codedor\LocaleCollection\Facades\LocaleCollection;
use Codedor\LocaleCollection\Locale;
use Codedor\TranslatableTabs\Forms\TranslatableTabs;
use Codedor\TranslatableTabs\Resources\Traits\HasTranslations;
use Filament\Actions\Action;
use Filament\Forms\Components\Grid;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Components\Toggle;
use Filament\Forms\Get;
use Filament\Notifications\Notification;
use Filament\Resources\Pages\Concerns;
Expand All @@ -23,7 +19,6 @@
class MenuBuilder extends Page
{
use Concerns\InteractsWithRecord;
use HasTranslations;

protected static string $resource = MenuResource::class;

Expand Down Expand Up @@ -59,69 +54,47 @@ public function editAction(): Action

public function formAction(): Action
{
$types = collect(config('filament-menu.navigation-elements', []))
->mapWithKeys(fn (string $element) => [$element => $element::name()]);

return Action::make('edit')
->fillForm(function (array $arguments, array $data) {
$menuItem = isset($arguments['menuItem']) ? MenuItem::find($arguments['menuItem']) : new MenuItem();
->fillForm(function (array $arguments) {
$menuItem = isset($arguments['menuItem'])
? MenuItem::find($arguments['menuItem'])
: new MenuItem;

return [
'working_title' => $menuItem->working_title,
'link' => is_string($menuItem->link)
? json_decode($menuItem->link ?? '[]', true)
: $menuItem->link,
...LocaleCollection::mapWithKeys(function (Locale $locale) use ($menuItem) {
$locale = $locale->locale();

$menuItem->setLocale($locale);

return [
$locale => [
'label' => $menuItem->label,
'translated_link' => $menuItem->translated_link,
'online' => $menuItem->online,
],
];
}),
'type' => $menuItem->type,
...$menuItem->data ?? [],
];
})
->form([
TranslatableTabs::make()
->columnSpan(['lg' => 2])
->defaultFields([
TextInput::make('working_title')
->required()
->maxLength(255),

LinkPickerInput::make('link'),
])
->translatableFields(fn () => [
TextInput::make('label')
->label('Label')
->required(fn (Get $get) => $get('online')),

LinkPickerInput::make('translated_link')
->label('Link')
->helperText('If you want to override the link for this translation, you can do so here.'),

Toggle::make('online')
->label('Online'),
]),
->form(fn () => [
TextInput::make('working_title')
->required()
->maxLength(255),

Select::make('type')
->options($types)
->required()
->reactive(),

Grid::make(1)
->hidden(fn (Get $get) => empty($get('type')))
->schema(fn (Get $get) => $get('type') ? $get('type')::make()->schema() : []),
])
->action(function (array $arguments, array $data) {
$data['menu_id'] = $this->record->id;

$menuItem = MenuItem::updateOrCreate(
[
'id' => $arguments['menuItem'] ?? null,
],
$this->mutateFormDataBeforeSave($data),
);

$title = $menuItem->wasRecentlyCreated
? __('filament-menu::menu-builder.successfully created')
: __('filament-menu::menu-builder.successfully updated');
MenuItem::updateOrCreate([
'id' => $arguments['menuItem'] ?? null,
'menu_id' => $this->record->id,
], [
'working_title' => $data['working_title'],
'type' => $data['type'],
'data' => collect($data)->except('type', 'working_title'),
]);

Notification::make()
->title($title)
->title(__('filament-menu::menu-builder.successfully updated'))
->success()
->send();

Expand All @@ -138,13 +111,12 @@ public function deleteAction(): Action
->color('danger')
->link()
->requiresConfirmation()
->action(function (array $arguments, array $data) {
->action(function (array $arguments) {
$menuItem = MenuItem::findOrFail($arguments['menuItem'] ?? null);

MenuItem::where('parent_id', $menuItem->id)
->update([
'parent_id' => null,
]);
MenuItem::where('parent_id', $menuItem->id)->update([
'parent_id' => null,
]);

$menuItem->delete();

Expand All @@ -161,10 +133,9 @@ public function handleNewOrder(string $statePath, array $items)
{
$itemIds = collect($items)->map(fn ($item) => Str::afterLast($item, '.'));

MenuItem::whereIn('id', $itemIds)
->update([
'parent_id' => ($statePath === 'data.items') ? null : Str::afterLast($statePath, '.'),
]);
MenuItem::whereIn('id', $itemIds)->update([
'parent_id' => ($statePath === 'data.items') ? null : Str::afterLast($statePath, '.'),
]);

MenuItem::setNewOrder($itemIds, 1000);

Expand All @@ -191,7 +162,7 @@ protected function resolveRecord($key): Menu
$record = static::getResource()::resolveRecordRouteBinding($key);

if ($record === null) {
throw (new ModelNotFoundException())->setModel(Menu::class, [$key]);
throw (new ModelNotFoundException)->setModel(Menu::class, [$key]);
}

return $record;
Expand Down
Loading