Collapse.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. * Collapse renders an accordion bootstrap javascript component.
  13. *
  14. * For example:
  15. *
  16. * ```php
  17. * echo Collapse::widget([
  18. * 'items' => [
  19. * // equivalent to the above
  20. * 'Collapsible Group Item #1' => [
  21. * 'content' => 'Anim pariatur cliche...',
  22. * // open its content by default
  23. * 'contentOptions' => ['class' => 'in']
  24. * ],
  25. * // another group item
  26. * 'Collapsible Group Item #2' => [
  27. * 'content' => 'Anim pariatur cliche...',
  28. * 'contentOptions' => [...],
  29. * 'options' => [...],
  30. * ],
  31. * ]
  32. * ]);
  33. * ```
  34. *
  35. * @see http://getbootstrap.com/javascript/#collapse
  36. * @author Antonio Ramirez <amigo.cobos@gmail.com>
  37. * @since 2.0
  38. */
  39. class Collapse extends Widget
  40. {
  41. /**
  42. * @var array list of groups in the collapse widget. Each array element represents a single
  43. * group with the following structure:
  44. *
  45. * ```php
  46. * // item key is the actual group header
  47. * 'Collapsible Group Item #1' => [
  48. * // required, the content (HTML) of the group
  49. * 'content' => 'Anim pariatur cliche...',
  50. * // optional the HTML attributes of the content group
  51. * 'contentOptions' => [],
  52. * // optional the HTML attributes of the group
  53. * 'options' => [],
  54. * ]
  55. * ```
  56. */
  57. public $items = [];
  58. /**
  59. * Initializes the widget.
  60. */
  61. public function init()
  62. {
  63. parent::init();
  64. Html::addCssClass($this->options, 'panel-group');
  65. }
  66. /**
  67. * Renders the widget.
  68. */
  69. public function run()
  70. {
  71. echo Html::beginTag('div', $this->options) . "\n";
  72. echo $this->renderItems() . "\n";
  73. echo Html::endTag('div') . "\n";
  74. $this->registerPlugin('collapse');
  75. }
  76. /**
  77. * Renders collapsible items as specified on [[items]].
  78. * @return string the rendering result
  79. */
  80. public function renderItems()
  81. {
  82. $items = [];
  83. $index = 0;
  84. foreach ($this->items as $header => $item) {
  85. $options = ArrayHelper::getValue($item, 'options', []);
  86. Html::addCssClass($options, 'panel panel-default');
  87. $items[] = Html::tag('div', $this->renderItem($header, $item, ++$index), $options);
  88. }
  89. return implode("\n", $items);
  90. }
  91. /**
  92. * Renders a single collapsible item group
  93. * @param string $header a label of the item group [[items]]
  94. * @param array $item a single item from [[items]]
  95. * @param integer $index the item index as each item group content must have an id
  96. * @return string the rendering result
  97. * @throws InvalidConfigException
  98. */
  99. public function renderItem($header, $item, $index)
  100. {
  101. if (isset($item['content'])) {
  102. $id = $this->options['id'] . '-collapse' . $index;
  103. $options = ArrayHelper::getValue($item, 'contentOptions', []);
  104. $options['id'] = $id;
  105. Html::addCssClass($options, 'panel-collapse collapse');
  106. $headerToggle = Html::a($header, '#' . $id, [
  107. 'class' => 'collapse-toggle',
  108. 'data-toggle' => 'collapse',
  109. 'data-parent' => '#' . $this->options['id']
  110. ]) . "\n";
  111. $header = Html::tag('h4', $headerToggle, ['class' => 'panel-title']);
  112. $content = Html::tag('div', $item['content'], ['class' => 'panel-body']) . "\n";
  113. } else {
  114. throw new InvalidConfigException('The "content" option is required.');
  115. }
  116. $group = [];
  117. $group[] = Html::tag('div', $header, ['class' => 'panel-heading']);
  118. $group[] = Html::tag('div', $content, $options);
  119. return implode("\n", $group);
  120. }
  121. }