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
			| 
											13 years ago
										 | <?php
 | ||
|  | /**
 | ||
|  |  * @link http://www.yiiframework.com/
 | ||
|  |  * @copyright Copyright (c) 2008 Yii Software LLC
 | ||
|  |  * @license http://www.yiiframework.com/license/
 | ||
|  |  */
 | ||
|  | 
 | ||
|  | namespace yii\bootstrap;
 | ||
|  | 
 | ||
| 
											13 years ago
										 | use yii\base\InvalidConfigException;
 | ||
| 
											13 years ago
										 | use yii\helpers\ArrayHelper;
 | ||
| 
											13 years ago
										 | use yii\helpers\Html;
 | ||
|  | 
 | ||
|  | /**
 | ||
|  |  * Carousel renders a carousel bootstrap javascript component.
 | ||
|  |  *
 | ||
| 
											13 years ago
										 |  * For example:
 | ||
| 
											13 years ago
										 |  *
 | ||
|  |  * ```php
 | ||
| 
											12 years ago
										 |  * echo Carousel::widget([
 | ||
|  |  *     'items' => [
 | ||
| 
											13 years ago
										 |  *         // the item contains only the image
 | ||
| 
											13 years ago
										 |  *         '<img src="http://twitter.github.io/bootstrap/assets/img/bootstrap-mdo-sfmoma-01.jpg"/>',
 | ||
| 
											13 years ago
										 |  *         // equivalent to the above
 | ||
| 
											12 years ago
										 |  *         ['content' => '<img src="http://twitter.github.io/bootstrap/assets/img/bootstrap-mdo-sfmoma-02.jpg"/>'],
 | ||
| 
											13 years ago
										 |  *         // the item contains both the image and the caption
 | ||
| 
											12 years ago
										 |  *         [
 | ||
| 
											13 years ago
										 |  *             'content' => '<img src="http://twitter.github.io/bootstrap/assets/img/bootstrap-mdo-sfmoma-03.jpg"/>',
 | ||
| 
											13 years ago
										 |  *             'caption' => '<h4>This is title</h4><p>This is the caption text</p>',
 | ||
| 
											12 years ago
										 |  *             'options' => [...],
 | ||
|  |  *         ],
 | ||
| 
											12 years ago
										 |  *     ]
 | ||
|  |  * ]);
 | ||
| 
											13 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
 | ||
|  | {
 | ||
|  | 	/**
 | ||
| 
											13 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.
 | ||
| 
											13 years ago
										 | 	 */
 | ||
| 
											12 years ago
										 | 	public $controls = ['‹', '›'];
 | ||
| 
											13 years ago
										 | 	/**
 | ||
| 
											13 years ago
										 | 	 * @var array list of slides in the carousel. Each array element represents a single
 | ||
|  | 	 * slide with the following structure:
 | ||
| 
											13 years ago
										 | 	 *
 | ||
|  | 	 * ```php
 | ||
| 
											12 years ago
										 | 	 * [
 | ||
| 
											13 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>',
 | ||
| 
											13 years ago
										 | 	 *     // optional the HTML attributes of the slide container
 | ||
| 
											12 years ago
										 | 	 *     'options' => [],
 | ||
|  | 	 * ]
 | ||
| 
											13 years ago
										 | 	 * ```
 | ||
|  | 	 */
 | ||
| 
											12 years ago
										 | 	public $items = [];
 | ||
| 
											13 years ago
										 | 
 | ||
|  | 
 | ||
|  | 	/**
 | ||
|  | 	 * Initializes the widget.
 | ||
|  | 	 */
 | ||
|  | 	public function init()
 | ||
|  | 	{
 | ||
|  | 		parent::init();
 | ||
| 
											13 years ago
										 | 		Html::addCssClass($this->options, 'carousel');
 | ||
| 
											13 years ago
										 | 	}
 | ||
|  | 
 | ||
|  | 	/**
 | ||
|  | 	 * Renders the widget.
 | ||
|  | 	 */
 | ||
|  | 	public function run()
 | ||
|  | 	{
 | ||
|  | 		echo Html::beginTag('div', $this->options) . "\n";
 | ||
| 
											13 years ago
										 | 		echo $this->renderIndicators() . "\n";
 | ||
|  | 		echo $this->renderItems() . "\n";
 | ||
| 
											13 years ago
										 | 		echo $this->renderControls() . "\n";
 | ||
| 
											13 years ago
										 | 		echo Html::endTag('div') . "\n";
 | ||
| 
											13 years ago
										 | 		$this->registerPlugin('carousel');
 | ||
| 
											13 years ago
										 | 	}
 | ||
|  | 
 | ||
|  | 	/**
 | ||
| 
											13 years ago
										 | 	 * Renders carousel indicators.
 | ||
|  | 	 * @return string the rendering result
 | ||
| 
											13 years ago
										 | 	 */
 | ||
|  | 	public function renderIndicators()
 | ||
|  | 	{
 | ||
| 
											12 years ago
										 | 		$indicators = [];
 | ||
| 
											13 years ago
										 | 		for ($i = 0, $count = count($this->items); $i < $count; $i++) {
 | ||
| 
											12 years ago
										 | 			$options = ['data-target' => '#' . $this->options['id'], 'data-slide-to' => $i];
 | ||
| 
											13 years ago
										 | 			if ($i === 0) {
 | ||
| 
											13 years ago
										 | 				Html::addCssClass($options, 'active');
 | ||
| 
											13 years ago
										 | 			}
 | ||
| 
											13 years ago
										 | 			$indicators[] = Html::tag('li', '', $options);
 | ||
| 
											13 years ago
										 | 		}
 | ||
| 
											12 years ago
										 | 		return Html::tag('ol', implode("\n", $indicators), ['class' => 'carousel-indicators']);
 | ||
| 
											13 years ago
										 | 	}
 | ||
|  | 
 | ||
|  | 	/**
 | ||
| 
											13 years ago
										 | 	 * Renders carousel items as specified on [[items]].
 | ||
|  | 	 * @return string the rendering result
 | ||
| 
											13 years ago
										 | 	 */
 | ||
|  | 	public function renderItems()
 | ||
|  | 	{
 | ||
| 
											12 years ago
										 | 		$items = [];
 | ||
| 
											13 years ago
										 | 		for ($i = 0, $count = count($this->items); $i < $count; $i++) {
 | ||
| 
											13 years ago
										 | 			$items[] = $this->renderItem($this->items[$i], $i);
 | ||
| 
											13 years ago
										 | 		}
 | ||
| 
											12 years ago
										 | 		return Html::tag('div', implode("\n", $items), ['class' => 'carousel-inner']);
 | ||
| 
											13 years ago
										 | 	}
 | ||
|  | 
 | ||
|  | 	/**
 | ||
|  | 	 * Renders a single carousel item
 | ||
| 
											13 years ago
										 | 	 * @param string|array $item a single item from [[items]]
 | ||
| 
											13 years ago
										 | 	 * @param integer $index the item index as the first item should be set to `active`
 | ||
| 
											13 years ago
										 | 	 * @return string the rendering result
 | ||
|  | 	 * @throws InvalidConfigException if the item is invalid
 | ||
| 
											13 years ago
										 | 	 */
 | ||
|  | 	public function renderItem($item, $index)
 | ||
|  | 	{
 | ||
| 
											13 years ago
										 | 		if (is_string($item)) {
 | ||
| 
											13 years ago
										 | 			$content = $item;
 | ||
|  | 			$caption = null;
 | ||
| 
											12 years ago
										 | 			$options = [];
 | ||
| 
											13 years ago
										 | 		} elseif (isset($item['content'])) {
 | ||
|  | 			$content = $item['content'];
 | ||
|  | 			$caption = ArrayHelper::getValue($item, 'caption');
 | ||
|  | 			if ($caption !== null) {
 | ||
| 
											12 years ago
										 | 				$caption = Html::tag('div', $caption, ['class' => 'carousel-caption']);
 | ||
| 
											13 years ago
										 | 			}
 | ||
| 
											12 years ago
										 | 			$options = ArrayHelper::getValue($item, 'options', []);
 | ||
| 
											13 years ago
										 | 		} else {
 | ||
| 
											13 years ago
										 | 			throw new InvalidConfigException('The "content" option is required.');
 | ||
| 
											13 years ago
										 | 		}
 | ||
|  | 
 | ||
| 
											13 years ago
										 | 		Html::addCssClass($options, 'item');
 | ||
| 
											13 years ago
										 | 		if ($index === 0) {
 | ||
| 
											13 years ago
										 | 			Html::addCssClass($options, 'active');
 | ||
| 
											13 years ago
										 | 		}
 | ||
| 
											13 years ago
										 | 
 | ||
| 
											13 years ago
										 | 		return Html::tag('div', $content . "\n" . $caption, $options);
 | ||
| 
											13 years ago
										 | 	}
 | ||
|  | 
 | ||
|  | 	/**
 | ||
| 
											13 years ago
										 | 	 * Renders previous and next control buttons.
 | ||
|  | 	 * @throws InvalidConfigException if [[controls]] is invalid.
 | ||
| 
											13 years ago
										 | 	 */
 | ||
| 
											13 years ago
										 | 	public function renderControls()
 | ||
| 
											13 years ago
										 | 	{
 | ||
| 
											13 years ago
										 | 		if (isset($this->controls[0], $this->controls[1])) {
 | ||
| 
											12 years ago
										 | 			return Html::a($this->controls[0], '#' . $this->options['id'], [
 | ||
| 
											13 years ago
										 | 				'class' => 'left carousel-control',
 | ||
| 
											13 years ago
										 | 				'data-slide' => 'prev',
 | ||
| 
											12 years ago
										 | 			]) . "\n"
 | ||
|  | 			. Html::a($this->controls[1], '#' . $this->options['id'], [
 | ||
| 
											13 years ago
										 | 				'class' => 'right carousel-control',
 | ||
| 
											13 years ago
										 | 				'data-slide' => 'next',
 | ||
| 
											12 years ago
										 | 			]);
 | ||
| 
											13 years ago
										 | 		} elseif ($this->controls === false) {
 | ||
|  | 			return '';
 | ||
|  | 		} else {
 | ||
|  | 			throw new InvalidConfigException('The "controls" property must be either false or an array of two elements.');
 | ||
|  | 		}
 | ||
| 
											13 years ago
										 | 	}
 | ||
| 
											13 years ago
										 | }
 |