HasActions.php
2.5 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
<?php
namespace App\Admin\Rewrite\Grid\Concerns;
use Closure;
use Encore\Admin\Grid;
use App\Admin\Rewrite\Grid as RewriteGrid;
trait HasActions
{
/**
* Callback for grid actions.
*
* @var Closure
*/
protected $actionsCallback;
/**
* Actions column display class.
*
* @var string
*/
protected $actionsClass;
/**
* Set grid action callback.
*
* @param Closure|string $actions
*
* @return $this
*/
public function actions($actions)
{
if ($actions instanceof Closure) {
$this->actionsCallback = $actions;
}
return $this;
}
/**
* Get action display class.
*
* @return \Illuminate\Config\Repository|mixed|string
*/
public function getActionClass()
{
if ($this->actionsClass) {
return $this->actionsClass;
}
if ($class = config('admin.grid_action_class')) {
return $class;
}
return RewriteGrid\Displayers\Actions::class;
}
/**
* @param string $actionClass
*
* @return $this
*/
public function setActionClass(string $actionClass)
{
if (is_subclass_of($actionClass, RewriteGrid\Displayers\Actions::class)) {
$this->actionsClass = $actionClass;
}
return $this;
}
/**
* Disable all actions.
*
* @return $this
*/
public function disableActions(bool $disable = true)
{
return $this->option('show_actions', !$disable);
}
/**
* Set grid batch-action callback.
*
* @param Closure $closure
*
* @return $this
*/
public function batchActions(Closure $closure)
{
$this->tools(function (Grid\Tools $tools) use ($closure) {
$tools->batch($closure);
});
return $this;
}
/**
* @param bool $disable
*
* @return Grid|mixed
*/
public function disableBatchActions(bool $disable = true)
{
$this->tools->disableBatchActions($disable);
return $this->option('show_row_selector', !$disable);
}
/**
* Add `actions` column for grid.
*
* @return void
*/
protected function appendActionsColumn()
{
if (!$this->option('show_actions')) {
return;
}
$this->addColumn(Grid\Column::ACTION_COLUMN_NAME, trans('admin.action'))
->displayUsing($this->getActionClass(), [$this->actionsCallback]);
}
}