Skip to content

Commit 0438228

Browse files
committed
add CardToolsSupportTrait
1 parent ba044d0 commit 0438228

File tree

4 files changed

+121
-181
lines changed

4 files changed

+121
-181
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ AdminLTE 3 widgets for Yii2. At present time the extension includes
1111
* [ProfileCardWidget](#profilecardwidget)
1212
* [ContactCardWidget](#contactcardwidget)
1313

14-
More widgets, helpers and Gii will be added in the future.
14+
**Based on [AdminLTE 3.1.0](https://github.com/ColorlibHQ/AdminLTE/releases/tag/v3.1.0)** More widgets, helpers and Gii will be added in the future.
1515

1616
## Installation
1717

src/CardToolsSupportTrait.php

+106
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
<?php
2+
namespace co0lc0der\Lte3Widgets;
3+
4+
use yii\bootstrap\Html;
5+
6+
/**
7+
* Trait CardToolsSupportTrait
8+
* @package co0lc0der\Lte3Widgets
9+
*/
10+
trait CardToolsSupportTrait
11+
{
12+
/**
13+
* show / hide collapse button inside card header
14+
* @var bool
15+
*/
16+
public bool $collapse = true;
17+
18+
/**
19+
* show / hide a collapsed card after initialization
20+
* @var bool
21+
*/
22+
public bool $hide = false;
23+
24+
/**
25+
* show / hide collapse button inside card header
26+
* @var bool
27+
*/
28+
29+
public bool $expand = false;
30+
31+
/**
32+
* show / hide close button inside card header
33+
* @var bool
34+
*/
35+
public bool $close = false;
36+
37+
/**
38+
* list of header custom tools (labels, buttons, links)
39+
* @var array
40+
*/
41+
public array $tools = [];
42+
43+
/**
44+
* @return string
45+
*/
46+
private function getCardTools(): string
47+
{
48+
$html = '';
49+
50+
if (is_array($this->tools)) {
51+
foreach ($this->tools as $item) {
52+
if ($item[0] == 'button') {
53+
$html .= Html::button($item[1], array_merge(['class' => 'btn btn-tool'], $item[2]));
54+
} else if ($item[0] == 'label') {
55+
$html .= Html::tag('span', $item[1], $item[2]);
56+
} else {
57+
$html .= Html::a($item[1], $item[2], array_merge(['class' => 'btn btn-tool'], $item[3]));
58+
}
59+
}
60+
}
61+
62+
return (!empty($html)) ? Html::tag('div', $html, ['class' => 'card-tools']) : '';
63+
}
64+
65+
/**
66+
* @return void
67+
*/
68+
private function addStandardTools(): void
69+
{
70+
if ($this->collapse) {
71+
$this->items[] = [
72+
'button',
73+
($this->hide) ? '<i class="fas fa-plus"></i>' : '<i class="fas fa-minus"></i>',
74+
[
75+
'class' => 'btn btn-tool',
76+
'data-card-widget' => 'collapse',
77+
'title' => 'Collapse/Restore',
78+
]
79+
];
80+
}
81+
82+
if ($this->expand) {
83+
$this->items[] = [
84+
'button',
85+
'<i class="fas fa-expand"></i>',
86+
[
87+
'class' => 'btn btn-tool',
88+
'data-card-widget' => 'maximize',
89+
'title' => 'Maximize',
90+
]
91+
];
92+
}
93+
94+
if ($this->close) {
95+
$this->items[] = [
96+
'button',
97+
'<i class="fas fa-times"></i>',
98+
[
99+
'class' => 'btn btn-tool',
100+
'data-card-widget' => 'remove',
101+
'title' => 'Close',
102+
]
103+
];
104+
}
105+
}
106+
}

src/CardWidget.php

+12-93
Original file line numberDiff line numberDiff line change
@@ -46,30 +46,6 @@ class CardWidget extends \yii\base\Widget
4646
*/
4747
public string $footer = '';
4848

49-
/**
50-
* show / hide collapse button inside card header
51-
* @var bool
52-
*/
53-
public bool $collapse = true;
54-
55-
/**
56-
* show / hide a collapsed card after initialization
57-
* @var bool
58-
*/
59-
public bool $hide = false;
60-
61-
/**
62-
* show / hide collapse button inside card header
63-
* @var bool
64-
*/
65-
public bool $expand = false;
66-
67-
/**
68-
* show / hide close button inside card header
69-
* @var bool
70-
*/
71-
public bool $close = false;
72-
7349
/**
7450
* URL for loading data
7551
* if it is not empty it shows a spinner before data loaded
@@ -90,11 +66,7 @@ class CardWidget extends \yii\base\Widget
9066
*/
9167
public string $shadow = '';
9268

93-
/**
94-
* list of header custom tools (labels, buttons, links)
95-
* @var array
96-
*/
97-
public array $tools = [];
69+
use CardToolsSupportTrait;
9870

9971
/**
10072
* @return void
@@ -103,50 +75,19 @@ public function init()
10375
{
10476
parent::init();
10577

106-
if ($this->collapse) {
107-
$this->items[] = [
108-
'button',
109-
($this->hide) ? '<i class="fas fa-plus"></i>' : '<i class="fas fa-minus"></i>',
110-
[
111-
'class' => 'btn btn-tool',
112-
'data-card-widget' => 'collapse',
113-
'title' => 'Collapse/Развернуть',
114-
]
115-
];
116-
}
117-
118-
if ($this->expand) {
119-
$this->items[] = [
120-
'button',
121-
'<i class="fas fa-expand"></i>',
122-
[
123-
'class' => 'btn btn-tool',
124-
'data-card-widget' => 'maximize',
125-
'title' => 'Maximize',
126-
]
127-
];
128-
}
129-
130-
if ($this->close) {
131-
$this->items[] = [
132-
'button',
133-
'<i class="fas fa-times"></i>',
134-
[
135-
'class' => 'btn btn-tool',
136-
'data-card-widget' => 'remove',
137-
'title' => 'Close',
138-
]
139-
];
140-
}
78+
$this->addStandardTools();
14179

14280
ob_start();
14381
}
14482

83+
/**
84+
* @return string
85+
*/
14586
public function run(): string
14687
{
14788
$this->registerJs();
14889
$content = ob_get_clean();
149-
$html = Html::beginTag('div', ['class' => $this->getCardClass()]);
90+
$html = Html::beginTag('div', ['class' => $this->getCardClass(), 'data-widget' => 'card-widget']);
15091

15192
$html .= $this->getCardHeader();
15293
$html .= $this->getCardBody($content);
@@ -200,28 +141,6 @@ private function getCardFooter(): string
200141
return (!empty($this->footer)) ? Html::tag('div', $this->footer, ['class' => $this->getCardFooterClass()]) : '';
201142
}
202143

203-
/**
204-
* @return string
205-
*/
206-
private function getCardTools(): string
207-
{
208-
$html = '';
209-
210-
if (is_array($this->tools)) {
211-
foreach ($this->tools as $item) {
212-
if ($item[0] == 'button') {
213-
$html .= Html::button($item[1], array_merge(['class' => 'btn btn-tool'], $item[2]));
214-
} else if ($item[0] == 'label') {
215-
$html .= Html::tag('span', $item[1], $item[2]);
216-
} else {
217-
$html .= Html::a($item[1], $item[2], array_merge(['class' => 'btn btn-tool'], $item[3]));
218-
}
219-
}
220-
}
221-
222-
return (!empty($html)) ? Html::tag('div', $html, ['class' => 'card-tools']) : '';
223-
}
224-
225144
/**
226145
* @return string
227146
*/
@@ -270,12 +189,12 @@ private function registerJs(): void
270189
{
271190
if ($this->ajaxLoad) {
272191
$this->view->registerJs("
273-
$.each($('[data-ajax-load-url]'), function(i, el) {
274-
let url = $(el).attr('data-ajax-load-url');
275-
$(el).siblings('.card-body').load(url, function() {
276-
$(el).remove();
277-
});
278-
});
192+
$.each($('[data-ajax-load-url]'), function(i, el) {
193+
let url = $(el).attr('data-ajax-load-url');
194+
$(el).siblings('.card-body').load(url, function() {
195+
$(el).remove();
196+
});
197+
});
279198
", View::POS_READY, 'ajaxLoad');
280199
}
281200
}

src/ContactCardWidget.php

+2-87
Original file line numberDiff line numberDiff line change
@@ -72,43 +72,14 @@ class ContactCardWidget extends \yii\base\Widget
7272
*/
7373
public $footer = '';
7474

75-
/**
76-
* show / hide collapse button inside card header
77-
* @var bool
78-
*/
79-
public bool $collapse = true;
80-
81-
/**
82-
* show / hide a collapsed card after initialization
83-
* @var bool
84-
*/
85-
public bool $hide = false;
86-
87-
/**
88-
* show / hide collapse button inside card header
89-
* @var bool
90-
*/
91-
92-
public bool $expand = false;
93-
94-
/**
95-
* show / hide close button inside card header
96-
* @var bool
97-
*/
98-
public bool $close = false;
99-
10075
/**
10176
* type of card shadow
10277
* ('shadow-none', 'shadow-sm', 'shadow', 'shadow-lg')
10378
* @var string
10479
*/
10580
public string $shadow = '';
10681

107-
/**
108-
* list of header custom tools (labels, buttons, links)
109-
* @var array
110-
*/
111-
public array $tools = [];
82+
use CardToolsSupportTrait;
11283

11384
/**
11485
* @return void
@@ -117,41 +88,7 @@ public function init()
11788
{
11889
parent::init();
11990

120-
if ($this->collapse) {
121-
$this->tools[] = [
122-
'button',
123-
($this->hide) ? '<i class="fas fa-plus"></i>' : '<i class="fas fa-minus"></i>',
124-
[
125-
'class' => 'btn btn-tool',
126-
'data-card-widget' => 'collapse',
127-
'title' => 'Свернуть/Развернуть',
128-
]
129-
];
130-
}
131-
132-
if ($this->expand) {
133-
$this->tools[] = [
134-
'button',
135-
'<i class="fas fa-expand"></i>',
136-
[
137-
'class' => 'btn btn-tool',
138-
'data-card-widget' => 'maximize',
139-
'title' => 'На весь экран',
140-
]
141-
];
142-
}
143-
144-
if ($this->close) {
145-
$this->tools[] = [
146-
'button',
147-
'<i class="fas fa-times"></i>',
148-
[
149-
'class' => 'btn btn-tool',
150-
'data-card-widget' => 'remove',
151-
'title' => 'Закрыть',
152-
]
153-
];
154-
}
91+
$this->addStandardTools();
15592
}
15693

15794
/**
@@ -191,28 +128,6 @@ private function getCardTitle(): string
191128
return (!empty($this->position)) ? Html::encode($this->position) : '';
192129
}
193130

194-
/**
195-
* @return string
196-
*/
197-
private function getCardTools(): string
198-
{
199-
$html = '';
200-
201-
if (is_array($this->tools)) {
202-
foreach ($this->tools as $item) {
203-
if ($item[0] == 'button') {
204-
$html .= Html::button($item[1], array_merge(['class' => 'btn btn-tool'], $item[2]));
205-
} else if ($item[0] == 'label') {
206-
$html .= Html::tag('span', $item[1], $item[2]);
207-
} else {
208-
$html .= Html::a($item[1], $item[2], array_merge(['class' => 'btn btn-tool'], $item[3]));
209-
}
210-
}
211-
}
212-
213-
return (!empty($html)) ? Html::tag('div', $html, ['class' => 'card-tools']) : '';
214-
}
215-
216131
/**
217132
* @param string $content
218133
* @return string

0 commit comments

Comments
 (0)