DropdownActions.php
3.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
<?php
namespace App\Admin\Rewrite\Grid\Displayers;
use App\Admin\Rewrite\Actions\RowAction;
use Encore\Admin\Admin;
use App\Admin\Rewrite\Grid\Actions\Delete;
use App\Admin\Rewrite\Grid\Actions\Edit;
use App\Admin\Rewrite\Grid\Actions\Show;
class DropdownActions extends Actions
{
protected $view = 'admin::grid.actions.dropdown';
/**
* @var array
*/
protected $custom = [];
/**
* @var array
*/
protected $default = [];
/**
* @var array
*/
protected $defaultClass = [Edit::class, Show::class, Delete::class];
/**
* @param \Encore\Admin\Actions\RowAction $action
*
* @return \Encore\Admin\Grid\Displayers\DropdownActions
*/
public function add(RowAction $action)
{
$this->prepareAction($action);
array_push($this->custom, $action);
return $this;
}
/**
* Prepend default `edit` `view` `delete` actions.
*/
protected function prependDefaultActions()
{
foreach ($this->defaultClass as $class) {
/** @var RowAction $action */
$action = new $class();
$this->prepareAction($action);
array_push($this->default, $action);
}
}
/**
* @param RowAction $action
*/
protected function prepareAction(RowAction $action)
{
$action->setGrid($this->grid)
->setColumn($this->column)
->setRow($this->row);
}
/**
* Disable view action.
*
* @param bool $disable
*
* @return $this
*/
public function disableView(bool $disable = true)
{
if ($disable) {
array_delete($this->defaultClass, Show::class);
} elseif (!in_array(Show::class, $this->defaultClass)) {
array_push($this->defaultClass, Show::class);
}
return $this;
}
/**
* Disable delete.
*
* @param bool $disable
*
* @return $this.
*/
public function disableDelete(bool $disable = true)
{
if ($disable) {
array_delete($this->defaultClass, Delete::class);
} elseif (!in_array(Delete::class, $this->defaultClass)) {
array_push($this->defaultClass, Delete::class);
}
return $this;
}
/**
* Disable edit.
*
* @param bool $disable
*
* @return $this
*/
public function disableEdit(bool $disable = true)
{
if ($disable) {
array_delete($this->defaultClass, Edit::class);
} elseif (!in_array(Edit::class, $this->defaultClass)) {
array_push($this->defaultClass, Edit::class);
}
return $this;
}
/**
* @param null|\Closure $callback
*
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View|string
*/
public function display($callback = null)
{
if ($callback instanceof \Closure) {
$callback->call($this, $this);
}
if ($this->disableAll) {
return '';
}
$this->prependDefaultActions();
$variables = [
'default' => $this->default,
'custom' => $this->custom,
];
if (empty($variables['default']) && empty($variables['custom'])) {
return;
}
return Admin::component($this->view, $variables);
}
}