Skip to content

Commit

Permalink
add readme, prepare for first release
Browse files Browse the repository at this point in the history
  • Loading branch information
milewski committed Sep 28, 2019
1 parent 01bcc55 commit 1b41c3e
Show file tree
Hide file tree
Showing 12 changed files with 249 additions and 102 deletions.
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2019 Digital Creative

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
84 changes: 84 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
# Collapsible Resource Manager

[![Latest Version on Packagist](https://img.shields.io/packagist/v/digital-creative/collapsible-resource-manager.svg)](https://packagist.org/packages/digital-creative/collapsible-resource-manager)
[![Total Downloads](https://img.shields.io/packagist/dt/digital-creative/collapsible-resource-manager.svg)](https://packagist.org/packages/digital-creative/collapsible-resource-manager)
[![License](https://img.shields.io/packagist/l/digital-creative/collapsible-resource-manager.svg)](https://github.com/digital-creative/collapsible-resource-manager/blob/master/LICENSE)

![Laravel Nova Collapsible Resource Manager in action](https://github.com/dcasia/collapsible-resource-manager/screenshoots/demo-1.png)

Provides an easy way to order and group your resources on the sidebar.

# Installation

You can install the package via composer:

```
composer require digital-creative/collapsible-resource-manager
```

Next up, you must register the tool with Nova. This is typically done in the `tools` method of the `NovaServiceProvider.`

```php
class NovaServiceProvider extends NovaApplicationServiceProvider
{
public function tools()
{
return [
// ...
new CollapsibleResourceManager()
];

}
}
```

Publish the configuration file

```bash
php artisan vendor:publish --provider="DigitalCreative\CollapsibleResourceManager\CollapsibleResourceManagerServiceProvider" --tag="config"
```

Config file reference:

```php
return [
/**
* If false the default resource manager will coexist with this tool
*/
'disable_default_resource_manage' => true,

/**
* If true all titles within navigation array will pass through laravel trans helper
*/
'translate_title' => false,

/**
* Main navigation, each item on this array creates a new entry on the sidebar with an icon
*/
'navigation' => [
[
'title' => 'Resource Title',
'icon' => null, //<svg></svg> or <img src=""/>
'groups' => [
[
'title' => 'Group Title',
'expanded' => true,
'resources' => [
\App\Nova\User::class,
]
]
]
]
]
];
```

# Notes

A key difference between `CollapsibleResourceManager` and the default Nova `ResourceManager` is that the later auto scan
for resources within the `App\Nova` directory and adds it to the navigation, `CollapsibleResourceManager` expects
every entry to be added manually from the configuration file.

## License

The MIT License (MIT). Please see [License File](https://github.com/dcasia/collapsible-resource-manager/raw/master/LICENSE) for more information.
9 changes: 6 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
{
"name": "digital-creative/collapsible-resource-manager",
"description": "A Laravel Nova tool.",
"description": "A custom sidebar menu with collapsible groups",
"keywords": [
"laravel",
"nova"
"nova",
"sidebar",
"collapse",
"collapsible"
],
"license": "MIT",
"require": {
Expand All @@ -17,7 +20,7 @@
"extra": {
"laravel": {
"providers": [
"DigitalCreative\\CollapsibleResourceManager\\ToolServiceProvider"
"DigitalCreative\\CollapsibleResourceManager\\CollapsibleResourceManagerServiceProvider"
]
}
},
Expand Down
32 changes: 32 additions & 0 deletions config/collapsible-resource-manager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

return [
/**
* If false the default resource manager will coexist with this tool
*/
'disable_default_resource_manage' => true,

/**
* If true all titles within navigation array will pass through laravel trans helper
*/
'translate_title' => false,

/**
* Main navigation, each item on this array creates a new entry on the sidebar with an icon
*/
'navigation' => [
[
'title' => 'Resource Title',
'icon' => null, //<svg></svg> or <img src=""/>
'groups' => [
[
'title' => 'Group Title',
'expanded' => true,
'resources' => [
\App\Nova\User::class,
]
]
]
]
]
];
2 changes: 1 addition & 1 deletion dist/js/tool.js

Large diffs are not rendered by default.

15 changes: 9 additions & 6 deletions resources/js/components/CollapsibleResourceManager.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@

<h3 class="flex items-center font-normal text-white mb-6 text-base no-underline">

<div v-if="icon" class="sidebar-icon" v-html="icon"/>
<div v-if="data.icon" class="sidebar-icon" v-html="data.icon"/>

<svg v-else class="sidebar-icon" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20">
<path fill="var(--sidebar-icon)"
d="M3 1h4c1.1045695 0 2 .8954305 2 2v4c0 1.1045695-.8954305 2-2 2H3c-1.1045695 0-2-.8954305-2-2V3c0-1.1045695.8954305-2 2-2zm0 2v4h4V3H3zm10-2h4c1.1045695 0 2 .8954305 2 2v4c0 1.1045695-.8954305 2-2 2h-4c-1.1045695 0-2-.8954305-2-2V3c0-1.1045695.8954305-2 2-2zm0 2v4h4V3h-4zM3 11h4c1.1045695 0 2 .8954305 2 2v4c0 1.1045695-.8954305 2-2 2H3c-1.1045695 0-2-.8954305-2-2v-4c0-1.1045695.8954305-2 2-2zm0 2v4h4v-4H3zm10-2h4c1.1045695 0 2 .8954305 2 2v4c0 1.1045695-.8954305 2-2 2h-4c-1.1045695 0-2-.8954305-2-2v-4c0-1.1045695.8954305-2 2-2zm0 2v4h4v-4h-4z"/>
</svg>

<span class="sidebar-label">{{ title }}</span>
<span class="sidebar-label">{{ data.title }}</span>

</h3>

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

<h4 class="relative select-none mt-4 ml-8 text-xs text-white-50% uppercase tracking-wide cursor-pointer"
v-if="group.title"
Expand Down Expand Up @@ -59,12 +59,12 @@
import {CollapseTransition} from 'vue2-transitions'
export default {
name: 'BetterMenu',
name: 'CollapsibleResourceManager',
components: {CollapseTransition},
props: ['title', 'icon', 'groups'],
props: ['data'],
data() {
return {
activeMenu: this.groups.map(group => !!group.expanded)
activeMenu: this.data.groups.map(group => !!group.expanded)
}
},
methods: {
Expand All @@ -73,9 +73,11 @@
}
}
}
</script>

<style scoped>
.collapsible-indicator {
left: -20px;
width: 12px;
Expand All @@ -84,4 +86,5 @@
justify-content: center;
align-content: center;
}
</style>
7 changes: 5 additions & 2 deletions resources/views/navigation.blade.php
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
<collapsible-resource-manager :groups='@json($groups)' title="{{ $title }}" icon="{{ $icon }}">
</collapsible-resource-manager>
@foreach($navigation as $group)

<collapsible-resource-manager :data='@json($group)'></collapsible-resource-manager>

@endforeach
Binary file added screenshoots/demo-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added screenshoots/demo-2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
129 changes: 57 additions & 72 deletions src/CollapsibleResourceManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,32 +10,6 @@

class CollapsibleResourceManager extends Tool
{
/**
* @var string $title
*/
private $title;

/**
* @var Collection $groups
*/
private $groups;

/**
* @var string $icon
*/
private $icon;

/**
* BetterMenu constructor.
*
* @param null|string $title
*/
public function __construct(string $title = null)
{
$this->title = $title;
$this->groups = collect();
}

/**
* Perform any tasks that need to happen when the tool is booted.
*
Expand All @@ -46,14 +20,18 @@ public function boot()

Nova::script('collapsible-resource-manager', __DIR__ . '/../dist/js/tool.js');

/**
* Remove the default resource manager
*/
foreach (Nova::$tools as $index => $tool) {
if (config('collapsible-resource-manager.disable_default_resource_manage', true)) {

/**
* Remove the default resource manager
*/
foreach (Nova::$tools as $index => $tool) {

if ($tool instanceof ResourceManager) {

if ($tool instanceof ResourceManager) {
unset(Nova::$tools[ $index ]);

unset(Nova::$tools[ $index ]);
}

}

Expand All @@ -69,66 +47,73 @@ public function boot()
public function renderNavigation()
{
return view('collapsible-resource-manager::navigation', [
'groups' => $this->serializeGroups($this->groups),
'title' => $this->title,
'icon' => $this->icon,
'navigation' => $this->serializeGroups(config('collapsible-resource-manager.navigation')),
]);
}

/**
* @param string $icon
*
* @return $this
* @param array $navigation
* @return array
*/
public function icon(string $icon): self
private function serializeGroups(array $navigation): array
{
$this->icon = $icon;

return $this;
}
foreach ($navigation as &$item) {

/**
* @param string $title
*
* @return $this
*/
public function title(string $title): self
{
$this->title = $title;
if (config('collapsible-resource-manager.translate_title', false)) {

$this->translateTitle($item);

}

if (isset($item[ 'groups' ])) {

$item[ 'groups' ] = $this->parseGroup($item[ 'groups' ]);

}

}

return $navigation;

return $this;
}

/**
* @param array $group
*
* @return $this
*/
public function addGroup(array $group): self

private function translateTitle(array &$data)
{
$this->groups->push($group);
if (isset($data[ 'title' ])) {

$data[ 'title' ] = trans($data[ 'title' ]);

return $this;
}
}

/**
* @param Collection $groups
*
* @return Collection
*/
private function serializeGroups(Collection $groups): Collection
private function parseGroup(array $groups): Collection
{
return $groups->map(function (array $group) {
return collect($groups)->map(function (array $group) {

if (config('collapsible-resource-manager.translate_title', false)) {

$this->translateTitle($group);

}

$group[ 'resources' ] = collect($group[ 'resources' ])->map(function (string $resource) {

if ($resource::authorizedToViewAny(request())) {

$group[ 'resources' ] = collect($group[ 'resources' ])->map(function ($resource) {
return [
'label' => $resource::label(),
'route' => $resource::uriKey()
];
});
return [
'label' => $resource::label(),
'route' => $resource::uriKey()
];

}

})->filter();

return $group;

});

}
}
Loading

0 comments on commit 1b41c3e

Please sign in to comment.