-
Notifications
You must be signed in to change notification settings - Fork 0
/
Widget.php
74 lines (66 loc) · 1.86 KB
/
Widget.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
<?php
namespace pjkui\uikit;
use Yii;
use yii\helpers\Json;
/**
* pjkui\uikit\Widget is the base class for all UIkit widgets.
*
* @author Quinn Pan <pjkui@qq.com>
* @since 3.0
*/
class Widget extends \yii\base\Widget
{
/**
* @var array the HTML attributes for the widget container tag.
*/
public $options = [];
/**
* @var array the options for the underlying JS plugin.
* Please refer to the corresponding plugin Web page for possible options.
* For example, [this page](https://getuikit.com/docs/components) shows
* how to use the "Modal" plugin and the supported options (e.g. "remote").
*/
public $clientOptions = [];
/**
* @var array the event handlers for the underlying JS plugin.
* Please refer to the corresponding plugin Web page for possible events.
* For example, [this page](https://getuikit.com/docs/components) shows
* how to use the "Modal" plugin and the supported events (e.g. "shown").
*/
public $clientEvents = [];
/**
* Initializes the widget.
* This method will register the bootstrap asset bundle. If you override this method,
* make sure you call the parent implementation first.
*/
public function init()
{
parent::init();
if (!isset($this->options['id'])) {
$this->options['id'] = $this->getId();
}
}
/**
* Registers assets and the related events
*/
protected function registerAsset()
{
$view = $this->getView();
UIkitPluginAsset::register($view);
$id = $this->options['id'];
if (!empty($this->clientEvents)) {
$js = [];
foreach ($this->clientEvents as $event => $handler) {
$js[] = "jQuery('#$id').on('$event', $handler);";
}
$view->registerJs(implode("\n", $js));
}
}
/**
* @return string options array as json string
*/
protected function jsonClientOptions()
{
return empty($this->clientOptions) ? '' : Json::encode($this->clientOptions);
}
}