first upload all files
This commit is contained in:
88
Modules/Admin/Ui/AdminTable.php
Normal file
88
Modules/Admin/Ui/AdminTable.php
Normal file
@@ -0,0 +1,88 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Admin\Ui;
|
||||
|
||||
use Illuminate\Contracts\Support\Responsable;
|
||||
|
||||
class AdminTable implements Responsable
|
||||
{
|
||||
/**
|
||||
* Raw columns that will not be escaped.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $rawColumns = [];
|
||||
|
||||
/**
|
||||
* Raw columns that will not be escaped.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $defaultRawColumns = [
|
||||
'checkbox', 'thumbnail', 'status', 'created',
|
||||
];
|
||||
|
||||
/**
|
||||
* Source of the table.
|
||||
*
|
||||
* @var \Illuminate\Database\Eloquent\Builder
|
||||
*/
|
||||
protected $source;
|
||||
|
||||
/**
|
||||
* Create a new table instance.
|
||||
*
|
||||
* @param \Illuminate\Database\Eloquent\Builder $source
|
||||
* @return void
|
||||
*/
|
||||
public function __construct($source = null)
|
||||
{
|
||||
$this->source = $source;
|
||||
}
|
||||
|
||||
/**
|
||||
* Make table response for the resource.
|
||||
*
|
||||
* @param mixed $source
|
||||
* @return \Illuminate\Http\JsonResponse
|
||||
*/
|
||||
public function make()
|
||||
{
|
||||
return $this->newTable();
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new datatable instance;
|
||||
*
|
||||
* @param mixed $source
|
||||
* @return \Yajra\DataTables\DataTables
|
||||
*/
|
||||
public function newTable()
|
||||
{
|
||||
return datatables($this->source)
|
||||
->addColumn('checkbox', function ($entity) {
|
||||
return view('admin::partials.table.checkbox', compact('entity'));
|
||||
})
|
||||
->editColumn('status', function ($entity) {
|
||||
return $entity->is_active
|
||||
? '<span class="dot green"></span>'
|
||||
: '<span class="dot red"></span>';
|
||||
})
|
||||
->editColumn('created', function ($entity) {
|
||||
return view('admin::partials.table.date')->with('date', $entity->created_at);
|
||||
})
|
||||
->rawColumns(array_merge($this->defaultRawColumns, $this->rawColumns))
|
||||
->removeColumn('translations');
|
||||
}
|
||||
|
||||
/**
|
||||
* Create an HTTP response that represents the object.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function toResponse($request)
|
||||
{
|
||||
return $this->make()->toJson();
|
||||
}
|
||||
}
|
||||
225
Modules/Admin/Ui/Concerns/InputFields.php
Normal file
225
Modules/Admin/Ui/Concerns/InputFields.php
Normal file
@@ -0,0 +1,225 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Admin\Ui\Concerns;
|
||||
|
||||
use LogicException;
|
||||
use Modules\Support\Money;
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Support\HtmlString;
|
||||
use Illuminate\Database\Eloquent\Relations\Relation;
|
||||
|
||||
trait InputFields
|
||||
{
|
||||
protected function inputField($name, $value, $class, $attributes, $options)
|
||||
{
|
||||
$readonly = array_pull($options, 'readonly', false);
|
||||
$disabled = array_get($options, 'disabled', false);
|
||||
|
||||
return "<input
|
||||
name='{$name}'
|
||||
class='form-control {$class}'
|
||||
id='{$name}'
|
||||
value='{$value}'
|
||||
{$attributes}"
|
||||
. ($disabled ? 'disabled' : '')
|
||||
. ($readonly ? 'readonly ' : '') .
|
||||
'>';
|
||||
}
|
||||
|
||||
protected function textareaField($name, $value, $class, $attributes, $options)
|
||||
{
|
||||
$readonly = array_pull($options, 'readonly', false);
|
||||
$disabled = array_get($options, 'disabled', false);
|
||||
|
||||
return "<textarea
|
||||
name='{$name}'
|
||||
class='form-control {$class}'
|
||||
id='{$name}'
|
||||
{$attributes}"
|
||||
. ($disabled ? 'disabled' : '')
|
||||
. ($readonly ? 'readonly ' : '') .
|
||||
">{$value}</textarea>";
|
||||
}
|
||||
|
||||
protected function checkboxField($name, $value, $class, $attributes, $options, $label)
|
||||
{
|
||||
$checked = array_pull($options, 'checked', false);
|
||||
$disabled = array_get($options, 'disabled', false);
|
||||
|
||||
if (! is_null($value)) {
|
||||
$checked = $value;
|
||||
}
|
||||
|
||||
$html = '<div class="checkbox">';
|
||||
|
||||
if (! $disabled) {
|
||||
$html .= "<input type='hidden' value='0' name='{$name}'>";
|
||||
}
|
||||
|
||||
$html .= "<input
|
||||
type='checkbox'
|
||||
name='{$name}'
|
||||
class='{$class}'
|
||||
id='{$name}'
|
||||
{$attributes}
|
||||
value='1'"
|
||||
. ($checked ? 'checked ' : '')
|
||||
. ($disabled ? 'disabled' : '') .
|
||||
'>';
|
||||
|
||||
$html .= "<label for='{$name}'>{$label}</label>";
|
||||
$html .= '</div>';
|
||||
|
||||
return $html;
|
||||
}
|
||||
|
||||
protected function selectField($name, $value, $class, $attributes, $options, $list)
|
||||
{
|
||||
$multiple = array_get($options, 'multiple', false);
|
||||
$disabled = array_get($options, 'disabled', false);
|
||||
$readonly = array_pull($options, 'readonly', false);
|
||||
|
||||
$html = "<select
|
||||
name='{$name}'
|
||||
class='form-control custom-select-black {$class}'
|
||||
id='{$name}'
|
||||
{$attributes}"
|
||||
. ($disabled ? 'disabled' : '')
|
||||
. ($readonly ? 'readonly ' : '') .
|
||||
'>';
|
||||
|
||||
foreach ($list as $listValue => $listName) {
|
||||
$listValue = e($listValue);
|
||||
$listName = e($listName);
|
||||
|
||||
if ($multiple && $value instanceof Collection) {
|
||||
$selected = $value->where('id', $listValue)->isNotEmpty() ? 'selected' : '';
|
||||
} elseif ($multiple && is_array($value)) {
|
||||
$selected = in_array($listValue, $value) ? 'selected' : '';
|
||||
} else {
|
||||
$selected = (! is_null($value) && $value == $listValue) ? 'selected' : '';
|
||||
}
|
||||
|
||||
$html .= "<option value='{$listValue}' {$selected}>{$listName}</option>";
|
||||
}
|
||||
|
||||
$html .= '</select>';
|
||||
|
||||
return $html;
|
||||
}
|
||||
|
||||
protected function field($name, $title, $errors, $entity, $options, callable $fieldCallback, ...$args)
|
||||
{
|
||||
$value = $this->getValue($entity, $name);
|
||||
|
||||
if (is_string($value)) {
|
||||
$value = e($value);
|
||||
}
|
||||
|
||||
$normalizedName = $this->normalizeTranslatableFieldName($name);
|
||||
$name = array_get($options, 'multiple', false) ? "{$name}[]" : $name;
|
||||
$required = array_pull($options, 'required', false);
|
||||
$help = array_pull($options, 'help', false);
|
||||
|
||||
$params = array_merge([
|
||||
$name,
|
||||
$value,
|
||||
array_pull($options, 'class'),
|
||||
$this->generateHtmlAttributes($options),
|
||||
$options,
|
||||
], $args);
|
||||
|
||||
$labelCol = array_pull($options, 'labelCol', 3);
|
||||
$fieldCol = 12 - $labelCol;
|
||||
|
||||
$html = '<div class="form-group">';
|
||||
|
||||
$html .= $this->label($name, $title, $labelCol, $required);
|
||||
|
||||
$html .= "<div class='col-md-{$fieldCol}'>";
|
||||
$html .= call_user_func_array($fieldCallback, $params);
|
||||
|
||||
if ($help && ! $errors->has($normalizedName)) {
|
||||
$html .= "<span class='help-block'>{$help}</span>";
|
||||
}
|
||||
|
||||
$html .= $errors->first($normalizedName, '<span class="help-block text-red">:message</span>');
|
||||
|
||||
$html .= '</div>';
|
||||
$html .= '</div>';
|
||||
|
||||
return new HtmlString($html);
|
||||
}
|
||||
|
||||
private function normalizeTranslatableFieldName($name)
|
||||
{
|
||||
if (starts_with($name, 'translatable[')) {
|
||||
return 'translatable.' . str_between($name, 'translatable[', ']');
|
||||
}
|
||||
|
||||
return $name;
|
||||
}
|
||||
|
||||
protected function label($name, $title, $labelCol = 3, $required = false)
|
||||
{
|
||||
$html = "<label for='{$name}' class='col-md-{$labelCol} control-label text-left'>{$title}";
|
||||
|
||||
if ($required) {
|
||||
$html .= '<span class="m-l-5 text-red">*</span>';
|
||||
}
|
||||
|
||||
return $html .= '</label>';
|
||||
}
|
||||
|
||||
private function getValue($entity, $name)
|
||||
{
|
||||
if (is_object($entity) && method_exists($entity, 'translate') && $entity->isTranslationAttribute($name)) {
|
||||
$translatedValue = optional($entity->translate(locale(), false))->$name;
|
||||
|
||||
return old($name, $translatedValue);
|
||||
}
|
||||
|
||||
$camelCaseName = camel_case($name);
|
||||
|
||||
if (is_object($entity) && method_exists($entity, $camelCaseName) && $entity->{$camelCaseName}() instanceof Relation) {
|
||||
$name = $camelCaseName;
|
||||
}
|
||||
|
||||
$normalizedName = $this->normalizeTranslatableFieldName($name);
|
||||
$name = str_between($name, 'translatable[', ']');
|
||||
|
||||
try {
|
||||
$value = data_get($entity, $name);
|
||||
} catch (LogicException $e) {
|
||||
$value = $entity->getOriginal('url');
|
||||
}
|
||||
|
||||
if ($value instanceof Money) {
|
||||
$value = $value->amount();
|
||||
}
|
||||
|
||||
return old($normalizedName, $value);
|
||||
}
|
||||
|
||||
protected function generateHtmlAttributes($options = [])
|
||||
{
|
||||
$this->unsetUnnecessaryAttributes($options);
|
||||
|
||||
$attributes = '';
|
||||
|
||||
foreach ($options as $attr => $value) {
|
||||
$attributes .= "{$attr}='{$value}' ";
|
||||
}
|
||||
|
||||
return $attributes;
|
||||
}
|
||||
|
||||
protected function unsetUnnecessaryAttributes(&$options = [])
|
||||
{
|
||||
foreach ($this->unnecessaryAttributes as $attribute) {
|
||||
if (array_key_exists($attribute, $options)) {
|
||||
unset($options[$attribute]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
19
Modules/Admin/Ui/Facades/Form.php
Normal file
19
Modules/Admin/Ui/Facades/Form.php
Normal file
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Admin\Ui\Facades;
|
||||
|
||||
use Illuminate\Support\Facades\Facade;
|
||||
use Modules\Admin\Ui\Form as FormBuilder;
|
||||
|
||||
class Form extends Facade
|
||||
{
|
||||
/**
|
||||
* Get the registered name of the component.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected static function getFacadeAccessor()
|
||||
{
|
||||
return FormBuilder::class;
|
||||
}
|
||||
}
|
||||
24
Modules/Admin/Ui/Facades/TabManager.php
Normal file
24
Modules/Admin/Ui/Facades/TabManager.php
Normal file
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Admin\Ui\Facades;
|
||||
|
||||
use Illuminate\Support\Facades\Facade;
|
||||
|
||||
/**
|
||||
* @method static void register(string $name, string $tabsClass)
|
||||
* @method static void extend(string $name, string $extenderClass)
|
||||
*
|
||||
* @see \Modules\Admin\Ui\TabManager
|
||||
*/
|
||||
class TabManager extends Facade
|
||||
{
|
||||
/**
|
||||
* Get the registered name of the component.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected static function getFacadeAccessor()
|
||||
{
|
||||
return \Modules\Admin\Ui\TabManager::class;
|
||||
}
|
||||
}
|
||||
71
Modules/Admin/Ui/Form.php
Normal file
71
Modules/Admin/Ui/Form.php
Normal file
@@ -0,0 +1,71 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Admin\Ui;
|
||||
|
||||
use Modules\Admin\Ui\Concerns\InputFields;
|
||||
|
||||
class Form
|
||||
{
|
||||
use InputFields;
|
||||
|
||||
protected $unnecessaryAttributes = ['disabled', 'readonly', 'checked'];
|
||||
|
||||
public function text($name, $title, $errors, $entity = null, $options = [])
|
||||
{
|
||||
return $this->input($name, $title, $errors, $entity, array_merge($options, ['type' => 'text']));
|
||||
}
|
||||
|
||||
public function password($name, $title, $errors, $entity = null, $options = [])
|
||||
{
|
||||
return $this->input($name, $title, $errors, $entity, array_merge($options, ['type' => 'password']));
|
||||
}
|
||||
|
||||
public function number($name, $title, $errors, $entity = null, $options = [])
|
||||
{
|
||||
return $this->input($name, $title, $errors, $entity, array_merge($options, ['type' => 'number']));
|
||||
}
|
||||
|
||||
public function email($name, $title, $errors, $entity = null, $options = [])
|
||||
{
|
||||
return $this->input($name, $title, $errors, $entity, array_merge($options, ['type' => 'email']));
|
||||
}
|
||||
|
||||
public function file($name, $title, $errors, $entity = null, $options = [])
|
||||
{
|
||||
return $this->input($name, $title, $errors, $entity, array_merge($options, ['type' => 'file']));
|
||||
}
|
||||
|
||||
public function color($name, $title, $errors, $entity = null, $options = [])
|
||||
{
|
||||
return $this->input($name, $title, $errors, $entity, array_merge($options, ['type' => 'color']));
|
||||
}
|
||||
|
||||
public function input($name, $title, $errors, $entity = null, $options = [])
|
||||
{
|
||||
return $this->field($name, $title, $errors, $entity, $options, [$this, 'inputField']);
|
||||
}
|
||||
|
||||
public function textarea($name, $title, $errors, $entity = null, $options = [])
|
||||
{
|
||||
$options = array_merge(['rows' => 10, 'cols' => 10], $options);
|
||||
|
||||
return $this->field($name, $title, $errors, $entity, $options, [$this, 'textareaField']);
|
||||
}
|
||||
|
||||
public function wysiwyg($name, $title, $errors, $entity = null, $options = [])
|
||||
{
|
||||
$options['class'] = array_get($options, 'class', '') . ' wysiwyg';
|
||||
|
||||
return $this->textarea($name, $title, $errors, $entity, $options);
|
||||
}
|
||||
|
||||
public function checkbox($name, $title, $label, $errors, $entity = null, $options = [])
|
||||
{
|
||||
return $this->field($name, $title, $errors, $entity, $options, [$this, 'checkboxField'], $label);
|
||||
}
|
||||
|
||||
public function select($name, $title, $errors, $list = [], $entity = null, $options = [])
|
||||
{
|
||||
return $this->field($name, $title, $errors, $entity, $options, [$this, 'selectField'], $list);
|
||||
}
|
||||
}
|
||||
204
Modules/Admin/Ui/Tab.php
Normal file
204
Modules/Admin/Ui/Tab.php
Normal file
@@ -0,0 +1,204 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Admin\Ui;
|
||||
|
||||
use Illuminate\Support\ViewErrorBag;
|
||||
|
||||
class Tab
|
||||
{
|
||||
/**
|
||||
* Active state of the tab.
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
private $active = false;
|
||||
|
||||
/**
|
||||
* Name of the tab.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $name;
|
||||
|
||||
/**
|
||||
* Label of the tab.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $label;
|
||||
|
||||
/**
|
||||
* Weight of the tab.
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
private $weight = 0;
|
||||
|
||||
/**
|
||||
* Available fields on the tab.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
private $fields = [];
|
||||
|
||||
/**
|
||||
* View of the tab.
|
||||
*
|
||||
* @var string|\Closure
|
||||
*/
|
||||
private $view;
|
||||
|
||||
/**
|
||||
* Data for the tab view.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
private $data;
|
||||
|
||||
/**
|
||||
* Error message bag.
|
||||
*
|
||||
* @var \Illuminate\Support\ViewErrorBag
|
||||
*/
|
||||
private $errors;
|
||||
|
||||
/**
|
||||
* Create a new Tab instance.
|
||||
*
|
||||
* @param string $name
|
||||
* @param string $label
|
||||
* @return void
|
||||
*/
|
||||
public function __construct($name, $label)
|
||||
{
|
||||
$this->name = $name;
|
||||
$this->label = $label;
|
||||
$this->errors = request()->session()->get('errors') ?: new ViewErrorBag;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set tab as active.
|
||||
*
|
||||
* @return self
|
||||
*/
|
||||
public function active()
|
||||
{
|
||||
$this->active = true;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set weight of tab.
|
||||
*
|
||||
* @param int $weight
|
||||
* @return self
|
||||
*/
|
||||
public function weight($weight)
|
||||
{
|
||||
$this->weight = $weight;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get weight of the tab.
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function getWeight()
|
||||
{
|
||||
return $this->weight;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get nav of the tab.
|
||||
*
|
||||
* @param array $data
|
||||
* @return string
|
||||
*/
|
||||
public function getNav()
|
||||
{
|
||||
return "<li class='{$this->activeClass()} {$this->errorClass()}'>
|
||||
<a href='#{$this->name}' data-toggle='tab'>{$this->label}</a>
|
||||
</li>";
|
||||
}
|
||||
|
||||
/**
|
||||
* Return active class if tab is active.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
private function activeClass()
|
||||
{
|
||||
return $this->active ? 'active' : '';
|
||||
}
|
||||
|
||||
/**
|
||||
* Return error class if tab fields has any error.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
private function errorClass()
|
||||
{
|
||||
return $this->errors->hasAny($this->fields) ? 'has-error' : '';
|
||||
}
|
||||
|
||||
/**
|
||||
* Set fields of the tab.
|
||||
*
|
||||
* @param array|string $fields
|
||||
* @return self
|
||||
*/
|
||||
public function fields($fields)
|
||||
{
|
||||
$this->fields = is_array($fields) ? $fields : func_get_args();
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get fields of the tab.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getFields()
|
||||
{
|
||||
return $this->fields;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set view of the tab.
|
||||
*
|
||||
* @param \Closure|string $view
|
||||
* @param array $data
|
||||
* @return self
|
||||
*/
|
||||
public function view($view, $data = [])
|
||||
{
|
||||
$this->view = $view;
|
||||
$this->data = $data;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get view of the tab.
|
||||
*
|
||||
* @param array $data
|
||||
* @return string
|
||||
*/
|
||||
public function getView($data = [])
|
||||
{
|
||||
$html = "<div class='tab-pane fade in {$this->activeClass()}' id='{$this->name}'>";
|
||||
$html .= "<h3 class='tab-content-title'>{$this->label}</h3>";
|
||||
|
||||
if (is_callable($this->view)) {
|
||||
$html .= call_user_func($this->view, array_merge($this->data, $data));
|
||||
} else {
|
||||
$html .= view($this->view)->with(array_merge($this->data, $data))->render();
|
||||
}
|
||||
|
||||
return $html .= '</div>';
|
||||
}
|
||||
}
|
||||
77
Modules/Admin/Ui/TabManager.php
Normal file
77
Modules/Admin/Ui/TabManager.php
Normal file
@@ -0,0 +1,77 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Admin\Ui;
|
||||
|
||||
class TabManager
|
||||
{
|
||||
/**
|
||||
* The array of all Tabs.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
private $tabs = [];
|
||||
|
||||
/**
|
||||
* The array of all tabs extenders.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
private $extends = [];
|
||||
|
||||
/**
|
||||
* Register a new Tabs.
|
||||
*
|
||||
* @param string $name
|
||||
* @param string $tabs
|
||||
* @return void
|
||||
*/
|
||||
public function register($name, $tabs)
|
||||
{
|
||||
$this->tabs[$name] = $tabs;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a new Tabs extender.
|
||||
*
|
||||
* @param string $name
|
||||
* @param string $extender
|
||||
* @return void
|
||||
*/
|
||||
public function extend($name, $extender)
|
||||
{
|
||||
$this->extends[$name][] = $extender;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get tabs for the given name.
|
||||
*
|
||||
* @param string $name
|
||||
* @return \Modules\Admin\Ui\Tabs
|
||||
*/
|
||||
public function get($name)
|
||||
{
|
||||
if (! array_key_exists($name, $this->tabs)) {
|
||||
return;
|
||||
}
|
||||
|
||||
return tap(resolve($this->tabs[$name]), function (Tabs $tabs) use ($name) {
|
||||
$tabs->make();
|
||||
|
||||
$this->extendTabs($tabs, array_get($this->extends, $name, []));
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Extend the given tabs using the given extenders.
|
||||
*
|
||||
* @param \Modules\Admin\Ui\Tabs $tabs
|
||||
* @param array $extenders
|
||||
* @return void
|
||||
*/
|
||||
private function extendTabs(Tabs $tabs, array $extenders)
|
||||
{
|
||||
foreach ($extenders as $extender) {
|
||||
resolve($extender)->extend($tabs);
|
||||
}
|
||||
}
|
||||
}
|
||||
223
Modules/Admin/Ui/Tabs.php
Normal file
223
Modules/Admin/Ui/Tabs.php
Normal file
@@ -0,0 +1,223 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Admin\Ui;
|
||||
|
||||
use InvalidArgumentException;
|
||||
use Illuminate\Support\HtmlString;
|
||||
use Illuminate\Support\ViewErrorBag;
|
||||
|
||||
abstract class Tabs
|
||||
{
|
||||
/**
|
||||
* Array of all groups.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $groups = [];
|
||||
|
||||
/**
|
||||
* Current group name of the tabs.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $group;
|
||||
|
||||
/**
|
||||
* Array of all tabs.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $tabs = [];
|
||||
|
||||
/**
|
||||
* Indicate that submit button should add offset class.
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
protected $buttonOffset = true;
|
||||
|
||||
/**
|
||||
* Make new tabs with groups.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
abstract public function make();
|
||||
|
||||
/**
|
||||
* Set group name.
|
||||
*
|
||||
* @param string $name
|
||||
* @param string $title
|
||||
* @return self
|
||||
*/
|
||||
public function group($name, $title = null)
|
||||
{
|
||||
$this->group = $name;
|
||||
|
||||
if (! is_null($title)) {
|
||||
$this->groups[$name]['title'] = $title;
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set current group as active group.
|
||||
*
|
||||
* @return self
|
||||
*/
|
||||
public function active()
|
||||
{
|
||||
$this->groups[$this->group]['active'] = true;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add new tab.
|
||||
*
|
||||
* @param \Modules\Admin\Ui\Tab|null $tab
|
||||
* @return void
|
||||
*/
|
||||
public function add($tab)
|
||||
{
|
||||
if (! is_null($tab)) {
|
||||
$this->tabs[$this->group][] = $tab;
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if tabs fields has any error.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function hasError()
|
||||
{
|
||||
return $this->getErrors()->hasAny($this->getTabFields());
|
||||
}
|
||||
|
||||
/**
|
||||
* Get error message bag.
|
||||
*
|
||||
* @return \Illuminate\Support\ViewErrorBag
|
||||
*/
|
||||
protected function getErrors()
|
||||
{
|
||||
return request()->session()->get('errors') ?: new ViewErrorBag;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all tabs fields.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function getTabFields()
|
||||
{
|
||||
return array_reduce($this->getSortedTabs(), function ($fields, Tab $tab) {
|
||||
return array_merge($fields, $tab->getFields());
|
||||
}, []);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate navs for the tabs.
|
||||
*
|
||||
* @param array $data
|
||||
* @return \Illuminate\Support\HtmlString
|
||||
*/
|
||||
public function navs($data = [])
|
||||
{
|
||||
return new HtmlString($this->getTabsData('nav', $data));
|
||||
}
|
||||
|
||||
/**
|
||||
* Render the tabs,
|
||||
*
|
||||
* @param array $data
|
||||
* @return void
|
||||
*/
|
||||
public function render($data = [])
|
||||
{
|
||||
return view('admin::components.accordion', [
|
||||
'tabs' => $this,
|
||||
'name' => class_basename($this),
|
||||
'groups' => $this->groups(),
|
||||
'contents' => $this->contents($data),
|
||||
'buttonOffset' => $this->buttonOffset,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all groups with it's options.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function groups()
|
||||
{
|
||||
$groups = [];
|
||||
|
||||
foreach ($this->groups as $group => $options) {
|
||||
$groups[$group] = $options;
|
||||
}
|
||||
|
||||
return $groups;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate contents for the tabs.
|
||||
*
|
||||
* @param array $data
|
||||
* @return \Illuminate\Support\HtmlString
|
||||
*/
|
||||
protected function contents($data = [])
|
||||
{
|
||||
$contents = '';
|
||||
|
||||
foreach ($this->groups as $group => $options) {
|
||||
$contents .= $this->group($group)->getTabsData('view', $data);
|
||||
}
|
||||
|
||||
return new HtmlString($contents);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get tabs data for the given type.
|
||||
*
|
||||
* @param string $type
|
||||
* @param array $data
|
||||
* @return string
|
||||
*
|
||||
* @throws \InvalidArgumentException
|
||||
*/
|
||||
protected function getTabsData($type, $data = [])
|
||||
{
|
||||
if (! array_key_exists($this->group, $this->tabs)) {
|
||||
throw new InvalidArgumentException("Group [$this->group] is not registered.");
|
||||
}
|
||||
|
||||
$html = '';
|
||||
|
||||
foreach ($this->getSortedTabs() as $tab) {
|
||||
$method = 'get' . ucfirst($type);
|
||||
|
||||
if (method_exists($tab, $method)) {
|
||||
$html .= call_user_func_array([$tab, $method], [$data]);
|
||||
}
|
||||
}
|
||||
|
||||
return $html;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get sorted tabs.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function getSortedTabs()
|
||||
{
|
||||
return collect($this->tabs[$this->group])->sortBy(function (Tab $tab) {
|
||||
return $tab->getWeight();
|
||||
})->all();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user