yii2-bootstrap5/src/Carousel.php

225 lines
7.4 KiB
PHP
Raw Normal View History

2021-02-10 05:04:59 +08:00
<?php
2021-08-07 05:38:53 +08:00
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
2021-02-10 05:04:59 +08:00
declare(strict_types=1);
namespace yii\bootstrap5;
use Exception;
2021-02-10 05:04:59 +08:00
use yii\base\InvalidConfigException;
use yii\helpers\ArrayHelper;
/**
* Carousel renders a carousel bootstrap javascript component.
*
* For example:
*
* ```php
* echo Carousel::widget([
* 'items' => [
* // the item contains only the image
* '<img src="http://twitter.github.io/bootstrap/assets/img/bootstrap-mdo-sfmoma-01.jpg"/>',
* // equivalent to the above
* ['content' => '<img src="http://twitter.github.io/bootstrap/assets/img/bootstrap-mdo-sfmoma-02.jpg"/>'],
* // the item contains both the image and the caption
* [
* 'content' => '<img src="http://twitter.github.io/bootstrap/assets/img/bootstrap-mdo-sfmoma-03.jpg"/>',
* 'caption' => '<h4>This is title</h4><p>This is the caption text</p>',
* 'captionOptions' => ['class' => ['d-none', 'd-md-block']]
* 'options' => [...],
* ],
* ]
* ]);
* ```
*
2021-08-05 15:21:05 +08:00
* @see https://getbootstrap.com/docs/5.1/components/carousel/
2021-02-10 05:04:59 +08:00
* @author Antonio Ramirez <amigo.cobos@gmail.com>
* @author Simon Karlen <simi.albi@outlook.com>
*/
class Carousel extends Widget
{
/**
* @var array|null the labels for the previous and the next control buttons.
* If null, it means the previous and the next control buttons should not be displayed.
*/
public $controls = [
2021-02-10 05:04:59 +08:00
'<span class="carousel-control-prev-icon" aria-hidden="true"></span><span class="visually-hidden">Previous</span>',
'<span class="carousel-control-next-icon" aria-hidden="true"></span><span class="visually-hidden">Next</span>',
];
/**
* @var bool whether carousel indicators (<ol> tag with anchors to items) should be displayed or not.
*/
public $showIndicators = true;
2021-02-10 05:04:59 +08:00
/**
* @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 = [];
2021-02-10 05:04:59 +08:00
/**
* @var bool Animate slides with a fade transition instead of a slide. Defaults to `false`
*/
public $crossfade = false;
2021-02-10 05:04:59 +08:00
/**
* {@inheritdoc}
*/
public $options = ['data-bs-ride' => 'carousel'];
2021-02-10 05:04:59 +08:00
/**
2021-07-12 23:22:29 +08:00
* {@inheritDoc}
* @throws InvalidConfigException
2021-02-10 05:04:59 +08:00
*/
public function init()
{
parent::init();
Html::addCssClass($this->options, ['widget' => 'carousel slide']);
if ($this->crossfade) {
Html::addCssClass($this->options, ['animation' => 'carousel-fade']);
}
}
/**
* {@inheritdoc}
* @throws InvalidConfigException
*/
2021-07-12 23:22:29 +08:00
public function run(): string
2021-02-10 05:04:59 +08:00
{
$this->registerPlugin('carousel');
2021-07-12 23:22:29 +08:00
2021-02-10 05:04:59 +08:00
return implode("\n", [
Html::beginTag('div', $this->options),
$this->renderIndicators(),
$this->renderItems(),
$this->renderControls(),
Html::endTag('div'),
]) . "\n";
}
/**
* Renders carousel indicators.
* @return string the rendering result
*/
2021-07-12 23:22:29 +08:00
public function renderIndicators(): string
2021-02-10 05:04:59 +08:00
{
if ($this->showIndicators === false) {
return '';
}
$indicators = [];
for ($i = 0, $count = count($this->items); $i < $count; $i++) {
2021-07-12 23:22:29 +08:00
$options = [
'data' => [
'bs-target' => '#' . $this->options['id'],
'bs-slide-to' => $i
],
'type' => 'button'
];
2021-02-10 05:04:59 +08:00
if ($i === 0) {
Html::addCssClass($options, ['activate' => 'active']);
2021-07-12 23:22:29 +08:00
$options['aria']['current'] = 'true';
2021-02-10 05:04:59 +08:00
}
2021-07-12 23:22:29 +08:00
$indicators[] = Html::tag('button', '', $options);
2021-02-10 05:04:59 +08:00
}
2021-07-12 23:22:29 +08:00
return Html::tag('div', implode("\n", $indicators), ['class' => ['carousel-indicators']]);
2021-02-10 05:04:59 +08:00
}
/**
* Renders carousel items as specified on [[items]].
* @return string the rendering result
* @throws InvalidConfigException
*/
2021-07-12 23:22:29 +08:00
public function renderItems(): string
2021-02-10 05:04:59 +08:00
{
$items = [];
for ($i = 0, $count = count($this->items); $i < $count; $i++) {
$items[] = $this->renderItem($this->items[$i], $i);
}
return Html::tag('div', implode("\n", $items), ['class' => 'carousel-inner']);
}
/**
* Renders a single carousel item
* @param string|array $item a single item from [[items]]
* @param int $index the item index as the first item should be set to `active`
* @return string the rendering result
* @throws InvalidConfigException if the item is invalid
* @throws Exception
2021-02-10 05:04:59 +08:00
*/
2021-07-12 23:22:29 +08:00
public function renderItem($item, int $index): string
2021-02-10 05:04:59 +08:00
{
if (is_string($item)) {
$content = $item;
$caption = null;
$options = [];
} elseif (isset($item['content'])) {
$content = $item['content'];
$caption = ArrayHelper::getValue($item, 'caption');
if ($caption !== null) {
$captionOptions = ArrayHelper::remove($item, 'captionOptions', []);
Html::addCssClass($captionOptions, ['widget' => 'carousel-caption']);
$caption = Html::tag('div', $caption, $captionOptions);
}
$options = ArrayHelper::getValue($item, 'options', []);
} else {
throw new InvalidConfigException('The "content" option is required.');
}
Html::addCssClass($options, ['widget' => 'carousel-item']);
if ($index === 0) {
Html::addCssClass($options, ['activate' => 'active']);
}
return Html::tag('div', $content . "\n" . $caption, $options);
}
/**
* Renders previous and next control buttons.
*
* @return string The rendered controls
*
2021-02-10 05:04:59 +08:00
* @throws InvalidConfigException if [[controls]] is invalid.
*/
public function renderControls(): string
2021-02-10 05:04:59 +08:00
{
if (isset($this->controls[0], $this->controls[1])) {
2021-07-12 23:22:29 +08:00
return Html::button($this->controls[0], [
2021-02-10 05:04:59 +08:00
'class' => 'carousel-control-prev',
2021-07-12 23:22:29 +08:00
'data' => [
'bs-target' => '#' . $this->options['id'],
'bs-slide' => 'prev'
],
'type' => 'button',
2021-02-10 05:04:59 +08:00
]) . "\n"
2021-07-12 23:22:29 +08:00
. Html::button($this->controls[1], [
2021-02-10 05:04:59 +08:00
'class' => 'carousel-control-next',
2021-07-12 23:22:29 +08:00
'data' => [
'bs-target' => '#' . $this->options['id'],
'bs-slide' => 'next'
],
'type' => 'button',
2021-02-10 05:04:59 +08:00
]);
} elseif ($this->controls === false) {
return '';
} else {
throw new InvalidConfigException('The "controls" property must be either false or an array of two elements.');
}
}
}