4
4
use yii \bootstrap \Html ;
5
5
use yii \web \View ;
6
6
7
+ /**
8
+ * Class CardWidget
9
+ * @package co0lc0der\Lte3Widgets
10
+ */
7
11
class CardWidget extends \yii \base \Widget
8
12
{
9
- public string $ title ; // title of a card
10
- public string $ color = '' ; // color of a card header (Bootstrap 4 colors. 'success', 'danger' еtс.)
11
- public bool $ outline = false ; // makes an outlined card
12
- public bool $ background = false ; // makes a colored card, uses $color property (Bootstrap 4 colors)
13
- public bool $ gradient = false ; // makes a gradient card, uses $color property (Bootstrap 4 colors)
14
- public string $ footer = '' ; // content of card footer
15
- public bool $ collapse = true ; // show/hide collapse button inside card header
16
- public bool $ hide = false ; // show/hide a collapsed card after initialization
17
- public bool $ expand = false ; // show/hide maximize button inside card header
18
- public bool $ close = false ; // show/hide close button inside card header
19
- public string $ ajaxLoad = '' ; // show loading spinner
20
- public string $ ajaxOverlay = 'overlay ' ;// type of loading overlay ('overlay', 'dark')
21
- public string $ shadow = '' ; // type of loading overlay ('shadow-none', 'shadow-sm', 'shadow', 'shadow-lg')
22
- public array $ items = []; // list of header custom items (labels, buttons, links)
13
+ /**
14
+ * title of a card
15
+ * @var string
16
+ */
17
+ public string $ title ;
23
18
19
+ /**
20
+ * color of a card header (Bootstrap 4 colors. 'success', 'danger' еtс.)
21
+ * @var string
22
+ */
23
+ public string $ color = '' ;
24
+
25
+ /**
26
+ * makes an outlined card
27
+ * @var bool
28
+ */
29
+ public bool $ outline = false ;
30
+
31
+ /**
32
+ * makes a colored card, uses $color property (Bootstrap 4 colors)
33
+ * @var bool
34
+ */
35
+ public bool $ background = false ;
36
+
37
+ /**
38
+ * makes a gradient card, uses $color property (Bootstrap 4 colors)
39
+ * @var bool
40
+ */
41
+ public bool $ gradient = false ;
42
+
43
+ /**
44
+ * content of card footer
45
+ * @var string
46
+ */
47
+ public string $ footer = '' ;
48
+
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
+ * show / hide maximize button inside card header
68
+ * @var bool
69
+ */
70
+ public bool $ close = false ;
71
+
72
+ /**
73
+ * URL for loading data, if it is not empty it shows a spinner before data loaded
74
+ * @var string
75
+ */
76
+ public string $ ajaxLoad = '' ;
77
+
78
+ /**
79
+ * type of loading overlay ('overlay', 'dark')
80
+ * @var string
81
+ */
82
+ public string $ ajaxOverlay = 'overlay ' ;
83
+
84
+ /**
85
+ * type of loading overlay
86
+ * ('shadow-none', 'shadow-sm', 'shadow', 'shadow-lg')
87
+ * @var string
88
+ */
89
+ public string $ shadow = '' ;
90
+
91
+ /**
92
+ * list of header custom tools (labels, buttons, links)
93
+ * @var array
94
+ */
95
+ public array $ tools = [];
96
+
97
+ /**
98
+ * @return void
99
+ */
24
100
public function init ()
25
101
{
26
102
parent ::init ();
@@ -60,22 +136,26 @@ public function init()
60
136
]
61
137
];
62
138
}
139
+
63
140
ob_start ();
64
141
}
65
142
143
+ /**
144
+ * @return string
145
+ */
66
146
public function run (): string
67
147
{
68
148
$ this ->registerJs ();
69
149
$ content = ob_get_clean ();
70
150
$ html = Html::beginTag ('div ' , ['class ' => $ this ->getCardClass ()]);
71
151
72
152
$ html .= Html::beginTag ('div ' , ['class ' => $ this ->getCardHeaderClass ()]);
73
- $ html .= (! empty ( $ this ->title )) ? Html:: tag ( ' h3 ' , $ this -> title , [ ' class ' => ' card-title ' ]) : '' ;
74
- $ html .= Html:: tag ( ' div ' , $ this ->getCardTools (), [ ' class ' => ' card-tools ' ] );
153
+ $ html .= $ this ->getCardTitle () ;
154
+ $ html .= $ this ->getCardTools ();
75
155
$ html .= Html::endTag ('div ' ); // the end of a card header
76
156
77
- $ html .= Html:: tag ( ' div ' , $ content, [ ' class ' => ' card-body ' ] );
78
- $ html .= ( $ this ->footer ) ? Html:: tag ( ' div ' , $ this -> footer , [ ' class ' => ' card-footer ' ]) : '' ;
157
+ $ html .= $ this -> getCardBody ( $ content );
158
+ $ html .= $ this ->getCardFooter () ;
79
159
80
160
if ($ this ->ajaxLoad ) {
81
161
$ overlay = ($ this ->ajaxOverlay == 'dark ' ) ? 'overlay dark ' : 'overlay ' ;
@@ -87,6 +167,34 @@ public function run(): string
87
167
return $ html ;
88
168
}
89
169
170
+ /**
171
+ * @return string
172
+ */
173
+ private function getCardTitle (): string
174
+ {
175
+ return (!empty ($ this ->title )) ? Html::tag ('h3 ' , $ this ->title , ['class ' => 'card-title ' ]) : '' ;
176
+ }
177
+
178
+ /**
179
+ * @param string $content
180
+ * @return string
181
+ */
182
+ private function getCardBody (string $ content = '' ): string
183
+ {
184
+ return (!empty ($ content )) ? Html::tag ('div ' , $ content , ['class ' => 'card-body ' ]) : '' ;
185
+ }
186
+
187
+ /**
188
+ * @return string
189
+ */
190
+ private function getCardFooter (): string
191
+ {
192
+ return (!empty ($ this ->footer )) ? Html::tag ('div ' , $ this ->footer , ['class ' => 'card-footer ' ]) : '' ;
193
+ }
194
+
195
+ /**
196
+ * @return string
197
+ */
90
198
private function getCardClass (): string
91
199
{
92
200
$ class = "card " ;
@@ -101,19 +209,25 @@ private function getCardClass(): string
101
209
return $ class ;
102
210
}
103
211
212
+ /**
213
+ * @return string
214
+ */
104
215
private function getCardHeaderClass (): string
105
216
{
106
217
$ class = 'card-header ' ;
107
218
108
219
return $ class ;
109
220
}
110
221
222
+ /**
223
+ * @return string
224
+ */
111
225
private function getCardTools (): string
112
226
{
113
227
$ html = '' ;
114
228
115
- if (is_array ($ this ->items )) {
116
- foreach ($ this ->items as $ item ){
229
+ if (is_array ($ this ->tools )) {
230
+ foreach ($ this ->tools as $ item ) {
117
231
if ($ item [0 ] == 'button ' ) {
118
232
$ html .= Html::button ($ item [1 ], array_merge (['class ' => 'btn btn-tool ' ], $ item [2 ]));
119
233
} else if ($ item [0 ] == 'label ' ) {
@@ -124,21 +238,23 @@ private function getCardTools(): string
124
238
}
125
239
}
126
240
127
- return $ html ;
241
+ return (! empty ( $ html)) ? Html:: tag ( ' div ' , $ html , [ ' class ' => ' card-tools ' ]) : '' ;
128
242
}
129
243
244
+ /**
245
+ * @return void
246
+ */
130
247
private function registerJs (): void
131
248
{
132
249
if ($ this ->ajaxLoad ) {
133
250
$ this ->view ->registerJs ("
134
- $.each($('[data-ajax-load-url]'), function(i, el) {
135
- let url = $(el).attr('data-ajax-load-url');
136
- $(el).siblings('.card-body').load(url, function() {
137
- $(el).remove();
138
- });
139
- });
251
+ $.each($('[data-ajax-load-url]'), function(i, el) {
252
+ let url = $(el).attr('data-ajax-load-url');
253
+ $(el).siblings('.card-body').load(url, function() {
254
+ $(el).remove();
255
+ });
256
+ });
140
257
" , View::POS_READY , 'ajaxLoad ' );
141
258
}
142
259
}
143
260
}
144
-
0 commit comments