-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathTabsCardWidget.php
224 lines (187 loc) · 4.99 KB
/
TabsCardWidget.php
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
<?php
namespace co0lc0der\Lte3Widgets;
use yii\bootstrap4\Html;
/**
* Class TabsCardWidget
* @package co0lc0der\Lte3Widgets
*/
class TabsCardWidget extends \yii\base\Widget
{
use ShadowSupportTrait;
use CustomCssSupportTrait;
use ColorSupportTrait;
/**
* title of a card
* if title is empty tabs will be placed on the left side of the card header
* @var string
*/
public string $title;
/**
* makes an outlined card
* @var bool
*/
public bool $outline = false;
/**
* makes a colored card, uses $color property (Bootstrap 4 colors)
* @var bool
*/
public bool $background = false;
/**
* makes a gradient card, uses $color property (Bootstrap 4 colors)
* @var bool
*/
public bool $gradient = false;
/**
* content of card footer
* @var string|array
*/
public $footer = '';
/**
* list of tabs
* [
* [
* 'title' => 'Tab1',
* 'id' => 'tab_1',
* 'content' => 'A wonderful serenity has taken possession of my entire soul,
* like these sweet mornings of spring which I enjoy with my whole heart.',
* 'active' => true,
* ],
* [
* 'title' => 'Tab2',
* 'id' => 'tab_2',
* 'content' => 'The European languages are members of the same family. Their separate existence is a myth.
* For science, music, sport, etc, Europe uses the same vocabulary.',
* ]
* ]
* @var array
*/
public array $tabs = [];
/**
* @return void
*/
public function init()
{
parent::init();
}
/**
* @return string
*/
public function run(): string
{
$html = Html::beginTag('div', ['class' => $this->getCardClass(), 'data-widget' => 'card-widget']);
$html .= $this->getCardHeader();
$html .= $this->getCardBody();
$html .= $this->getCardFooter();
$html .= Html::endTag('div'); // the end of a card
return $html;
}
/**
* @return string
*/
protected function getCardHeader(): string
{
$html = Html::beginTag('div', ['class' => $this->getCardHeaderClass()]);
$html .= $this->getCardTitle();
$html .= $this->getCardTabs();
$html .= Html::endTag('div'); // the end of a card header
return $html;
}
/**
* @return string
*/
protected function getCardTabs(): string
{
$html = '';
foreach ($this->tabs as $tab) {
$class = (array_key_exists('active', $tab) && $tab['active']) ? 'nav-link active' : 'nav-link';
$link = Html::a($tab['title'] ?? '', "#{$tab['id']}" ?? '#', ['class' => $class, 'data-toggle' => 'tab']);
$html .= Html::tag('li', $link, ['class' => 'nav-item']);
}
$left = (!empty($this->title)) ? ' ml-auto' : '';
return (!empty($html)) ? Html::tag('ul', $html, ['class' => "nav nav-pills{$left} p-2"]) : '';
}
/**
* @return string
*/
protected function getCardTitle(): string
{
return (!empty($this->title)) ? Html::tag('h3', Html::encode($this->title), ['class' => $this->getCardTitleClass()]) : '';
}
/**
* @return string
*/
protected function getCardBody(): string
{
$html = Html::beginTag('div', ['class' => 'tab-content']);
foreach ($this->tabs as $tab) {
$class = (array_key_exists('active', $tab) && $tab['active']) ? 'tab-pane active' : 'tab-pane';
$html .= Html::tag('div', $tab['content'] ?? '', ['class' => $class, 'id' => $tab['id'] ?? '']);
}
$html .= Html::endTag('div');
return Html::tag('div', $html, ['class' => $this->getCardBodyClass()]);
}
/**
* @return string
*/
protected function getCardFooter(): string
{
if (empty($this->footer)) return '';
if (is_array($this->footer)) {
$footer = '';
foreach ($this->footer as $item) {
$footer .= Html::a($item[0] ?? '', $item[2] ?? '#', array_merge(['class' => 'btn btn-sm ' . $item[1] ?? ''], $item[3] ?? [])) . ' ';
}
$html = Html::tag('div', $footer, ['class' => 'text-right']);
} else {
$html = $this->footer; //Html::encode($this->footer)?
}
return Html::tag('div', $html, ['class' => $this->getCardFooterClass()]);
}
/**
* @return string
*/
protected function getCardClass(): string
{
$class = "card";
if ($this->isColor($this->color)) {
$class .= (!$this->background && !$this->gradient) ? " card-{$this->color}" : '';
$class .= ($this->outline) ? ' card-outline' : '';
$class .= ($this->background) ? " bg-{$this->color}" : '';
$class .= ($this->gradient) ? " bg-gradient-{$this->color}" : '';
}
$class .= $this->getShadowClass();
return $class . $this->getCustomCssClass(0);
}
/**
* @return string
*/
protected function getCardHeaderClass(): string
{
$class = 'card-header';
$class .= (!empty($this->tabs)) ? ' d-flex p-0' : '';
return $class . $this->getCustomCssClass(1);
}
/**
* @return string
*/
protected function getCardTitleClass(): string
{
$class = 'card-title';
$class .= (!empty($this->tabs)) ? ' p-3' : '';
return $class . $this->getCustomCssClass(2);
}
/**
* @return string
*/
protected function getCardBodyClass(): string
{
return 'card-body' . $this->getCustomCssClass(3);
}
/**
* @return string
*/
protected function getCardFooterClass(): string
{
return 'card-footer' . $this->getCustomCssClass(4);
}
}