Skip to content

Commit

Permalink
add ability to persist the state of the open/close menus
Browse files Browse the repository at this point in the history
  • Loading branch information
milewski committed Oct 26, 2019
1 parent 7fd333d commit 09452bc
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 11 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ return [
*/
'translate_title' => false,

/**
* If true the state of the collapsible menu will be persisted upon refresh
*/
'remember_menu_state' => false,

/**
* Main navigation, each item on this array creates a new entry on the sidebar with an icon
*/
Expand Down
5 changes: 5 additions & 0 deletions config/collapsible-resource-manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@
*/
'translate_title' => false,

/**
* If true the state of the collapsible menu will be persisted upon refresh
*/
'remember_menu_state' => false,

/**
* Main navigation, each item on this array creates a new entry on the sidebar with an icon
*/
Expand Down
2 changes: 1 addition & 1 deletion dist/js/tool.js

Large diffs are not rendered by default.

48 changes: 45 additions & 3 deletions resources/js/components/CollapsibleResourceManager.vue
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@

</h3>

<ResourceList class="resources-only" v-if="data.resources" :resources="data.resources" :recursive="data.recursive"/>
<ResourceList class="resources-only"
v-if="data.resources"
:resources="data.resources"
:recursive="data.recursive"
:remember-menu-state="rememberMenuState"/>

<template v-for="(group, index) of data.groups" v-if="group.resources.length">

Expand Down Expand Up @@ -45,7 +49,10 @@

<CollapseTransition :duration="150">

<ResourceList v-if="activeMenu[index]" :resources="group.resources" :recursive="data.recursive"/>
<ResourceList v-if="activeMenu[index]"
:resources="group.resources"
:recursive="data.recursive"
:remember-menu-state="rememberMenuState"/>

</CollapseTransition>

Expand All @@ -64,14 +71,45 @@
name: 'CollapsibleResourceManager',
components: { CollapseTransition, ResourceList },
props: {
data: { type: Object, required: true }
data: { type: Object, required: true },
rememberMenuState: { type: Boolean, default: false }
},
data() {
return {
activeMenu: this.data.groups.map(group => !!group.expanded)
}
},
created() {
if (this.rememberMenuState) {
const state = localStorage.getItem(this.cacheKey)
if (state) {
const activeMenu = JSON.parse(state)
if (Array.isArray(activeMenu)) {
this.activeMenu = activeMenu
}
}
this.$watch(() => this.activeMenu, (state) => {
localStorage.setItem(this.cacheKey, JSON.stringify(state))
})
}
},
computed: {
cacheKey() {
return `menu-state.${this.data.key}`
},
isEmpty() {
return this.data.resources.length + this.data.groups.filter(group => group.resources.length).length === 0
}
Expand All @@ -96,6 +134,10 @@
padding-top: 0;
}
.recursive h4:first-child {
margin-top: 0;
}
.recursive h4 {
margin-left: 0 !important;
}
Expand Down
8 changes: 6 additions & 2 deletions resources/js/components/ResourceList.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@

<li class="leading-tight pt-4 text-sm" v-for="resource of resources" :class="{ 'ml-8': !recursive }">

<collapsible-resource-manager v-if="resource.recursive" :data="resource" :recursive="true"/>
<collapsible-resource-manager v-if="resource.recursive"
:data="resource"
:recursive="true"
:remember-menu-state="rememberMenuState"/>

<template v-else>

Expand Down Expand Up @@ -52,7 +55,8 @@
name: 'ResourceList',
props: {
resources: { type: Array, required: true },
recursive: { type: Boolean, default: false }
recursive: { type: Boolean, default: false },
rememberMenuState: { type: Boolean, required: true },
}
}
Expand Down
4 changes: 3 additions & 1 deletion resources/views/navigation.blade.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
@foreach($navigation as $group)

<collapsible-resource-manager :data='@json($group)'></collapsible-resource-manager>
<collapsible-resource-manager :data='@json($group)'
:remember-menu-state="@json($rememberMenuState)">
</collapsible-resource-manager>

@endforeach
13 changes: 9 additions & 4 deletions src/CollapsibleResourceManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,20 +47,24 @@ public function boot()
public function renderNavigation()
{
return view('collapsible-resource-manager::navigation', [
'rememberMenuState' => config('collapsible-resource-manager.remember_menu_state'),
'navigation' => $this->serializeGroups(config('collapsible-resource-manager.navigation')),
]);
}

/**
* @param array $navigation
*
* @param null $parentKey
*
* @return array
*/
private function serializeGroups(array $navigation): array
private function serializeGroups(array $navigation, $parentKey = null): array
{

foreach ($navigation as &$item) {
foreach ($navigation as $key => &$item) {

$item[ 'key' ] = "_{$parentKey}_{$key}_";
$item[ 'title' ] = isset($item[ 'title' ]) ? $this->translateKey($item[ 'title' ]) : null;
$item[ 'resources' ] = $this->resolveResources($item[ 'resources' ] ?? []);
$item[ 'groups' ] = $this->parseGroups($item[ 'groups' ] ?? []);
Expand Down Expand Up @@ -90,7 +94,7 @@ private function resolveResources(array $resources): Collection

if (is_array($resource)) {

return array_merge($this->serializeGroups([ $resource ])[ 0 ], [ 'recursive' => true ]);
return array_merge($this->serializeGroups([ $resource ], $key)[ 0 ], [ 'recursive' => true ]);

}

Expand All @@ -114,7 +118,7 @@ private function resolveResources(array $resources): Collection

}

})->filter();
})->filter()->values();
}

private function parseGroups(array $groups): Collection
Expand All @@ -129,4 +133,5 @@ private function parseGroups(array $groups): Collection
});

}

}

0 comments on commit 09452bc

Please sign in to comment.