Skip to content

Commit

Permalink
feat: add menu item new tab toggle (#320)
Browse files Browse the repository at this point in the history
  • Loading branch information
andreiio authored Apr 20, 2023
1 parent 4b5f402 commit db5d428
Show file tree
Hide file tree
Showing 12 changed files with 70 additions and 33 deletions.
1 change: 1 addition & 0 deletions app/Http/Controllers/Admin/MenuController.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ protected function prepareItems(array $items, string $location, int $depth = 0):
'type' => $item['type'],
'url' => $item['type'] === 'external' ? ($item['url'] ?? null) : null,
'route' => $item['type'] === 'route' ? ($item['route'] ?? null) : null,
'new_tab' => $item['new_tab'] ?? false,
'children' => $this->prepareItems($item['children'] ?? [], $location, ++$depth),
];

Expand Down
1 change: 1 addition & 0 deletions app/Http/Requests/Admin/MenuRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ protected function nestedRules(int $depth): array
'%prefix%.*.url' => ['required_if:%prefix%.*.type,external', 'nullable', 'url'],
'%prefix%.*.model' => ['required_if:%prefix%.*.type,' . MenuItem::allowedModels()->keys()->join(',')],
'%prefix%.*.route' => ['required_if:%prefix%.*.type,route'],
'%prefix%.*.new_tab' => ['boolean'],
];

$rules = collect();
Expand Down
1 change: 1 addition & 0 deletions app/Http/Resources/MenuItemResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ protected function default(Request $request): array
'model_type' => $this->model_type,
'model' => $this->model_id,
'route' => $this->route,
'new_tab' => $this->new_tab,
'children' => self::collection($this->children),
];
}
Expand Down
2 changes: 1 addition & 1 deletion app/Models/MenuItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class MenuItem extends Model
];

protected $fillable = [
'id', 'type', 'location', 'position', 'model_type', 'model_id', 'route',
'id', 'type', 'location', 'position', 'model_type', 'model_id', 'route', 'new_tab',
];

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

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

return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up(): void
{
Schema::table('menu_items', function (Blueprint $table) {
$table->boolean('new_tab')->default(false);
});
}
};
3 changes: 2 additions & 1 deletion lang/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@
"field.min_value": "Minimum value",
"field.name": "Name",
"field.number": "Number",
"field.open_in_new_tab": "Open in new tab",
"field.options": "Options",
"field.page": "Page",
"field.parent": "Parent",
Expand Down Expand Up @@ -268,7 +269,7 @@

"form.label": "Form|Forms",
"form.action.create": "Add form",
"form.action.edit": "Edit formu",
"form.action.edit": "Edit form",
"form.action.update": "Update form",
"form.action.delete": "Delete form",
"form.action.submit": "Submit",
Expand Down
1 change: 1 addition & 0 deletions lang/ro.json
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@
"field.min_value": "Valoare minimă",
"field.name": "Nume",
"field.number": "Număr",
"field.open_in_new_tab": "Deschide în tab nou",
"field.options": "Opțiuni",
"field.page": "Pagină",
"field.parent": "Părinte",
Expand Down
67 changes: 37 additions & 30 deletions resources/js/components/MenuBuilder/Item.vue
Original file line number Diff line number Diff line change
Expand Up @@ -77,40 +77,47 @@
required
/>

<template v-if="item.type === 'external'">
<localized-field
field="form-input"
type="url"
:label="$t('field.url')"
:name="`${prefix}.url`"
v-model="item.url"
required
/>
</template>

<template v-else-if="item.type === 'text'">
<template v-if="item.type === 'text'">
<!-- blank -->
</template>

<template v-else-if="item.type === 'route'">
<form-select
:label="$t(`field.${item.type}`)"
:name="`${prefix}.route`"
v-model="item.route"
:options="routes"
required
/>
</template>
<template v-else>
<template v-if="item.type === 'external'">
<localized-field
field="form-input"
type="url"
:label="$t('field.url')"
:name="`${prefix}.url`"
v-model="item.url"
required
/>
</template>

<template v-else-if="item.type === 'route'">
<form-select
:label="$t(`field.${item.type}`)"
:name="`${prefix}.route`"
v-model="item.route"
:options="routes"
required
/>
</template>

<template v-else-if="models.length">
<form-select
:label="$t(`field.${item.type}`)"
:name="`${prefix}.model`"
v-model="item.model"
:options="models"
option-value-key="id"
option-label-key="title"
required
/>
</template>

<template v-else-if="models.length">
<form-select
:label="$t(`field.${item.type}`)"
:name="`${prefix}.model`"
v-model="item.model"
:options="models"
option-value-key="id"
option-label-key="title"
required
<form-toggle
:label="$t('field.open_in_new_tab')"
v-model="item.new_tab"
/>
</template>
</div>
Expand Down
1 change: 1 addition & 0 deletions resources/js/components/MenuBuilder/List.vue
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
props.items.push({
label: {},
type: 'external',
new_tab: false,
url: {},
children: [],
});
Expand Down
2 changes: 1 addition & 1 deletion resources/views/components/menu-item/external.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<a
{{ $attributes->merge([
'href' => $item->url,
'target' => '_blank',
'target' => $item->new_tab ? '_blank' : null,
'rel' => 'noopener',
'class' => $inactiveClass,
]) }}>
Expand Down
1 change: 1 addition & 0 deletions resources/views/components/menu-item/model.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<a
{{ $attributes->merge([
'href' => $item->model->url,
'target' => $item->new_tab && !$item->isCurrentUrl() ? '_blank' : null,
'class' => $item->isCurrentUrl() ? $activeClass : $inactiveClass,
]) }}>
{{ $item->label }}
Expand Down
1 change: 1 addition & 0 deletions resources/views/components/menu-item/route.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<a
{{ $attributes->merge([
'href' => localized_route("front.$item->route"),
'target' => $item->new_tab && !$item->isCurrentUrl() ? '_blank' : null,
'class' => $item->isCurrentUrl() ? $activeClass : $inactiveClass,
]) }}>
{{ $item->label }}
Expand Down

0 comments on commit db5d428

Please sign in to comment.