-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathStandardList.php
121 lines (104 loc) · 2.89 KB
/
StandardList.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
<?php
namespace demogorgorn\uikit;
use Yii;
use yii\base\InvalidConfigException;
use yii\helpers\ArrayHelper;
use yii\helpers\Html;
/**
* StandardList renders a List UIKit component, a nicely looking list, which come in different styles.
*
* For example:
*
* ```php
* echo StandardList::widget([
* 'items' => [
* [
* 'body' => 'Home',
* 'options' => [ 'class' => '...' ],
* ],
* [
* 'body' => '<b>Text</b>',
* ],
* ],
* ]);
* ```
*
* @see http://www.getuikit.com/docs/list.html
*
* @author Oleg Martemjanov <[email protected]>
* @since 2.0
*/
class StandardList extends Widget
{
/**
* @var array list of items in the list widget. Each array element represents a single
* item which can be either a string or an array with the following structure:
*
* - body: string, required, the list item. It can contain everything.
* - options: array, optional, the HTML attributes of the item container (LI).
*/
public $items = [];
/**
* @var bool add the .uk-list-line class to separate list items with lines.
*/
public $addLine = false;
/**
* @var bool add zebra-striping to a list using the .uk-list-striped class.
*/
public $isStriped = false;
/**
* @var bool add the .uk-list-space class to increase the spacing between list items.
*/
public $addSpace = false;
/**
* Initializes the widget.
*/
public function init()
{
parent::init();
Html::addCssClass($this->options, 'uk-list');
if ($this->addLine)
Html::addCssClass($this->options, 'uk-list-line');
if ($this->isStriped)
Html::addCssClass($this->options, 'uk-list-striped');
if ($this->addSpace)
Html::addCssClass($this->options, 'uk-list-space');
}
/**
* Renders the widget.
*/
public function run()
{
echo $this->renderItems();
$this->registerAsset();
}
/**
* Renders widget items.
*/
public function renderItems()
{
$items = [];
foreach ($this->items as $i => $item) {
$items[] = $this->renderItem($item);
}
return Html::tag('ul', implode("\n", $items), $this->options);
}
/**
* Renders a widget's item.
* @param string|array $item the item to render.
* @return string the rendering result.
* @throws InvalidConfigException
*/
public function renderItem($item)
{
if (is_string($item)) {
return Html::tag('li', $item, []);
}
if (!isset($item['body'])) {
throw new InvalidConfigException("The 'body' option is required.");
}
$body = $item['body'];
$options = ArrayHelper::getValue($item, 'options', []);
return Html::tag('li', $body, $options);
}
}