From 0277948f3b2af48337d644f2c965ad759ae3d3d1 Mon Sep 17 00:00:00 2001 From: Stepan Postler Date: Thu, 20 May 2021 16:45:53 +0200 Subject: [PATCH] initial commit --- composer.json | 27 ++++++++ license.md | 21 +++++++ readme.md | 2 + src/Button.php | 132 ++++++++++++++++++++++++++++++++++++++++ src/UI/Menu.php | 82 +++++++++++++++++++++++++ src/UI/MenuTemplate.php | 29 +++++++++ src/UI/menu.latte | 42 +++++++++++++ src/Utils.php | 38 ++++++++++++ 8 files changed, 373 insertions(+) create mode 100644 composer.json create mode 100644 license.md create mode 100644 readme.md create mode 100644 src/Button.php create mode 100644 src/UI/Menu.php create mode 100644 src/UI/MenuTemplate.php create mode 100644 src/UI/menu.latte create mode 100644 src/Utils.php diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..99e23db --- /dev/null +++ b/composer.json @@ -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/" + } + } +} diff --git a/license.md b/license.md new file mode 100644 index 0000000..cb009ed --- /dev/null +++ b/license.md @@ -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. diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..1c872d2 --- /dev/null +++ b/readme.md @@ -0,0 +1,2 @@ +Menu +==== diff --git a/src/Button.php b/src/Button.php new file mode 100644 index 0000000..fd87661 --- /dev/null +++ b/src/Button.php @@ -0,0 +1,132 @@ +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; + } +} diff --git a/src/UI/Menu.php b/src/UI/Menu.php new file mode 100644 index 0000000..7e9b3fe --- /dev/null +++ b/src/UI/Menu.php @@ -0,0 +1,82 @@ +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; + } +} diff --git a/src/UI/MenuTemplate.php b/src/UI/MenuTemplate.php new file mode 100644 index 0000000..e10af22 --- /dev/null +++ b/src/UI/MenuTemplate.php @@ -0,0 +1,29 @@ + +
  • + {if $button->destination} + {$button->label} + {elseif $button->selector} + + {elseif $button->buttons} + + + {/} +
  • +
  • + {if $button->destination} + {$button->label} + {elseif $button->selector} + + {elseif $button->buttons} + + + {/} +
  • + diff --git a/src/Utils.php b/src/Utils.php new file mode 100644 index 0000000..04203fd --- /dev/null +++ b/src/Utils.php @@ -0,0 +1,38 @@ + $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; + } +}