Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. <?php
  2. /**
  3. * @link http://www.yiiframework.com/
  4. * @copyright Copyright (c) 2008 Yii Software LLC
  5. * @license http://www.yiiframework.com/license/
  6. */
  7. namespace yii\bootstrap;
  8. use yii\base\InvalidConfigException;
  9. use yii\helpers\ArrayHelper;
  10. use yii\helpers\Html;
  11. /**
  12. * Carousel renders a carousel bootstrap javascript component.
  13. *
  14. * For example:
  15. *
  16. * ```php
  17. * echo Carousel::widget([
  18. * 'items' => [
  19. * // the item contains only the image
  20. * '<img src="http://twitter.github.io/bootstrap/assets/img/bootstrap-mdo-sfmoma-01.jpg"/>',
  21. * // equivalent to the above
  22. * ['content' => '<img src="http://twitter.github.io/bootstrap/assets/img/bootstrap-mdo-sfmoma-02.jpg"/>'],
  23. * // the item contains both the image and the caption
  24. * [
  25. * 'content' => '<img src="http://twitter.github.io/bootstrap/assets/img/bootstrap-mdo-sfmoma-03.jpg"/>',
  26. * 'caption' => '<h4>This is title</h4><p>This is the caption text</p>',
  27. * 'options' => [...],
  28. * ],
  29. * ]
  30. * ]);
  31. * ```
  32. *
  33. * @see http://getbootstrap.com/javascript/#carousel
  34. * @author Antonio Ramirez <amigo.cobos@gmail.com>
  35. * @since 2.0
  36. */
  37. class Carousel extends Widget
  38. {
  39. /**
  40. * @var array|boolean the labels for the previous and the next control buttons.
  41. * If false, it means the previous and the next control buttons should not be displayed.
  42. */
  43. public $controls = ['&lsaquo;', '&rsaquo;'];
  44. /**
  45. * @var boolean
  46. * If false carousel indicators (<ol> tag with anchors to items) should not be displayed.
  47. */
  48. public $showIndicators = true;
  49. /**
  50. * @var array list of slides in the carousel. Each array element represents a single
  51. * slide with the following structure:
  52. *
  53. * ```php
  54. * [
  55. * // required, slide content (HTML), such as an image tag
  56. * 'content' => '<img src="http://twitter.github.io/bootstrap/assets/img/bootstrap-mdo-sfmoma-01.jpg"/>',
  57. * // optional, the caption (HTML) of the slide
  58. * 'caption' => '<h4>This is title</h4><p>This is the caption text</p>',
  59. * // optional the HTML attributes of the slide container
  60. * 'options' => [],
  61. * ]
  62. * ```
  63. */
  64. public $items = [];
  65. /**
  66. * Initializes the widget.
  67. */
  68. public function init()
  69. {
  70. parent::init();
  71. Html::addCssClass($this->options, 'carousel');
  72. }
  73. /**
  74. * Renders the widget.
  75. */
  76. public function run()
  77. {
  78. $this->registerPlugin('carousel');
  79. return implode("\n", [
  80. Html::beginTag('div', $this->options),
  81. $this->renderIndicators(),
  82. $this->renderItems(),
  83. $this->renderControls(),
  84. Html::endTag('div')
  85. ]) . "\n";
  86. }
  87. /**
  88. * Renders carousel indicators.
  89. * @return string the rendering result
  90. */
  91. public function renderIndicators()
  92. {
  93. if ($this->showIndicators === false) {
  94. return '';
  95. }
  96. $indicators = [];
  97. for ($i = 0, $count = count($this->items); $i < $count; $i++) {
  98. $options = ['data-target' => '#' . $this->options['id'], 'data-slide-to' => $i];
  99. if ($i === 0) {
  100. Html::addCssClass($options, 'active');
  101. }
  102. $indicators[] = Html::tag('li', '', $options);
  103. }
  104. return Html::tag('ol', implode("\n", $indicators), ['class' => 'carousel-indicators']);
  105. }
  106. /**
  107. * Renders carousel items as specified on [[items]].
  108. * @return string the rendering result
  109. */
  110. public function renderItems()
  111. {
  112. $items = [];
  113. for ($i = 0, $count = count($this->items); $i < $count; $i++) {
  114. $items[] = $this->renderItem($this->items[$i], $i);
  115. }
  116. return Html::tag('div', implode("\n", $items), ['class' => 'carousel-inner']);
  117. }
  118. /**
  119. * Renders a single carousel item
  120. * @param string|array $item a single item from [[items]]
  121. * @param integer $index the item index as the first item should be set to `active`
  122. * @return string the rendering result
  123. * @throws InvalidConfigException if the item is invalid
  124. */
  125. public function renderItem($item, $index)
  126. {
  127. if (is_string($item)) {
  128. $content = $item;
  129. $caption = null;
  130. $options = [];
  131. } elseif (isset($item['content'])) {
  132. $content = $item['content'];
  133. $caption = ArrayHelper::getValue($item, 'caption');
  134. if ($caption !== null) {
  135. $caption = Html::tag('div', $caption, ['class' => 'carousel-caption']);
  136. }
  137. $options = ArrayHelper::getValue($item, 'options', []);
  138. } else {
  139. throw new InvalidConfigException('The "content" option is required.');
  140. }
  141. Html::addCssClass($options, 'item');
  142. if ($index === 0) {
  143. Html::addCssClass($options, 'active');
  144. }
  145. return Html::tag('div', $content . "\n" . $caption, $options);
  146. }
  147. /**
  148. * Renders previous and next control buttons.
  149. * @throws InvalidConfigException if [[controls]] is invalid.
  150. */
  151. public function renderControls()
  152. {
  153. if (isset($this->controls[0], $this->controls[1])) {
  154. return Html::a($this->controls[0], '#' . $this->options['id'], [
  155. 'class' => 'left carousel-control',
  156. 'data-slide' => 'prev',
  157. ]) . "\n"
  158. . Html::a($this->controls[1], '#' . $this->options['id'], [
  159. 'class' => 'right carousel-control',
  160. 'data-slide' => 'next',
  161. ]);
  162. } elseif ($this->controls === false) {
  163. return '';
  164. } else {
  165. throw new InvalidConfigException('The "controls" property must be either false or an array of two elements.');
  166. }
  167. }
  168. }