|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * @copyright Copyright (c) 2015 Yiister |
| 4 | + * @license https://github.com/yiister/yii2-gentelella/blob/master/LICENSE |
| 5 | + * @link http://gentelella.yiister.ru |
| 6 | + */ |
| 7 | + |
| 8 | +namespace yiister\gentelella\widgets; |
| 9 | + |
| 10 | +use yii\base\Widget; |
| 11 | +use yii\helpers\ArrayHelper; |
| 12 | +use yii\helpers\Html; |
| 13 | + |
| 14 | +class Accordion extends Widget |
| 15 | +{ |
| 16 | + /** |
| 17 | + * @var array the list of items to render |
| 18 | + * Each item allows the next string attributes: |
| 19 | + * title - the string header of item |
| 20 | + * active - whether the item is opened |
| 21 | + * id - the string identificator for the item |
| 22 | + * content - the string content of this item |
| 23 | + * options - the array of a HTML options for the content tag |
| 24 | + */ |
| 25 | + public $items = []; |
| 26 | + |
| 27 | + /** |
| 28 | + * @var array the HTML attributes for the widget container tag |
| 29 | + */ |
| 30 | + public $options = [ |
| 31 | + 'class' => 'accordion', |
| 32 | + 'role' => 'tablist', |
| 33 | + 'aria-multiselectable' => 'true', |
| 34 | + ]; |
| 35 | + |
| 36 | + /** |
| 37 | + * @inheritdoc |
| 38 | + */ |
| 39 | + public function run() |
| 40 | + { |
| 41 | + if (!isset($this->options['id'])) { |
| 42 | + $this->options['id'] = $this->getId(); |
| 43 | + } |
| 44 | + echo Html::beginTag('div', $this->options); |
| 45 | + foreach ($this->items as $key => $item) { |
| 46 | + $itemOptions = ArrayHelper::getValue($item, 'options', []); |
| 47 | + if (!isset($itemOptions['id'])) { |
| 48 | + $itemOptions['id'] = isset($item['id']) |
| 49 | + ? $item['id'] |
| 50 | + : $this->options['id'] . '-' . $key; |
| 51 | + } |
| 52 | + $itemOptions = ArrayHelper::merge( |
| 53 | + $itemOptions, |
| 54 | + [ |
| 55 | + 'aria-labelledby' => 'heading-' . $itemOptions['id'], |
| 56 | + 'class' => 'panel-collapse collapse', |
| 57 | + 'id' => $itemOptions['id'], |
| 58 | + 'role' => 'tabpanel', |
| 59 | + ] |
| 60 | + ); |
| 61 | + $isActive = ArrayHelper::getValue($item, 'active', false); |
| 62 | + if ($isActive) { |
| 63 | + Html::addCssClass($itemOptions, 'in'); |
| 64 | + } |
| 65 | + echo Html::beginTag('div', ['class' => 'panel']); |
| 66 | + echo Html::a( |
| 67 | + Html::tag( |
| 68 | + 'h4', |
| 69 | + $item['title'], |
| 70 | + [ |
| 71 | + 'class' => 'panel-title', |
| 72 | + ] |
| 73 | + ), |
| 74 | + '#' . $itemOptions['id'], |
| 75 | + [ |
| 76 | + 'class' => 'panel-heading', |
| 77 | + 'id' => 'heading-' . $itemOptions['id'], |
| 78 | + 'role' => 'tab', |
| 79 | + 'data-toggle' => 'collapse', |
| 80 | + 'data-parent' => '#' . $this->options['id'], |
| 81 | + 'aria-expanded' => $isActive ? 'true' : 'false', |
| 82 | + 'aria-controls' => $itemOptions['id'], |
| 83 | + ] |
| 84 | + ); |
| 85 | + echo Html::tag( |
| 86 | + 'div', |
| 87 | + Html::tag('div', $item['content'], ['class' => 'panel-body']), |
| 88 | + $itemOptions |
| 89 | + ); |
| 90 | + echo Html::endTag('div'); |
| 91 | + } |
| 92 | + echo Html::endTag('div'); |
| 93 | + } |
| 94 | +} |
0 commit comments