|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * @link https://github.com/MacGyer/yii2-materializecss |
| 4 | + * @copyright Copyright (c) 2016 ... MacGyer for pluspunkt coding |
| 5 | + * @license https://github.com/MacGyer/yii2-materializecss/blob/master/LICENSE |
| 6 | + */ |
| 7 | + |
| 8 | +namespace macgyer\yii2materializecss\widgets\media; |
| 9 | + |
| 10 | +use macgyer\yii2materializecss\lib\BaseWidget; |
| 11 | +use macgyer\yii2materializecss\lib\Html; |
| 12 | +use yii\helpers\ArrayHelper; |
| 13 | + |
| 14 | +/** |
| 15 | + * Carousel is a robust and versatile component that can be an image slider or an item carousel with arbitrary HTML content. |
| 16 | + * |
| 17 | + * Simply provide the [[slides]] as an array of items. |
| 18 | + * For each item you must define the `image` key with the image's `src`. Additionally you can define and align a caption |
| 19 | + * for every slide individually. Caption content can be HTML and will <strong>not</strong> be encoded. |
| 20 | + * |
| 21 | + * ```php |
| 22 | + * 'itemOptions' => [ |
| 23 | + * 'class' => 'amber white-text' // this class will be used for all carousel elements |
| 24 | + * ], |
| 25 | + * 'items' => [ |
| 26 | + * [ |
| 27 | + * 'content' => Html::img('http://lorempixel.com/800/800/sports/2'), |
| 28 | + * ], |
| 29 | + * [ |
| 30 | + * 'content' => '<h2>Carousel item heading</h2><p>Arbitrary content</p>' |
| 31 | + * 'options' => ['class' => 'carusel-item-override'] // overrides $itemOptions |
| 32 | + * ] |
| 33 | + * ], |
| 34 | + * 'fixedItemOptions' => [ |
| 35 | + * 'tag' => 'p', |
| 36 | + * 'content' => 'Some content', |
| 37 | + * ], |
| 38 | + * ``` |
| 39 | + * @author Christoph Erdmann <[email protected]> |
| 40 | + * @package widgets |
| 41 | + * @subpackage media |
| 42 | + * |
| 43 | + * @see http://materializecss.com/carousel.html |
| 44 | + */ |
| 45 | +class Carousel extends BaseWidget |
| 46 | +{ |
| 47 | + /** |
| 48 | + * @var array the HTML attributes for the carousel container tag. |
| 49 | + * @see [yii\helpers\BaseHtml::renderTagAttributes()](http://www.yiiframework.com/doc-2.0/yii-helpers-basehtml.html#renderTagAttributes()-detail) |
| 50 | + * for details on how attributes are being rendered. |
| 51 | + */ |
| 52 | + public $carouselOptions = []; |
| 53 | + |
| 54 | + /** |
| 55 | + * @var array the HTML attributes for each carousel item's tag. |
| 56 | + * These options will be merged with the individual item options. |
| 57 | + * |
| 58 | + * @see [yii\helpers\BaseHtml::renderTagAttributes()](http://www.yiiframework.com/doc-2.0/yii-helpers-basehtml.html#renderTagAttributes()-detail) |
| 59 | + * for details on how attributes are being rendered. |
| 60 | + */ |
| 61 | + public $itemOptions = []; |
| 62 | + |
| 63 | + /** |
| 64 | + * @var false|array the configuration for the fixed item. |
| 65 | + * |
| 66 | + * The following special options are recognized: |
| 67 | + * - tag: the fixed item's HTML tag name |
| 68 | + * - content: the content of the fixed item. Please note: this can be HTML and will not be encoded. |
| 69 | + * |
| 70 | + * If you do not want the fixed item to be rendered, set this option to `false`. |
| 71 | + * @see http://materializecss.com/carousel.html#special |
| 72 | + */ |
| 73 | + public $fixedItemOptions = false; |
| 74 | + |
| 75 | + /** |
| 76 | + * @var array the carousel items. |
| 77 | + * Provide a sub-array for each item which can have the keys `tag`, `content` and `options`. |
| 78 | + */ |
| 79 | + public $items = []; |
| 80 | + |
| 81 | + /** |
| 82 | + * @var boolean whether the carousel has full width. |
| 83 | + */ |
| 84 | + public $fullWidth = false; |
| 85 | + |
| 86 | + /** |
| 87 | + * @var boolean whether to show navigation indicators. |
| 88 | + */ |
| 89 | + public $showIndicators = false; |
| 90 | + |
| 91 | + /** |
| 92 | + * @var boolean whether to start with first slide at the end. |
| 93 | + * @see http://materializecss.com/carousel.html#options |
| 94 | + */ |
| 95 | + public $noWrap = false; |
| 96 | + |
| 97 | + /** |
| 98 | + * Initialize the widget. |
| 99 | + */ |
| 100 | + public function init() |
| 101 | + { |
| 102 | + parent::init(); |
| 103 | + |
| 104 | + Html::addCssClass($this->carouselOptions, ['plugin' => 'carousel']); |
| 105 | + if ($this->fullWidth) { |
| 106 | + Html::addCssClass($this->carouselOptions, ['fullwidth' => 'carousel-slider']); |
| 107 | + $this->clientOptions['fullWidth'] = true; |
| 108 | + } |
| 109 | + |
| 110 | + $this->clientOptions['noWrap'] = $this->noWrap; |
| 111 | + $this->clientOptions['indicators'] = $this->showIndicators; |
| 112 | + |
| 113 | + $this->registerPlugin('carousel', '.carousel'); |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * Execute the widget. |
| 118 | + */ |
| 119 | + public function run() |
| 120 | + { |
| 121 | + $tag = ArrayHelper::remove($this->options, 'tag', 'div'); |
| 122 | + $html[] = Html::beginTag($tag, $this->options); |
| 123 | + $html[] = Html::beginTag('div', $this->carouselOptions); |
| 124 | + $html[] = $this->renderFixedItem(); |
| 125 | + $html[] = $this->renderItems(); |
| 126 | + $html[] = Html::endTag('div'); |
| 127 | + $html[] = Html::endTag($tag); |
| 128 | + |
| 129 | + return implode("\n", $html); |
| 130 | + } |
| 131 | + |
| 132 | + /** |
| 133 | + * Parses all [[items]] and renders item list. |
| 134 | + * |
| 135 | + * @return string the item list markup |
| 136 | + */ |
| 137 | + protected function renderItems() |
| 138 | + { |
| 139 | + if (!$this->items) { |
| 140 | + return ''; |
| 141 | + } |
| 142 | + |
| 143 | + $html = []; |
| 144 | + |
| 145 | + foreach ($this->items as $item) { |
| 146 | + $html[] = $this->renderItem($item); |
| 147 | + } |
| 148 | + |
| 149 | + return implode("\n", $html); |
| 150 | + } |
| 151 | + |
| 152 | + /** |
| 153 | + * Renders a single carousel item. |
| 154 | + * |
| 155 | + * @param array $item the item configuration |
| 156 | + * @return string the item markup |
| 157 | + */ |
| 158 | + protected function renderItem($item = []) |
| 159 | + { |
| 160 | + $tag = ArrayHelper::getValue($item, 'tag', 'div'); |
| 161 | + $content = ArrayHelper::getValue($item, 'content', ''); |
| 162 | + $options = ArrayHelper::getValue($item, 'options', []); |
| 163 | + $options = ArrayHelper::merge($this->itemOptions, $options); |
| 164 | + |
| 165 | + Html::addCssClass($options, ['item' => 'carousel-item']); |
| 166 | + |
| 167 | + return Html::tag($tag, $content, $options); |
| 168 | + } |
| 169 | + |
| 170 | + /** |
| 171 | + * Renders the optional fixed item. |
| 172 | + * |
| 173 | + * @return string the fixed item's markup |
| 174 | + */ |
| 175 | + protected function renderFixedItem() |
| 176 | + { |
| 177 | + if ($this->fixedItemOptions === false) { |
| 178 | + return ''; |
| 179 | + } |
| 180 | + |
| 181 | + $tag = ArrayHelper::remove($this->fixedItemOptions, 'tag', 'div'); |
| 182 | + $content = ArrayHelper::remove($this->fixedItemOptions, 'content', ''); |
| 183 | + |
| 184 | + Html::addCssClass($this->fixedItemOptions, ['fixed-item' => 'carousel-fixed-item']); |
| 185 | + |
| 186 | + return Html::tag($tag, $content, $this->fixedItemOptions); |
| 187 | + } |
| 188 | +} |
0 commit comments