-
Notifications
You must be signed in to change notification settings - Fork 12
/
Slick.php
129 lines (102 loc) · 2.92 KB
/
Slick.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
122
123
124
125
126
127
128
129
<?php
namespace evgeniyrru\yii2slick;
use yii\base\Widget;
use yii\helpers\Html;
use yii\base\Exception;
use yii\helpers\Json;
use yii\web\View;
use yii\web\JsExpression;
/**
* This is a wrapper for Slick Carousel plugin
* @see http://kenwheeler.github.io/slick/
*
*
* @author Evgeniy Chernishev <[email protected]>
*/
class Slick extends Widget
{
/**
* @var array options to call an event such as "init", "destroy", etc..
*/
public $events = [];
/**
* @var array options to populate Slick jQuery object
*/
public $clientOptions = [];
/**
* @var integer position for inclusion javascript widget code to web page
* @link http://www.yiiframework.com/doc-2.0/yii-web-view.html#registerJs()-detail
*/
public $jsPosition = View::POS_READY;
/**
* @var array HTML attributes to render on the container
*/
public $containerOptions = [];
/**
* @var string HTML tag to render the container
*/
public $containerTag = 'div';
/**
* @var string HTML tag to render items for the carousel
*/
public $itemContainer = 'div';
/**
* @var array HTML attributes for the one item
*/
public $itemOptions = [];
/**
* @var array elements for the carousel
*/
public $items = [];
/**
* @inheritdoc
*/
public function init()
{
$this->normalizeOptions();
// not allowed empty Items
if(empty($this->items)) {
throw new Exception('Not allowed without items');
}
}
/**
* Preparing some options for this widgets
*/
protected function normalizeOptions()
{
// not allowed empty container
!$this->containerTag && $this->containerTag = 'div';
if(!isset($this->containerOptions['id']) || empty($this->containerOptions['id'])) {
$this->containerOptions['id'] = $this->getId();
}
}
/**
* Register required scripts for the Slick plugin
*/
protected function registerClientScript()
{
$view = $this->getView();
SlickAsset::register($view);
$options = Json::encode($this->clientOptions);
$id = $this->containerOptions['id'];
$js[] = ";";
$js[] = "jQuery('#$id').slick($options);";
$view->registerJs(implode(PHP_EOL, $js), $this->jsPosition);
foreach ($this->events as $key => $value) {
$view->registerJs(new JsExpression("$('#".$this->id."').on('".$key."', ".$value.");"), $this->jsPosition);
}
}
/**
* @inheritdoc
*/
public function run()
{
$slider = Html::beginTag($this->containerTag, $this->containerOptions);
foreach($this->items as $item) {
$slider .= Html::tag($this->itemContainer, $item, $this->itemOptions);
}
$slider .= Html::endTag($this->containerTag);
echo $slider;
$this->registerClientScript();
}
}