Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
datlechin committed Aug 4, 2024
1 parent e049739 commit 193ba83
Show file tree
Hide file tree
Showing 19 changed files with 211 additions and 154 deletions.
1 change: 1 addition & 0 deletions config/filament-menu-builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@
'tables' => [
'menus' => 'menus',
'menu_items' => 'menu_items',
'menu_locations' => 'menu_locations',
],
];
8 changes: 8 additions & 0 deletions database/migrations/create_menus_table.php.stub
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,18 @@ return new class extends Migration
$table->integer('order')->default(0);
$table->timestamps();
});

Schema::create(config('filament-menu-builder.tables.menu_locations'), function (Blueprint $table) {
$table->id();
$table->foreignIdFor(Menu::class);
$table->string('location', 50);
$table->timestamps();
});
}

public function down(): void
{
Schema::dropIfExists('menu_locations');
Schema::dropIfExists('menu_items');
Schema::dropIfExists('menus');
}
Expand Down
45 changes: 15 additions & 30 deletions resources/views/edit-record.blade.php
Original file line number Diff line number Diff line change
@@ -1,45 +1,30 @@
<x-filament-panels::page
@class([
'fi-resource-edit-record-page',
'fi-resource-' . str_replace('/', '-', $this->getResource()::getSlug()),
'fi-resource-record-' . $record->getKey(),
])
>
<x-filament-panels::page @class([
'fi-resource-edit-record-page',
'fi-resource-' . str_replace('/', '-', $this->getResource()::getSlug()),
'fi-resource-record-' . $record->getKey(),
])>
@capture($form)
<x-filament-panels::form
id="form"
:wire:key="$this->getId() . '.forms.' . $this->getFormStatePath()"
wire:submit="save"
>
{{ $this->form }}
<x-filament-panels::form id="form" :wire:key="$this->getId() . '.forms.' . $this->getFormStatePath()"
wire:submit="save">
{{ $this->form }}

<x-filament-panels::form.actions
:actions="$this->getCachedFormActions()"
:full-width="$this->hasFullWidthFormActions()"
/>
</x-filament-panels::form>
<x-filament-panels::form.actions :actions="$this->getCachedFormActions()" :full-width="$this->hasFullWidthFormActions()" />
</x-filament-panels::form>
@endcapture

@php
$relationManagers = $this->getRelationManagers();
$hasCombinedRelationManagerTabsWithContent = $this->hasCombinedRelationManagerTabsWithContent();
@endphp

@if ((! $hasCombinedRelationManagerTabsWithContent) || (! count($relationManagers)))
@if (!$hasCombinedRelationManagerTabsWithContent || !count($relationManagers))
{{ $form() }}
@endif

@if (count($relationManagers))
<x-filament-panels::resources.relation-managers
:active-locale="isset($activeLocale) ? $activeLocale : null"
:active-manager="$this->activeRelationManager ?? ($hasCombinedRelationManagerTabsWithContent ? null : array_key_first($relationManagers))"
:content-tab-label="$this->getContentTabLabel()"
:content-tab-icon="$this->getContentTabIcon()"
:content-tab-position="$this->getContentTabPosition()"
:managers="$relationManagers"
:owner-record="$record"
:page-class="static::class"
>
<x-filament-panels::resources.relation-managers :active-locale="isset($activeLocale) ? $activeLocale : null" :active-manager="$this->activeRelationManager ??
($hasCombinedRelationManagerTabsWithContent ? null : array_key_first($relationManagers))" :content-tab-label="$this->getContentTabLabel()"
:content-tab-icon="$this->getContentTabIcon()" :content-tab-position="$this->getContentTabPosition()" :managers="$relationManagers" :owner-record="$record" :page-class="static::class">
@if ($hasCombinedRelationManagerTabsWithContent)
<x-slot name="content">
{{ $form() }}
Expand All @@ -50,7 +35,7 @@

<div class="grid grid-cols-12 gap-4">
<div class="flex flex-col col-span-12 gap-4 sm:col-span-4">
@foreach(\Datlechin\FilamentMenuBuilder\FilamentMenuBuilderPlugin::get()->getMenuPanels() as $menuPanel)
@foreach (\Datlechin\FilamentMenuBuilder\FilamentMenuBuilderPlugin::get()->getMenuPanels() as $menuPanel)
<livewire:menu-builder-panel :menu="$record" :menuPanel="$menuPanel" />
@endforeach

Expand Down
7 changes: 1 addition & 6 deletions resources/views/livewire/panel.blade.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
<form wire:submit="add">
<x-filament::section
:heading="$heading"
:collapsible="true"
:persist-collapsed="true"
id="menu-panel"
>
<x-filament::section :heading="$name" :collapsible="true" :persist-collapsed="true" id="menu-panel">
{{ $this->form }}

<x-slot:footerActions>
Expand Down
14 changes: 14 additions & 0 deletions src/Contracts/MenuPanel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

declare(strict_types=1);

namespace Datlechin\FilamentMenuBuilder\Contracts;

interface MenuPanel
{
public function getName(): string;

public function getItems(): array;

public function getSort(): int;
}
38 changes: 21 additions & 17 deletions src/FilamentMenuBuilderPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Datlechin\FilamentMenuBuilder;

use Closure;
use Datlechin\FilamentMenuBuilder\Contracts\MenuPanel;
use Datlechin\FilamentMenuBuilder\Resources\MenuResource;
use Filament\Contracts\Plugin;
use Filament\Panel;
Expand All @@ -14,7 +14,7 @@ class FilamentMenuBuilderPlugin implements Plugin
protected array $locations = [];

/**
* @var array<MenuPanel>
* @var MenuPanel[]
*/
protected array $menuPanels = [];

Expand Down Expand Up @@ -48,27 +48,29 @@ public static function get(): static
return $plugin;
}

public function location(string $key, string $label): static
public function addLocation(string $key, string $label): static
{
$this->locations[$key] = $label;

return $this;
}

public function menuPanel(Closure $callback): static
public function addMenuPanel(MenuPanel $menuPanel): static
{
$panel = value($callback);

if ($panel instanceof StaticMenu) {
if (! $panel->getItems()) {
return $this;
}

$this->menuPanels[] = MenuPanel::make()
->heading('Liên kết cố định')
->addItems($panel->getItems());
} else {
$this->menuPanels[] = $panel;
if ($menuPanel->getItems()) {
$this->menuPanels[] = $menuPanel;
}

return $this;
}

/**
* @param array<MenuPanel> $menuPanels
*/
public function addMenuPanels(array $menuPanels): static
{
foreach ($menuPanels as $menuPanel) {
$this->addMenuPanel($menuPanel);
}

return $this;
Expand All @@ -79,7 +81,9 @@ public function menuPanel(Closure $callback): static
*/
public function getMenuPanels(): array
{
return $this->menuPanels;
return collect($this->menuPanels)
->sortBy(fn(MenuPanel $menuPanel) => $menuPanel->getSort())
->all();
}

public function getLocations(): array
Expand Down
1 change: 1 addition & 0 deletions src/FilamentMenuBuilderServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Datlechin\FilamentMenuBuilder\Livewire\CreateCustomLink;
use Datlechin\FilamentMenuBuilder\Livewire\MenuItems;
use Datlechin\FilamentMenuBuilder\Livewire\MenuPanel;
use Filament\Support\Assets\AlpineComponent;
use Filament\Support\Assets\Css;
use Filament\Support\Facades\FilamentAsset;
Expand Down
8 changes: 4 additions & 4 deletions src/Livewire/MenuItems.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function reorder(array $order, ?string $parentId = null): void
->update([
'order' => DB::raw(
'case ' . collect($order)
->map(fn ($recordKey, int $recordIndex): string => 'when id = ' . DB::getPdo()->quote($recordKey) . ' then ' . ($recordIndex + 1))
->map(fn($recordKey, int $recordIndex): string => 'when id = ' . DB::getPdo()->quote($recordKey) . ' then ' . ($recordIndex + 1))
->implode(' ') . ' end',
),
'parent_id' => $parentId,
Expand All @@ -74,9 +74,9 @@ public function editAction(): Action
->label(__('filament-actions::edit.single.label'))
->iconButton()
->size(ActionSize::Small)
->modalHeading(fn (array $arguments): string => __('filament-actions::edit.single.modal.heading', ['label' => $arguments['title']]))
->modalHeading(fn(array $arguments): string => __('filament-actions::edit.single.modal.heading', ['label' => $arguments['title']]))
->icon('heroicon-m-pencil-square')
->fillForm(fn (array $arguments): array => MenuItem::query()
->fillForm(fn(array $arguments): array => MenuItem::query()
->where('id', $arguments['id'])
->select(['id', 'title', 'url', 'target'])
->first()
Expand Down Expand Up @@ -112,7 +112,7 @@ public function deleteAction(): Action
->iconButton()
->size(ActionSize::Small)
->requiresConfirmation()
->modalHeading(fn (array $arguments): string => __('filament-actions::delete.single.modal.heading', ['label' => $arguments['title']]))
->modalHeading(fn(array $arguments): string => __('filament-actions::delete.single.modal.heading', ['label' => $arguments['title']]))
->modalSubmitActionLabel(__('filament-actions::delete.single.modal.actions.delete.label'))
->modalIcon(FilamentIcon::resolve('actions::delete-action.modal') ?? 'heroicon-o-trash')
->action(function (array $arguments): void {
Expand Down
18 changes: 12 additions & 6 deletions src/Livewire/MenuPanel.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Datlechin\FilamentMenuBuilder\Livewire;

use Datlechin\FilamentMenuBuilder\Contracts\MenuPanel as ContractsMenuPanel;
use Datlechin\FilamentMenuBuilder\Models\Menu;
use Filament\Forms\Components\CheckboxList;
use Filament\Forms\Concerns\InteractsWithForms;
Expand All @@ -19,17 +20,22 @@ class MenuPanel extends Component implements HasForms

public Menu $menu;

public string $heading;
public string $name;

public array $items = [];

#[Validate('required|array')]
public array $data = [];

public function mount(\Datlechin\FilamentMenuBuilder\MenuPanel $menuPanel): void
public function mount(ContractsMenuPanel $menuPanel): void
{
$this->heading = $menuPanel->getHeading();
$this->items = $menuPanel->getItems();
$this->name = $menuPanel->getName();
$this->items = collect($menuPanel->getItems())->map(function ($item) {
return [
'title' => $item['title'],
'url' => value($item['url']),
];
})->all();
}

public function add(): void
Expand All @@ -39,7 +45,7 @@ public function add(): void
$order = $this->menu->menuItems()->max('order') ?? 0;

$selectedItems = collect($this->items)
->filter(fn ($item) => in_array($item['title'], $this->data))
->filter(fn($item) => in_array($item['title'], $this->data))
->map(function ($item) use (&$order) {
return [
...$item,
Expand All @@ -65,7 +71,7 @@ public function form(Form $form): Form
->hiddenLabel()
->required()
->bulkToggleable()
->options(collect($this->items)->mapWithKeys(fn ($item) => [$item['title'] => $item['title']])),
->options(collect($this->items)->mapWithKeys(fn($item) => [$item['title'] => $item['title']])),
]);
}

Expand Down
24 changes: 24 additions & 0 deletions src/MenuPanel/AbstractMenuPanel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

declare(strict_types=1);

namespace Datlechin\FilamentMenuBuilder\MenuPanel;

use Datlechin\FilamentMenuBuilder\Contracts\MenuPanel;

abstract class AbstractMenuPanel implements MenuPanel
{
protected int $sort = 0;

public function sort(int $sort): static
{
$this->sort = $sort;

return $this;
}

public function getSort(): int
{
return $this->sort;
}
}
53 changes: 0 additions & 53 deletions src/MenuPanel/MenuPanel.php

This file was deleted.

30 changes: 0 additions & 30 deletions src/MenuPanel/StaticMenu.php

This file was deleted.

Loading

0 comments on commit 193ba83

Please sign in to comment.