Skip to content

Commit

Permalink
updated menu builder
Browse files Browse the repository at this point in the history
  • Loading branch information
indpurvesh committed Aug 9, 2019
1 parent 67519f4 commit df2ca92
Show file tree
Hide file tree
Showing 13 changed files with 178 additions and 58 deletions.
8 changes: 7 additions & 1 deletion resources/components/cms/menu/MenuSave.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
import isNil from 'lodash/isNil';
export default {
props: ['propCategories', 'baseUrl', 'propMenus', 'menuGroup'],
props: ['propCategories', 'baseUrl', 'propMenus', 'menuGroup', 'propFrontMenus'],
data () {
return {
categories: [],
frontMenus: [],
selected: null,
menus: [],
form: this.$form.createForm(this),
Expand Down Expand Up @@ -49,6 +50,11 @@ export default {
if (!isNil(this.propCategories)) {
this.propCategories.forEach(ele => this.categories.push(ele));
}
if (!isNil(this.propFrontMenus)) {
Object.keys(this.propFrontMenus).forEach(key => {
this.frontMenus.push(this.propFrontMenus[key])
});
}
if (!isNil(this.propMenus)) {
this.propMenus.forEach(ele => this.menus.push(ele));
}
Expand Down
1 change: 1 addition & 0 deletions resources/lang/en/cms.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
'identifier' => 'Menu Group Identifier',
'builder' => 'Menu Builder',
'category_list' => 'Categories',
'frontmenu_list' => 'Front Menus',
'create' => [
'title' => 'Menu Builder'
]
Expand Down
17 changes: 16 additions & 1 deletion resources/views/cms/menu/_fields.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@
<a-col :span="24">
<a-card title="{{ __('avored::cms.menu.builder') }}">
<a-col :span="12">

<h4>{{ __('avored::cms.menu.category_list') }}</h4>
<div class="ant-list menu-builder-list ant-list-split ant-list-bordered mr-1">
<vddl-list disabled-if="true" :drop="handleDrop" :wrapper="categories" :list="categories">
Expand All @@ -65,6 +64,22 @@ class="menu-item"
</vddl-draggable>
</vddl-list>
</div>

<h4 class="mt-1">{{ __('avored::cms.menu.frontmenu_list') }}</h4>
<div class="ant-list menu-builder-list ant-list-split ant-list-bordered mr-1">
<vddl-list disabled-if="true" :drop="handleDrop" :wrapper="frontMenus" :list="frontMenus">
<vddl-draggable
:draggable="item"
effect-allowed="copy"
class="menu-item"
:index="index"
:wrapper="frontMenus"
:key="'frontmenu-' + item.id"
v-for="(item, index) in frontMenus">
@{{item.name}}
</vddl-draggable>
</vddl-list>
</div>
</a-col>
<a-col :span="12">
<p>{{ __('avored::cms.menu.create.title') }}</p>
Expand Down
3 changes: 2 additions & 1 deletion resources/views/cms/menu/create.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
@section('content')
<a-row type="flex" justify="center">
<a-col :span="24">
<menu-save
<menu-save
:prop-front-menus="{{ $frontMenus }}"
:prop-categories="{{ $categories }}"
base-url="{{ asset(config('avored.admin_url')) }}"
inline-template>
Expand Down
1 change: 1 addition & 0 deletions resources/views/cms/menu/edit.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<a-col :span="24">
<menu-save
:prop-categories="{{ $categories }}"
:prop-front-menus="{{ $frontMenus }}"
:menu-group="{{ $menuGroup }}"
:prop-menus="{{ $menuGroup->menus }}"
base-url="{{ asset(config('avored.admin_url')) }}"
Expand Down
9 changes: 8 additions & 1 deletion src/Cms/Controllers/MenuGroupController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Illuminate\Http\Request;
use AvoRed\Framework\Database\Contracts\CategoryModelInterface;
use AvoRed\Framework\Database\Models\MenuGroup;
use AvoRed\Framework\Support\Facades\Menu as MenuFacade;

class MenuGroupController
{
Expand Down Expand Up @@ -64,8 +65,10 @@ public function index()
public function create()
{
$categories = $this->categoryRepository->getCategoryOptionForMenuBuilder();

$frontMenus = MenuFacade::frontMenus();

return view('avored::cms.menu.create')
->with('frontMenus', $frontMenus)
->with('categories', $categories);
}

Expand Down Expand Up @@ -93,8 +96,11 @@ public function store(MenuRequest $request)
public function edit(MenuGroup $menuGroup)
{
$categories = $this->categoryRepository->getCategoryOptionForMenuBuilder();
$frontMenus = MenuFacade::frontMenus();

return view('avored::cms.menu.edit')
->with('categories', $categories)
->with('frontMenus', $frontMenus)
->with('menuGroup', $menuGroup);
}

Expand All @@ -107,6 +113,7 @@ public function edit(MenuGroup $menuGroup)
*/
public function update(MenuRequest $request, MenuGroup $menuGroup)
{
$menuGroup->menus()->delete();
$menuGroup->update($request->all());
$menus = json_decode($request->get('menu_json'));

Expand Down
8 changes: 8 additions & 0 deletions src/Database/Contracts/MenuGroupModelInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use AvoRed\Framework\Database\Models\MenuGroup;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Support\Collection as SupportCollection;

interface MenuGroupModelInterface
{
Expand All @@ -21,6 +22,13 @@ public function create(array $data) : MenuGroup;
*/
public function find(int $id) : MenuGroup;

/**
* Get Menus Resource from data store
* @param string $identifier
* @return \AvoRed\Framework\Database\Models\MenuGroup $menuGroup
*/
public function getTreeByIdentifier(string $identifier) : SupportCollection;

/**
* Delete MenuGroup Resource from a database
* @param int $id
Expand Down
10 changes: 10 additions & 0 deletions src/Database/Models/Menu.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,14 @@ class Menu extends Model
* @var array
*/
protected $fillable = ['name', 'url', 'sort_order', 'menu_group_id', 'parent_id'];


/**
* Menu has many sub menus
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function submenus()
{
return $this->hasMany(self::class, 'parent_id');
}
}
21 changes: 21 additions & 0 deletions src/Database/Repository/MenuGroupRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use AvoRed\Framework\Database\Models\MenuGroup;
use AvoRed\Framework\Database\Contracts\MenuGroupModelInterface;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Support\Collection as SupportCollection;

class MenuGroupRepository implements MenuGroupModelInterface
{
Expand Down Expand Up @@ -38,6 +39,26 @@ public function delete(int $id): int
return MenuGroup::destroy($id);
}

/**
* Find MenuGroup Resource from data store
* @param string $identifier
* @return \AvoRed\Framework\Database\Models\MenuGroup $menuGroup
*/
public function getTreeByIdentifier(string $identifier) : SupportCollection
{
$menus = collect();
$menuGroup = MenuGroup::whereIdentifier($identifier)->first();


$modelMenus = $menuGroup->menus()->whereNull('parent_id')->get();
foreach ($modelMenus as $modelMenu) {
$modelMenu->submenus;
$menus->push($modelMenu);
}

return $menus;
}

/**
* Get all the categories from the connected database
* @return \Illuminate\Database\Eloquent\Collection $menuGroups
Expand Down
70 changes: 35 additions & 35 deletions src/Menu/MenuBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
namespace AvoRed\Framework\Menu;

use Illuminate\Support\Collection;
use stdClass;

class MenuBuilder
{
Expand All @@ -11,24 +12,12 @@ class MenuBuilder
*/
protected $collection;

/**
* Admin Menu Collection.
* @var \Illuminate\Support\Collection
*/
protected $adminCollection;
/**
* Admin Menu Flag.
* @var bool
*/
protected $admin = false;

/**
* Construct for the menu builder
*/
public function __construct()
{
$this->collection = Collection::make([]);
$this->adminCollection = Collection::make([]);
}

/**
Expand All @@ -41,36 +30,36 @@ public function make($key, callable $callable)
{
$menu = new MenuItem($callable);
$menu->key($key);
if ($this->admin) {
$this->adminCollection->put($key, $menu);
} else {
$this->collection->put($key, $menu);
}
$this->collection->put($key, $menu);

return $this;
}

/**
* Make a admin flag true.
* @return self
* Return Menu Object.
* @var string
* @return \AvoRed\Framework\Menu\Menu
*/
public function admin()
public function get($key)
{
$this->admin = true;

return $this;
return $this->collection->get($key);
}

/**
* Return Menu Object.
* @var string
* @return \AvoRed\Framework\Menu\Menu
* Return all available Menu in Menu.
* @param void
* @return \Illuminate\Support\Collection
*/
public function get($key)
public function all($admin = false)
{
if ($this->admin) {
return $this->adminCollection->get($key);
if ($admin) {
return $this->collection->filter(function ($item) {
return $item->type() === MenuItem::ADMIN;
});
} else {
return $this->collection->get($key);
return $this->collection->filter(function ($item) {
return $item->type() === MenuItem::FRONT;
});
}
}

Expand All @@ -79,12 +68,23 @@ public function get($key)
* @param void
* @return \Illuminate\Support\Collection
*/
public function all()
public function frontMenus()
{
if ($this->admin) {
return $this->adminCollection->all();
} else {
return $this->collection->all();
$frontMenus = collect();

$i = 1;
foreach ($this->collection as $item) {
if ($item->type() === MenuItem::FRONT) {
$menu = new stdClass;
$menu->id = $i;
$menu->name = $item->label;
$menu->url = route($item->route(), $item->params());
$menu->submenus = $item->submenus ?? [];
$frontMenus->push($menu);
$i++;
}
}

return $frontMenus;
}
}
42 changes: 36 additions & 6 deletions src/Menu/MenuItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,52 @@

class MenuItem implements MenuInterface
{
/**
* Constant Front
* @var string FRONT
*/
const FRONT = "front";

/**
* Constant Admin
* @var string ADMIN
*/
const ADMIN = "admin";

/**
* @var string $label
*/
protected $label;
public $label;

/**
* @var string $type
*/
public $type;

/**
* @var string $icon
*/
protected $icon;
public $icon;

/**
* @var array $attributes
*/
protected $attributes;
public $attributes;

/**
* @var string $key
*/
protected $key;
public $key;

/**
* @var string $params
*/
protected $params;
public $params;

/**
* @var string $routeName
*/
protected $routeName;
public $routeName;

/**
* AvoRed Front Menu Construct method.
Expand All @@ -57,6 +74,19 @@ public function label($label = null)
return trans($this->label);
}

/**
* Get/Set Admin Menu Type.
* @return mixed
*/
public function type($type = null)
{
if (null !== $type) {
$this->type = $type;
return $this;
}
return $this->type;
}

/**
* Get/Set Admin Menu Identifier.
* @return \AvoRed\Framework\Menu\Menu|string
Expand Down
Loading

0 comments on commit df2ca92

Please sign in to comment.