|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Encore\Admin\Form; |
|
4
|
|
|
|
|
5
|
|
|
use Encore\Admin\Facades\Admin; |
|
6
|
|
|
use Encore\Admin\Form; |
|
7
|
|
|
use Illuminate\Contracts\Support\Htmlable; |
|
8
|
|
|
use Illuminate\Contracts\Support\Renderable; |
|
9
|
|
|
use Illuminate\Support\Collection; |
|
10
|
|
|
use Illuminate\Support\Str; |
|
11
|
|
|
|
|
12
|
|
|
class Tools implements Renderable |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* @var Builder |
|
16
|
|
|
*/ |
|
17
|
|
|
protected $form; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Collection of tools. |
|
21
|
|
|
* |
|
22
|
|
|
* @var Collection |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $tools; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var array |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $options = [ |
|
30
|
|
|
'enableListButton' => true, |
|
31
|
|
|
'enableBackButton' => true, |
|
32
|
|
|
]; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Create a new Tools instance. |
|
36
|
|
|
* |
|
37
|
|
|
* @param Builder $builder |
|
38
|
|
|
*/ |
|
39
|
|
|
public function __construct(Builder $builder) |
|
40
|
|
|
{ |
|
41
|
|
|
$this->form = $builder; |
|
42
|
|
|
|
|
43
|
|
|
$this->tools = new Collection(); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @return string |
|
48
|
|
|
*/ |
|
49
|
|
|
protected function backButton() |
|
50
|
|
|
{ |
|
51
|
|
|
$script = <<<'EOT' |
|
52
|
|
|
$('.form-history-back').on('click', function (event) { |
|
53
|
|
|
event.preventDefault(); |
|
54
|
|
|
history.back(1); |
|
55
|
|
|
}); |
|
56
|
|
|
EOT; |
|
57
|
|
|
|
|
58
|
|
|
Admin::script($script); |
|
59
|
|
|
|
|
60
|
|
|
$text = trans('admin.back'); |
|
61
|
|
|
|
|
62
|
|
|
return <<<EOT |
|
63
|
|
|
<div class="btn-group pull-right" style="margin-right: 10px"> |
|
64
|
|
|
<a class="btn btn-sm btn-default form-history-back"><i class="fa fa-arrow-left"></i> $text</a> |
|
65
|
|
|
</div> |
|
66
|
|
|
EOT; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
public function listButton() |
|
70
|
|
|
{ |
|
71
|
|
|
$slice = Str::contains($this->form->getResource(0), '/edit') ? null : -1; |
|
72
|
|
|
$resource = $this->form->getResource($slice); |
|
73
|
|
|
|
|
74
|
|
|
$text = trans('admin.list'); |
|
75
|
|
|
|
|
76
|
|
|
return <<<EOT |
|
77
|
|
|
<div class="btn-group pull-right" style="margin-right: 10px"> |
|
78
|
|
|
<a href="$resource" class="btn btn-sm btn-default"><i class="fa fa-list"></i> $text</a> |
|
79
|
|
|
</div> |
|
80
|
|
|
EOT; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Prepend a tool. |
|
85
|
|
|
* |
|
86
|
|
|
* @param string $tool |
|
87
|
|
|
* |
|
88
|
|
|
* @return $this |
|
89
|
|
|
*/ |
|
90
|
|
|
public function add($tool) |
|
91
|
|
|
{ |
|
92
|
|
|
$this->tools->push($tool); |
|
93
|
|
|
|
|
94
|
|
|
return $this; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Disable back button. |
|
99
|
|
|
* |
|
100
|
|
|
* @return $this |
|
101
|
|
|
*/ |
|
102
|
|
|
public function disableBackButton() |
|
103
|
|
|
{ |
|
104
|
|
|
$this->options['enableBackButton'] = false; |
|
105
|
|
|
|
|
106
|
|
|
return $this; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* Disable list button. |
|
111
|
|
|
* |
|
112
|
|
|
* @return $this |
|
113
|
|
|
*/ |
|
114
|
|
|
public function disableListButton() |
|
115
|
|
|
{ |
|
116
|
|
|
$this->options['enableListButton'] = false; |
|
117
|
|
|
|
|
118
|
|
|
return $this; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* Render header tools bar. |
|
123
|
|
|
* |
|
124
|
|
|
* @return string |
|
125
|
|
|
*/ |
|
126
|
|
|
public function render() |
|
127
|
|
|
{ |
|
128
|
|
|
if ($this->options['enableListButton']) { |
|
129
|
|
|
$this->add($this->listButton()); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
if ($this->options['enableBackButton']) { |
|
133
|
|
|
$this->add($this->backButton()); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
return $this->tools->map(function ($tool) { |
|
137
|
|
|
if ($tool instanceof Renderable) { |
|
138
|
|
|
return $tool->render(); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
if ($tool instanceof Htmlable) { |
|
142
|
|
|
return $tool->toHtml(); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
return (string) $tool; |
|
146
|
|
|
})->implode(' '); |
|
147
|
|
|
} |
|
148
|
|
|
} |
|
149
|
|
|
|