1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Encore\Admin\Widgets; |
4
|
|
|
|
5
|
|
|
use Illuminate\Contracts\Support\Renderable; |
6
|
|
|
|
7
|
|
|
class Tab extends Widget implements Renderable |
8
|
|
|
{ |
9
|
|
|
const TYPE_CONTENT = 1; |
10
|
|
|
const TYPE_LINK = 2; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @var string |
14
|
|
|
*/ |
15
|
|
|
protected $view = 'admin::widgets.tab'; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var array |
19
|
|
|
*/ |
20
|
|
|
protected $data = [ |
21
|
|
|
'id' => '', |
22
|
|
|
'title' => '', |
23
|
|
|
'tabs' => [], |
24
|
|
|
'dropDown' => [], |
25
|
|
|
'active' => 0, |
26
|
|
|
]; |
27
|
|
|
|
28
|
|
|
public function __construct() |
29
|
|
|
{ |
30
|
|
|
$this->class('nav-tabs-custom'); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Add a tab and its contents. |
35
|
|
|
* |
36
|
|
|
* @param string $title |
37
|
|
|
* @param string|Renderable $content |
38
|
|
|
* @param bool $active |
39
|
|
|
* |
40
|
|
|
* @return $this |
41
|
|
|
*/ |
42
|
|
View Code Duplication |
public function add($title, $content, $active = false) |
|
|
|
|
43
|
|
|
{ |
44
|
|
|
$this->data['tabs'][] = [ |
45
|
|
|
'id' => mt_rand(), |
46
|
|
|
'title' => $title, |
47
|
|
|
'content' => $content, |
48
|
|
|
'type' => static::TYPE_CONTENT, |
49
|
|
|
]; |
50
|
|
|
|
51
|
|
|
if ($active) { |
52
|
|
|
$this->data['active'] = count($this->data['tabs']) - 1; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
return $this; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Add a link on tab. |
60
|
|
|
* |
61
|
|
|
* @param string $title |
62
|
|
|
* @param string $href |
63
|
|
|
* @param bool $active |
64
|
|
|
* |
65
|
|
|
* @return $this |
66
|
|
|
*/ |
67
|
|
View Code Duplication |
public function addLink($title, $href, $active = false) |
|
|
|
|
68
|
|
|
{ |
69
|
|
|
$this->data['tabs'][] = [ |
70
|
|
|
'id' => mt_rand(), |
71
|
|
|
'title' => $title, |
72
|
|
|
'href' => $href, |
73
|
|
|
'type' => static::TYPE_LINK, |
74
|
|
|
]; |
75
|
|
|
|
76
|
|
|
if ($active) { |
77
|
|
|
$this->data['active'] = count($this->data['tabs']) - 1; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return $this; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Set title. |
85
|
|
|
* |
86
|
|
|
* @param string $title |
87
|
|
|
*/ |
88
|
|
|
public function title($title = '') |
89
|
|
|
{ |
90
|
|
|
$this->data['title'] = $title; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Set drop-down items. |
95
|
|
|
* |
96
|
|
|
* @param array $links |
97
|
|
|
* |
98
|
|
|
* @return $this |
99
|
|
|
*/ |
100
|
|
|
public function dropDown(array $links) |
101
|
|
|
{ |
102
|
|
|
if (is_array($links[0])) { |
103
|
|
|
foreach ($links as $link) { |
104
|
|
|
call_user_func([$this, 'dropDown'], $link); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
return $this; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
$this->data['dropDown'][] = [ |
111
|
|
|
'name' => $links[0], |
112
|
|
|
'href' => $links[1], |
113
|
|
|
]; |
114
|
|
|
|
115
|
|
|
return $this; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Render Tab. |
120
|
|
|
* |
121
|
|
|
* @return string |
122
|
|
|
*/ |
123
|
|
|
public function render() |
124
|
|
|
{ |
125
|
|
|
$data = array_merge( |
126
|
|
|
$this->data, |
127
|
|
|
['attributes' => $this->formatAttributes()] |
128
|
|
|
); |
129
|
|
|
|
130
|
|
|
return view($this->view, $data)->render(); |
|
|
|
|
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|
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.