-
Notifications
You must be signed in to change notification settings - Fork 1
/
SequenceJs.php
219 lines (197 loc) · 6.54 KB
/
SequenceJs.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
<?php
/**
* @link https://github.com/amilna/yii2-sequence-widget
* @copyright Copyright (c) 2015 Amilna
* @license http://opensource.org/licenses/MIT MIT
*/
namespace amilna\sequencejs;
use Yii;
use yii\helpers\Html;
use yii\base\Widget;
use yii\helpers\Json;
/**
* Widget renders a Sequence JS widget.
*
* For example:
*
* ```php
* echo SequenceJs::widget([
* 'dataProvider'=>$dataProvider, // active data provider
* 'targetId'=>'sequence', //id of rendered sequencejs (the container will constructed by the widget with the given id)
* 'imageKey'=>'front_image', //model attribute to be used as image
* 'backgroundKey'=>'image', //model attribute to be used as background
* 'theme' => 'parallax', //available themes: default, parallax, modern
*
* 'css' => 'test.css', // url of css to overide default css relative from @web
*
* // example to overide default themes
* 'itemView'=>function ($model, $key, $widget) {
* $type = ['aeroplane','balloon','kite'];
* $html = '<li>
* <div class="info">
* <h2>'.$model->title.'</h2>
* <p>'.$model->description.'</p>
* </div>
* <img class="sky" src="'.$model->image.'" alt="Blue Sky" />
* <img class="'.$type[$key%3].'" src="'.$model->front_image.'" alt="Aeroplane" />
* </li>';
*
* return $html;
* },
*
*
* // example to overide default options more options on http://sequencejs.com
* 'options'=>[
* 'autoPlay'=> true,
* 'autoPlayDelay'=> 3000,
* 'cycle'=>true,
* 'nextButton'=> true,
* 'prevButton'=> true,
* 'preloader'=> true,
* 'navigationSkip'=> false
* ],
*
* // example to use widget without active data provider (the target selector should already rendered)
* 'targets' => [
* '.sequencejs' => [
* 'autoPlay'=> true,
* 'autoPlayDelay'=> 3000,
* 'cycle'=>true,
* 'nextButton'=> true,
* 'prevButton'=> true,
* 'preloader'=> true,
* 'navigationSkip'=> false
* ],
* ],
*
* ]);
* ```
*
* @author Amilna
* @see http://www.sequencejs.com/
* @package amilna\sequencejs
*/
class SequenceJs extends Widget
{
public $dataProvider = null;
public $itemView = null;
public $itemPager = null;
public $titleKey = 'title';
public $imageKey = 'image';
public $descriptionKey = 'description';
public $backgroundKey = 'background';
public $urlKey = 'url';
public $options = [
'autoPlay'=> true,
'nextButton'=> true,
'prevButton'=> true,
'pagination'=> true,
'animateStartingFrameIn'=> true,
'autoPlay'=> true,
'autoPlayDelay'=> 3000,
'preloader'=> true
];
public $targetId = 'sequence';
public $showText = true;
public $theme = 'default'; // available options 'parallax','default'
public $css = null;
/** @var array $targets only use without active data provider*/
public $targets = [];
private $bundle = null;
public function init()
{
parent::init();
$view = $this->getView();
$bundle = SequenceJsAsset::register($view);
$this->bundle = $bundle;
if ($this->theme !== false && $this->css == null) {
$view->registerCssFile("{$bundle->baseUrl}/themes/{$this->theme}/style.css");
}
else
{
$view->registerCssFile("@web/{$this->css}");
}
if (count($this->dataProvider->getModels()) > 0)
{
echo '<div id="'.$this->targetId.$this->dataProvider->id.'" class="sequencejs">';
if (isset($this->options['prevButton']))
{
echo $this->options['prevButton']?'<img class="sequence-prev" src="'.$bundle->baseUrl.'/images/bt-prev.png" alt="Previous" />':'';
}
if (isset($this->options['nextButton']))
{
echo $this->options['nextButton']?'<img class="sequence-next" src="'.$bundle->baseUrl.'/images/bt-next.png" alt="Next" />':'';
}
echo ' <ul class="sequence-canvas">';
$pager = '';
foreach ($this->dataProvider->getModels() as $key=>$model)
{
echo $this->renderItem($model,$key);
$pager .= $this->renderPager($model,$key);
}
echo ' </ul>
<div class="sequence-pagination-wrapper">
<ul class="sequence-pagination">
'.$pager.'
</ul>
</div>
</div>';
$this->targets = [
'#'.$this->targetId.$this->dataProvider->id => $this->options,
];
}
if (!empty($this->targets)) {
$script = '';
foreach ($this->targets as $selector => $options) {
$options = Json::encode($options);
$script .= "
var sequence = $('$selector').sequence($options).data('sequence');
$('.sequence-canvas li').click(function(){
if (typeof $(this).attr('data-url') !== 'undefined') {
window.location = $(this).attr('data-url');
}
});
/*sequence.afterLoaded = function(){
$('.sequence-prev, .sequence-next').fadeIn(500);
}*/
" . PHP_EOL;
}
$view->registerJs($script);
}
}
public function renderPager($model, $key)
{
if ($this->itemPager === null) {
$content = '<li>'.$key.'</li>';
} elseif (is_string($this->itemPager)) {
$content = $this->getView()->render($this->itemView, array_merge([
'model' => $model,
'key' => $key,
'widget' => $this,
], $this->viewParams));
} else {
$content = call_user_func($this->itemPager, $model, $key, $this);
}
return $content;
}
public function renderItem($model, $key)
{
if ($this->itemView === null) {
$backgroundKey = $this->backgroundKey;
$titleKey = $this->titleKey;
$imageKey = $this->imageKey;
$descriptionKey = $this->descriptionKey;
$urlKey = $this->urlKey;
$content = $this->render('@webroot/assets/'.basename($this->bundle->baseUrl).'/themes/'.$this->theme.'/view',['model'=>$model,'key'=>$key,'widget'=>$this]);
} elseif (is_string($this->itemView)) {
$content = $this->getView()->render($this->itemView, array_merge([
'model' => $model,
'key' => $key,
'widget' => $this,
], $this->viewParams));
} else {
$content = call_user_func($this->itemView, $model, $key, $this);
}
return $content;
}
}