-
Notifications
You must be signed in to change notification settings - Fork 1
/
Swiper.php
103 lines (86 loc) · 2.48 KB
/
Swiper.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
<?php
namespace chenkby\swiper;
use yii\base\Widget;
use yii\web\View;
/**
* swiper widget
* @see also http://idangero.us/swiper
* <?=\chenkby\swiper\Swiper::widget([
* 'items'=>[
* Html::img('http://img-src'),
* Html::img('http://img-src'),
* ],
* 'jquery'=>false,
* 'clientOptions'=>[
* 'loop'=>true,
* ]
* ]);?>
*/
class Swiper extends Widget
{
public $items=[];
/**
* @var bool jquery
*/
public $jquery=true;
/**
* @var bool pagination
*/
public $pagination=true;
/**
* @var bool navigation buttons
*/
public $navigation=true;
/**
* @var bool scrollbar
*/
public $scrollbar=false;
/**
* @var array The HTML attributes for the container tag
*/
public $options=[];
/**
* @var array client options
* @see also http://idangero.us/swiper/api/#.VvSsy1WF6Uk
*/
public $clientOptions=[];
public function init()
{
parent::init();
if(empty($this->items))return null;
if(!isset($this->options['class'])){
$this->options['class']='swiper-container';
}
if(!isset($this->options['id'])){
$this->options['id']=$this->id;
}
if($this->pagination){
$this->clientOptions['pagination']='.swiper-pagination';
}
if($this->navigation){
$this->clientOptions['nextButton']='.swiper-button-next';
$this->clientOptions['prevButton']='.swiper-button-prev';
}
if($this->scrollbar){
$this->clientOptions['scrollbar']='.swiper-scrollbar';
}
$this->registerAsset();
$clientOptions=!empty($this->clientOptions) ? \yii\helpers\Json::encode($this->clientOptions) : '{}';
$clientObj='swiper'.ucwords($this->options['id']);
$js="var {$clientObj}=new Swiper('#{$this->options['id']}',{$clientOptions})";
$position=$this->jquery ? View::POS_READY : View::POS_END;
$this->view->registerJs($js,$position);
}
public function run()
{
if(empty($this->items))return null;
return $this->render('swiper');
}
/**
* Registers this asset bundle with a view
*/
public function registerAsset()
{
$this->jquery ? SwiperJqueryAsset::register($this->view) : SwiperAsset::register($this->view);
}
}