1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Encore\Admin\Widgets; |
4
|
|
|
|
5
|
|
|
use Encore\Admin\Facades\Admin; |
6
|
|
|
use Illuminate\Contracts\Support\Renderable; |
7
|
|
|
|
8
|
|
|
class Tab extends Widget implements Renderable |
9
|
|
|
{ |
10
|
|
|
use ContainsForms; |
11
|
|
|
|
12
|
|
|
const TYPE_CONTENT = 1; |
13
|
|
|
const TYPE_LINK = 2; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var string |
17
|
|
|
*/ |
18
|
|
|
protected $view = 'admin::widgets.tab'; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var array |
22
|
|
|
*/ |
23
|
|
|
protected $data = [ |
24
|
|
|
'id' => '', |
25
|
|
|
'title' => '', |
26
|
|
|
'tabs' => [], |
27
|
|
|
'dropDown' => [], |
28
|
|
|
'active' => 0, |
29
|
|
|
]; |
30
|
|
|
|
31
|
|
|
public function __construct() |
32
|
|
|
{ |
33
|
|
|
$this->class('nav-tabs-custom'); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Add a tab and its contents. |
38
|
|
|
* |
39
|
|
|
* @param string $title |
40
|
|
|
* @param string|Renderable $content |
41
|
|
|
* @param bool $active |
42
|
|
|
* @param string|null $id |
43
|
|
|
* |
44
|
|
|
* @return $this |
45
|
|
|
*/ |
46
|
|
View Code Duplication |
public function add($title, $content, $active = false, $id = null) |
|
|
|
|
47
|
|
|
{ |
48
|
|
|
$this->data['tabs'][] = [ |
49
|
|
|
'id' => $id ?: mt_rand(), |
50
|
|
|
'title' => $title, |
51
|
|
|
'content' => $content, |
52
|
|
|
'type' => static::TYPE_CONTENT, |
53
|
|
|
]; |
54
|
|
|
|
55
|
|
|
if ($active) { |
56
|
|
|
$this->data['active'] = count($this->data['tabs']) - 1; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
return $this; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Add a link on tab. |
64
|
|
|
* |
65
|
|
|
* @param string $title |
66
|
|
|
* @param string $href |
67
|
|
|
* @param bool $active |
68
|
|
|
* |
69
|
|
|
* @return $this |
70
|
|
|
*/ |
71
|
|
View Code Duplication |
public function addLink($title, $href, $active = false) |
|
|
|
|
72
|
|
|
{ |
73
|
|
|
$this->data['tabs'][] = [ |
74
|
|
|
'id' => mt_rand(), |
75
|
|
|
'title' => $title, |
76
|
|
|
'href' => $href, |
77
|
|
|
'type' => static::TYPE_LINK, |
78
|
|
|
]; |
79
|
|
|
|
80
|
|
|
if ($active) { |
81
|
|
|
$this->data['active'] = count($this->data['tabs']) - 1; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
return $this; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Set title. |
89
|
|
|
* |
90
|
|
|
* @param string $title |
91
|
|
|
*/ |
92
|
|
|
public function title($title = '') |
93
|
|
|
{ |
94
|
|
|
$this->data['title'] = $title; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Set drop-down items. |
99
|
|
|
* |
100
|
|
|
* @param array $links |
101
|
|
|
* |
102
|
|
|
* @return $this |
103
|
|
|
*/ |
104
|
|
|
public function dropDown(array $links) |
105
|
|
|
{ |
106
|
|
|
if (is_array($links[0])) { |
107
|
|
|
foreach ($links as $link) { |
108
|
|
|
call_user_func([$this, 'dropDown'], $link); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
return $this; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
$this->data['dropDown'][] = [ |
115
|
|
|
'name' => $links[0], |
116
|
|
|
'href' => $links[1], |
117
|
|
|
]; |
118
|
|
|
|
119
|
|
|
return $this; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Render Tab. |
124
|
|
|
* |
125
|
|
|
* @return string |
126
|
|
|
*/ |
127
|
|
|
public function render() |
128
|
|
|
{ |
129
|
|
|
$data = array_merge( |
130
|
|
|
$this->data, |
131
|
|
|
['attributes' => $this->formatAttributes()] |
132
|
|
|
); |
133
|
|
|
|
134
|
|
|
$this->setupScript(); |
135
|
|
|
|
136
|
|
|
return view($this->view, $data)->render(); |
|
|
|
|
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Setup script. |
141
|
|
|
*/ |
142
|
|
|
protected function setupScript() |
143
|
|
|
{ |
144
|
|
|
$script = <<<SCRIPT |
145
|
|
|
var hash = document.location.hash; |
146
|
|
|
if (hash) { |
147
|
|
|
$('.nav-tabs a[href="' + hash + '"]').tab('show'); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
// Change hash for page-reload |
151
|
|
|
$('.nav-tabs a').on('shown.bs.tab', function (e) { |
152
|
|
|
history.pushState(null,null, e.target.hash); |
153
|
|
|
}); |
154
|
|
|
SCRIPT; |
155
|
|
|
Admin::script($script); |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.