-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 0277948
Showing
8 changed files
with
373 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
{ | ||
"name": "stepapo/menu", | ||
"type": "library", | ||
"description": "Component for Nette Framework, that helps generate menus with links and action buttons.", | ||
"keywords": ["nette", "menu", "component"], | ||
"homepage": "https://github.com/stepapo/menu", | ||
"license": "MIT", | ||
"authors": [ | ||
{ | ||
"name": "Štěpán Postler" | ||
} | ||
], | ||
"support": { | ||
"issues": "https://github.com/stepapo/menu/issues" | ||
}, | ||
"require": { | ||
"php": ">=8.0", | ||
"nette/application": "^3.1", | ||
"nette/neon": "^3.0", | ||
"nette/utils": "^3.1" | ||
}, | ||
"autoload": { | ||
"psr-4": { | ||
"Stepapo\\Menu\\": "src/" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2020 espicko | ||
|
||
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Menu | ||
==== |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Stepapo\Menu; | ||
|
||
|
||
class Button | ||
{ | ||
/** @param $buttons Button[] */ | ||
public function __construct( | ||
public ?string $label = null, | ||
public ?string $destination = null, | ||
public array $parameters = [], | ||
public ?string $selector = null, | ||
public ?string $icon = null, | ||
public ?string $type = null, | ||
public bool $ajax = false, | ||
public bool $history = false, | ||
public bool $hide = false, | ||
public array $buttons = [], | ||
) {} | ||
|
||
|
||
public static function createFromArray(array $config): Button | ||
{ | ||
$button = new self(); | ||
if (array_key_exists('label', $config)) { | ||
$button->setLabel($config['label']); | ||
} | ||
if (array_key_exists('destination', $config)) { | ||
$button->setDestination($config['destination']); | ||
} | ||
if (array_key_exists('parameters', $config)) { | ||
$button->setParameters($config['parameters']); | ||
} | ||
if (array_key_exists('selector', $config)) { | ||
$button->setSelector($config['selector']); | ||
} | ||
if (array_key_exists('icon', $config)) { | ||
$button->setIcon($config['icon']); | ||
} | ||
if (array_key_exists('type', $config)) { | ||
$button->setType($config['type']); | ||
} | ||
if (array_key_exists('ajax', $config)) { | ||
$button->setAjax($config['ajax']); | ||
} | ||
if (array_key_exists('history', $config)) { | ||
$button->setHistory($config['history']); | ||
} | ||
if (array_key_exists('hide', $config)) { | ||
$button->setHide($config['hide']); | ||
} | ||
if (array_key_exists('buttons', $config)) { | ||
foreach ($config['buttons'] as $buttonConfig) { | ||
$button->addButton(Button::createFromArray($buttonConfig)); | ||
} | ||
} | ||
return $button; | ||
} | ||
|
||
|
||
public function setLabel(string $label): Button | ||
{ | ||
$this->label = $label; | ||
return $this; | ||
} | ||
|
||
|
||
public function setDestination(?string $destination): Button | ||
{ | ||
$this->destination = $destination; | ||
return $this; | ||
} | ||
|
||
|
||
public function setParameters(array $parameters): Button | ||
{ | ||
$this->parameters = $parameters; | ||
return $this; | ||
} | ||
|
||
|
||
public function setSelector(?string $selector): Button | ||
{ | ||
$this->selector = $selector; | ||
return $this; | ||
} | ||
|
||
|
||
public function setIcon(?string $icon): Button | ||
{ | ||
$this->icon = $icon; | ||
return $this; | ||
} | ||
|
||
|
||
public function setType(?string $type): Button | ||
{ | ||
$this->type = $type; | ||
return $this; | ||
} | ||
|
||
|
||
public function setAjax(bool $ajax): Button | ||
{ | ||
$this->ajax = $ajax; | ||
return $this; | ||
} | ||
|
||
|
||
public function setHistory(bool $history): Button | ||
{ | ||
$this->history = $history; | ||
return $this; | ||
} | ||
|
||
|
||
public function setHide(bool $hide): Button | ||
{ | ||
$this->hide = $hide; | ||
return $this; | ||
} | ||
|
||
|
||
public function addButton(Button $button): Button | ||
{ | ||
$this->buttons[] = $button; | ||
return $this; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Stepapo\Menu\UI; | ||
|
||
use Nette\Application\UI\Control; | ||
use Nette\Neon\Neon; | ||
use Nette\Utils\FileSystem; | ||
use Stepapo\Menu\Button; | ||
use Stepapo\Menu\Utils; | ||
|
||
|
||
class Menu extends Control | ||
{ | ||
/** | ||
* @param Button[] $buttons | ||
* @param Button[] $actions | ||
*/ | ||
public function __construct( | ||
private array $buttons = [], | ||
private array $actions = [], | ||
private ?string $templateFile = null, | ||
) {} | ||
|
||
|
||
public static function createFromNeon(string $file, array $params = []): Menu | ||
{ | ||
$config = (array) Neon::decode(FileSystem::read($file)); | ||
$parsedConfig = Utils::replaceParams($config, $params); | ||
return self::createFromArray($parsedConfig); | ||
} | ||
|
||
|
||
public static function createFromArray(array $config): Menu | ||
{ | ||
$menu = new self(); | ||
if (array_key_exists('buttons', $config)) { | ||
foreach ($config['buttons'] as $buttonConfig) { | ||
$menu->addButton(Button::createFromArray($buttonConfig)); | ||
} | ||
} | ||
if (array_key_exists('actions', $config)) { | ||
foreach ($config['actions'] as $buttonConfig) { | ||
$menu->addAction(Button::createFromArray($buttonConfig)); | ||
} | ||
} | ||
if (array_key_exists('templateFile', $config)) { | ||
$menu->setTemplateFile($config['templateFile']); | ||
} | ||
return $menu; | ||
} | ||
|
||
|
||
public function render() | ||
{ | ||
$this->template->buttons = $this->buttons; | ||
$this->template->actions = $this->actions; | ||
$this->template->render($this->templateFile ?: __DIR__ . '/menu.latte'); | ||
} | ||
|
||
|
||
public function addButton(Button $button): Menu | ||
{ | ||
$this->buttons[] = $button; | ||
return $this; | ||
} | ||
|
||
|
||
public function addAction(Button $button): Menu | ||
{ | ||
$this->actions[] = $button; | ||
return $this; | ||
} | ||
|
||
|
||
public function setTemplateFile(?string $templateFile): self | ||
{ | ||
$this->templateFile = $templateFile; | ||
return $this; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Stepapo\Menu\UI; | ||
|
||
use Nette\Application\UI\Presenter; | ||
use Nette\Security\User; | ||
use Nextras\Orm\Entity\IEntity; | ||
use Nette\Bridges\ApplicationLatte\Template; | ||
use Stepapo\Menu\Button; | ||
|
||
|
||
class MenuTemplate extends Template | ||
{ | ||
public Presenter $presenter; | ||
|
||
public User $user; | ||
|
||
public string $basePath; | ||
|
||
public ?IEntity $entity; | ||
|
||
/** @var Button[] */ | ||
public array $buttons; | ||
|
||
/** @var Button[] */ | ||
public array $actions; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
{templateType Stepapo\Menu\UI\MenuTemplate} | ||
|
||
<ul n:if="$buttons || $actions"> | ||
<li n:foreach="$buttons as $button" n:if="!$button->hide"> | ||
{if $button->destination} | ||
<a href="{plink $button->destination ...$button->parameters}">{$button->label}</a> | ||
{elseif $button->selector} | ||
<a href="#" data-toggle="modal" data-target="{$button->selector}"></a> | ||
{elseif $button->buttons} | ||
<button>{$button->label}</button> | ||
<ul> | ||
<li n:foreach="$button->buttons as $subButton" n:if="!$button->hide"> | ||
{dump $subButton} | ||
{if $subButton->destination} | ||
<a href="{plink $subButton->destination ...$subButton->parameters}">{$subButton->label}</a> | ||
{elseif $subButton->selector} | ||
<a href="#" data-toggle="modal" data-target="{$subButton->selector}">{$subButton->label}</a> | ||
{/} | ||
</li> | ||
</ul> | ||
{/} | ||
</li> | ||
<li n:foreach="$actions as $button" n:if="!$button->hide"> | ||
{if $button->destination} | ||
<a href="{plink $button->destination ...$button->parameters}">{$button->label}</a> | ||
{elseif $button->selector} | ||
<a href="#" data-toggle="modal" data-target="{$button->selector}"></a> | ||
{elseif $button->buttons} | ||
<button>{$button->label}</button> | ||
<ul> | ||
<li n:foreach="$button->buttons as $subButton"> | ||
{dump $subButton} | ||
{if $subButton->destination} | ||
<a href="{plink $subButton->destination ...$subButton->parameters}">{$subButton->label}</a> | ||
{elseif $subButton->selector} | ||
<a href="#" data-toggle="modal" data-target="{$subButton->selector}">{$subButton->label}</a> | ||
{/} | ||
</li> | ||
</ul> | ||
{/} | ||
</li> | ||
</ul> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Stepapo\Menu; | ||
|
||
|
||
class Utils | ||
{ | ||
public static function replaceParams(array $array, mixed $params) | ||
{ | ||
$parsedArray = []; | ||
foreach ($array as $key => $value) { | ||
$parsedArray[self::replace($key, $params)] = is_array($value) | ||
? self::replaceParams($value, $params) | ||
: self::replace($value, $params); | ||
} | ||
return $parsedArray; | ||
} | ||
|
||
|
||
public static function replace(mixed $value, array $params) | ||
{ | ||
if (is_array($value)) { | ||
array_walk($value, function(&$v) use ($params) { | ||
$v = self::replace($v, $params); | ||
}); | ||
return $value; | ||
} else if (is_string($value)) { | ||
preg_match('/^%(.*)%$/', $value, $m); | ||
if (isset($m[1])) { | ||
return array_key_exists($m[1], $params) ? $params[$m[1]] : $value; | ||
} | ||
return $value; | ||
} | ||
return $value; | ||
} | ||
} |