1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Encore\Admin\Grid\Tools; |
4
|
|
|
|
5
|
|
|
use Encore\Admin\Admin; |
6
|
|
|
use Illuminate\Support\Collection; |
7
|
|
|
|
8
|
|
|
class BatchActions extends AbstractTool |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var Collection |
12
|
|
|
*/ |
13
|
|
|
protected $actions; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var bool |
17
|
|
|
*/ |
18
|
|
|
protected $enableDelete = true; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var bool |
22
|
|
|
*/ |
23
|
|
|
private $isHoldSelectAllCheckbox = false; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* BatchActions constructor. |
27
|
|
|
*/ |
28
|
|
|
public function __construct() |
29
|
|
|
{ |
30
|
|
|
$this->actions = new Collection(); |
31
|
|
|
|
32
|
|
|
$this->appendDefaultAction(); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Append default action(batch delete action). |
37
|
|
|
* |
38
|
|
|
* return void |
39
|
|
|
*/ |
40
|
|
|
protected function appendDefaultAction() |
41
|
|
|
{ |
42
|
|
|
$this->add(new BatchDelete(trans('admin.delete'))); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Disable delete. |
47
|
|
|
* |
48
|
|
|
* @return $this |
49
|
|
|
*/ |
50
|
|
|
public function disableDelete() |
51
|
|
|
{ |
52
|
|
|
$this->enableDelete = false; |
53
|
|
|
|
54
|
|
|
return $this; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Disable delete And Hode SelectAll Checkbox |
59
|
|
|
* |
60
|
|
|
* @return $this |
61
|
|
|
*/ |
62
|
|
|
public function disableDeleteAndHodeSelectAll() |
63
|
|
|
{ |
64
|
|
|
$this->enableDelete = false; |
65
|
|
|
|
66
|
|
|
$this->isHoldSelectAllCheckbox = true; |
67
|
|
|
|
68
|
|
|
return $this; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Add a batch action. |
73
|
|
|
* |
74
|
|
|
* @param $title |
75
|
|
|
* @param BatchAction|null $action |
76
|
|
|
* |
77
|
|
|
* @return $this |
78
|
|
|
*/ |
79
|
|
|
public function add($title, BatchAction $action = null) |
80
|
|
|
{ |
81
|
|
|
$id = $this->actions->count(); |
82
|
|
|
|
83
|
|
|
if (func_num_args() == 1) { |
84
|
|
|
$action = $title; |
85
|
|
|
$action->setId($id); |
86
|
|
|
} elseif (func_num_args() == 2) { |
87
|
|
|
$action->setId($id); |
|
|
|
|
88
|
|
|
$action->setTitle($title); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
$this->actions->push($action); |
92
|
|
|
|
93
|
|
|
return $this; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Setup scripts of batch actions. |
98
|
|
|
* |
99
|
|
|
* @return void |
100
|
|
|
*/ |
101
|
|
|
protected function setUpScripts() |
102
|
|
|
{ |
103
|
|
|
Admin::script($this->script()); |
104
|
|
|
|
105
|
|
|
foreach ($this->actions as $action) { |
106
|
|
|
$action->setGrid($this->grid); |
107
|
|
|
|
108
|
|
|
Admin::script($action->script()); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Scripts of BatchActions button groups. |
114
|
|
|
* |
115
|
|
|
* @return string |
116
|
|
|
*/ |
117
|
|
|
protected function script() |
118
|
|
|
{ |
119
|
|
|
return <<<EOT |
120
|
|
|
|
121
|
|
|
$('.{$this->grid->getSelectAllName()}').iCheck({checkboxClass:'icheckbox_minimal-blue'}); |
122
|
|
|
|
123
|
|
|
$('.{$this->grid->getSelectAllName()}').on('ifChanged', function(event) { |
124
|
|
|
if (this.checked) { |
125
|
|
|
$('.{$this->grid->getGridRowName()}-checkbox').iCheck('check'); |
126
|
|
|
} else { |
127
|
|
|
$('.{$this->grid->getGridRowName()}-checkbox').iCheck('uncheck'); |
128
|
|
|
} |
129
|
|
|
}); |
130
|
|
|
|
131
|
|
|
EOT; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Render BatchActions button groups. |
136
|
|
|
* |
137
|
|
|
* @return string |
138
|
|
|
*/ |
139
|
|
|
public function render() |
140
|
|
|
{ |
141
|
|
|
if (!$this->enableDelete) { |
142
|
|
|
$this->actions->shift(); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
if ($this->actions->isEmpty()) { |
146
|
|
|
return ''; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
$this->setUpScripts(); |
150
|
|
|
|
151
|
|
|
$data = [ |
152
|
|
|
'actions' => $this->actions, |
153
|
|
|
'selectAllName' => $this->grid->getSelectAllName(), |
154
|
|
|
'isHoldSelectAllCheckbox' => $this->isHoldSelectAllCheckbox, |
155
|
|
|
]; |
156
|
|
|
|
157
|
|
|
return view('admin::grid.batch-actions', $data)->render(); |
|
|
|
|
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
|
If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe: