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.
171 lines
4.9 KiB
171 lines
4.9 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
|
use yii\helpers\Html;
|
||
|
|
||
|
/**
|
||
|
* 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
|
* ```
|
||
|
*
|
||
|
* @see http://twitter.github.io/bootstrap/javascript.html#carousel
|
||
|
* @author Antonio Ramirez <amigo.cobos@gmail.com>
|
||
|
* @since 2.0
|
||
|
*/
|
||
|
class Carousel extends Widget
|
||
|
{
|
||
|
/**
|
||
12 years ago
|
* @var array|boolean the labels for the previous and the next control buttons.
|
||
|
* If false, it means the previous and the next control buttons should not be displayed.
|
||
12 years ago
|
*/
|
||
11 years ago
|
public $controls = ['‹', '›'];
|
||
12 years ago
|
/**
|
||
12 years ago
|
* @var array list of slides in the carousel. Each array element represents a single
|
||
|
* slide with the following structure:
|
||
12 years ago
|
*
|
||
|
* ```php
|
||
11 years ago
|
* [
|
||
12 years ago
|
* // 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
|
||
12 years ago
|
* 'caption' => '<h4>This is title</h4><p>This is the caption text</p>',
|
||
12 years ago
|
* // optional the HTML attributes of the slide container
|
||
11 years ago
|
* 'options' => [],
|
||
|
* ]
|
||
12 years ago
|
* ```
|
||
|
*/
|
||
11 years ago
|
public $items = [];
|
||
12 years ago
|
|
||
|
|
||
|
/**
|
||
|
* Initializes the widget.
|
||
|
*/
|
||
|
public function init()
|
||
|
{
|
||
|
parent::init();
|
||
12 years ago
|
Html::addCssClass($this->options, 'carousel');
|
||
12 years ago
|
}
|
||
|
|
||
|
/**
|
||
|
* Renders the widget.
|
||
|
*/
|
||
|
public function run()
|
||
|
{
|
||
|
echo Html::beginTag('div', $this->options) . "\n";
|
||
12 years ago
|
echo $this->renderIndicators() . "\n";
|
||
|
echo $this->renderItems() . "\n";
|
||
12 years ago
|
echo $this->renderControls() . "\n";
|
||
12 years ago
|
echo Html::endTag('div') . "\n";
|
||
12 years ago
|
$this->registerPlugin('carousel');
|
||
12 years ago
|
}
|
||
|
|
||
|
/**
|
||
12 years ago
|
* Renders carousel indicators.
|
||
|
* @return string the rendering result
|
||
12 years ago
|
*/
|
||
|
public function renderIndicators()
|
||
|
{
|
||
11 years ago
|
$indicators = [];
|
||
12 years ago
|
for ($i = 0, $count = count($this->items); $i < $count; $i++) {
|
||
11 years ago
|
$options = ['data-target' => '#' . $this->options['id'], 'data-slide-to' => $i];
|
||
12 years ago
|
if ($i === 0) {
|
||
12 years ago
|
Html::addCssClass($options, 'active');
|
||
12 years ago
|
}
|
||
12 years ago
|
$indicators[] = Html::tag('li', '', $options);
|
||
12 years ago
|
}
|
||
11 years ago
|
return Html::tag('ol', implode("\n", $indicators), ['class' => 'carousel-indicators']);
|
||
12 years ago
|
}
|
||
|
|
||
|
/**
|
||
12 years ago
|
* Renders carousel items as specified on [[items]].
|
||
|
* @return string the rendering result
|
||
12 years ago
|
*/
|
||
|
public function renderItems()
|
||
|
{
|
||
11 years ago
|
$items = [];
|
||
12 years ago
|
for ($i = 0, $count = count($this->items); $i < $count; $i++) {
|
||
12 years ago
|
$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
|
}
|
||
|
|
||
|
/**
|
||
|
* Renders a single carousel item
|
||
12 years ago
|
* @param string|array $item a single item from [[items]]
|
||
12 years ago
|
* @param integer $index the item index as the first item should be set to `active`
|
||
12 years ago
|
* @return string the rendering result
|
||
|
* @throws InvalidConfigException if the item is invalid
|
||
12 years ago
|
*/
|
||
|
public function renderItem($item, $index)
|
||
|
{
|
||
12 years ago
|
if (is_string($item)) {
|
||
12 years ago
|
$content = $item;
|
||
|
$caption = null;
|
||
11 years ago
|
$options = [];
|
||
12 years ago
|
} elseif (isset($item['content'])) {
|
||
|
$content = $item['content'];
|
||
|
$caption = ArrayHelper::getValue($item, 'caption');
|
||
|
if ($caption !== null) {
|
||
11 years ago
|
$caption = Html::tag('div', $caption, ['class' => 'carousel-caption']);
|
||
12 years ago
|
}
|
||
11 years ago
|
$options = ArrayHelper::getValue($item, 'options', []);
|
||
12 years ago
|
} else {
|
||
12 years ago
|
throw new InvalidConfigException('The "content" option is required.');
|
||
12 years ago
|
}
|
||
|
|
||
12 years ago
|
Html::addCssClass($options, 'item');
|
||
12 years ago
|
if ($index === 0) {
|
||
12 years ago
|
Html::addCssClass($options, 'active');
|
||
12 years ago
|
}
|
||
12 years ago
|
|
||
12 years ago
|
return Html::tag('div', $content . "\n" . $caption, $options);
|
||
12 years ago
|
}
|
||
|
|
||
|
/**
|
||
12 years ago
|
* Renders previous and next control buttons.
|
||
|
* @throws InvalidConfigException if [[controls]] is invalid.
|
||
12 years ago
|
*/
|
||
12 years ago
|
public function renderControls()
|
||
12 years ago
|
{
|
||
12 years ago
|
if (isset($this->controls[0], $this->controls[1])) {
|
||
11 years ago
|
return Html::a($this->controls[0], '#' . $this->options['id'], [
|
||
12 years ago
|
'class' => 'left carousel-control',
|
||
12 years ago
|
'data-slide' => 'prev',
|
||
11 years ago
|
]) . "\n"
|
||
|
. Html::a($this->controls[1], '#' . $this->options['id'], [
|
||
12 years ago
|
'class' => 'right carousel-control',
|
||
12 years ago
|
'data-slide' => 'next',
|
||
11 years ago
|
]);
|
||
12 years ago
|
} 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
|
}
|
||
12 years ago
|
}
|