1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
*## TbCarousel class file. |
4
|
|
|
* |
5
|
|
|
* @author Christoffer Niska <[email protected]> |
6
|
|
|
* @copyright Copyright © Christoffer Niska 2011- |
7
|
|
|
* @license [New BSD License](http://www.opensource.org/licenses/bsd-license.php) |
8
|
|
|
* @since 0.9.10 |
9
|
|
|
* |
10
|
|
|
* @author Amr Bedair <[email protected]> |
11
|
|
|
* @since v4.0.0 - modified to work with bootstrap 3.1.1 |
12
|
|
|
* |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
*## Bootstrap carousel widget. |
17
|
|
|
* |
18
|
|
|
* @see <http://twitter.github.com/bootstrap/javascript.html#carousel> |
19
|
|
|
* |
20
|
|
|
* @package booster.widgets.grouping |
21
|
|
|
*/ |
22
|
|
|
class TbCarousel extends CWidget { |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var string the previous button label. Defaults to '‹'. |
26
|
|
|
*/ |
27
|
|
|
public $prevLabel = '<span class="glyphicon glyphicon-chevron-left"></span>'; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var string the next button label. Defaults to '›'. |
31
|
|
|
*/ |
32
|
|
|
public $nextLabel = '<span class="glyphicon glyphicon-chevron-right"></span>'; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var boolean indicates whether the carousel should slide items. |
36
|
|
|
*/ |
37
|
|
|
public $slide = true; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var boolean indicates whether to display the previous and next links. |
41
|
|
|
*/ |
42
|
|
|
public $displayPrevAndNext = true; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var array the carousel items configuration. |
46
|
|
|
*/ |
47
|
|
|
public $items = array(); |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var array the options for the Bootstrap Javascript plugin. |
51
|
|
|
*/ |
52
|
|
|
public $options = array(); |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @var string[] the Javascript event handlers. |
56
|
|
|
*/ |
57
|
|
|
public $events = array(); |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @var array the HTML attributes for the widget container. |
61
|
|
|
*/ |
62
|
|
|
public $htmlOptions = array(); |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
*### .init() |
66
|
|
|
* |
67
|
|
|
* Initializes the widget. |
68
|
|
|
*/ |
69
|
|
|
public function init() { |
70
|
|
|
|
71
|
|
|
if (!isset($this->htmlOptions['id'])) { |
72
|
|
|
$this->htmlOptions['id'] = $this->getId(); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
$classes = array('carousel'); |
76
|
|
|
|
77
|
|
|
if ($this->slide === true) { |
78
|
|
|
$classes[] = 'slide'; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
if (!empty($classes)) { |
82
|
|
|
$classes = implode(' ', $classes); |
83
|
|
|
if (isset($this->htmlOptions['class'])) { |
84
|
|
|
$this->htmlOptions['class'] .= ' ' . $classes; |
85
|
|
|
} else { |
86
|
|
|
$this->htmlOptions['class'] = $classes; |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
*### .run() |
93
|
|
|
* |
94
|
|
|
* Runs the widget. |
95
|
|
|
*/ |
96
|
|
|
public function run() { |
97
|
|
|
|
98
|
|
|
$id = $this->htmlOptions['id']; |
99
|
|
|
|
100
|
|
|
echo CHtml::openTag('div', $this->htmlOptions); |
101
|
|
|
$this->renderIndicators(); |
102
|
|
|
echo '<div class="carousel-inner">'; |
103
|
|
|
$this->renderItems($this->items); |
104
|
|
|
echo '</div>'; |
105
|
|
|
|
106
|
|
|
if ($this->displayPrevAndNext) { |
107
|
|
|
echo '<a class="carousel-control left" href="#' . $id . '" data-slide="prev">' . $this->prevLabel . '</a>'; |
108
|
|
|
echo '<a class="carousel-control right" href="#' . $id . '" data-slide="next">' . $this->nextLabel . '</a>'; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
echo '</div>'; |
112
|
|
|
|
113
|
|
|
/** @var CClientScript $cs */ |
114
|
|
|
$cs = Yii::app()->getClientScript(); |
115
|
|
|
$options = !empty($this->options) ? CJavaScript::encode($this->options) : ''; |
116
|
|
|
$cs->registerScript(__CLASS__ . '#' . $id, "jQuery('#{$id}').carousel({$options});"); |
117
|
|
|
|
118
|
|
|
foreach ($this->events as $name => $handler) { |
119
|
|
|
$handler = CJavaScript::encode($handler); |
120
|
|
|
$cs->registerScript(__CLASS__ . '#' . $id . '_' . $name, "jQuery('#{$id}').on('{$name}', {$handler});"); |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* |
126
|
|
|
*/ |
127
|
|
|
protected function renderIndicators() { |
128
|
|
|
|
129
|
|
|
echo '<ol class="carousel-indicators">'; |
130
|
|
|
$count = count($this->items); |
131
|
|
|
for ($i = 0; $i < $count; $i++) { |
132
|
|
|
echo '<li data-target="#'.$this->id.'" data-slide-to="'.$i.'" class="'.($i===0?'active':'').'"></li>'; |
133
|
|
|
} |
134
|
|
|
echo '</ol>'; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
*### .renderItems() |
139
|
|
|
* |
140
|
|
|
* Renders the carousel items. |
141
|
|
|
* |
142
|
|
|
* @param array $items the item configuration. |
143
|
|
|
*/ |
144
|
|
|
protected function renderItems($items) { |
145
|
|
|
|
146
|
|
|
foreach ($items as $i => $item) { |
147
|
|
|
if (!is_array($item)) { |
148
|
|
|
continue; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
if (isset($item['visible']) && $item['visible'] === false) { |
152
|
|
|
continue; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
if (!isset($item['itemOptions'])) { |
156
|
|
|
$item['itemOptions'] = array(); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
$classes = array('item'); |
160
|
|
|
|
161
|
|
|
if ($i === 0) { |
162
|
|
|
$classes[] = 'active'; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
$classes = implode(' ', $classes); |
166
|
|
|
if (isset($item['itemOptions']['class'])) { |
167
|
|
|
$item['itemOptions']['class'] .= ' ' . $classes; |
168
|
|
|
} else { |
169
|
|
|
$item['itemOptions']['class'] = $classes; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
echo CHtml::openTag('div', $item['itemOptions']); |
173
|
|
|
|
174
|
|
|
if (isset($item['image'])) { |
175
|
|
|
if (!isset($item['alt'])) { |
176
|
|
|
$item['alt'] = ''; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
if (!isset($item['imageOptions'])) { |
180
|
|
|
$item['imageOptions'] = array(); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Is this image should be a link? |
185
|
|
|
* @since 2.1.0 |
186
|
|
|
*/ |
187
|
|
|
if (isset($item['link'])) { |
188
|
|
|
// Any kind of link options |
189
|
|
|
if (!isset($item['linkOptions']) || !is_array($item['linkOptions'])) { |
190
|
|
|
$item['linkOptions'] = array(); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
// URL |
194
|
|
|
if(is_array($item['link'])) { |
195
|
|
|
$route = isset($item['link'][0]) ? $item['link'][0] : ''; |
196
|
|
|
$item['linkOptions']['href'] = Yii::app()->createUrl($route, array_splice($item['link'], 1)); |
197
|
|
|
} else { |
198
|
|
|
$item['linkOptions']['href'] = $item['link']; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
// Print out 'A' tag |
202
|
|
|
echo CHtml::openTag('a', $item['linkOptions']); |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
echo CHtml::image($item['image'], $item['alt'], $item['imageOptions']); |
206
|
|
|
|
207
|
|
|
if (isset($item['link'])) { |
208
|
|
|
echo '</a>'; |
209
|
|
|
} |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
if (!empty($item['caption']) && (isset($item['label']) || isset($item['caption']))) { |
213
|
|
|
if (!isset($item['captionOptions'])) { |
214
|
|
|
$item['captionOptions'] = array(); |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
if (isset($item['captionOptions']['class'])) { |
218
|
|
|
$item['captionOptions']['class'] .= ' carousel-caption'; |
219
|
|
|
} else { |
220
|
|
|
$item['captionOptions']['class'] = 'carousel-caption'; |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
echo CHtml::openTag('div', $item['captionOptions']); |
224
|
|
|
|
225
|
|
|
if (isset($item['label'])) { |
226
|
|
|
echo '<h3>' . $item['label'] . '</h3>'; |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
if (isset($item['caption'])) { |
230
|
|
|
echo '<p>' . $item['caption'] . '</p>'; |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
echo '</div>'; |
234
|
|
|
} |
235
|
|
|
echo '</div>'; |
236
|
|
|
} |
237
|
|
|
} |
238
|
|
|
} |
239
|
|
|
|