You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
181 lines
5.7 KiB
181 lines
5.7 KiB
12 years ago
|
<?php
|
||
|
/**
|
||
|
* @link http://www.yiiframework.com/
|
||
|
* @copyright Copyright (c) 2008 Yii Software LLC
|
||
|
* @license http://www.yiiframework.com/license/
|
||
|
*/
|
||
|
|
||
|
namespace yii\bootstrap;
|
||
|
|
||
12 years ago
|
use yii\base\InvalidConfigException;
|
||
12 years ago
|
use yii\helpers\ArrayHelper;
|
||
12 years ago
|
|
||
|
/**
|
||
|
* Carousel renders a carousel bootstrap javascript component.
|
||
|
*
|
||
12 years ago
|
* For example:
|
||
12 years ago
|
*
|
||
|
* ```php
|
||
11 years ago
|
* echo Carousel::widget([
|
||
|
* 'items' => [
|
||
12 years ago
|
* // the item contains only the image
|
||
12 years ago
|
* '<img src="http://twitter.github.io/bootstrap/assets/img/bootstrap-mdo-sfmoma-01.jpg"/>',
|
||
12 years ago
|
* // equivalent to the above
|
||
11 years ago
|
* ['content' => '<img src="http://twitter.github.io/bootstrap/assets/img/bootstrap-mdo-sfmoma-02.jpg"/>'],
|
||
12 years ago
|
* // the item contains both the image and the caption
|
||
11 years ago
|
* [
|
||
12 years ago
|
* 'content' => '<img src="http://twitter.github.io/bootstrap/assets/img/bootstrap-mdo-sfmoma-03.jpg"/>',
|
||
12 years ago
|
* 'caption' => '<h4>This is title</h4><p>This is the caption text</p>',
|
||
11 years ago
|
* 'options' => [...],
|
||
|
* ],
|
||
11 years ago
|
* ]
|
||
|
* ]);
|
||
12 years ago
|
* ```
|
||
|
*
|
||
11 years ago
|
* @see http://getbootstrap.com/javascript/#carousel
|
||
12 years ago
|
* @author Antonio Ramirez <amigo.cobos@gmail.com>
|
||
|
* @since 2.0
|
||
|
*/
|
||
|
class Carousel extends Widget
|
||
|
{
|
||
11 years ago
|
/**
|
||
7 years ago
|
* @var array|bool the labels for the previous and the next control buttons.
|
||
11 years ago
|
* If false, it means the previous and the next control buttons should not be displayed.
|
||
|
*/
|
||
|
public $controls = ['‹', '›'];
|
||
|
/**
|
||
7 years ago
|
* @var bool whether carousel indicators (<ol> tag with anchors to items) should be displayed or not.
|
||
11 years ago
|
*/
|
||
|
public $showIndicators = true;
|
||
|
/**
|
||
11 years ago
|
* @var array list of slides in the carousel. Each array element represents a single
|
||
|
* slide with the following structure:
|
||
|
*
|
||
|
* ```php
|
||
|
* [
|
||
|
* // required, slide content (HTML), such as an image tag
|
||
|
* 'content' => '<img src="http://twitter.github.io/bootstrap/assets/img/bootstrap-mdo-sfmoma-01.jpg"/>',
|
||
|
* // optional, the caption (HTML) of the slide
|
||
|
* 'caption' => '<h4>This is title</h4><p>This is the caption text</p>',
|
||
|
* // optional the HTML attributes of the slide container
|
||
|
* 'options' => [],
|
||
|
* ]
|
||
|
* ```
|
||
|
*/
|
||
|
public $items = [];
|
||
12 years ago
|
|
||
11 years ago
|
|
||
11 years ago
|
/**
|
||
|
* Initializes the widget.
|
||
|
*/
|
||
|
public function init()
|
||
|
{
|
||
|
parent::init();
|
||
10 years ago
|
Html::addCssClass($this->options, ['widget' => 'carousel']);
|
||
11 years ago
|
}
|
||
12 years ago
|
|
||
11 years ago
|
/**
|
||
|
* Renders the widget.
|
||
|
*/
|
||
|
public function run()
|
||
|
{
|
||
|
$this->registerPlugin('carousel');
|
||
10 years ago
|
return implode("\n", [
|
||
|
Html::beginTag('div', $this->options),
|
||
|
$this->renderIndicators(),
|
||
|
$this->renderItems(),
|
||
|
$this->renderControls(),
|
||
|
Html::endTag('div')
|
||
10 years ago
|
]) . "\n";
|
||
11 years ago
|
}
|
||
12 years ago
|
|
||
11 years ago
|
/**
|
||
|
* Renders carousel indicators.
|
||
|
* @return string the rendering result
|
||
|
*/
|
||
|
public function renderIndicators()
|
||
|
{
|
||
11 years ago
|
if ($this->showIndicators === false) {
|
||
|
return '';
|
||
|
}
|
||
11 years ago
|
$indicators = [];
|
||
|
for ($i = 0, $count = count($this->items); $i < $count; $i++) {
|
||
|
$options = ['data-target' => '#' . $this->options['id'], 'data-slide-to' => $i];
|
||
|
if ($i === 0) {
|
||
|
Html::addCssClass($options, 'active');
|
||
|
}
|
||
|
$indicators[] = Html::tag('li', '', $options);
|
||
|
}
|
||
12 years ago
|
|
||
11 years ago
|
return Html::tag('ol', implode("\n", $indicators), ['class' => 'carousel-indicators']);
|
||
|
}
|
||
12 years ago
|
|
||
11 years ago
|
/**
|
||
|
* Renders carousel items as specified on [[items]].
|
||
|
* @return string the rendering result
|
||
|
*/
|
||
|
public function renderItems()
|
||
|
{
|
||
|
$items = [];
|
||
|
for ($i = 0, $count = count($this->items); $i < $count; $i++) {
|
||
|
$items[] = $this->renderItem($this->items[$i], $i);
|
||
|
}
|
||
12 years ago
|
|
||
11 years ago
|
return Html::tag('div', implode("\n", $items), ['class' => 'carousel-inner']);
|
||
|
}
|
||
12 years ago
|
|
||
11 years ago
|
/**
|
||
|
* Renders a single carousel item
|
||
11 years ago
|
* @param string|array $item a single item from [[items]]
|
||
7 years ago
|
* @param int $index the item index as the first item should be set to `active`
|
||
11 years ago
|
* @return string the rendering result
|
||
11 years ago
|
* @throws InvalidConfigException if the item is invalid
|
||
|
*/
|
||
|
public function renderItem($item, $index)
|
||
|
{
|
||
|
if (is_string($item)) {
|
||
|
$content = $item;
|
||
|
$caption = null;
|
||
|
$options = [];
|
||
|
} elseif (isset($item['content'])) {
|
||
|
$content = $item['content'];
|
||
|
$caption = ArrayHelper::getValue($item, 'caption');
|
||
|
if ($caption !== null) {
|
||
|
$caption = Html::tag('div', $caption, ['class' => 'carousel-caption']);
|
||
|
}
|
||
|
$options = ArrayHelper::getValue($item, 'options', []);
|
||
|
} else {
|
||
|
throw new InvalidConfigException('The "content" option is required.');
|
||
|
}
|
||
12 years ago
|
|
||
10 years ago
|
Html::addCssClass($options, ['widget' => 'item']);
|
||
11 years ago
|
if ($index === 0) {
|
||
|
Html::addCssClass($options, 'active');
|
||
|
}
|
||
12 years ago
|
|
||
11 years ago
|
return Html::tag('div', $content . "\n" . $caption, $options);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Renders previous and next control buttons.
|
||
|
* @throws InvalidConfigException if [[controls]] is invalid.
|
||
|
*/
|
||
|
public function renderControls()
|
||
|
{
|
||
|
if (isset($this->controls[0], $this->controls[1])) {
|
||
|
return Html::a($this->controls[0], '#' . $this->options['id'], [
|
||
|
'class' => 'left carousel-control',
|
||
|
'data-slide' => 'prev',
|
||
|
]) . "\n"
|
||
|
. Html::a($this->controls[1], '#' . $this->options['id'], [
|
||
|
'class' => 'right carousel-control',
|
||
|
'data-slide' => 'next',
|
||
|
]);
|
||
|
} elseif ($this->controls === false) {
|
||
|
return '';
|
||
|
} else {
|
||
|
throw new InvalidConfigException('The "controls" property must be either false or an array of two elements.');
|
||
|
}
|
||
|
}
|
||
12 years ago
|
}
|