¨4.0.1¨
This commit is contained in:
@@ -36,6 +36,7 @@ abstract class Tabs
|
||||
*/
|
||||
protected $buttonOffset = true;
|
||||
|
||||
|
||||
/**
|
||||
* Make new tabs with groups.
|
||||
*
|
||||
@@ -43,23 +44,6 @@ abstract class Tabs
|
||||
*/
|
||||
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.
|
||||
@@ -73,21 +57,24 @@ abstract class Tabs
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Add new tab.
|
||||
*
|
||||
* @param \Modules\Admin\Ui\Tab|null $tab
|
||||
* @param Tab|null $tab
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function add($tab)
|
||||
{
|
||||
if (! is_null($tab)) {
|
||||
if (!is_null($tab)) {
|
||||
$this->tabs[$this->group][] = $tab;
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Determine if tabs fields has any error.
|
||||
*
|
||||
@@ -98,16 +85,70 @@ abstract class Tabs
|
||||
return $this->getErrors()->hasAny($this->getTabFields());
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Generate navs for the tabs.
|
||||
*
|
||||
* @param array $data
|
||||
*
|
||||
* @return 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,
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get error message bag.
|
||||
*
|
||||
* @return \Illuminate\Support\ViewErrorBag
|
||||
* @return ViewErrorBag
|
||||
*/
|
||||
protected function getErrors()
|
||||
{
|
||||
return request()->session()->get('errors') ?: new ViewErrorBag;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get all tabs fields.
|
||||
*
|
||||
@@ -120,34 +161,50 @@ abstract class Tabs
|
||||
}, []);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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,
|
||||
* Get sorted tabs.
|
||||
*
|
||||
* @param array $data
|
||||
* @return void
|
||||
* @return array
|
||||
*/
|
||||
public function render($data = [])
|
||||
protected function getSortedTabs()
|
||||
{
|
||||
return view('admin::components.accordion', [
|
||||
'tabs' => $this,
|
||||
'name' => class_basename($this),
|
||||
'groups' => $this->groups(),
|
||||
'contents' => $this->contents($data),
|
||||
'buttonOffset' => $this->buttonOffset,
|
||||
]);
|
||||
return collect($this->tabs[$this->group])->sortBy(function (Tab $tab) {
|
||||
return $tab->getWeight();
|
||||
})->all();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 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 all groups with it's options.
|
||||
*
|
||||
@@ -164,11 +221,13 @@ abstract class Tabs
|
||||
return $groups;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Generate contents for the tabs.
|
||||
*
|
||||
* @param array $data
|
||||
* @return \Illuminate\Support\HtmlString
|
||||
*
|
||||
* @return HtmlString
|
||||
*/
|
||||
protected function contents($data = [])
|
||||
{
|
||||
@@ -180,44 +239,4 @@ abstract class Tabs
|
||||
|
||||
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