-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChip.php
executable file
·113 lines (96 loc) · 3.21 KB
/
Chip.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
<?php
namespace makroxyz\materializecss;
use yii\base\InvalidConfigException;
use yii\helpers\ArrayHelper;
/**
* Class Chip
* @package makroxyz\materializecss
*
* @see http://materializecss.com/chips.html
*/
class Chip extends Widget
{
/**
* @var array the HTML attributes for the widget container tag. The following special options are recognized:
*
* - tag: string, defaults to "div", the name of the container tag.
*
* @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
*/
public $options;
/**
* @var string the content of the chip besides the optional image and/or icon
*/
public $content;
/**
* @var bool whether to encode the content
*/
public $encodeContent = true;
/**
* @var array the HTML attributes for the img tag
*
* Specifiy at least the "src" key representing the source of the image
*
* @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
*/
public $imageOptions;
/**
* @var array the options for the optional icon
*
* if there is an icon present in the chip element, materializecss will treat it as a close (i. e. remove) trigger.
*
* To specify an icon you can use the following parameters
*
* ```php
* [
* 'name' => 'name of the icon', // optional, defaults to 'close'
* 'options' => 'the HTML attributes for the img', // optional
* ]
* ```
*
* @see makroxyz\materializecssIcon::run()
*/
public $icon;
/**
* @var bool whether to render the icon inside the chip
*/
public $renderClose = false;
/**
* Initialize the widget.
*
* @throws InvalidConfigException if the src property ot the image is not defined
*/
public function init()
{
parent::init();
Html::addCssClass($this->options, ['widget' => 'chip']);
if ($this->imageOptions && !isset($this->imageOptions['src'])) {
throw new InvalidConfigException('The "src" attribute for the image is required.');
}
if ($this->renderClose && !isset($this->icon['name'])) {
$this->icon['name'] = 'close';
}
}
/**
* Executes the widget.
* @return string the result of widget execution to be outputted.
*/
public function run()
{
$tag = ArrayHelper::remove($this->options, 'tag', 'div');
$html = Html::beginTag($tag, $this->options);
if ($this->imageOptions) {
$src = ArrayHelper::remove($this->imageOptions, 'src', '');
$html .= Html::img($src, $this->imageOptions);
}
$html .= $this->encodeContent ? Html::encode($this->content) : $this->content;
if ($this->renderClose) {
$html .= Icon::widget([
'name' => ArrayHelper::getValue($this->icon, 'name', null),
'options' => ArrayHelper::getValue($this->icon, 'options', [])
]);
}
$html .= Html::endTag($tag);
return $html;
}
}